Skip to content

Commit

Permalink
Issue #4516: fix first bunch of pmd violations for JUnitAssertionsSho…
Browse files Browse the repository at this point in the history
…uldIncludeMessage
  • Loading branch information
Nimfadora authored and romani committed Jun 24, 2017
1 parent 985dfc7 commit 1c38e2e
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 84 deletions.
139 changes: 92 additions & 47 deletions src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java
Expand Up @@ -275,9 +275,12 @@ public void testFileExtensions() throws Exception {

// comparing to 1 as there is only one legal file in input
final int numLegalFiles = 1;
assertEquals(numLegalFiles, counter);
assertEquals(numLegalFiles, auditAdapter.getNumFilesStarted());
assertEquals(numLegalFiles, auditAdapter.getNumFilesFinished());
assertEquals("There were more legal files than expected",
numLegalFiles, counter);
assertEquals("Audit was started on larger amount of files than expected",
numLegalFiles, auditAdapter.getNumFilesStarted());
assertEquals("Audit was finished on larger amount of files than expected",
numLegalFiles, auditAdapter.getNumFilesFinished());
}

@Test
Expand All @@ -303,9 +306,12 @@ public void testIgnoredFileExtensions() throws Exception {

// comparing to 0 as there is no legal file in input
final int numLegalFiles = 0;
assertEquals(numLegalFiles, counter);
assertEquals(numLegalFiles, auditAdapter.getNumFilesStarted());
assertEquals(numLegalFiles, auditAdapter.getNumFilesFinished());
assertEquals("There were more legal files than expected",
numLegalFiles, counter);
assertEquals("Audit was started on larger amount of files than expected",
numLegalFiles, auditAdapter.getNumFilesStarted());
assertEquals("Audit was finished on larger amount of files than expected",
numLegalFiles, auditAdapter.getNumFilesFinished());
}

@SuppressWarnings("deprecation")
Expand All @@ -330,7 +336,8 @@ public void testSetters() {
fail("Exception is expected");
}
catch (UnsupportedEncodingException ex) {
assertEquals("unsupported charset: 'UNKNOWN-CHARSET'", ex.getMessage());
assertEquals("Error message is not expected",
"unsupported charset: 'UNKNOWN-CHARSET'", ex.getMessage());
}
}

Expand All @@ -343,8 +350,9 @@ public void testNoClassLoaderNoModuleFactory() {
fail("Exception is expected");
}
catch (CheckstyleException ex) {
assertEquals("if no custom moduleFactory is set, "
+ "moduleClassLoader must be specified", ex.getMessage());
assertEquals("Error message is not expected",
"if no custom moduleFactory is set, moduleClassLoader must be specified",
ex.getMessage());
}
}

Expand All @@ -370,15 +378,19 @@ public void testFinishLocalSetupFullyInitialized() throws Exception {
checker.finishLocalSetup();

final Context context = (Context) Whitebox.getInternalState(checker, "childContext");
assertEquals(System.getProperty("file.encoding", "UTF-8"), context.get("charset"));
assertEquals(contextClassLoader, context.get("classLoader"));
assertEquals("error", context.get("severity"));
assertEquals("testBaseDir", context.get("basedir"));
assertEquals("Charset was different than expected",
System.getProperty("file.encoding", "UTF-8"), context.get("charset"));
assertEquals("Was used unsufficient classloader",
contextClassLoader, context.get("classLoader"));
assertEquals("Severity is set to unexpected value",
"error", context.get("severity"));
assertEquals("Basedir is set to unexpected value",
"testBaseDir", context.get("basedir"));

final Field sLocale = LocalizedMessage.class.getDeclaredField("sLocale");
sLocale.setAccessible(true);
final Locale locale = (Locale) sLocale.get(null);
assertEquals(Locale.ITALY, locale);
assertEquals("Locale is set to unexpected value", Locale.ITALY, locale);
}

@Test
Expand All @@ -394,7 +406,8 @@ public void testSetupChildExceptions() {
fail("Exception is expected");
}
catch (CheckstyleException ex) {
assertEquals("java.lang.String is not allowed as a child in Checker", ex.getMessage());
assertEquals("Error message is not expected",
"java.lang.String is not allowed as a child in Checker", ex.getMessage());
}
}

Expand Down Expand Up @@ -426,7 +439,8 @@ public void testDestroyCheckerWithWrongCacheFileNameLength() throws Exception {
fail("Exception did not happen");
}
catch (IllegalStateException ex) {
assertTrue(ex.getCause() instanceof IOException);
assertTrue("Cause of exception differs from IOException",
ex.getCause() instanceof IOException);
}
}

Expand All @@ -437,7 +451,8 @@ public void testDestroyCheckerWithWrongCacheFileNameLength() throws Exception {
@Test
public void testCacheAndCheckWhichDoesNotImplementExternalResourceHolderInterface()
throws Exception {
assertFalse(ExternalResourceHolder.class.isAssignableFrom(HiddenFieldCheck.class));
assertFalse("ExternalResourceHolder has changed his parent",
ExternalResourceHolder.class.isAssignableFrom(HiddenFieldCheck.class));
final DefaultConfiguration checkConfig = createCheckConfig(HiddenFieldCheck.class);

final DefaultConfiguration treeWalkerConfig = createCheckConfig(TreeWalker.class);
Expand Down Expand Up @@ -470,7 +485,8 @@ public void testCacheAndCheckWhichDoesNotImplementExternalResourceHolderInterfac
final Properties cacheAfterSecondRun = new Properties();
cacheAfterSecondRun.load(Files.newBufferedReader(cacheFile.toPath()));

assertEquals(cacheAfterFirstRun, cacheAfterSecondRun);
assertEquals("Cache from first run differs from second run cache",
cacheAfterFirstRun, cacheAfterSecondRun);
}

@Test
Expand Down Expand Up @@ -498,12 +514,15 @@ public void testWithCacheWithNoViolation() throws Exception {

// There should 2 objects in cache: processed file (file.java) and checker configuration.
final int expectedNumberOfObjectsInCache = 2;
assertEquals(expectedNumberOfObjectsInCache, cache.size());
assertEquals("Cache has unexpected size",
expectedNumberOfObjectsInCache, cache.size());

final String expectedConfigHash = "68EE3C3B4593FD8D86159C670C504542E20C6FA0";
assertEquals(expectedConfigHash, cache.getProperty(PropertyCacheFile.CONFIG_HASH_KEY));
assertEquals("Cache has unexpected hash",
expectedConfigHash, cache.getProperty(PropertyCacheFile.CONFIG_HASH_KEY));

assertNotNull(cache.getProperty(tmpFile.getPath()));
assertNotNull("Cache file has null path",
cache.getProperty(tmpFile.getPath()));
}

@Test
Expand Down Expand Up @@ -531,8 +550,10 @@ public void testClearExistingCache() throws Exception {
final Properties cacheAfterClear = new Properties();
cacheAfterClear.load(Files.newBufferedReader(cacheFile.toPath()));

assertEquals(1, cacheAfterClear.size());
assertNotNull(cacheAfterClear.getProperty(PropertyCacheFile.CONFIG_HASH_KEY));
assertEquals("Cache has unexpected size",
1, cacheAfterClear.size());
assertNotNull("Cache has null hash",
cacheAfterClear.getProperty(PropertyCacheFile.CONFIG_HASH_KEY));

final String pathToEmptyFile = temporaryFolder.newFile("file.java").getPath();
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
Expand All @@ -542,13 +563,15 @@ public void testClearExistingCache() throws Exception {
final Properties cacheAfterSecondRun = new Properties();
cacheAfterSecondRun.load(Files.newBufferedReader(cacheFile.toPath()));

assertNotNull(cacheAfterSecondRun.getProperty(pathToEmptyFile));
assertEquals(
assertNotNull("Cache has null path",
cacheAfterSecondRun.getProperty(pathToEmptyFile));
assertEquals("Cash have changed it hash",
cacheAfterClear.getProperty(PropertyCacheFile.CONFIG_HASH_KEY),
cacheAfterSecondRun.getProperty(PropertyCacheFile.CONFIG_HASH_KEY)
);
final int expectedNumberOfObjectsInCacheAfterSecondRun = 2;
assertEquals(expectedNumberOfObjectsInCacheAfterSecondRun, cacheAfterSecondRun.size());
assertEquals("Cache has changed number of items",
expectedNumberOfObjectsInCacheAfterSecondRun, cacheAfterSecondRun.size());
}

@Test
Expand Down Expand Up @@ -581,9 +604,12 @@ public void testCatchErrorInProcessFilesMethod() throws Exception {
}
// -@cs[IllegalCatchExtended] Testing for catch Error is part of 100% coverage.
catch (Error error) {
assertThat(error.getCause(), instanceOf(IOError.class));
assertThat(error.getCause().getCause(), instanceOf(InternalError.class));
assertEquals(errorMessage, error.getCause().getCause().getMessage());
assertThat("Error cause differs from IOError",
error.getCause(), instanceOf(IOError.class));
assertThat("Error cause is not InternalError",
error.getCause().getCause(), instanceOf(InternalError.class));
assertEquals("Error message is not expected",
errorMessage, error.getCause().getCause().getMessage());
}
}

Expand All @@ -594,7 +620,8 @@ public void testCatchErrorInProcessFilesMethod() throws Exception {
@Test
public void testCacheAndFilterWhichDoesNotImplementExternalResourceHolderInterface()
throws Exception {
assertFalse(ExternalResourceHolder.class.isAssignableFrom(DummyFilter.class));
assertFalse("ExternalResourceHolder has changed its parent",
ExternalResourceHolder.class.isAssignableFrom(DummyFilter.class));
final DefaultConfiguration filterConfig = createCheckConfig(DummyFilter.class);

final DefaultConfiguration checkerConfig = new DefaultConfiguration("checkstyle_checks");
Expand All @@ -620,16 +647,20 @@ public void testCacheAndFilterWhichDoesNotImplementExternalResourceHolderInterfa
cacheAfterSecondRun.load(Files.newBufferedReader(cacheFile.toPath()));

assertEquals(
"Cache file has changed its path",
cacheAfterFirstRun.getProperty(pathToEmptyFile),
cacheAfterSecondRun.getProperty(pathToEmptyFile)
);
assertEquals(
"Cache has changed its hash",
cacheAfterFirstRun.getProperty(PropertyCacheFile.CONFIG_HASH_KEY),
cacheAfterSecondRun.getProperty(PropertyCacheFile.CONFIG_HASH_KEY)
);
final int expectedNumberOfObjectsInCache = 2;
assertEquals(expectedNumberOfObjectsInCache, cacheAfterFirstRun.size());
assertEquals(expectedNumberOfObjectsInCache, cacheAfterSecondRun.size());
assertEquals("Number of items in cache differs from expected",
expectedNumberOfObjectsInCache, cacheAfterFirstRun.size());
assertEquals("Number of items in cache differs from expected",
expectedNumberOfObjectsInCache, cacheAfterSecondRun.size());
}

/**
Expand Down Expand Up @@ -672,7 +703,8 @@ public void testCacheAndCheckWhichAddsNewResourceLocationButKeepsSameCheckerInst
cacheAfterFirstRun.load(Files.newBufferedReader(cacheFile.toPath()));

final int expectedNumberOfObjectsInCacheAfterFirstRun = 3;
assertEquals(expectedNumberOfObjectsInCacheAfterFirstRun, cacheAfterFirstRun.size());
assertEquals("Number of items in cache differs from expected",
expectedNumberOfObjectsInCacheAfterFirstRun, cacheAfterFirstRun.size());

// Change a list of external resources which are used by the check
final String secondExternalResourceLocation = "checks" + File.separator
Expand All @@ -685,23 +717,28 @@ public void testCacheAndCheckWhichAddsNewResourceLocationButKeepsSameCheckerInst
final Properties cacheAfterSecondRun = new Properties();
cacheAfterSecondRun.load(Files.newBufferedReader(cacheFile.toPath()));

assertEquals(
assertEquals("Cache file has changed its path",
cacheAfterFirstRun.getProperty(pathToEmptyFile),
cacheAfterSecondRun.getProperty(pathToEmptyFile)
);
assertEquals(
"Cache has changed its hash",
cacheAfterFirstRun.getProperty(PropertyCacheFile.CONFIG_HASH_KEY),
cacheAfterSecondRun.getProperty(PropertyCacheFile.CONFIG_HASH_KEY)
);
assertEquals(
assertEquals("Cache has changed its resource key",
cacheAfterFirstRun.getProperty(firstExternalResourceKey),
cacheAfterSecondRun.getProperty(firstExternalResourceKey)
);
assertNotNull(cacheAfterFirstRun.getProperty(firstExternalResourceKey));
assertNotNull("Cache has null as a resource key",
cacheAfterFirstRun.getProperty(firstExternalResourceKey));
final int expectedNumberOfObjectsInCacheAfterSecondRun = 4;
assertEquals(expectedNumberOfObjectsInCacheAfterSecondRun, cacheAfterSecondRun.size());
assertNull(cacheAfterFirstRun.getProperty(secondExternalResourceKey));
assertNotNull(cacheAfterSecondRun.getProperty(secondExternalResourceKey));
assertEquals("Number of items in cache differs from expected",
expectedNumberOfObjectsInCacheAfterSecondRun, cacheAfterSecondRun.size());
assertNull("Cache has not null as a resource key",
cacheAfterFirstRun.getProperty(secondExternalResourceKey));
assertNotNull("Cache has null as a resource key",
cacheAfterSecondRun.getProperty(secondExternalResourceKey));
}

@Test
Expand Down Expand Up @@ -795,15 +832,17 @@ public void testCheckerProcessCallAllNeededMethodsOfFileSets() throws Exception
checker.process(Collections.singletonList(new File("dummy.java")));
final List<String> expected =
Arrays.asList("beginProcessing", "finishProcessing", "destroy");
assertArrayEquals(expected.toArray(), fileSet.getMethodCalls().toArray());
assertArrayEquals("Method calls were not expected",
expected.toArray(), fileSet.getMethodCalls().toArray());
}

@Test
public void testSetFileSetCheckSetsMessageDispatcher() throws Exception {
final DummyFileSet fileSet = new DummyFileSet();
final Checker checker = new Checker();
checker.addFileSetCheck(fileSet);
assertEquals(checker, fileSet.getInternalMessageDispatcher());
assertEquals("Message dispatcher was not expected",
checker, fileSet.getInternalMessageDispatcher());
}

@Test
Expand Down Expand Up @@ -886,10 +925,14 @@ public void testDefaultLoggerClosesItStreams() throws Exception {

verify(checker, tmpFile.getPath(), tmpFile.getPath(), expected);

assertEquals(1, testInfoOutputStream.getCloseCount());
assertEquals(3, testInfoOutputStream.getFlushCount());
assertEquals(1, testErrorOutputStream.getCloseCount());
assertEquals(1, testErrorOutputStream.getFlushCount());
assertEquals("Close count was not expected",
1, testInfoOutputStream.getCloseCount());
assertEquals("Flush count was not expected",
3, testInfoOutputStream.getFlushCount());
assertEquals("Close count was not expected",
1, testErrorOutputStream.getCloseCount());
assertEquals("Flush count was not expected",
1, testErrorOutputStream.getFlushCount());
}

@Test
Expand All @@ -905,8 +948,10 @@ public void testXmlLoggerClosesItStreams() throws Exception {

verify(checker, tmpFile.getPath(), tmpFile.getPath(), expected);

assertEquals(1, testInfoOutputStream.getCloseCount());
assertEquals(0, testInfoOutputStream.getFlushCount());
assertEquals("Close count was not expected",
1, testInfoOutputStream.getCloseCount());
assertEquals("Flush count was not expected",
0, testInfoOutputStream.getFlushCount());
}

private static class DummyFilter implements Filter {
Expand Down

0 comments on commit 1c38e2e

Please sign in to comment.