Skip to content

Commit

Permalink
net-sf-ucanaccess-fork: Review unit test failures under Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Nov 5, 2023
1 parent 3dd5cee commit 406deab
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ void testBlobOLE(AccessVersion _accessVersion) throws SQLException, IOException
assertThat((long) initialBlobBytes.length).isLessThan(binaryFileSize);
Statement st = ucanaccess.createStatement();
ResultSet rs = st.executeQuery("SELECT Ole FROM OleTable ORDER BY ID");
File fl = createTempFileName("Copied", ".jpeg");
fl.deleteOnExit();
File file = createTempFileName("Copied", ".jpeg");
rs.next();
@SuppressWarnings("unused")
Object obj = rs.getObject(1);
try (InputStream isFromDb = rs.getBlob(1).getBinaryStream()) {
copyFile(isFromDb, fl.toPath());
copyFile(isFromDb, file).deleteOnExit();
}
assertEquals(fl.length(), binaryFileSize);
getLogger().info("File was created in {}, size: {} bytes", fl.getAbsolutePath(), fl.length());
assertEquals(file.length(), binaryFileSize);
getLogger().info("File was created in {}, size: {} bytes", file.getAbsolutePath(), file.length());
byte[] finalBlobBytes = getBlobBytes();
getLogger().info("BLOB size in backing database after retrieval: {} bytes", finalBlobBytes.length);
if (!Arrays.equals(initialBlobBytes, finalBlobBytes)) {
Expand Down
84 changes: 41 additions & 43 deletions src/test/java/net/ucanaccess/test/integration/BlobOleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@

class BlobOleTest extends UcanaccessBaseTest {

private static final String IMG_FILE_NAME = "BlobOleTest/elisaArt.jpeg";
private static final String PPTX_FILE_NAME = "BlobOleTest/test.pptx";

@Override
protected void init(AccessVersion _accessVersion) throws SQLException {
super.init(_accessVersion);
Expand All @@ -34,50 +31,55 @@ void afterEachTest() throws SQLException {
@EnumSource(value = AccessVersion.class)
void testBlobOle(AccessVersion _accessVersion) throws SQLException, IOException {
init(_accessVersion);
File imgFileTemp = createTempFile(IMG_FILE_NAME);


String imgFileName = String.join(File.separator, getClass().getSimpleName(), "blobOleTest.jpeg");

Blob blob = ucanaccess.createBlob();
try (InputStream is = getClass().getClassLoader().getResourceAsStream(IMG_FILE_NAME);
try (InputStream is = getClass().getClassLoader().getResourceAsStream(imgFileName.replace(File.separatorChar, '/'));
OutputStream out = blob.setBinaryStream(1)) {
is.transferTo(out);
}

String descr = "TestOle";
try (PreparedStatement ps1 = ucanaccess.prepareStatement("INSERT INTO t_ole_test (c_descr, c_ole) VALUES(?, ?)")) {
ps1.setString(1, descr);
ps1.setBlob(2, blob);
ps1.execute();
try (PreparedStatement ps = ucanaccess.prepareStatement("INSERT INTO t_ole_test (c_descr, c_ole) VALUES(?, ?)")) {
ps.setString(1, descr);
ps.setBlob(2, blob);
ps.execute();
}

Statement st = ucanaccess.createStatement();
ResultSet rs1 = st.executeQuery("SELECT c_ole FROM t_ole_test");
rs1.next();

try (InputStream isFromDb = rs1.getBinaryStream(1)) {

copyFile(isFromDb, imgFileTemp.toPath());

File imgFileTemp = createTempFileName(imgFileName, null);
copyFile(isFromDb, imgFileTemp).deleteOnExit();
getLogger().info("Image file was created in {}", imgFileTemp.getAbsolutePath());

byte[] fileBytes = Files.readAllBytes(imgFileTemp.toPath());

checkQuery("SELECT * FROM t_ole_test", new Object[][] {{1, descr, fileBytes}});

PreparedStatement ps2 = ucanaccess.prepareStatement("UPDATE t_ole_test SET c_descr=? WHERE c_descr=?");
ps2.setString(1, descr + "_OK");
ps2.setString(2, descr);
ps2.executeUpdate();
PreparedStatement ps = ucanaccess.prepareStatement("UPDATE t_ole_test SET c_descr=? WHERE c_descr=?");
ps.setString(1, descr + "_OK");
ps.setString(2, descr);
ps.executeUpdate();
checkQuery("SELECT * FROM t_ole_test");
checkQuery("SELECT * FROM t_ole_test", 1, descr + "_OK", fileBytes);
}

try (PreparedStatement ps3 = ucanaccess.prepareStatement("UPDATE t_ole_test SET c_ole=? WHERE c_descr=?")) {
File fl1 = copyFileFromClasspath(PPTX_FILE_NAME);
blob = ucanaccess.createBlob(fl1);
ps3.setObject(1, fl1);
ps3.setString(2, descr + "_OK");
ps3.executeUpdate();
fl1.delete();
getLogger().info("PPTX file was created in {}", getFileAccDb());
try (PreparedStatement ps = ucanaccess.prepareStatement("UPDATE t_ole_test SET c_ole=? WHERE c_descr=?")) {
String binFileName = String.join(File.separator, getClass().getSimpleName(), "blobOleTest.mp4");
File file = createTempFileName(binFileName, null);
try (InputStream is = getClass().getClassLoader().getResourceAsStream(binFileName.replace('/', File.separatorChar))) {
copyFile(is, file).deleteOnExit();
}
blob = ucanaccess.createBlob(file);
ps.setObject(1, file);
ps.setString(2, descr + "_OK");
ps.executeUpdate();
getLogger().info("Binary file was created in {}", getFileAccDb());
checkQuery("SELECT * FROM t_ole_test");
}

Expand All @@ -99,22 +101,30 @@ void testBlobOle(AccessVersion _accessVersion) throws SQLException, IOException

// It only works with JRE 1.6 and later (JDBC 3)

/**
* @param _accessVersion
* @throws SQLException
* @throws IOException
*/
@ParameterizedTest(name = "[{index}] {0}")
@EnumSource(value = AccessVersion.class)
void testBlobPackaged(AccessVersion _accessVersion) throws SQLException, IOException {
init(_accessVersion);
PreparedStatement ps = null;
File fl1 = copyFileFromClasspath(PPTX_FILE_NAME);
Blob blob = ucanaccess.createBlob(fl1);
ps = ucanaccess.prepareStatement("INSERT INTO t_ole_test (c_descr, c_ole) VALUES( ?,?)");

String binFileName = "BlobOleTest/blobOleTest.mp4";
File file = createTempFileName(binFileName.replace('/', File.separatorChar), null);
try (InputStream is = getClass().getClassLoader().getResourceAsStream(binFileName)) {
copyFile(is, file).deleteOnExit();
}
Blob blob = ucanaccess.createBlob(file);
PreparedStatement ps = ucanaccess.prepareStatement("INSERT INTO t_ole_test (c_descr, c_ole) VALUES( ?,?)");
ps.setString(1, "TestOle");
ps.setBlob(2, blob);

ps.execute();
getLogger().info("PPTX file was created in {}", getFileAccDb());
getLogger().info("Binary file was created in {}", getFileAccDb());
checkQuery("SELECT * FROM t_ole_test");
Statement st = ucanaccess.createStatement();
fl1.delete();
st.execute("DELETE FROM t_ole_test");
}

Expand All @@ -139,16 +149,4 @@ void testTwoColumnPk(AccessVersion _accessVersion) throws SQLException {
}
}

private File copyFileFromClasspath(String _fn) throws IOException {
int idxLastDot = _fn.lastIndexOf('.');
String prefix = idxLastDot < 0 ? _fn : _fn.substring(0, idxLastDot);
String suffix = idxLastDot < 0 ? ".tmp" : _fn.substring(idxLastDot).toLowerCase();
File tmpFile = createTempFileName(prefix, suffix);
tmpFile.deleteOnExit();
try (InputStream is = getClass().getClassLoader().getResourceAsStream(_fn)) {
copyFile(is, tmpFile.toPath());
}
return tmpFile;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,24 @@ void confirmNotNullColumnUsingJet(AccessVersion _accessVersion) throws Exception
return;
}

String cscriptPath = System.getenv("SystemRoot")
+ (System.getProperty("sun.arch.data.model").equals("64") ? "\\SYSWOW64" : "\\SYSTEM32")
+ "\\CSCRIPT.EXE";
String cscriptPath = String.join("\\",
System.getenv("SystemRoot"),
System.getProperty("sun.arch.data.model").equals("64") ? "SYSWOW64" : "SYSTEM32",
"CSCRIPT.EXE");

String command = "\"" + cscriptPath + "\" \"" + vbsFile.getAbsolutePath() + "\"";
Process proc = Runtime.getRuntime().exec(command);
proc.waitFor(10, TimeUnit.SECONDS);
proc.waitFor(5, TimeUnit.SECONDS);

assertThat(proc.exitValue()).isEqualTo(0);

try (BufferedReader output = new BufferedReader(new InputStreamReader(proc.getErrorStream()))) {
List<String> errLines = output.lines().collect(Collectors.toList());
String stderr = output.lines().collect(Collectors.joining(System.lineSeparator()));

if (errLines.isEmpty()) {
if (stderr.isEmpty()) {
fail("The VBScript should have thrown an error, but it did not");
}
assertThat(errLines).contains("table1.txt_required").withFailMessage("The VBScript threw an unexpected error");
assertThat(stderr).contains("table1.txt_required").withFailMessage("The VBScript threw an unexpected error");
}


Expand Down
15 changes: 11 additions & 4 deletions src/test/java/net/ucanaccess/test/util/AbstractBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Objects;
import java.util.Optional;

/**
Expand Down Expand Up @@ -117,17 +118,23 @@ protected static String getTempDir() {
return tmpDir;
}

protected static void copyFile(Path _source, Path _target) {
protected static File copyFile(Path _source, File _target) {
Objects.requireNonNull(_source, "Source file required");
Objects.requireNonNull(_target, "Target file required");
try {
Files.copy(_source, _target, StandardCopyOption.REPLACE_EXISTING);
Files.copy(_source, _target.toPath(), StandardCopyOption.REPLACE_EXISTING);
return _target;
} catch (IOException _ex) {
throw new UncheckedIOException("Failed to copy '" + _source + "' to '" + _target + "'", _ex);
}
}

protected static void copyFile(InputStream _in, Path _target) {
protected static File copyFile(InputStream _in, File _target) {
Objects.requireNonNull(_in, "Input stream required");
Objects.requireNonNull(_target, "Target file required");
try {
Files.copy(_in, _target, StandardCopyOption.REPLACE_EXISTING);
Files.copy(_in, _target.toPath(), StandardCopyOption.REPLACE_EXISTING);
return _target;
} catch (IOException _ex) {
throw new UncheckedIOException("Failed to copy to '" + _target + "'", _ex);
}
Expand Down
19 changes: 14 additions & 5 deletions src/test/java/net/ucanaccess/test/util/UcanaccessBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void checkQuery(String _query) throws SQLException, IOException {
initVerifyConnection();
try (Statement st1 = ucanaccess.createStatement();
Statement st2 = verifyConnection.createStatement()) {

ResultSet firstRs = st1.executeQuery(_query);
ResultSet verifyRs = st2.executeQuery(_query);

Expand Down Expand Up @@ -284,7 +284,17 @@ protected static File createTempFileName(String _prefix, String _suffix) {
if (!name.isBlank() && !name.endsWith("-")) {
name += "-";
}
name += new TempFileNameString() + _suffix;
String suffix = _suffix;
if (suffix == null || suffix.isBlank()) {
int idxLastDot = _prefix.lastIndexOf('.');
if (idxLastDot > -1) {
suffix = _prefix.substring(idxLastDot);
}
if (suffix.isEmpty() || suffix.length() > 6) {
suffix = ".tmp";
}
}
name += new TempFileNameString() + suffix;
return new File(TEST_TEMP_DIR, name);
}

Expand Down Expand Up @@ -335,8 +345,7 @@ protected File copyResourceToTempFile(String _resourcePath) {
}
File tempFile = createTempFile(resourceFile.getName().replace(".", "_"));
getLogger().info("Copying resource '{}' to '{}'", _resourcePath, tempFile.getAbsolutePath());
copyFile(is, tempFile.toPath());
return tempFile;
return copyFile(is, tempFile);
} catch (IOException _ex) {
throw new UncheckedIOException(_ex);
}
Expand Down Expand Up @@ -407,7 +416,7 @@ protected void appendToJdbcURL(String s) {

protected void initVerifyConnection() throws SQLException {
File tempVerifyFile = createTempFile(fileAccDb.getName().replace(".", "_") + "_verify");
copyFile(fileAccDb.toPath(), tempVerifyFile.toPath());
copyFile(fileAccDb.toPath(), tempVerifyFile);

if (verifyConnection != null) {
verifyConnection.close();
Expand Down
Binary file added src/test/resources/BlobOleTest/blobOleTest.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/test/resources/BlobOleTest/blobOleTest.mp4
Binary file not shown.
Binary file removed src/test/resources/BlobOleTest/elisaArt.jpeg
Binary file not shown.
Binary file removed src/test/resources/BlobOleTest/test.pptx
Binary file not shown.

0 comments on commit 406deab

Please sign in to comment.