Skip to content

Commit

Permalink
Migrate some failing tests to JUnit and fix (most of) them (#125)
Browse files Browse the repository at this point in the history
* Move tests to src/test/java/package because tooling expects them there

* Update HashDigest tests

They now use JUnit and fixed the hashes since the file used to test the hashing algorithms has been changed.

* Move image tests to junit, include test data in repo

* Migrate SSR tests to JUnit, introduce tags for tests

Many of the SSR tests require additional information (passwords, accounts, local server). This change tags with what is needed. Making the tests pass in JUnit is a future task.

* Move Opendap test to JUnit (tagged with Thredds)

* Clean up test boilerplate from classes that no longer need it

* Format new test files

* Migrate NCHelper tests to JUnit

Also bring over resources the tests depend on

* Removing calls to test functions that have been migrated to JUnit

* Format code and move image test files to the test resources directory

* Update Regex tests to not rely on a separate installed program directory

* Clean up unused dependencies in files that have had tests migrated

* Clean up old test function RegexFilenameFilter

* Migrate and fix OpendapHelper tests

Tags breaking due to thredds are tagged.

Also update image tests to use a junit temp directory.

* Use a proper temp directory for nchelper tests

* Add a tag for relying on an external ERDDAP server.

This should be one of the easiest tags to fix, but for right now it can cause test failures due to issues outside of the current code.

* Migrate GridDataSetThredds tests to JUnit (tagged THREDDS)

* Fix values for RegexFilenameFilterTests

If this number keeps changing, consider removing the specific assert.

* Comment out the byte size checking part of gatherinfo test because it is flaky

* Migrate GridDataSet OpenData and Anomaly tests to JUnit (TagThredds)

* Migrate Table Tests to junit

This adds a few new tags for why tests are currently failing.

* TableTests: Move some resources from c: to the test resources directory

Also update several paths to use the resources directory instead of hardcoded absolute paths.

* TableTests splitting long lines

* Use a resource instead of a file relative to the class path

* Label a test as using thredds

It was causing intermittent failures

* Reduce the logging from Table Tests
  • Loading branch information
ChrisJohnNOAA authored Jan 17, 2024
1 parent 0642947 commit 7bab64c
Show file tree
Hide file tree
Showing 10,486 changed files with 45,123 additions and 23,016 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
43 changes: 0 additions & 43 deletions WEB-INF/classes/com/cohort/array/ByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -1629,48 +1629,5 @@ public int[] getNMinMaxIndex() {
}
return new int[]{n, tmini, tmaxi};
}


/**
* This runs all of the interactive or not interactive tests for this class.
*
* @param errorSB all caught exceptions are logged to this.
* @param interactive If true, this runs all of the interactive tests;
* otherwise, this runs all of the non-interactive tests.
* @param doSlowTestsToo If true, this runs the slow tests, too.
* @param firstTest The first test to be run (0...). Test numbers may change.
* @param lastTest The last test to be run, inclusive (0..., or -1 for the last test).
* Test numbers may change.
*/
public static void test(StringBuilder errorSB, boolean interactive,
boolean doSlowTestsToo, int firstTest, int lastTest) {
if (lastTest < 0)
lastTest = interactive? -1 : 0;
String msg = "\n^^^ ByteArray.test(" + interactive + ") test=";

for (int test = firstTest; test <= lastTest; test++) {
try {
long time = System.currentTimeMillis();
String2.log(msg + test);

if (interactive) {
//if (test == 0) ...;

} else {

}

String2.log(msg + test + " finished successfully in " + (System.currentTimeMillis() - time) + " ms.");
} catch (Throwable testThrowable) {
String eMsg = msg + test + " caught throwable:\n" +
MustBe.throwableToString(testThrowable);
errorSB.append(eMsg);
String2.log(eMsg);
if (interactive)
String2.pressEnterToContinue("");
}
}
}

}

76 changes: 0 additions & 76 deletions WEB-INF/classes/com/cohort/util/HashDigest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
*/
package com.cohort.util;

import com.cohort.util.String2;
import com.cohort.util.File2;

import java.io.FileWriter;
import java.io.IOException;

/**
* This generates a hash digest (e.g., MD5, SHA256) of a password or a file.
*
Expand Down Expand Up @@ -313,34 +307,6 @@ private static int fmix32(int hash) {
" HashDigest filename:myFileName type:algorithm -file\n" +
"where algorithm can be MD5, SHA-1, or SHA-256.\n";

/**
* This tests this class.
*/
public static void basicTest() throws Throwable {
System.out.println("*** HashDigest.basicTest");
String tName = String2.replaceAll(
File2.getClassPath() + "com\\cohort\\util\\License.txt", "\\", "/");
Test.ensureEqual(doIt(new String[]{"type:MD5"}),
"Neither password or filename was specified.\n" + usage, "");
Test.ensureEqual(doIt(new String[]{"password:myPassword", "type:MD-5"}),
"Invalid algorithm.\n" + usage, "");
Test.ensureEqual(doIt(new String[]{"password:myPassword", "type:MD5"}),
"deb1536f480475f7d593219aa1afd74c", "");
Test.ensureEqual(doIt(new String[]{"password:myPassword", "type:SHA-1"}),
"5413ee24723bba2c5a6ba2d0196c78b3ee4628d1", "");
Test.ensureEqual(doIt(new String[]{"password:myPassword", "type:SHA-256"}),
"76549b827ec46e705fd03831813fa52172338f0dfcbd711ed44b81a96dac51c6", "");
Test.ensureEqual(doIt(new String[]{"filename:" + tName, "type:MD5"}),
"327fbb2aa6c6297d4fdd5fdf4b14e289", "");
Test.ensureEqual(doIt(new String[]{"filename:" + tName, "type:SHA-256"}),
"e376c88953b2d56b00783fed071f1875e8ed94230f4e14eee5bce8bd608de5e6", "");
Test.ensureEqual(doIt(new String[]{"filename:" + tName, "type:SHA-256", "-file"}),
"Created " + tName + ".sha256", "");
Test.ensureEqual(File2.readFromFileUtf8(tName + ".sha256")[1],
"e376c88953b2d56b00783fed071f1875e8ed94230f4e14eee5bce8bd608de5e6 License.txt\n", "");
File2.delete(tName + ".sha256");
}

/**
* This does the work.
*/
Expand Down Expand Up @@ -372,48 +338,6 @@ public static String doIt(String args[]) throws Throwable {
}
}

/**
* This runs all of the interactive or not interactive tests for this class.
*
* @param errorSB all caught exceptions are logged to this.
* @param interactive If true, this runs all of the interactive tests;
* otherwise, this runs all of the non-interactive tests.
* @param doSlowTestsToo If true, this runs the slow tests, too.
* @param firstTest The first test to be run (0...). Test numbers may change.
* @param lastTest The last test to be run, inclusive (0..., or -1 for the last test).
* Test numbers may change.
*/
public static void test(StringBuilder errorSB, boolean interactive,
boolean doSlowTestsToo, int firstTest, int lastTest) {
if (lastTest < 0)
lastTest = interactive? -1 : 0;
String msg = "\n^^^ HashDigest.test(" + interactive + ") test=";

for (int test = firstTest; test <= lastTest; test++) {
try {
long time = System.currentTimeMillis();
String2.log(msg + test);

if (interactive) {
//if (test == 0) ...;

} else {
if (test == 0) basicTest();
}

String2.log(msg + test + " finished successfully in " + (System.currentTimeMillis() - time) + " ms.");
} catch (Throwable testThrowable) {
String eMsg = msg + test + " caught throwable:\n" +
MustBe.throwableToString(testThrowable);
errorSB.append(eMsg);
String2.log(eMsg);
if (interactive)
String2.pressEnterToContinue("");
}
}
}


/**
* This is used when called from the command line.
* It explicitly calls System.exit(0) when done.
Expand Down
158 changes: 23 additions & 135 deletions WEB-INF/classes/com/cohort/util/Image2.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,18 @@
package com.cohort.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.awt.image.RenderedImage;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Writer;

import java.net.URL;

Expand Down Expand Up @@ -61,7 +51,9 @@ public class Image2 {
/** Java font drawing isn't consistent in minor ways.
* Change this to change the sensitivity of Image2.compareImages().
*/
public static int DEFAULT_ALLOW_N_PIXELS_DIFFERENT = 250;
public static final int DEFAULT_ALLOW_N_PIXELS_DIFFERENT = 250;
public static final boolean DEFAULT_AUTO_CREATE_IF_MISSING = true;
public static final boolean DEFAULT_DISPLAY_IMAGES = false;

/**
* This tries to load the specified image (gif/jpg/png).
Expand Down Expand Up @@ -469,6 +461,7 @@ public static BufferedImage makeImageBackgroundTransparent(Image image,
return makeImageFromArray(pixels, imageWidth, imageHeight, millis);
}

// TODO: Move this to test file after all uses of this are moved to test files.
/**
* This is like the other testImagesIdentical, but uses DEFAULT_ALLOW_N_PIXELS_DIFFERENT.
*
Expand All @@ -480,7 +473,7 @@ public static BufferedImage makeImageBackgroundTransparent(Image image,
*/
public static void testImagesIdentical(String observed, String expected,
String diffName) throws Exception {
testImagesIdentical(observed, expected, diffName, DEFAULT_ALLOW_N_PIXELS_DIFFERENT);
testImagesIdentical(observed, expected, diffName, DEFAULT_ALLOW_N_PIXELS_DIFFERENT, DEFAULT_AUTO_CREATE_IF_MISSING, DEFAULT_DISPLAY_IMAGES);
}

/**
Expand All @@ -496,17 +489,23 @@ public static void testImagesIdentical(String observed, String expected,
* @throws Exception if the images are different or there is trouble
*/
public static void testImagesIdentical(String observed, String expected,
String diffName, int allowNPixelsDifferent) throws Exception {
String diffName, int allowNPixelsDifferent, boolean autoCreateMissing, boolean displayImages) throws Exception {

//if expected doesn't exist, save observed as expected?
if (!File2.isFile(expected)) {
Test.displayInBrowser("file://" + observed);
if (String2.getStringFromSystemIn("Error at\n" + MustBe.getStackTrace() +
"testImagesIdentical: expected image file doesn't exist. Create it from observed (y/n)? ").equals("y")) {
if (autoCreateMissing) {
System.out.println("Expected image file doesn't exist, creating it from observed.");
File2.copy(observed, expected);
return;
}
throw new RuntimeException("expectedFile=" + expected + " doesn't exist.");
}else {
if (String2.getStringFromSystemIn("Error at\n" + MustBe.getStackTrace() +
"testImagesIdentical: expected image file doesn't exist. Create it from observed (y/n)? ").equals("y")) {
File2.copy(observed, expected);
return;
}
throw new RuntimeException("expectedFile=" + expected + " doesn't exist.");
}
return;
}

//if diffName not .png, throw exception
Expand All @@ -519,10 +518,12 @@ public static void testImagesIdentical(String observed, String expected,
String error = compareImages(obsImg, expImg, diffName, allowNPixelsDifferent); //error.length>0 if > allowNPixelsDifferent
if (error.length() == 0)
return;
Test.displayInBrowser("file://" + observed);
Test.displayInBrowser("file://" + expected);
if (File2.isFile(diffName))
Test.displayInBrowser("file://" + diffName);
if (displayImages) {
Test.displayInBrowser("file://" + observed);
Test.displayInBrowser("file://" + expected);
if (File2.isFile(diffName))
Test.displayInBrowser("file://" + diffName);
}
throw new RuntimeException(
"testImagesIdentical found differences:\n" +
error + "\n" +
Expand Down Expand Up @@ -779,118 +780,5 @@ public static int rgbToWeb216Color(int rgb) {
// */

//}

/**
* This intentionally throws an Exception to test testImagesIdentical().
*/
public static void testTestImagesIdentical() throws Exception {
String2.log("\n*** Image2.testTestImagesIdentical");

//test images which are identical
String testDir = String2.unitTestDataDir + "images/";
String tempDir = File2.getSystemTempDirectory();
testImagesIdentical(
testDir + "testImagesIdentical_1.png",
testDir + "testImagesIdentical_1.png",
tempDir + "testImagesIdentical_diff.png");

//test images which aren't identical
try {

//one time: createImage with transparent background
//BufferedImage image2 = getImage(testDir + "testImagesIdentical_1.png", 2000, false);
//image2 = makeImageBackgroundTransparent(image2, Color.white, 10000);
//saveAsPng(image2, testDir + "testImagesIdentical_2.png");

//test images which aren't identical
testImagesIdentical(
testDir + "testImagesIdentical_1.png",
testDir + "testImagesIdentical_2.png",
tempDir + "testImagesIdentical_diff.png");
} catch (Exception e) {
Test.knownProblem("Not a problem! I'm just testing that Image2.testImagesIdentical() works.");
}
throw new RuntimeException("shouldn't get here");
}

/**
* Test the methods in Image2.
*/
public static void basicTest() throws Exception {
String2.log("\n*** Image2.basicTest");

String imageDir = File2.getClassPath() + //with / separator and / at the end
"com/cohort/util/";

//test ImageIO
BufferedImage bi = ImageIO.read(new File(imageDir + "testmap.gif"));
Graphics g = bi.getGraphics();
ImageIO.write(bi, "png", new File(imageDir + "temp.png"));
Image2.saveAsGif(bi, imageDir + "temp.gif");

long localTime = System.currentTimeMillis();
String2.log("test() here 1");
Color gray = new Color(128,128,128);
String2.log("test() here 2=" + (System.currentTimeMillis() - localTime));
Image image = Image2.getImage(imageDir + "temp.gif",
10000, false); // javaShouldCache
String2.log("test() here 3=" + (System.currentTimeMillis() - localTime));

//convert 128,128,128 to transparent
image = Image2.makeImageBackgroundTransparent(image, gray, 10000);
String2.log("test() here 4=" + (System.currentTimeMillis() - localTime));

//save as gif again
Image2.saveAsGif(image, imageDir + "temp2.gif");
String2.log("test() here 5=" + (System.currentTimeMillis() - localTime));

File2.delete(imageDir + "temp.png");
File2.delete(imageDir + "temp.gif");
File2.delete(imageDir + "temp2.gif");

}

/**
* This runs all of the interactive or not interactive tests for this class.
*
* @param errorSB all caught exceptions are logged to this.
* @param interactive If true, this runs all of the interactive tests;
* otherwise, this runs all of the non-interactive tests.
* @param doSlowTestsToo If true, this runs the slow tests, too.
* @param firstTest The first test to be run (0...). Test numbers may change.
* @param lastTest The last test to be run, inclusive (0..., or -1 for the last test).
* Test numbers may change.
*/
public static void test(StringBuilder errorSB, boolean interactive,
boolean doSlowTestsToo, int firstTest, int lastTest) {
if (lastTest < 0)
lastTest = interactive? -1 : 1;
String msg = "\n^^^ Image2.test(" + interactive + ") test=";

for (int test = firstTest; test <= lastTest; test++) {
try {
long time = System.currentTimeMillis();
String2.log(msg + test);

if (interactive) {
//if (test == 0) ...;

} else {
if (test == 0) basicTest();
if (test == 1) testTestImagesIdentical();
}

String2.log(msg + test + " finished successfully in " + (System.currentTimeMillis() - time) + " ms.");
} catch (Throwable testThrowable) {
String eMsg = msg + test + " caught throwable:\n" +
MustBe.throwableToString(testThrowable);
errorSB.append(eMsg);
String2.log(eMsg);
if (interactive)
String2.pressEnterToContinue("");
}
}
}

}

Loading

0 comments on commit 7bab64c

Please sign in to comment.