From dc3fa902da0209c05f9a032f36341567d3af1dce Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Wed, 3 Aug 2005 12:35:04 +0000 Subject: [PATCH] Added unit tests for ImportControlCheck. --- .../checkstyle/import-control_broken.xml | 7 ++ .../tools/checkstyle/import-control_one.xml | 12 +++ .../tools/checkstyle/import-control_two.xml | 14 ++++ .../imports/InputImportControl.java | 10 +++ .../imports/ImportControlCheckTest.java | 80 +++++++++++++++++++ 5 files changed, 123 insertions(+) create mode 100755 src/testinputs/com/puppycrawl/tools/checkstyle/import-control_broken.xml create mode 100755 src/testinputs/com/puppycrawl/tools/checkstyle/import-control_one.xml create mode 100755 src/testinputs/com/puppycrawl/tools/checkstyle/import-control_two.xml create mode 100755 src/testinputs/com/puppycrawl/tools/checkstyle/imports/InputImportControl.java create mode 100755 src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheckTest.java diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/import-control_broken.xml b/src/testinputs/com/puppycrawl/tools/checkstyle/import-control_broken.xml new file mode 100755 index 000000000..02f9b152d --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/import-control_broken.xml @@ -0,0 +1,7 @@ + + + + + diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/import-control_one.xml b/src/testinputs/com/puppycrawl/tools/checkstyle/import-control_one.xml new file mode 100755 index 000000000..869dc6565 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/import-control_one.xml @@ -0,0 +1,12 @@ + + + + + + + + + + diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/import-control_two.xml b/src/testinputs/com/puppycrawl/tools/checkstyle/import-control_two.xml new file mode 100755 index 000000000..1ae5264d8 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/import-control_two.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/imports/InputImportControl.java b/src/testinputs/com/puppycrawl/tools/checkstyle/imports/InputImportControl.java new file mode 100755 index 000000000..c9b79b33d --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/imports/InputImportControl.java @@ -0,0 +1,10 @@ +package com.puppycrawl.tools.checkstyle.imports; + +import java.awt.Image; +import javax.swing.border.*; +import java.io.File; + +public class InputImportControl +{ + +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheckTest.java new file mode 100755 index 000000000..107d078a4 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlCheckTest.java @@ -0,0 +1,80 @@ +package com.puppycrawl.tools.checkstyle.checks.imports; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import java.io.File; + +public class ImportControlCheckTest extends BaseCheckTestCase +{ + public void testOne() throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class); + checkConfig.addAttribute("file", System.getProperty("testinputs.dir") + + "/import-control_one.xml"); + final String[] expected = {"5:1: Disallowed import - java.io.File."}; + + verify(checkConfig, getPath("imports" + File.separator + + "InputImportControl.java"), expected); + } + + public void testTwo() throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class); + checkConfig.addAttribute("file", System.getProperty("testinputs.dir") + + "/import-control_two.xml"); + final String[] expected = {"3:1: Disallowed import - java.awt.Image.", + "4:1: Disallowed import - javax.swing.border.*."}; + + verify(checkConfig, getPath("imports" + File.separator + + "InputImportControl.java"), expected); + } + + public void testMissing() throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class); + final String[] expected = {"1:40: Missing an import control file."}; + verify(checkConfig, getPath("imports" + File.separator + + "InputImportControl.java"), expected); + } + + public void testEmpty() throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class); + checkConfig.addAttribute("file", " "); + final String[] expected = {"1:40: Missing an import control file."}; + verify(checkConfig, getPath("imports" + File.separator + + "InputImportControl.java"), expected); + } + + public void testWrong() throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class); + checkConfig.addAttribute("file", "unknown-file"); + final String[] expected = {}; + try { + verify(checkConfig, getPath("imports" + File.separator + + "InputImportControl.java"), expected); + fail("should fail"); + } + catch (CheckstyleException ex) { + ; + } + } + + public void testBroken() throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(ImportControlCheck.class); + checkConfig.addAttribute("file", System.getProperty("testinputs.dir") + + "/import-control_broken.xml"); + final String[] expected = {}; + try { + verify(checkConfig, getPath("imports" + File.separator + + "InputImportControl.java"), expected); + fail("should fail"); + } + catch (CheckstyleException ex) { + ; + } + } +}