From ae9c87fdbab4841c939f9ee746903a0d31868043 Mon Sep 17 00:00:00 2001 From: Sudhir Mohanraj Date: Tue, 25 Feb 2014 22:15:53 -0600 Subject: [PATCH 1/2] Issue1-Add test cases for ReportGenerator. --- AUTHORS.adoc | 5 + .../sf/javaanpr/gui/ReportGeneratorTest.java | 181 ++++++++++++++++++ .../sf/javaanpr/test/util/TestUtility.java | 22 +++ 3 files changed, 208 insertions(+) create mode 100644 src/test/java/net/sf/javaanpr/gui/ReportGeneratorTest.java create mode 100644 src/test/java/net/sf/javaanpr/test/util/TestUtility.java diff --git a/AUTHORS.adoc b/AUTHORS.adoc index ca9d5dc..f541035 100644 --- a/AUTHORS.adoc +++ b/AUTHORS.adoc @@ -17,4 +17,9 @@ |skopekondrej@gmail.com |Maintainer +|Sudhir Mohanraj +|sudhirmohanraj +|sudhirmohan87@gmail.com +|Contributor + |=== diff --git a/src/test/java/net/sf/javaanpr/gui/ReportGeneratorTest.java b/src/test/java/net/sf/javaanpr/gui/ReportGeneratorTest.java new file mode 100644 index 0000000..cca6eec --- /dev/null +++ b/src/test/java/net/sf/javaanpr/gui/ReportGeneratorTest.java @@ -0,0 +1,181 @@ +package net.sf.javaanpr.gui; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +import net.sf.javaanpr.imageanalysis.CarSnapshot; +import net.sf.javaanpr.test.util.TestUtility; + +import org.junit.Test; + +/** + * Tests the class {@link ReportGenerator} + */ +public class ReportGeneratorTest { + TestUtility testUtility = new TestUtility(); + private CarSnapshot carSnapshot; + + /** + * Tests {@link ReportGenerator#insertImage(BufferedImage, String, int, int)} + * @throws IllegalArgumentException the illegal argument exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testInsertImage_Valid() throws IllegalArgumentException, IOException { + final int w = 1; + final CarSnapshot carSnapshot = new CarSnapshot("snapshots/test_001.jpg"); + final BufferedImage image = carSnapshot.renderGraph(); + final String cls = "test"; + final int h = 1; + final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + try { + reportGenerator.insertImage(image, cls, w, h); + } catch (final Exception e) { + fail(); + } + } + + /** + * Tests {@link ReportGenerator#insertImage(BufferedImage, String, int, int)} throws an error when the input is not + * valid. + * @throws IllegalArgumentException the illegal argument exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testInsertImage_BadInput() throws IllegalArgumentException, IOException { + try { + final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final int w = 1; + final CarSnapshot carSnapshot = new CarSnapshot("snapshots/test_00.jpg"); + final BufferedImage image = carSnapshot.renderGraph(); + final String cls = "test"; + final int h = 1; + reportGenerator.insertImage(image, cls, w, h); + } catch (final Exception e) { + assertEquals("input == null!", e.getMessage()); + } + } + + /** + * Tests {@link ReportGenerator#insertText(String)} with null input. + * @throws IllegalArgumentException the illegal argument exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testInsertText_NullInput() throws IllegalArgumentException, IOException { + try { + final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + reportGenerator.insertText(null); + } catch (final Exception e) { + fail(); + } + } + + /** + * Tests {@link ReportGenerator#insertText(String)} with empty string input. + * @throws IllegalArgumentException the illegal argument exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testInsertText_EmptyInput() throws IllegalArgumentException, IOException { + try { + final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + reportGenerator.insertText(""); + } catch (final Exception e) { + fail(); + } + } + + /** + * Tests {@link ReportGenerator#saveStreamToFile(java.io.InputStream, java.io.File)} with null input stream. + * @throws IllegalArgumentException the illegal argument exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testSaveStreamToFile_InvalidInput() throws IllegalArgumentException, IOException { + try { + final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final InputStream inStream = null; + final File io = new File("out.txt"); + reportGenerator.saveStreamToFile(null, io); + } catch (final Exception e) { + assertEquals(null, e.getMessage()); + } + } + + /** + * Tests {@link ReportGenerator#saveStreamToFile(java.io.InputStream, java.io.File)} with null output stream input. + * @throws IllegalArgumentException the illegal argument exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testSaveStreamToFile_InvalidOutput() throws IllegalArgumentException, IOException { + try { + final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final InputStream inStream = new FileInputStream("src/test/resources/snapshots/test_001.jpg"); + reportGenerator.saveStreamToFile(inStream, null); + } catch (final Exception e) { + assertEquals(null, e.getMessage()); + } + } + + /** + * Tests {@link ReportGenerator#saveStreamToFile(java.io.InputStream, java.io.File)} with valid input and output + * stream. + * @throws IllegalArgumentException the illegal argument exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testSaveStreamToFile_Valid() throws IllegalArgumentException, IOException { + final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final InputStream inStream = new FileInputStream("src/test/resources/snapshots/test_001.jpg"); + final File io = new File("src/test/resources/out.txt"); + reportGenerator.saveStreamToFile(inStream, io); + StringBuilder sb = new StringBuilder(); + sb = testUtility.readFile("src/test/resources/out.txt"); + assertEquals(true, sb.toString().contains("Hewlett-Packard")); + } + + /** + * Tests {@link ReportGenerator#saveImage(BufferedImage, String)} with valid input. + * @throws IllegalArgumentException the illegal argument exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testSaveImage_Valid() throws IllegalArgumentException, IOException { + try { + final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final CarSnapshot carSnapshot = new CarSnapshot("snapshots/test_001.jpg"); + final BufferedImage image = carSnapshot.renderGraph(); + final String cls = "test"; + final int h = 0; + final int w = 0; + reportGenerator.saveImage(image, "png"); + } catch (final Exception e) { + fail(e.getMessage()); + } + } + + /** + * Tests {@link ReportGenerator#saveImage(BufferedImage, String)} with valid input. + * @throws IllegalArgumentException the illegal argument exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testSaveImage_InvalidInput() throws IllegalArgumentException, IOException { + try { + final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); //$NON-NLS-1$ + final CarSnapshot carSnapshot = new CarSnapshot("snapshots/test_001.jpg"); //$NON-NLS-1$ + final BufferedImage image = carSnapshot.renderGraph(); + reportGenerator.saveImage(image, "txt"); //$NON-NLS-1$ + } catch (final Exception e) { + assertEquals("Unsupported file format", e.getMessage()); //$NON-NLS-1$ + } + } +} \ No newline at end of file diff --git a/src/test/java/net/sf/javaanpr/test/util/TestUtility.java b/src/test/java/net/sf/javaanpr/test/util/TestUtility.java new file mode 100644 index 0000000..32275b8 --- /dev/null +++ b/src/test/java/net/sf/javaanpr/test/util/TestUtility.java @@ -0,0 +1,22 @@ +package net.sf.javaanpr.test.util; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +/** + * Utility class which helps in having methods for Testing. + */ +public class TestUtility { + + public StringBuilder readFile(final String filename) throws IOException { + final BufferedReader br = new BufferedReader(new FileReader(filename)); + final StringBuilder sb = new StringBuilder(); + String currentLine = null; + while ((currentLine = br.readLine()) != null) { + sb.append(currentLine); + } + return sb; + } + +} From c5875ffbf96f3f907d942d441280e6e746594f4f Mon Sep 17 00:00:00 2001 From: Sudhir Mohanraj Date: Fri, 28 Feb 2014 15:48:29 -0600 Subject: [PATCH 2/2] Issue1-Pull Request Comments Fixed --- .../sf/javaanpr/gui/ReportGeneratorTest.java | 101 +++++++++++++++--- .../sf/javaanpr/test/util/TestUtility.java | 67 ++++++++++++ 2 files changed, 151 insertions(+), 17 deletions(-) diff --git a/src/test/java/net/sf/javaanpr/gui/ReportGeneratorTest.java b/src/test/java/net/sf/javaanpr/gui/ReportGeneratorTest.java index cca6eec..ea8239d 100644 --- a/src/test/java/net/sf/javaanpr/gui/ReportGeneratorTest.java +++ b/src/test/java/net/sf/javaanpr/gui/ReportGeneratorTest.java @@ -1,3 +1,70 @@ +/* +------------------------------------------------------------------------ +JavaANPR - Automatic Number Plate Recognition System for Java +------------------------------------------------------------------------ + +This file is a part of the JavaANPR, licensed under the terms of the +Educational Community License + +Copyright (c) 2006-2007 Ondrej Martinsky. All rights reserved + +This Original Work, including software, source code, documents, or +other related items, is being provided by the copyright holder(s) +subject to the terms of the Educational Community License. By +obtaining, using and/or copying this Original Work, you agree that you +have read, understand, and will comply with the following terms and +conditions of the Educational Community License: + +Permission to use, copy, modify, merge, publish, distribute, and +sublicense this Original Work and its documentation, with or without +modification, for any purpose, and without fee or royalty to the +copyright holder(s) is hereby granted, provided that you include the +following on ALL copies of the Original Work or portions thereof, +including modifications or derivatives, that you make: + +# The full text of the Educational Community License in a location +viewable to users of the redistributed or derivative work. + +# Any pre-existing intellectual property disclaimers, notices, or terms +and conditions. + +# Notice of any changes or modifications to the Original Work, +including the date the changes were made. + +# Any modifications of the Original Work must be distributed in such a +manner as to avoid any confusion with the Original Work of the +copyright holders. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +The name and trademarks of copyright holder(s) may NOT be used in +advertising or publicity pertaining to the Original or Derivative Works +without specific, written prior permission. Title to copyright in the +Original Work and any associated documentation will at all times remain +with the copyright holders. + +If you want to alter upon this work, you MUST attribute it in +a) all source files +b) on every place, where is the copyright of derivated work +exactly by the following label : + +---- label begin ---- +This work is a derivate of the JavaANPR. JavaANPR is a intellectual +property of Ondrej Martinsky. Please visit http://javaanpr.sourceforge.net +for more info about JavaANPR. +---- label end ---- + +------------------------------------------------------------------------ + http://javaanpr.sourceforge.net +------------------------------------------------------------------------ + */ + package net.sf.javaanpr.gui; import static org.junit.Assert.assertEquals; @@ -22,7 +89,7 @@ public class ReportGeneratorTest { private CarSnapshot carSnapshot; /** - * Tests {@link ReportGenerator#insertImage(BufferedImage, String, int, int)} + * Tests {@link ReportGenerator#insertImage(BufferedImage, String, int, int)} with valid inputs. * @throws IllegalArgumentException the illegal argument exception * @throws IOException Signals that an I/O exception has occurred. */ @@ -33,7 +100,7 @@ public void testInsertImage_Valid() throws IllegalArgumentException, IOException final BufferedImage image = carSnapshot.renderGraph(); final String cls = "test"; final int h = 1; - final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final ReportGenerator reportGenerator = new ReportGenerator("target/test-classes/"); try { reportGenerator.insertImage(image, cls, w, h); } catch (final Exception e) { @@ -50,7 +117,7 @@ public void testInsertImage_Valid() throws IllegalArgumentException, IOException @Test public void testInsertImage_BadInput() throws IllegalArgumentException, IOException { try { - final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final ReportGenerator reportGenerator = new ReportGenerator("target/test-classes/"); final int w = 1; final CarSnapshot carSnapshot = new CarSnapshot("snapshots/test_00.jpg"); final BufferedImage image = carSnapshot.renderGraph(); @@ -70,7 +137,7 @@ public void testInsertImage_BadInput() throws IllegalArgumentException, IOExcept @Test public void testInsertText_NullInput() throws IllegalArgumentException, IOException { try { - final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final ReportGenerator reportGenerator = new ReportGenerator("target/test-classes/"); reportGenerator.insertText(null); } catch (final Exception e) { fail(); @@ -85,7 +152,7 @@ public void testInsertText_NullInput() throws IllegalArgumentException, IOExcept @Test public void testInsertText_EmptyInput() throws IllegalArgumentException, IOException { try { - final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final ReportGenerator reportGenerator = new ReportGenerator("target/test-classes/"); reportGenerator.insertText(""); } catch (final Exception e) { fail(); @@ -100,9 +167,9 @@ public void testInsertText_EmptyInput() throws IllegalArgumentException, IOExcep @Test public void testSaveStreamToFile_InvalidInput() throws IllegalArgumentException, IOException { try { - final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final ReportGenerator reportGenerator = new ReportGenerator("target/test-classes/"); final InputStream inStream = null; - final File io = new File("out.txt"); + final File io = new File("target/test-classes/out.txt"); reportGenerator.saveStreamToFile(null, io); } catch (final Exception e) { assertEquals(null, e.getMessage()); @@ -117,7 +184,7 @@ public void testSaveStreamToFile_InvalidInput() throws IllegalArgumentException, @Test public void testSaveStreamToFile_InvalidOutput() throws IllegalArgumentException, IOException { try { - final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final ReportGenerator reportGenerator = new ReportGenerator("target/test-classes/"); final InputStream inStream = new FileInputStream("src/test/resources/snapshots/test_001.jpg"); reportGenerator.saveStreamToFile(inStream, null); } catch (final Exception e) { @@ -133,12 +200,12 @@ public void testSaveStreamToFile_InvalidOutput() throws IllegalArgumentException */ @Test public void testSaveStreamToFile_Valid() throws IllegalArgumentException, IOException { - final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final ReportGenerator reportGenerator = new ReportGenerator("target/test-classes/"); final InputStream inStream = new FileInputStream("src/test/resources/snapshots/test_001.jpg"); - final File io = new File("src/test/resources/out.txt"); + final File io = new File("target/test-classes/out.txt"); reportGenerator.saveStreamToFile(inStream, io); StringBuilder sb = new StringBuilder(); - sb = testUtility.readFile("src/test/resources/out.txt"); + sb = testUtility.readFile("target/test-classes/out.txt"); assertEquals(true, sb.toString().contains("Hewlett-Packard")); } @@ -150,7 +217,7 @@ public void testSaveStreamToFile_Valid() throws IllegalArgumentException, IOExce @Test public void testSaveImage_Valid() throws IllegalArgumentException, IOException { try { - final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); + final ReportGenerator reportGenerator = new ReportGenerator("target/test-classes/"); final CarSnapshot carSnapshot = new CarSnapshot("snapshots/test_001.jpg"); final BufferedImage image = carSnapshot.renderGraph(); final String cls = "test"; @@ -163,19 +230,19 @@ public void testSaveImage_Valid() throws IllegalArgumentException, IOException { } /** - * Tests {@link ReportGenerator#saveImage(BufferedImage, String)} with valid input. + * Tests {@link ReportGenerator#saveImage(BufferedImage, String)} with invalid string input and a valid image input * @throws IllegalArgumentException the illegal argument exception * @throws IOException Signals that an I/O exception has occurred. */ @Test public void testSaveImage_InvalidInput() throws IllegalArgumentException, IOException { try { - final ReportGenerator reportGenerator = new ReportGenerator("src/test/resources/"); //$NON-NLS-1$ - final CarSnapshot carSnapshot = new CarSnapshot("snapshots/test_001.jpg"); //$NON-NLS-1$ + final ReportGenerator reportGenerator = new ReportGenerator("target/test-classes/"); + final CarSnapshot carSnapshot = new CarSnapshot("snapshots/test_001.jpg"); final BufferedImage image = carSnapshot.renderGraph(); - reportGenerator.saveImage(image, "txt"); //$NON-NLS-1$ + reportGenerator.saveImage(image, "target/test-classes/txt"); } catch (final Exception e) { - assertEquals("Unsupported file format", e.getMessage()); //$NON-NLS-1$ + assertEquals("Unsupported file format", e.getMessage()); } } } \ No newline at end of file diff --git a/src/test/java/net/sf/javaanpr/test/util/TestUtility.java b/src/test/java/net/sf/javaanpr/test/util/TestUtility.java index 32275b8..1d4f97c 100644 --- a/src/test/java/net/sf/javaanpr/test/util/TestUtility.java +++ b/src/test/java/net/sf/javaanpr/test/util/TestUtility.java @@ -1,3 +1,70 @@ +/* +------------------------------------------------------------------------ +JavaANPR - Automatic Number Plate Recognition System for Java +------------------------------------------------------------------------ + +This file is a part of the JavaANPR, licensed under the terms of the +Educational Community License + +Copyright (c) 2006-2007 Ondrej Martinsky. All rights reserved + +This Original Work, including software, source code, documents, or +other related items, is being provided by the copyright holder(s) +subject to the terms of the Educational Community License. By +obtaining, using and/or copying this Original Work, you agree that you +have read, understand, and will comply with the following terms and +conditions of the Educational Community License: + +Permission to use, copy, modify, merge, publish, distribute, and +sublicense this Original Work and its documentation, with or without +modification, for any purpose, and without fee or royalty to the +copyright holder(s) is hereby granted, provided that you include the +following on ALL copies of the Original Work or portions thereof, +including modifications or derivatives, that you make: + +# The full text of the Educational Community License in a location +viewable to users of the redistributed or derivative work. + +# Any pre-existing intellectual property disclaimers, notices, or terms +and conditions. + +# Notice of any changes or modifications to the Original Work, +including the date the changes were made. + +# Any modifications of the Original Work must be distributed in such a +manner as to avoid any confusion with the Original Work of the +copyright holders. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +The name and trademarks of copyright holder(s) may NOT be used in +advertising or publicity pertaining to the Original or Derivative Works +without specific, written prior permission. Title to copyright in the +Original Work and any associated documentation will at all times remain +with the copyright holders. + +If you want to alter upon this work, you MUST attribute it in +a) all source files +b) on every place, where is the copyright of derivated work +exactly by the following label : + +---- label begin ---- +This work is a derivate of the JavaANPR. JavaANPR is a intellectual +property of Ondrej Martinsky. Please visit http://javaanpr.sourceforge.net +for more info about JavaANPR. +---- label end ---- + +------------------------------------------------------------------------ + http://javaanpr.sourceforge.net +------------------------------------------------------------------------ + */ + package net.sf.javaanpr.test.util; import java.io.BufferedReader;