Use logging instead of System.out for finer-grained control

git-svn-id: https://zxing.googlecode.com/svn/trunk@2531 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2012-11-25 10:40:44 +00:00
parent e17d1c16ce
commit caf7734595
3 changed files with 52 additions and 42 deletions

View file

@ -49,6 +49,8 @@ import com.google.zxing.qrcode.QRCodeBlackBox3TestCase;
import com.google.zxing.qrcode.QRCodeBlackBox4TestCase;
import com.google.zxing.qrcode.QRCodeBlackBox5TestCase;
import java.util.logging.Logger;
/**
* This is a quick and dirty way to get totals across all the positive black box tests. It is
* necessary because we spawn multiple processes when using the standard test-blackbox Ant target.
@ -59,6 +61,8 @@ import com.google.zxing.qrcode.QRCodeBlackBox5TestCase;
*/
public final class AllPositiveBlackBoxTester {
private static final Logger log = Logger.getLogger(AllPositiveBlackBoxTester.class.getSimpleName());
// This list has to be manually kept up to date. I don't know any automatic way to include every
// subclass of AbstractBlackBoxTestCase, and furthermore to exclude subclasses of
// AbstractNegativeBlackBoxTestCase which derives from it.
@ -96,17 +100,18 @@ public final class AllPositiveBlackBoxTester {
};
private AllPositiveBlackBoxTester() {
System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s%6$s%n");
}
public static void main(String[] args) throws Exception {
long now = System.currentTimeMillis();
long start = System.currentTimeMillis();
SummaryResults results = new SummaryResults();
for (AbstractBlackBoxTestCase test : TESTS) {
results.add(test.testBlackBoxCountingResults(false));
}
now = System.currentTimeMillis() - now;
System.out.println(results.toString() + "\n Total time: " + now + " ms");
log.info(results.toString());
log.info(String.format("Total time: %d ms", System.currentTimeMillis() - start));
}
}

View file

@ -47,6 +47,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
/**
* @author Sean Owen
@ -54,6 +55,8 @@ import java.util.Properties;
*/
public abstract class AbstractBlackBoxTestCase extends Assert {
private static final Logger log = Logger.getLogger(AbstractBlackBoxTestCase.class.getSimpleName());
private static final Charset UTF8 = Charset.forName("UTF-8");
private static final Charset ISO88591 = Charset.forName("ISO-8859-1");
private static final FilenameFilter IMAGE_NAME_FILTER = new FilenameFilter() {
@ -83,6 +86,8 @@ public abstract class AbstractBlackBoxTestCase extends Assert {
this.barcodeReader = barcodeReader;
this.expectedFormat = expectedFormat;
testResults = new ArrayList<TestResult>();
System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s%6$s%n");
}
protected final void addTest(int mustPassCount, int tryHarderCount, float rotation) {
@ -135,7 +140,7 @@ public abstract class AbstractBlackBoxTestCase extends Assert {
int[] tryHaderMisreadCounts = new int[testCount];
for (File testImage : imageFiles) {
System.out.printf("Starting %s\n", testImage.getAbsolutePath());
log.info(String.format("Starting %s", testImage.getAbsolutePath()));
BufferedImage image = ImageIO.read(testImage);
@ -174,8 +179,7 @@ public abstract class AbstractBlackBoxTestCase extends Assert {
misreadCounts[x]++;
}
} catch (ReaderException re) {
System.out.printf("could not read at rotation %f\n", rotation);
// continue
log.fine(String.format("could not read at rotation %f", rotation));
}
try {
if (decode(bitmap, rotation, expectedText, expectedMetadata, true)) {
@ -184,8 +188,7 @@ public abstract class AbstractBlackBoxTestCase extends Assert {
tryHaderMisreadCounts[x]++;
}
} catch (ReaderException re) {
System.out.printf("could not read at rotation %f w/TH\n", rotation);
// continue
log.fine(String.format("could not read at rotation %f w/TH", rotation));
}
}
}
@ -198,17 +201,17 @@ public abstract class AbstractBlackBoxTestCase extends Assert {
for (int x = 0; x < testResults.size(); x++) {
TestResult testResult = testResults.get(x);
System.out.printf("Rotation %d degrees:\n", (int) testResult.getRotation());
System.out.printf(" %d of %d images passed (%d required)\n",
passedCounts[x], imageFiles.length, testResult.getMustPassCount());
log.info(String.format("Rotation %d degrees:", (int) testResult.getRotation()));
log.info(String.format(" %d of %d images passed (%d required)",
passedCounts[x], imageFiles.length, testResult.getMustPassCount()));
int failed = imageFiles.length - passedCounts[x];
System.out.printf(" %d failed due to misreads, %d not detected\n",
misreadCounts[x], failed - misreadCounts[x]);
System.out.printf(" %d of %d images passed with try harder (%d required)\n",
tryHarderCounts[x], imageFiles.length, testResult.getTryHarderCount());
log.info(String.format(" %d failed due to misreads, %d not detected",
misreadCounts[x], failed - misreadCounts[x]));
log.info(String.format(" %d of %d images passed with try harder (%d required)",
tryHarderCounts[x], imageFiles.length, testResult.getTryHarderCount()));
failed = imageFiles.length - tryHarderCounts[x];
System.out.printf(" %d failed due to misreads, %d not detected\n",
tryHaderMisreadCounts[x], failed - tryHaderMisreadCounts[x]);
log.info(String.format(" %d failed due to misreads, %d not detected",
tryHaderMisreadCounts[x], failed - tryHaderMisreadCounts[x]));
totalFound += passedCounts[x] + tryHarderCounts[x];
totalMustPass += testResult.getMustPassCount() + testResult.getTryHarderCount();
totalMisread += misreadCounts[x] + tryHaderMisreadCounts[x];
@ -216,22 +219,20 @@ public abstract class AbstractBlackBoxTestCase extends Assert {
}
int totalTests = imageFiles.length * testCount * 2;
System.out.printf("TOTALS:\nDecoded %d images out of %d (%d%%, %d required)\n",
totalFound, totalTests, totalFound * 100 / totalTests, totalMustPass);
log.info(String.format("Decoded %d images out of %d (%d%%, %d required)",
totalFound, totalTests, totalFound * 100 / totalTests, totalMustPass));
if (totalFound > totalMustPass) {
System.out.printf(" +++ Test too lax by %d images\n", totalFound - totalMustPass);
log.warning(String.format("+++ Test too lax by %d images", totalFound - totalMustPass));
} else if (totalFound < totalMustPass) {
System.out.printf(" --- Test failed by %d images\n", totalMustPass - totalFound);
log.warning(String.format("--- Test failed by %d images", totalMustPass - totalFound));
}
if (totalMisread < totalMaxMisread) {
System.out.printf(" +++ Test expects too many misreads by %d images\n", totalMaxMisread - totalMisread);
log.warning(String.format("+++ Test expects too many misreads by %d images", totalMaxMisread - totalMisread));
} else if (totalMisread > totalMaxMisread) {
System.out.printf(" --- Test had too many misreads by %d images\n", totalMisread - totalMaxMisread);
log.warning(String.format("--- Test had too many misreads by %d images", totalMisread - totalMaxMisread));
}
System.out.flush();
// Then run through again and assert if any failed
if (assertOnFailure) {
for (int x = 0; x < testCount; x++) {
@ -267,15 +268,15 @@ public abstract class AbstractBlackBoxTestCase extends Assert {
Result result = barcodeReader.decode(source, hints);
if (expectedFormat != result.getBarcodeFormat()) {
System.out.printf("Format mismatch: expected '%s' but got '%s'%s\n",
expectedFormat, result.getBarcodeFormat(), suffix);
log.info(String.format("Format mismatch: expected '%s' but got '%s'%s",
expectedFormat, result.getBarcodeFormat(), suffix));
return false;
}
String resultText = result.getText();
if (!expectedText.equals(resultText)) {
System.out.printf("Content mismatch: expected '%s' but got '%s'%s\n",
expectedText, resultText, suffix);
log.info(String.format("Content mismatch: expected '%s' but got '%s'%s",
expectedText, resultText, suffix));
return false;
}
@ -285,8 +286,8 @@ public abstract class AbstractBlackBoxTestCase extends Assert {
Object expectedValue = metadatum.getValue();
Object actualValue = resultMetadata == null ? null : resultMetadata.get(key);
if (!expectedValue.equals(actualValue)) {
System.out.printf("Metadata mismatch for key '%s': expected '%s' but got '%s'\n",
key, expectedValue, actualValue);
log.info(String.format("Metadata mismatch for key '%s': expected '%s' but got '%s'",
key, expectedValue, actualValue));
return false;
}
}

View file

@ -33,6 +33,7 @@ import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
/**
* This abstract class looks for negative results, i.e. it only allows a certain number of false
@ -42,6 +43,8 @@ import java.util.Map;
*/
public abstract class AbstractNegativeBlackBoxTestCase extends AbstractBlackBoxTestCase {
private static final Logger log = Logger.getLogger(AbstractNegativeBlackBoxTestCase.class.getSimpleName());
private static final class TestResult {
private final int falsePositivesAllowed;
private final float rotation;
@ -80,8 +83,7 @@ public abstract class AbstractNegativeBlackBoxTestCase extends AbstractBlackBoxT
File[] imageFiles = getImageFiles();
int[] falsePositives = new int[testResults.size()];
for (File testImage : imageFiles) {
System.out.printf("Starting %s\n", testImage.getAbsolutePath());
log.info(String.format("Starting %s", testImage.getAbsolutePath()));
BufferedImage image = ImageIO.read(testImage);
if (image == null) {
throw new IOException("Could not read image: " + testImage);
@ -104,16 +106,16 @@ public abstract class AbstractNegativeBlackBoxTestCase extends AbstractBlackBoxT
}
if (totalFalsePositives < totalAllowed) {
System.out.printf(" +++ Test too lax by %d images\n", totalAllowed - totalFalsePositives);
log.warning(String.format("+++ Test too lax by %d images", totalAllowed - totalFalsePositives));
} else if (totalFalsePositives > totalAllowed) {
System.out.printf(" --- Test failed by %d images\n", totalFalsePositives - totalAllowed);
log.warning(String.format("--- Test failed by %d images", totalFalsePositives - totalAllowed));
}
for (int x = 0; x < testResults.size(); x++) {
TestResult testResult = testResults.get(x);
System.out.printf("Rotation %d degrees: %d of %d images were false positives (%d allowed)\n",
(int) testResult.getRotation(), falsePositives[x], imageFiles.length,
testResult.getFalsePositivesAllowed());
log.info(String.format("Rotation %d degrees: %d of %d images were false positives (%d allowed)",
(int) testResult.getRotation(), falsePositives[x], imageFiles.length,
testResult.getFalsePositivesAllowed()));
assertTrue("Rotation " + testResult.getRotation() + " degrees: Too many false positives found",
falsePositives[x] <= testResult.getFalsePositivesAllowed());
}
@ -133,10 +135,11 @@ public abstract class AbstractNegativeBlackBoxTestCase extends AbstractBlackBoxT
Result result;
try {
result = getReader().decode(bitmap);
System.out.printf("Found false positive: '%s' with format '%s' (rotation: %d)\n",
result.getText(), result.getBarcodeFormat(), (int) rotationInDegrees);
log.info(String.format("Found false positive: '%s' with format '%s' (rotation: %d)",
result.getText(), result.getBarcodeFormat(), (int) rotationInDegrees));
return false;
} catch (ReaderException re) {
// continue
}
// Try "try harder" getMode
@ -144,10 +147,11 @@ public abstract class AbstractNegativeBlackBoxTestCase extends AbstractBlackBoxT
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
try {
result = getReader().decode(bitmap, hints);
System.out.printf("Try harder found false positive: '%s' with format '%s' (rotation: %d)\n",
result.getText(), result.getBarcodeFormat(), (int) rotationInDegrees);
log.info(String.format("Try harder found false positive: '%s' with format '%s' (rotation: %d)",
result.getText(), result.getBarcodeFormat(), (int) rotationInDegrees));
return false;
} catch (ReaderException re) {
// continue
}
return true;
}