Added BarcodeFormat to Result, indicating what type of barcode was detected. Added...
[zxing.git] / core / test / src / com / google / zxing / common / AbstractBlackBoxTestCase.java
1 /*
2  * Copyright 2008 Google Inc.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.common;
18
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;
26
27 import javax.imageio.ImageIO;
28 import java.awt.image.BufferedImage;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FilenameFilter;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.net.URL;
35
36 /**
37  * @author srowen@google.com (Sean Owen)
38  */
39 public abstract class AbstractBlackBoxTestCase extends TestCase {
40
41   private static final FilenameFilter IMAGE_NAME_FILTER = new FilenameFilter() {
42     public boolean accept(File dir, String name) {
43       String lowerCase = name.toLowerCase();
44       return
45           lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") ||
46               lowerCase.endsWith(".gif") || lowerCase.endsWith(".png") ||
47               lowerCase.endsWith(".url");
48     }
49   };
50
51   private final File testBase;
52   private final Reader barcodeReader;
53   private final double passPercent;
54   private final BarcodeFormat expectedFormat;
55
56   protected AbstractBlackBoxTestCase(File testBase,
57                                      Reader barcodeReader,
58                                      double passPercent,
59                                      BarcodeFormat expectedFormat) {
60     this.testBase = testBase;
61     this.barcodeReader = barcodeReader;
62     this.passPercent = passPercent;
63     this.expectedFormat = expectedFormat;
64   }
65
66   public void testBlackBox() throws IOException {
67
68     assertTrue("Please run from the 'core' directory", testBase.exists());
69
70     File[] imageFiles = testBase.listFiles(IMAGE_NAME_FILTER);
71     int passedCount = 0;
72     for (File testImage : imageFiles) {
73       System.out.println("Starting " + testImage.getAbsolutePath());
74
75       BufferedImage image;
76       if (testImage.getName().endsWith(".url")) {
77         String urlString = readFileAsString(testImage);
78         image = ImageIO.read(new URL(urlString));
79       } else {
80         image = ImageIO.read(testImage);
81       }
82       MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
83       Result result;
84       try {
85         result = barcodeReader.decode(source);
86       } catch (ReaderException re) {
87         System.out.println(re);
88         continue;
89       }
90
91       assertEquals(expectedFormat, result.getBarcodeFormat());
92
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();
98
99       if (expectedText.equals(resultText)) {
100         passedCount++;
101       } else {
102         System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText + '\'');
103       }
104
105     }
106
107     System.out.println(passedCount + " of " + imageFiles.length + " images passed");
108     assertTrue("Too many images failed", passedCount >= (int) (imageFiles.length * passPercent));
109   }
110
111   private static String readFileAsString(File file) throws IOException {
112     StringBuilder result = new StringBuilder();
113     InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
114     try {
115       char[] buffer = new char[256];
116       int charsRead;
117       while ((charsRead = reader.read(buffer)) > 0) {
118         result.append(buffer, 0, charsRead);
119       }
120     } finally {
121       reader.close();
122     }
123     return result.toString();
124   }
125
126 }