2 * Copyright 2008 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.common;
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.MonochromeBitmapSource;
21 import com.google.zxing.Reader;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.Result;
24 import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource;
25 import junit.framework.TestCase;
27 import javax.imageio.ImageIO;
28 import java.awt.image.BufferedImage;
30 import java.io.FileInputStream;
31 import java.io.FilenameFilter;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
37 * @author srowen@google.com (Sean Owen)
39 public abstract class AbstractBlackBoxTestCase extends TestCase {
41 private static final FilenameFilter IMAGE_NAME_FILTER = new FilenameFilter() {
42 public boolean accept(File dir, String name) {
43 String lowerCase = name.toLowerCase();
45 lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") ||
46 lowerCase.endsWith(".gif") || lowerCase.endsWith(".png") ||
47 lowerCase.endsWith(".url");
51 private final File testBase;
52 private final Reader barcodeReader;
53 private final double passPercent;
54 private final BarcodeFormat expectedFormat;
56 protected AbstractBlackBoxTestCase(File testBase,
59 BarcodeFormat expectedFormat) {
60 this.testBase = testBase;
61 this.barcodeReader = barcodeReader;
62 this.passPercent = passPercent;
63 this.expectedFormat = expectedFormat;
66 public void testBlackBox() throws IOException {
68 assertTrue("Please run from the 'core' directory", testBase.exists());
70 File[] imageFiles = testBase.listFiles(IMAGE_NAME_FILTER);
72 for (File testImage : imageFiles) {
73 System.out.println("Starting " + testImage.getAbsolutePath());
76 if (testImage.getName().endsWith(".url")) {
77 String urlString = readFileAsString(testImage);
78 image = ImageIO.read(new URL(urlString));
80 image = ImageIO.read(testImage);
82 MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
85 result = barcodeReader.decode(source);
86 } catch (ReaderException re) {
87 System.out.println(re);
91 assertEquals(expectedFormat, result.getBarcodeFormat());
93 String testImageFileName = testImage.getName();
94 File expectedTextFile =
95 new File(testBase, testImageFileName.substring(0, testImageFileName.indexOf('.')) + ".txt");
96 String expectedText = readFileAsString(expectedTextFile);
97 String resultText = result.getText();
99 if (expectedText.equals(resultText)) {
102 System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText + '\'');
107 System.out.println(passedCount + " of " + imageFiles.length + " images passed");
108 assertTrue("Too many images failed", passedCount >= (int) (imageFiles.length * passPercent));
111 private static String readFileAsString(File file) throws IOException {
112 StringBuilder result = new StringBuilder();
113 InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
115 char[] buffer = new char[256];
117 while ((charsRead = reader.read(buffer)) > 0) {
118 result.append(buffer, 0, charsRead);
123 return result.toString();