More minor javadoc, code tweaks
[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.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;
25
26 import javax.imageio.ImageIO;
27 import java.awt.image.BufferedImage;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FilenameFilter;
31 import java.io.IOException;
32 import java.io.InputStreamReader;
33 import java.net.URL;
34
35 /**
36  * @author srowen@google.com (Sean Owen)
37  */
38 public abstract class AbstractBlackBoxTestCase extends TestCase {
39
40   private static final FilenameFilter IMAGE_NAME_FILTER = new FilenameFilter() {
41     public boolean accept(File dir, String name) {
42       String lowerCase = name.toLowerCase();
43       return
44           lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") ||
45           lowerCase.endsWith(".gif") || lowerCase.endsWith(".png") ||
46                       lowerCase.endsWith(".url");
47     }
48   };
49
50   private final File testBase;
51   private final Reader barcodeReader;
52         private final double passPercent;
53
54   protected AbstractBlackBoxTestCase(File testBase, Reader barcodeReader, double passPercent) {
55     this.testBase = testBase;
56     this.barcodeReader = barcodeReader;
57           this.passPercent = passPercent;
58   }
59
60   public void testBlackBox() throws IOException {
61
62     assertTrue("Please run from the 'core' directory", testBase.exists());
63
64     File[] imageFiles = testBase.listFiles(IMAGE_NAME_FILTER);
65     int passedCount = 0;
66     for (File testImage : imageFiles) {
67       System.out.println("Starting " + testImage.getAbsolutePath());
68
69             BufferedImage image;
70             if (testImage.getName().endsWith(".url")) {
71                     String urlString = readFileAsString(testImage);
72                     image = ImageIO.read(new URL(urlString));
73             } else {
74                     image = ImageIO.read(testImage);
75             }
76       MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(image);
77       Result result;
78       try {
79         result = barcodeReader.decode(source);
80       } catch (ReaderException re) {
81         System.out.println(re);
82         continue;
83       }
84
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();
90
91       if (expectedText.equals(resultText)) {
92         passedCount++;
93       } else {
94         System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText + '\'');
95       }
96
97     }
98
99     System.out.println(passedCount + " of " + imageFiles.length + " images passed");
100     assertTrue("Too many images failed", passedCount >= (int) (imageFiles.length * passPercent));
101   }
102
103   private static String readFileAsString(File file) throws IOException {
104     StringBuilder result = new StringBuilder();
105     InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
106     try {
107       char[] buffer = new char[256];
108       int charsRead;
109       while ((charsRead = reader.read(buffer)) > 0) {
110         result.append(buffer, 0, charsRead);
111       }
112     } finally {
113       reader.close();
114     }
115     return result.toString();
116   }
117
118 }