From a7c5f3314115c62a419bb3d0977ac26d12bcbb96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20K=C3=BChne?= Date: Sun, 29 Sep 2002 12:58:27 +0000 Subject: [PATCH] Added test for FileLengthCheck --- .../checkstyle/checks/FileLengthCheck.java | 14 ++++- .../tools/checkstyle/FileLengthCheckTest.java | 58 +++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/FileLengthCheckTest.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/FileLengthCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/FileLengthCheck.java index fe2196bc9..a61edd12c 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/FileLengthCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/FileLengthCheck.java @@ -20,6 +20,7 @@ package com.puppycrawl.tools.checkstyle.checks; import com.puppycrawl.tools.checkstyle.api.Check; +import org.apache.commons.beanutils.ConversionException; // TODO: tests! @@ -59,9 +60,18 @@ public class FileLengthCheck extends Check /** * @param aLength the maximum length of a Java source file */ - public void setMax(int aLength) + // TODO: Should be an int argument. + // It seems like I don't know how to use beanutils, I thought + // that parsing this stuff is the core job of beanutils + public void setMax(String aLength) { - mMaxFileLength = aLength; + try { + mMaxFileLength = Integer.parseInt(aLength); + } + catch (NumberFormatException ex) { + throw new ConversionException("Can't parse '" + + aLength + "' as maximal file length", ex); + } } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/FileLengthCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/FileLengthCheckTest.java new file mode 100644 index 000000000..b2cd957f0 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/FileLengthCheckTest.java @@ -0,0 +1,58 @@ +package com.puppycrawl.tools.checkstyle; + +import com.puppycrawl.tools.checkstyle.checks.FileLengthCheck; + +public class FileLengthCheckTest + extends BaseCheckTestCase +{ + public FileLengthCheckTest(String aName) + { + super(aName); + } + + private void runIt(String aMax, String[] aExpected) throws Exception + { + final CheckConfiguration checkConfig = new CheckConfiguration(); + checkConfig.setClassname(FileLengthCheck.class.getName()); + checkConfig.addProperty("max", aMax); + final Checker c = createChecker(checkConfig); + final String fname = getPath("InputSimple.java"); + verify(c, fname, aExpected); + } + + public void testAlarm() throws Exception + { + final String[] expected = { + "1:1: File length is 198 lines (max allowed is 20)." + }; + runIt("20", expected); + } + + public void testOK() throws Exception + { + final String[] expected = { + }; + runIt("2000", expected); + } + + public void testArgs() throws Exception + { + final CheckConfiguration checkConfig = new CheckConfiguration(); + checkConfig.setClassname(FileLengthCheck.class.getName()); + try { + checkConfig.addProperty("max", "abc"); + createChecker(checkConfig); + fail("Should indicate illegal args"); + } + catch (Exception ex) + { + // Expected Exception because of illegal argument for "max" + + // TODO: I would have expected to receive a ConversionException here + // but in fact I do get an InvocationTargetException - another + // mystery in beanutils? + } + } + + +}