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.MonochromeBitmapSource;
20 import com.google.zxing.Reader;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.Result;
23 import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource;
24 import junit.framework.TestCase;
26 import javax.imageio.ImageIO;
27 import java.awt.image.BufferedImage;
29 import java.io.FileInputStream;
30 import java.io.FilenameFilter;
31 import java.io.IOException;
32 import java.io.InputStreamReader;
36 * @author srowen@google.com (Sean Owen)
38 public abstract class AbstractBlackBoxTestCase extends TestCase {
40 private static final FilenameFilter IMAGE_NAME_FILTER = new FilenameFilter() {
41 public boolean accept(File dir, String name) {
42 String lowerCase = name.toLowerCase();
44 lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") ||
45 lowerCase.endsWith(".gif") || lowerCase.endsWith(".png") ||
46 lowerCase.endsWith(".url");
50 private final File testBase;
51 private final Reader barcodeReader;
52 private final double passPercent;
54 protected AbstractBlackBoxTestCase(File testBase, Reader barcodeReader, double passPercent) {
55 this.testBase = testBase;
56 this.barcodeReader = barcodeReader;
57 this.passPercent = passPercent;
60 public void testBlackBox() throws IOException {
62 assertTrue("Please run from the 'core' directory", testBase.exists());
64 File[] imageFiles = testBase.listFiles(IMAGE_NAME_FILTER);
66 for (File testImage : imageFiles) {
67 System.out.println("Starting " + testImage.getAbsolutePath());
70 if (testImage.getName().endsWith(".url")) {
71 String urlString = readFileAsString(testImage);
72 image = ImageIO.read(new URL(urlString));
74 image = ImageIO.read(testImage);
76 MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
79 result = barcodeReader.decode(source);
80 } catch (ReaderException re) {
81 System.out.println(re);
85 String testImageFileName = testImage.getName();
86 File expectedTextFile =
87 new File(testBase, testImageFileName.substring(0, testImageFileName.indexOf('.')) + ".txt");
88 String expectedText = readFileAsString(expectedTextFile);
89 String resultText = result.getText();
91 if (expectedText.equals(resultText)) {
94 System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText + '\'');
99 System.out.println(passedCount + " of " + imageFiles.length + " images passed");
100 assertTrue("Too many images failed", passedCount >= (int) (imageFiles.length * passPercent));
103 private static String readFileAsString(File file) throws IOException {
104 StringBuilder result = new StringBuilder();
105 InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
107 char[] buffer = new char[256];
109 while ((charsRead = reader.read(buffer)) > 0) {
110 result.append(buffer, 0, charsRead);
115 return result.toString();