From ee68e92168e132ff16622a2593dcb5128ad024e6 Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Tue, 15 Jan 2008 10:55:13 +0000 Subject: [PATCH] Convert over the easy tests to JUnit 4.x --- .../tools/checkstyle/CheckerTest.java | 16 ++++++-- .../checkstyle/ConfigurationLoaderTest.java | 20 ++++++++-- .../tools/checkstyle/OptionTest.java | 37 ++++++++++--------- .../checkstyle/PackageNamesLoaderTest.java | 14 ++++--- .../checkstyle/PackageObjectFactoryTest.java | 24 ++++++------ .../checkstyle/StringArrayReaderTest.java | 14 ++++--- .../tools/checkstyle/UtilsTest.java | 19 ++++------ .../tools/checkstyle/XMLLoggerTest.java | 33 ++++++++--------- .../api/AbstractViolationReporterTest.java | 8 +++- .../tools/checkstyle/api/DetailASTTest.java | 15 ++++---- .../tools/checkstyle/api/ScopeTest.java | 15 +++----- .../tools/checkstyle/api/TokenTypesTest.java | 14 +++++-- .../checkstyle/checks/ClassResolverTest.java | 8 ++-- .../checks/imports/AccessResultTest.java | 20 +++++----- .../checkstyle/checks/imports/GuardTest.java | 15 ++++---- .../imports/ImportControlLoaderTest.java | 8 ++-- .../checks/imports/PkgControlTest.java | 25 +++++++------ .../checkstyle/filters/CSVFilterTest.java | 25 +++++++------ .../checkstyle/filters/FilterSetTest.java | 24 +++++++----- .../filters/IntMatchFilterTest.java | 14 ++++--- .../filters/IntRangeFilterTest.java | 22 ++++++----- .../filters/SeverityMatchFilterTest.java | 22 ++++------- .../filters/SuppressElementTest.java | 34 +++++++++-------- .../filters/SuppressionsLoaderTest.java | 13 +++++-- 24 files changed, 257 insertions(+), 202 deletions(-) diff --git a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java index e879f4ec0..ed93ef508 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java @@ -1,15 +1,20 @@ package com.puppycrawl.tools.checkstyle; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import com.puppycrawl.tools.checkstyle.api.AuditEvent; import com.puppycrawl.tools.checkstyle.api.AuditListener; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.Filter; import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; import java.io.File; -import junit.framework.TestCase; +import org.junit.Test; -public class CheckerTest extends TestCase +public class CheckerTest { + @Test public void testDosBasedir() throws Exception { Checker c = new Checker(); @@ -18,6 +23,7 @@ public class CheckerTest extends TestCase assertEquals("C:\\a\\b\\d", c.getBasedir()); } + @Test public void testOsBasedir() throws Exception { Checker c = new Checker(); @@ -34,7 +40,7 @@ public class CheckerTest extends TestCase assertTrue((testinputs_dir + "coding").equalsIgnoreCase(c.getBasedir())); } - + @Test public void testDestroy() throws Exception { DebugChecker c= new DebugChecker(); @@ -61,6 +67,7 @@ public class CheckerTest extends TestCase assertFalse("Checker.destroy() doesn't remove filters.", f.wasCalled()); } + @Test public void testAddListener() throws Exception { DebugChecker c= new DebugChecker(); @@ -92,6 +99,7 @@ public class CheckerTest extends TestCase assertTrue("Checker.fireErrors() doesn't call listener", aa.wasCalled()); } + @Test public void testRemoveListener() throws Exception { DebugChecker c= new DebugChecker(); @@ -132,6 +140,7 @@ public class CheckerTest extends TestCase } + @Test public void testAddFilter() throws Exception { DebugChecker c= new DebugChecker(); @@ -168,6 +177,7 @@ public class CheckerTest extends TestCase assertTrue("Checker.fireErrors() doesn't call filter", f.wasCalled()); } + @Test public void testRemoveFilter() throws Exception { DebugChecker c= new DebugChecker(); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java b/src/tests/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java index 8c062109c..c3e625ae4 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java @@ -1,18 +1,20 @@ package com.puppycrawl.tools.checkstyle; -import java.util.Properties; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.Configuration; -import junit.framework.TestCase; - +import java.util.Properties; +import org.junit.Test; /** * @author Rick Giles * @author lkuehne * @version $Revision$ */ -public class ConfigurationLoaderTest extends TestCase +public class ConfigurationLoaderTest { private Configuration loadConfiguration(String aName) throws CheckstyleException @@ -30,6 +32,7 @@ public class ConfigurationLoaderTest extends TestCase fName, new PropertiesExpander(aProps)); } + @Test public void testEmptyConfiguration() throws Exception { final DefaultConfiguration config = @@ -37,6 +40,7 @@ public class ConfigurationLoaderTest extends TestCase verifyConfigNode(config, "Checker", 0, new Properties()); } + @Test public void testMissingPropertyName() { try { @@ -51,6 +55,7 @@ public class ConfigurationLoaderTest extends TestCase } } + @Test public void testMissingPropertyValue() { try { @@ -65,6 +70,7 @@ public class ConfigurationLoaderTest extends TestCase } } + @Test public void testMissingConfigName() { try { @@ -79,6 +85,7 @@ public class ConfigurationLoaderTest extends TestCase } } + @Test public void testMissingConfigParent() { try { @@ -93,6 +100,7 @@ public class ConfigurationLoaderTest extends TestCase } } + @Test public void testCheckstyleChecks() throws Exception { final Properties props = new Properties(); @@ -167,6 +175,7 @@ public class ConfigurationLoaderTest extends TestCase } } + @Test public void testReplacePropertiesNoReplace() throws CheckstyleException { @@ -180,6 +189,7 @@ public class ConfigurationLoaderTest extends TestCase } } + @Test public void testReplacePropertiesSyntaxError() { final Properties props = initProperties(); @@ -193,6 +203,7 @@ public class ConfigurationLoaderTest extends TestCase } } + @Test public void testReplacePropertiesMissingProperty() { final Properties props = initProperties(); @@ -206,6 +217,7 @@ public class ConfigurationLoaderTest extends TestCase } } + @Test public void testReplacePropertiesReplace() throws CheckstyleException { diff --git a/src/tests/com/puppycrawl/tools/checkstyle/OptionTest.java b/src/tests/com/puppycrawl/tools/checkstyle/OptionTest.java index 954f36d6e..815b17e5d 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/OptionTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/OptionTest.java @@ -1,20 +1,23 @@ package com.puppycrawl.tools.checkstyle; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import com.puppycrawl.tools.checkstyle.checks.AbstractOption; import com.puppycrawl.tools.checkstyle.checks.blocks.BlockOption; import com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyOption; import com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyOption; import com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapOption; import com.puppycrawl.tools.checkstyle.checks.whitespace.PadOption; -import junit.framework.TestCase; +import org.junit.Test; /** * @author Rick Giles * @version 14-Nov-2002 */ -public class OptionTest extends TestCase { - - public void testBlockOption() +public class OptionTest +{ + @Test public void testBlockOption() { BlockOption stmtOpt = BlockOption.STMT; assertEquals("STMT", "stmt", stmtOpt.toString()); @@ -23,10 +26,10 @@ public class OptionTest extends TestCase { BlockOption stmtDecode = (BlockOption)(stmtOpt.decode("stmt")); assertTrue("STMT decode", stmtOpt == stmtDecode); BlockOption textDecode = (BlockOption)(stmtOpt.decode("text")); - assertTrue("TEXT decode", textOpt == textDecode); + assertTrue("TEXT decode", textOpt == textDecode); } - - public void testLeftCurlyOption() + + @Test public void testLeftCurlyOption() { LeftCurlyOption eolOpt = LeftCurlyOption.EOL; assertEquals("EOL", "eol", eolOpt.toString()); @@ -41,8 +44,8 @@ public class OptionTest extends TestCase { LeftCurlyOption nlowDecode = (LeftCurlyOption)(nlowOpt.decode("nlow")); assertTrue("NL decode", nlowOpt == nlowDecode); } - - public void testOperatorWrapOption() + + @Test public void testOperatorWrapOption() { OperatorWrapOption eolOpt = OperatorWrapOption.EOL; assertEquals("EOL", "eol", eolOpt.toString()); @@ -53,8 +56,8 @@ public class OptionTest extends TestCase { OperatorWrapOption nlDecode = (OperatorWrapOption)(nlOpt.decode("nl")); assertTrue("NL decode", nlOpt == nlDecode); } - - public void testPadOption() + + @Test public void testPadOption() { PadOption nospaceOpt = PadOption.NOSPACE; assertEquals("NOSPACE", "nospace", nospaceOpt.toString()); @@ -63,10 +66,10 @@ public class OptionTest extends TestCase { PadOption nospaceDecode = (PadOption)(nospaceOpt.decode("nospace")); assertTrue("NOSPACE decode", nospaceOpt == nospaceDecode); PadOption spaceDecode = (PadOption)(spaceOpt.decode("space")); - assertTrue("SPACE decode", spaceOpt == spaceDecode); + assertTrue("SPACE decode", spaceOpt == spaceDecode); } - - public void testRightCurlyOption() + + @Test public void testRightCurlyOption() { RightCurlyOption aloneOpt = RightCurlyOption.ALONE; assertEquals("ALONE", "alone", aloneOpt.toString()); @@ -77,8 +80,8 @@ public class OptionTest extends TestCase { RightCurlyOption sameDecode = (RightCurlyOption)(sameOpt.decode("same")); assertTrue("SAME decode", sameOpt == sameDecode); } - - public void testEqualKeys() + + @Test public void testEqualKeys() { LeftCurlyOption eolLeftCurl = LeftCurlyOption.EOL; LeftCurlyOption eolLeftCurlDecode = @@ -89,7 +92,7 @@ public class OptionTest extends TestCase { assertTrue("eol", (AbstractOption)eolLeftCurlDecode != (AbstractOption)eolOpWrapDecode); - + LeftCurlyOption nlLeftCurl = LeftCurlyOption.NL; LeftCurlyOption nlLeftCurlDecode = (LeftCurlyOption)(nlLeftCurl.decode("nl")); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/PackageNamesLoaderTest.java b/src/tests/com/puppycrawl/tools/checkstyle/PackageNamesLoaderTest.java index eb6c03763..086aed1d7 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/PackageNamesLoaderTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/PackageNamesLoaderTest.java @@ -1,10 +1,13 @@ package com.puppycrawl.tools.checkstyle; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import java.util.Arrays; import java.util.HashSet; import java.util.Set; -import junit.framework.TestCase; +import org.junit.Test; /** * Enter a description of class PackageNamesLoaderTest.java. @@ -12,9 +15,9 @@ import junit.framework.TestCase; * @author lkuehne * @version $Revision$ */ -public class PackageNamesLoaderTest extends TestCase +public class PackageNamesLoaderTest { - public void testDefault() + @Test public void testDefault() throws CheckstyleException { ModuleFactory moduleFactory = PackageNamesLoader @@ -23,7 +26,7 @@ public class PackageNamesLoaderTest extends TestCase validateFactory(moduleFactory); } - public void testNoFile() + @Test public void testNoFile() { try { PackageNamesLoader.loadModuleFactory("NoFile"); @@ -36,7 +39,7 @@ public class PackageNamesLoaderTest extends TestCase } } - public void testFile() + @Test public void testFile() throws CheckstyleException { final ModuleFactory moduleFactory = @@ -78,5 +81,4 @@ public class PackageNamesLoaderTest extends TestCase Set pkgNamesSet = new HashSet(Arrays.asList(pkgNames)); assertEquals("names set.", checkstylePackagesSet, pkgNamesSet); } - } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/PackageObjectFactoryTest.java b/src/tests/com/puppycrawl/tools/checkstyle/PackageObjectFactoryTest.java index 998cc85ec..4a07a5f84 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/PackageObjectFactoryTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/PackageObjectFactoryTest.java @@ -1,26 +1,24 @@ package com.puppycrawl.tools.checkstyle; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck; - -import junit.framework.TestCase; +import org.junit.Test; /** * Enter a description of class PackageObjectFactoryTest.java. * @author Rick Giles * @version 8-Dec-2002 */ -public class PackageObjectFactoryTest extends TestCase +public class PackageObjectFactoryTest { - private PackageObjectFactory mFactory = new PackageObjectFactory(); + private final PackageObjectFactory mFactory = new PackageObjectFactory(); - public void setUp() - { - mFactory = new PackageObjectFactory(); - } - - public void testMakeObjectFromName() + @Test public void testMakeObjectFromName() throws CheckstyleException { final Checker checker = @@ -29,7 +27,7 @@ public class PackageObjectFactoryTest extends TestCase assertNotNull(checker); } - public void testMakeCheckFromName() + @Test public void testMakeCheckFromName() throws CheckstyleException { final ConstantNameCheck check = @@ -38,7 +36,7 @@ public class PackageObjectFactoryTest extends TestCase assertNotNull(check); } - public void testMakeObectFromList() + @Test public void testMakeObectFromList() throws CheckstyleException { mFactory.addPackage("com."); @@ -48,7 +46,7 @@ public class PackageObjectFactoryTest extends TestCase assertNotNull(checker); } - public void testMakeObectNoClass() + @Test public void testMakeObectNoClass() { try { mFactory.createModule("NoClass"); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/StringArrayReaderTest.java b/src/tests/com/puppycrawl/tools/checkstyle/StringArrayReaderTest.java index 277db3466..23ce4c084 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/StringArrayReaderTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/StringArrayReaderTest.java @@ -1,13 +1,15 @@ package com.puppycrawl.tools.checkstyle; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; import java.io.IOException; +import org.junit.Test; public class StringArrayReaderTest - extends TestCase { - public void testClose() + @Test public void testClose() { final StringArrayReader o = new StringArrayReader(new String[] {""}); assertNotNull(o); @@ -20,7 +22,7 @@ public class StringArrayReaderTest } } - public void testLineBreakSingleChar() + @Test public void testLineBreakSingleChar() { final StringArrayReader o = new StringArrayReader(new String[] {"a", "bc"}); @@ -42,7 +44,7 @@ public class StringArrayReaderTest } } - public void testLineBreakCharArray() + @Test public void testLineBreakCharArray() { final StringArrayReader o = new StringArrayReader(new String[] {"a", "bc"}); @@ -66,7 +68,7 @@ public class StringArrayReaderTest } } - public void testNoLineBreakCharArray() + @Test public void testNoLineBreakCharArray() { final StringArrayReader o = new StringArrayReader(new String[] {"a", "bc"}); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/UtilsTest.java b/src/tests/com/puppycrawl/tools/checkstyle/UtilsTest.java index 6c5831d0e..59f596004 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/UtilsTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/UtilsTest.java @@ -1,17 +1,18 @@ package com.puppycrawl.tools.checkstyle; +import static org.junit.Assert.assertEquals; + import com.puppycrawl.tools.checkstyle.api.Utils; -import junit.framework.TestCase; -import org.apache.commons.beanutils.ConversionException; import java.util.regex.Pattern; +import org.apache.commons.beanutils.ConversionException; +import org.junit.Test; public class UtilsTest - extends TestCase { /** * Test Utils.countCharInString. */ - public void testLengthExpandedTabs() + @Test public void testLengthExpandedTabs() throws Exception { String s1 = "\t"; @@ -36,15 +37,9 @@ public class UtilsTest assertEquals(r1, r2); } + @Test(expected = ConversionException.class) public void testBadRegex() { - try { - Utils.createPattern("["); - fail("expected to get conversion exception"); - } - catch (ConversionException e) { - ; // what is expected - } + Utils.createPattern("["); } - } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java b/src/tests/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java index bd986fd22..87480f167 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java @@ -1,5 +1,9 @@ package com.puppycrawl.tools.checkstyle; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import com.puppycrawl.tools.checkstyle.api.AuditEvent; import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; import com.puppycrawl.tools.checkstyle.api.SeverityLevel; @@ -13,25 +17,18 @@ import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; -import junit.framework.TestCase; +import org.junit.Test; /** * Enter a description of class XMLLoggerTest.java. * @author Rick Giles * @version 11-Dec-2002 */ -public class XMLLoggerTest extends TestCase +public class XMLLoggerTest { - private ByteArrayOutputStream outStream; + private final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); - @Override - public void setUp() - throws Exception - { - outStream = new ByteArrayOutputStream(); - } - - public void testEncode() + @Test public void testEncode() throws IOException { final XMLLogger logger = new XMLLogger(outStream, false); @@ -51,7 +48,7 @@ public class XMLLoggerTest extends TestCase outStream.close(); } - public void testIsReference() + @Test public void testIsReference() throws IOException { final XMLLogger logger = new XMLLogger(outStream, false); @@ -80,7 +77,7 @@ public class XMLLoggerTest extends TestCase outStream.close(); } - public void testCloseStream() + @Test public void testCloseStream() throws IOException { final XMLLogger logger = new XMLLogger(outStream, true); @@ -90,7 +87,7 @@ public class XMLLoggerTest extends TestCase verifyLines(expectedLines); } - public void testNoCloseStream() + @Test public void testNoCloseStream() throws IOException { final XMLLogger logger = new XMLLogger(outStream, false); @@ -101,7 +98,7 @@ public class XMLLoggerTest extends TestCase verifyLines(expectedLines); } - public void testFileStarted() + @Test public void testFileStarted() throws IOException { final XMLLogger logger = new XMLLogger(outStream, true); @@ -113,7 +110,7 @@ public class XMLLoggerTest extends TestCase verifyLines(expectedLines); } - public void testFileFinished() + @Test public void testFileFinished() throws IOException { final XMLLogger logger = new XMLLogger(outStream, true); @@ -125,7 +122,7 @@ public class XMLLoggerTest extends TestCase verifyLines(expectedLines); } - public void testAddError() throws IOException { + @Test public void testAddError() throws IOException { final XMLLogger logger = new XMLLogger(outStream, true); logger.auditStarted(null); final LocalizedMessage message = @@ -140,7 +137,7 @@ public class XMLLoggerTest extends TestCase verifyLines(expectedLines); } - public void testAddException() + @Test public void testAddException() throws IOException { final XMLLogger logger = new XMLLogger(outStream, true); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/api/AbstractViolationReporterTest.java b/src/tests/com/puppycrawl/tools/checkstyle/api/AbstractViolationReporterTest.java index a1ff9f9b6..cee4f7317 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/api/AbstractViolationReporterTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/api/AbstractViolationReporterTest.java @@ -1,6 +1,8 @@ package com.puppycrawl.tools.checkstyle.api; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; /** * Tests to ensure that default messagebundle is determined correctly. @@ -8,22 +10,24 @@ import junit.framework.TestCase; * @author lkuehne */ public class AbstractViolationReporterTest - extends TestCase { private final Check emptyCheck = new Check() { + @Override public int[] getDefaultTokens() { return new int[0]; } }; + @Test public void testGetMessageBundleWithPackage() { assertEquals("com.mycompany.checks.messages", emptyCheck.getMessageBundle("com.mycompany.checks.MyCoolCheck")); } + @Test public void testGetMessageBundleWithoutPackage() { assertEquals("messages", diff --git a/src/tests/com/puppycrawl/tools/checkstyle/api/DetailASTTest.java b/src/tests/com/puppycrawl/tools/checkstyle/api/DetailASTTest.java index 6edcc930c..5a4115468 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/api/DetailASTTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/api/DetailASTTest.java @@ -1,20 +1,21 @@ package com.puppycrawl.tools.checkstyle.api; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import com.puppycrawl.tools.checkstyle.TreeWalker; import java.io.File; import java.io.FileFilter; import java.text.MessageFormat; - -import com.puppycrawl.tools.checkstyle.TreeWalker; +import org.junit.Test; /** * TestCase to check DetailAST. * @author Oliver Burn */ -public class DetailASTTest extends TestCase { - - public void testGetChildCount() { +public class DetailASTTest +{ + @Test public void testGetChildCount() { final DetailAST root = new DetailAST(); final DetailAST firstLevelA = new DetailAST(); final DetailAST firstLevelB = new DetailAST(); @@ -42,7 +43,7 @@ public class DetailASTTest extends TestCase { assertEquals(firstLevelA, firstLevelB.getPreviousSibling()); } - public void testTreeStructure() throws Exception + @Test public void testTreeStructure() throws Exception { checkDir(new File(System.getProperty("testinputs.dir"))); } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/api/ScopeTest.java b/src/tests/com/puppycrawl/tools/checkstyle/api/ScopeTest.java index ee664b665..970dbef04 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/api/ScopeTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/api/ScopeTest.java @@ -1,10 +1,13 @@ package com.puppycrawl.tools.checkstyle.api; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; public class ScopeTest - extends TestCase { + @Test(expected = IllegalArgumentException.class) public void testMisc() { final Scope o = Scope.getInstance("public"); @@ -12,12 +15,6 @@ public class ScopeTest assertEquals("Scope[1 (public)]", o.toString()); assertEquals("public", o.getName()); - try { - Scope.getInstance("unknown"); - fail(); - } - catch (IllegalArgumentException e) { - // As expected - } + Scope.getInstance("unknown"); // will fail } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/api/TokenTypesTest.java b/src/tests/com/puppycrawl/tools/checkstyle/api/TokenTypesTest.java index 2b14beb19..aee5dd67d 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/api/TokenTypesTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/api/TokenTypesTest.java @@ -1,10 +1,16 @@ package com.puppycrawl.tools.checkstyle.api; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; -public class TokenTypesTest extends TestCase +import org.junit.Test; + +public class TokenTypesTest { - public void testGetShortDescription() { - assertEquals("short description for EQUAL", "The == (equal) operator.", TokenTypes.getShortDescription("EQUAL")); + @Test + public void testGetShortDescription() + { + assertEquals("short description for EQUAL", + "The == (equal) operator.", TokenTypes + .getShortDescription("EQUAL")); } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/ClassResolverTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/ClassResolverTest.java index b0555464e..cae1dcf82 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/ClassResolverTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/ClassResolverTest.java @@ -1,12 +1,14 @@ package com.puppycrawl.tools.checkstyle.checks; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + import java.util.HashSet; import java.util.Set; -import junit.framework.TestCase; - +import org.junit.Test; public class ClassResolverTest - extends TestCase { + @Test public void testMisc() throws ClassNotFoundException { final Set imps = new HashSet(); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/AccessResultTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/AccessResultTest.java index 31b20d303..6481fa733 100755 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/AccessResultTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/AccessResultTest.java @@ -1,13 +1,19 @@ package com.puppycrawl.tools.checkstyle.checks.imports; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; -import junit.framework.TestCase; +import org.junit.Test; -public class AccessResultTest extends TestCase +public class AccessResultTest { + @Test public void testNormal() { assertEquals("ALLOWED", AccessResult.ALLOWED.getLabel()); @@ -23,17 +29,13 @@ public class AccessResultTest extends TestCase assertTrue(AccessResult.UNKNOWN == revived); } + @Test(expected = IllegalArgumentException.class) public void testBadname() { - try { - AccessResult.getInstance("badname"); - fail("should not get here"); - } - catch (IllegalArgumentException ex) { - ; - } + AccessResult.getInstance("badname"); } + @Test public void testSerial() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/GuardTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/GuardTest.java index 5eb5e91aa..410ec96d6 100755 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/GuardTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/GuardTest.java @@ -1,12 +1,13 @@ package com.puppycrawl.tools.checkstyle.checks.imports; -import com.puppycrawl.tools.checkstyle.checks.imports.AccessResult; -import com.puppycrawl.tools.checkstyle.checks.imports.Guard; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; -public class GuardTest extends TestCase +import org.junit.Test; + +public class GuardTest { - public void testPkgGuard1() + @Test public void testPkgGuard1() { final Guard g = new Guard(true, false, "pkg", false); assertNotNull(g); @@ -18,7 +19,7 @@ public class GuardTest extends TestCase assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg")); } - public void testPkgGuard2() + @Test public void testPkgGuard2() { final Guard g = new Guard(true, false, "pkg", true); assertNotNull(g); @@ -29,7 +30,7 @@ public class GuardTest extends TestCase assertEquals(AccessResult.UNKNOWN, g.verifyImport("pkg")); } - public void testClassGuard() + @Test public void testClassGuard() { final Guard g = new Guard(true, false, "pkg.a"); assertNotNull(g); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoaderTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoaderTest.java index 3eddc231c..b0d41cb54 100755 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoaderTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoaderTest.java @@ -1,12 +1,14 @@ package com.puppycrawl.tools.checkstyle.checks.imports; -import java.io.File; +import static org.junit.Assert.assertNotNull; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; -import junit.framework.TestCase; +import java.io.File; +import org.junit.Test; -public class ImportControlLoaderTest extends TestCase +public class ImportControlLoaderTest { + @Test public void testLoad() throws CheckstyleException { final PkgControl root = diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/PkgControlTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/PkgControlTest.java index 4a0725699..b3a4b483e 100755 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/PkgControlTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/imports/PkgControlTest.java @@ -1,18 +1,19 @@ package com.puppycrawl.tools.checkstyle.checks.imports; -import com.puppycrawl.tools.checkstyle.checks.imports.AccessResult; -import com.puppycrawl.tools.checkstyle.checks.imports.Guard; -import com.puppycrawl.tools.checkstyle.checks.imports.PkgControl; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; -public class PkgControlTest extends TestCase +import org.junit.Before; +import org.junit.Test; + +public class PkgControlTest { private final PkgControl pcRoot = new PkgControl("com.kazgroup.courtlink"); private final PkgControl pcCommon = new PkgControl(pcRoot, "common"); - protected void setUp() throws Exception + @Before + public void setUp() throws Exception { - super.setUp(); pcRoot.addGuard(new Guard(false, false, "org.springframework", false)); pcRoot.addGuard(new Guard(false, false, "org.hibernate", false)); pcRoot.addGuard(new Guard(true, false, "org.apache.commons", false)); @@ -20,13 +21,13 @@ public class PkgControlTest extends TestCase pcCommon.addGuard(new Guard(true, false, "org.hibernate", false)); } - public void testFullPkg() + @Test public void testFullPkg() { assertEquals("com.kazgroup.courtlink", pcRoot.getFullPackage()); assertEquals("com.kazgroup.courtlink.common", pcCommon.getFullPackage()); } - public void testLocateFinest() + @Test public void testLocateFinest() { assertEquals(pcRoot, pcRoot .locateFinest("com.kazgroup.courtlink.domain")); @@ -35,7 +36,7 @@ public class PkgControlTest extends TestCase assertNull(pcRoot.locateFinest("com")); } - public void testCheckAccess() + @Test public void testCheckAccess() { assertEquals(AccessResult.DISALLOWED, pcCommon.checkAccess( "org.springframework.something", @@ -52,8 +53,8 @@ public class PkgControlTest extends TestCase assertEquals(AccessResult.DISALLOWED, pcRoot.checkAccess( "org.hibernate.something", "com.kazgroup.courtlink")); } - - public void testUnknownPkg() + + @Test public void testUnknownPkg() { assertNull(pcRoot.locateFinest("net.another")); } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/filters/CSVFilterTest.java b/src/tests/com/puppycrawl/tools/checkstyle/filters/CSVFilterTest.java index 19ce3beb4..1284615fb 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/filters/CSVFilterTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/filters/CSVFilterTest.java @@ -1,19 +1,22 @@ package com.puppycrawl.tools.checkstyle.filters; -import junit.framework.TestCase; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; /** Tests CSVFilter */ -public class CSVFilterTest extends TestCase +public class CSVFilterTest { - public void testDecideSingle() + @Test public void testDecideSingle() { final IntFilter filter = new CSVFilter("0"); assertFalse("less than", filter.accept(new Integer(-1))); assertTrue("equal", filter.accept(new Integer(0))); assertFalse("greater than", filter.accept(new Integer(1))); } - - public void testDecidePair() + + @Test public void testDecidePair() { final IntFilter filter = new CSVFilter("0, 2"); assertFalse("less than", filter.accept(new Integer(-1))); @@ -21,8 +24,8 @@ public class CSVFilterTest extends TestCase assertFalse("greater than", filter.accept(new Integer(1))); assertTrue("equal 2", filter.accept(new Integer(2))); } - - public void testDecideRange() + + @Test public void testDecideRange() { final IntFilter filter = new CSVFilter("0-2"); assertFalse("less than", filter.accept(new Integer(-1))); @@ -31,8 +34,8 @@ public class CSVFilterTest extends TestCase assertTrue("equal 2", filter.accept(new Integer(2))); assertFalse("greater than", filter.accept(new Integer(3))); } - - public void testDecideEmptyRange() + + @Test public void testDecideEmptyRange() { final IntFilter filter = new CSVFilter("2-0"); assertFalse("less than", filter.accept(new Integer(-1))); @@ -41,8 +44,8 @@ public class CSVFilterTest extends TestCase assertFalse("equal 2", filter.accept(new Integer(2))); assertFalse("greater than", filter.accept(new Integer(3))); } - - public void testDecideRangePlusValue() + + @Test public void testDecideRangePlusValue() { final IntFilter filter = new CSVFilter("0-2, 10"); assertFalse("less than", filter.accept(new Integer(-1))); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/filters/FilterSetTest.java b/src/tests/com/puppycrawl/tools/checkstyle/filters/FilterSetTest.java index fca3869ba..f114afcda 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/filters/FilterSetTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/filters/FilterSetTest.java @@ -1,31 +1,35 @@ package com.puppycrawl.tools.checkstyle.filters; -import junit.framework.TestCase; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; /** Tests SuppressElementFilter */ // TODO: this test should be removed/rewritten -public class FilterSetTest extends TestCase +public class FilterSetTest { private CSVFilter filter; - - public void setUp() + + @Before public void setUp() { filter = new CSVFilter(""); } - - public void testEmptyChain() + + @Test public void testEmptyChain() { assertFalse("0", filter.accept(new Integer(0))); } - - public void testOneFilter() + + @Test public void testOneFilter() { filter.addFilter(new IntMatchFilter(0)); assertTrue("0", filter.accept(new Integer(0))); assertFalse("1", filter.accept(new Integer(1))); } - - public void testMultipleFilter() + + @Test public void testMultipleFilter() { filter.addFilter(new IntMatchFilter(0)); filter.addFilter(new IntRangeFilter(0, 2)); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/filters/IntMatchFilterTest.java b/src/tests/com/puppycrawl/tools/checkstyle/filters/IntMatchFilterTest.java index ef06bfc48..5a6146084 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/filters/IntMatchFilterTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/filters/IntMatchFilterTest.java @@ -1,19 +1,23 @@ package com.puppycrawl.tools.checkstyle.filters; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; /** Tests IntMatchFilter */ -public class IntMatchFilterTest extends TestCase +public class IntMatchFilterTest { - public void testDecide() + @Test public void testDecide() { final IntFilter filter = new IntMatchFilter(0); assertFalse("less than", filter.accept(new Integer(-1))); assertTrue("equal", filter.accept(new Integer(0))); assertFalse("greater than", filter.accept(new Integer(1))); } - - public void testEquals() + + @Test public void testEquals() { final IntFilter filter = new IntMatchFilter(0); final IntFilter filter2 = new IntMatchFilter(0); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/filters/IntRangeFilterTest.java b/src/tests/com/puppycrawl/tools/checkstyle/filters/IntRangeFilterTest.java index 8ff9e5cf3..f4cb72384 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/filters/IntRangeFilterTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/filters/IntRangeFilterTest.java @@ -1,11 +1,15 @@ package com.puppycrawl.tools.checkstyle.filters; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; /** Tests IntRangeFilter */ -public class IntRangeFilterTest extends TestCase +public class IntRangeFilterTest { - public void testDecide() + @Test public void testDecide() { final IntFilter filter = new IntRangeFilter(0, 10); assertFalse("less than", filter.accept(new Integer(-1))); @@ -14,8 +18,8 @@ public class IntRangeFilterTest extends TestCase assertTrue("in range", filter.accept(new Integer(10))); assertFalse("greater than", filter.accept(new Integer(11))); } - - public void testDecideSingle() + + @Test public void testDecideSingle() { final IntFilter filter = new IntRangeFilter(0, 0); assertFalse("less than", filter.accept(new Integer(-1))); @@ -23,7 +27,7 @@ public class IntRangeFilterTest extends TestCase assertFalse("greater than", filter.accept(new Integer(1))); } - public void testDecideEmpty() + @Test public void testDecideEmpty() { final IntFilter filter = new IntRangeFilter(10, 0); assertFalse("out", filter.accept(new Integer(-1))); @@ -32,8 +36,8 @@ public class IntRangeFilterTest extends TestCase assertFalse("out", filter.accept(new Integer(10))); assertFalse("out", filter.accept(new Integer(11))); } - - public void testEquals() + + @Test public void testEquals() { final IntFilter filter = new IntRangeFilter(0, 2); final IntFilter filter2 = new IntRangeFilter(0, 2); @@ -43,6 +47,6 @@ public class IntRangeFilterTest extends TestCase assertFalse("[0,2] != [0,1]", filter.equals(filter3)); assertFalse("[0,2] != [1,2]", filter.equals(filter4)); assertFalse("[0,2] != this", filter.equals(this)); - assertFalse("[0,2] != null", filter.equals(null)); + assertFalse("[0,2] != null", filter.equals(null)); } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/filters/SeverityMatchFilterTest.java b/src/tests/com/puppycrawl/tools/checkstyle/filters/SeverityMatchFilterTest.java index 6fdb99793..9c72e57fe 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/filters/SeverityMatchFilterTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/filters/SeverityMatchFilterTest.java @@ -1,25 +1,19 @@ package com.puppycrawl.tools.checkstyle.filters; -import java.util.regex.PatternSyntaxException; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import com.puppycrawl.tools.checkstyle.api.AuditEvent; import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; import com.puppycrawl.tools.checkstyle.api.SeverityLevel; - -import junit.framework.TestCase; +import org.junit.Test; /** Tests SeverityMatchFilter */ -public class SeverityMatchFilterTest extends TestCase +public class SeverityMatchFilterTest { - private SeverityMatchFilter filter; + private final SeverityMatchFilter filter = new SeverityMatchFilter(); - public void setUp() - throws PatternSyntaxException - { - filter = new SeverityMatchFilter(); - } - - public void testDefault() + @Test public void testDefault() { final AuditEvent ev = new AuditEvent(this, "Test.java"); assertFalse("no message", filter.accept(ev)); @@ -36,7 +30,7 @@ public class SeverityMatchFilterTest extends TestCase assertFalse("level:" + level, filter.accept(ev3)); } - public void testSeverity() + @Test public void testSeverity() { filter.setSeverity("info"); final AuditEvent ev = new AuditEvent(this, "Test.java"); @@ -55,7 +49,7 @@ public class SeverityMatchFilterTest extends TestCase assertTrue("level:" + level, filter.accept(ev3)); } - public void testAcceptOnMatch() + @Test public void testAcceptOnMatch() { filter.setSeverity("info"); filter.setAcceptOnMatch(false); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/filters/SuppressElementTest.java b/src/tests/com/puppycrawl/tools/checkstyle/filters/SuppressElementTest.java index 447fb4497..19fe24a87 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/filters/SuppressElementTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/filters/SuppressElementTest.java @@ -1,31 +1,35 @@ package com.puppycrawl.tools.checkstyle.filters; -import java.util.regex.PatternSyntaxException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import com.puppycrawl.tools.checkstyle.api.AuditEvent; import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; - -import junit.framework.TestCase; +import java.util.regex.PatternSyntaxException; +import org.junit.Before; +import org.junit.Test; /** Tests SuppressElementFilter */ -public class SuppressElementTest extends TestCase +public class SuppressElementTest { private SuppressElement filter; - + + @Before public void setUp() throws PatternSyntaxException { filter = new SuppressElement("Test"); filter.setChecks("Test"); } - - public void testDecideDefault() + + @Test public void testDecideDefault() { final AuditEvent ev = new AuditEvent(this, "Test.java"); assertTrue(ev.getFileName(), filter.accept(ev)); } - - public void testDecideLocalizedMessage() + + @Test public void testDecideLocalizedMessage() { LocalizedMessage message = new LocalizedMessage(0, 0, "", "", null, null, this.getClass()); @@ -33,8 +37,8 @@ public class SuppressElementTest extends TestCase //deny because there are matches on file and check names assertFalse("Names match", filter.accept(ev)); } - - public void testDecideByLine() + + @Test public void testDecideByLine() { LocalizedMessage message = new LocalizedMessage(10, 10, "", "", null, null, this.getClass()); @@ -45,8 +49,8 @@ public class SuppressElementTest extends TestCase filter.setLines("1-9, 11"); assertTrue("Not in 1-9, 11", filter.accept(ev)); } - - public void testDecideByColumn() + + @Test public void testDecideByColumn() { LocalizedMessage message = new LocalizedMessage(10, 10, "", "", null, null, this.getClass()); @@ -58,7 +62,7 @@ public class SuppressElementTest extends TestCase assertTrue("Not in 1-9, 1)", filter.accept(ev)); } - public void testEquals() throws PatternSyntaxException + @Test public void testEquals() throws PatternSyntaxException { final SuppressElement filter2 = new SuppressElement("Test"); filter2.setChecks("Test"); @@ -80,6 +84,6 @@ public class SuppressElementTest extends TestCase filter.setColumns("1-10"); assertFalse("filter, filter2", filter.equals(filter2)); filter2.setColumns("1-10"); - assertEquals("filter, filter2", filter, filter2); + assertEquals("filter, filter2", filter, filter2); } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java b/src/tests/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java index 5f8608f12..df0897cda 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java @@ -1,17 +1,20 @@ package com.puppycrawl.tools.checkstyle.filters; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.FilterSet; -import com.puppycrawl.tools.checkstyle.filters.SuppressElement; -import junit.framework.TestCase; import java.util.regex.PatternSyntaxException; +import org.junit.Test; /** * Tests SuppressionsLoader. * @author Rick Giles */ -public class SuppressionsLoaderTest extends TestCase +public class SuppressionsLoaderTest { + @Test public void testNoSuppressions() throws CheckstyleException { @@ -23,6 +26,7 @@ public class SuppressionsLoaderTest extends TestCase assertEquals(fc, fc2); } + @Test public void testMultipleSuppression() throws CheckstyleException, PatternSyntaxException { @@ -50,6 +54,7 @@ public class SuppressionsLoaderTest extends TestCase assertEquals(fc, fc2); } + @Test public void testNoFile() { final String fn = System.getProperty("testinputs.dir") @@ -64,6 +69,7 @@ public class SuppressionsLoaderTest extends TestCase } } + @Test public void testNoCheck() { final String fn = System.getProperty("testinputs.dir") @@ -78,6 +84,7 @@ public class SuppressionsLoaderTest extends TestCase } } + @Test public void testBadInt() { final String fn = System.getProperty("testinputs.dir")