Skip to content

Commit

Permalink
Issue checkstyle#3567: clarified tests with expected exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed Nov 22, 2016
1 parent 00d6091 commit 274b8bc
Show file tree
Hide file tree
Showing 30 changed files with 441 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.google.checkstyle.test.base.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.checks.javadoc.SingleLineJavadocCheck;

public class SingleLineJavadocTest extends BaseCheckTestSupport {
Expand Down Expand Up @@ -59,19 +58,4 @@ public void singleLineJavadocTest() throws Exception {
final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}

@Test(expected = Exception.class)
public void customInlineTagTest() throws Exception {
final String msg = getCheckMessage(SingleLineJavadocCheck.class, "singleline.javadoc");

final Configuration checkConfig = getCheckConfig("SingleLineJavadocCheck");
final String filePath = getPath("InputSingleLineJavadocCheckError.java");

final String[] expected = {
"4: " + msg,
};

final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.junit.Assert;
import org.junit.Test;

import antlr.NoViableAltException;

import com.puppycrawl.tools.checkstyle.api.CheckstyleException;

public class AstTreeStringPrinterTest {
Expand All @@ -46,10 +48,17 @@ public void testIsProperUtilsClass() throws ReflectiveOperationException {
assertUtilsClassHasPrivateConstructor(AstTreeStringPrinter.class);
}

@Test(expected = CheckstyleException.class)
@Test
public void testParseFileThrowable() throws Exception {
AstTreeStringPrinter.printFileAst(
new File(getNonCompilablePath("InputAstTreeStringPrinter.java")), false);
try {
AstTreeStringPrinter.printFileAst(
new File(getNonCompilablePath("InputAstTreeStringPrinter.java")), false);
Assert.fail("exception expected");
}
catch (CheckstyleException ex) {
Assert.assertSame(NoViableAltException.class, ex.getCause().getClass());
Assert.assertEquals("unexpected token: classD", ex.getCause().getMessage());
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@

public class PropertiesExpanderTest {

@Test(expected = IllegalArgumentException.class)
@Test
public void testCtorException() {
new PropertiesExpander(null);
try {
new PropertiesExpander(null);
Assert.fail("exception expected");
}
catch (IllegalArgumentException ex) {
Assert.assertEquals("cannot pass null", ex.getMessage());
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.puppycrawl.tools.checkstyle.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.lang.reflect.Method;
import java.util.SortedSet;
Expand Down Expand Up @@ -100,7 +101,7 @@ public void testCustomMessageWithParameters() throws Exception {
messages.first().getMessage());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testCustomMessageWithParametersNegative() throws Exception {
final DefaultConfiguration config = createCheckConfig(emptyCheck.getClass());
config.addMessage("msgKey", "This is a custom message {0.");
Expand All @@ -109,14 +110,13 @@ public void testCustomMessageWithParametersNegative() throws Exception {
final LocalizedMessages collector = new LocalizedMessages();
emptyCheck.setMessages(collector);

emptyCheck.log(0, "msgKey", "TestParam");

final SortedSet<LocalizedMessage> messages = collector.getMessages();
assertEquals(1, messages.size());

//we expect an exception here because of the bogus custom message
//format
messages.first().getMessage();
try {
emptyCheck.log(0, "msgKey", "TestParam");
fail("exception expected");
}
catch (IllegalArgumentException ex) {
assertEquals("Unmatched braces in the pattern.", ex.getMessage());
}
}

private static class EmptyCheck extends AbstractCheck {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,18 @@ public void testContextualizeConversionException() {
}
}

@Test(expected = IllegalStateException.class)
@Test
public void testTestBean() {
final TestBean testBean = new TestBean();
testBean.setVal(0);
testBean.setWrong("wrongVal");
testBean.setExceptionalMethod("someValue");
try {
testBean.setExceptionalMethod("someValue");
fail("exception expected");
}
catch (IllegalStateException ex) {
assertEquals("null,wrongVal,0,someValue", ex.getMessage());
}
}

private static class TestBean extends AutomaticBean {
Expand Down
13 changes: 10 additions & 3 deletions src/test/java/com/puppycrawl/tools/checkstyle/api/ScopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Locale;

Expand All @@ -44,15 +45,21 @@ public void testScopeValueOf() {
assertEquals(Scope.PRIVATE, scope);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testMisc() {
final Scope o = Scope.getInstance("public");
assertNotNull(o);
assertEquals("public", o.toString());
assertEquals("public", o.getName());

// will fail
Scope.getInstance("unknown");
try {
Scope.getInstance("unknown");
fail("exception expected");
}
catch (IllegalArgumentException ex) {
assertEquals("No enum constant com.puppycrawl.tools.checkstyle.api.Scope.UNKNOWN",
ex.getMessage());
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@
package com.puppycrawl.tools.checkstyle.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Test;

public class SeverityLevelCounterTest {

@Test(expected = IllegalArgumentException.class)
@Test
public void testCtorException() {
new SeverityLevelCounter(null);
try {
new SeverityLevelCounter(null);
fail("exception expected");
}
catch (IllegalArgumentException ex) {
assertEquals("'level' cannot be null", ex.getMessage());
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.util.Locale;

Expand All @@ -42,15 +43,22 @@ public void testSeverityLevelValueOf() {
assertEquals(SeverityLevel.INFO, level);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testMisc() {
final SeverityLevel o = SeverityLevel.getInstance("info");
assertNotNull(o);
assertEquals("info", o.toString());
assertEquals("info", o.getName());

// will fail
SeverityLevel.getInstance("unknown");
try {
SeverityLevel.getInstance("unknown");
fail("exception expected");
}
catch (IllegalArgumentException ex) {
assertEquals(
"No enum constant com.puppycrawl.tools.checkstyle.api.SeverityLevel.UNKNOWN",
ex.getMessage());
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static java.util.Locale.ENGLISH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

Expand Down Expand Up @@ -144,13 +145,22 @@ public void testNoNewlineAtEndOfFile() throws Exception {
expected);
}

@Test(expected = CheckstyleException.class)
@Test
public void testSetLineSeparatorFailure()
throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(NewlineAtEndOfFileCheck.class);
checkConfig.addAttribute("lineSeparator", "ct");
createChecker(checkConfig);
try {
createChecker(checkConfig);
fail("exception expected");
}
catch (CheckstyleException ex) {
assertTrue(ex.getMessage().startsWith(
"cannot initialize module com.puppycrawl.tools.checkstyle."
+ "checks.NewlineAtEndOfFileCheck - "
+ "Cannot set property 'lineSeparator' to 'ct' in module"));
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import static com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck.MSG_KEY_BLOCK_EMPTY;
import static com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck.MSG_KEY_BLOCK_NO_STMT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -138,12 +140,21 @@ public void allowEmptyLoopsText() throws Exception {
verify(checkConfig, getPath("InputSemantic2.java"), expected);
}

@Test(expected = CheckstyleException.class)
@Test
public void testInvalidOption() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(EmptyBlockCheck.class);
checkConfig.addAttribute("option", "invalid_option");
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputSemantic.java"), expected);
try {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputSemantic.java"), expected);
fail("exception expected");
}
catch (CheckstyleException ex) {
assertTrue(ex.getMessage().startsWith(
"cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
+ "Cannot set property 'option' to 'invalid_option' in module"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import static com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck.MSG_KEY_LINE_PREVIOUS;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -361,11 +363,20 @@ public void testCoverageIncrease() throws Exception {
verify(checkConfig, getPath("InputScopeInnerInterfaces2.java"), expected);
}

@Test(expected = CheckstyleException.class)
@Test
public void testInvalidOption() throws Exception {
checkConfig.addAttribute("option", "invalid_option");
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputScopeInnerInterfaces.java"), expected);
try {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputScopeInnerInterfaces.java"), expected);
fail("exception expected");
}
catch (CheckstyleException ex) {
assertTrue(ex.getMessage().startsWith(
"cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
+ "Cannot set property 'option' to 'invalid_option' in module"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import static com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck.MSG_KEY_LINE_NEW;
import static com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck.MSG_KEY_LINE_SAME;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -275,12 +277,21 @@ public void testSingleLineClass() throws Exception {
verify(checkConfig, getPath("InputRightCurly.java"), expected);
}

@Test(expected = CheckstyleException.class)
@Test
public void testInvalidOption() throws Exception {
checkConfig.addAttribute("option", "invalid_option");
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputRightCurly.java"), expected);
try {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputRightCurly.java"), expected);
fail("exception expected");
}
catch (CheckstyleException ex) {
assertTrue(ex.getMessage().startsWith(
"cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
+ "Cannot set property 'option' to 'invalid_option' in module"));
}
}

@Test
Expand Down
Loading

0 comments on commit 274b8bc

Please sign in to comment.