From 7a767fe900778657011e9c3d7f2fac268a1610ce Mon Sep 17 00:00:00 2001 From: jrm Date: Mon, 15 Jun 2015 07:22:31 -0700 Subject: [PATCH] [infer][Java] use the containsExactly matcher for resource leak tests --- .../endtoend/java/infer/CursorLeaksTest.java | 182 +-- .../infer/FilterInputStreamLeaksTest.java | 279 +---- .../infer/FilterOutputStreamLeaksTest.java | 313 +---- .../endtoend/java/infer/ReaderLeaksTest.java | 257 +--- .../java/infer/ResourceLeaksTest.java | 1115 +---------------- .../endtoend/java/infer/WriterLeaksTest.java | 230 +--- 6 files changed, 90 insertions(+), 2286 deletions(-) diff --git a/infer/tests/endtoend/java/infer/CursorLeaksTest.java b/infer/tests/endtoend/java/infer/CursorLeaksTest.java index 129fe587d..7c24f6220 100644 --- a/infer/tests/endtoend/java/infer/CursorLeaksTest.java +++ b/infer/tests/endtoend/java/infer/CursorLeaksTest.java @@ -7,9 +7,7 @@ package endtoend.java.infer; import static org.hamcrest.MatcherAssert.assertThat; -import static utils.matchers.ResultContainsErrorInMethod.contains; -import static utils.matchers.ResultContainsNoErrorInMethod.doesNotContain; -import static utils.matchers.ResultContainsOnlyTheseErrors.containsOnly; +import static utils.matchers.ResultContainsExactly.containsExactly; import org.junit.BeforeClass; import org.junit.Test; @@ -21,7 +19,7 @@ import utils.InferResults; public class CursorLeaksTest { - public static final String CursorLeaks = + public static final String SOURCE_FILE = "infer/tests/codetoanalyze/java/infer/CursorLeaks.java"; public static final String RESOURCE_LEAK = "RESOURCE_LEAK"; @@ -30,183 +28,29 @@ public class CursorLeaksTest { @BeforeClass public static void loadResults() throws IOException { - inferResults = InferResults.loadInferResults(CursorLeaksTest.class, CursorLeaks); + inferResults = InferResults.loadInferResults(CursorLeaksTest.class, SOURCE_FILE); } @Test - public void whenInferRunsOnCursorClosedThenResourceLeakIsFound() + public void test() throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, CursorLeaks, - "cursorClosed")); - } - - @Test - public void whenInferRunsOnCursorNotClosedThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - CursorLeaks, - "cursorNotClosed" - ) - ); - } - - @Test - public void whenInferRunsOnGetImageCountHelperNotClosedThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - CursorLeaks, - "getImageCountHelperNotClosed" - ) - ); - } - - @Test - public void whenInferRunsOnGetImageCountHelperClosedThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, CursorLeaks, - "getImageCountHelperClosed")); - } - - @Test - public void whenInferRunsOnGetBucketCountNotClosedThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - CursorLeaks, - "getBucketCountNotClosed" - ) - ); - } - - @Test - public void whenInferRunsOnGetBucketCountClosedThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, CursorLeaks, - "getBucketCountClosed")); - } - - @Test - public void whenInferRunsOnQueryUVMLegacyDbNotClosedThenResourceLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - CursorLeaks, - "queryUVMLegacyDbNotClosed" - ) - ); - } - - @Test - public void whenInferRunsOnQueryUVMLegacyDbClosedThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, CursorLeaks, - "queryUVMLegacyDbClosed")); - } - - @Test - public void whenInferRunsOnCompleteDownloadNotClosedThenResourceLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - CursorLeaks, - "completeDownloadNotClosed" - ) - ); - } - - @Test - public void whenInferRunsOnCompleteDownloadClosedThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, CursorLeaks, - "completeDownloadClosed")); - } - - @Test - public void whenInferRunsOnLoadPrefsFromContentProvNotClosedThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - CursorLeaks, - "loadPrefsFromContentProviderNotClosed" - ) - ); - } - - @Test - public void whenInferRunsOnLoadPrefsFromContentProvClosedThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, CursorLeaks, - "loadPrefsFromContentProviderClosed")); - } - - - @Test - public void whenInferRunsOnResourceLeaksThenOnlyTheExpectedErrorsAreFound() - throws InterruptedException, IOException, InferException { - String[] expectedMethods = { - "cursorClosed", + String[] methods = { "cursorNotClosed", - "getImageCountHelperClosed", "getImageCountHelperNotClosed", "getBucketCountNotClosed", - "getBucketCountClosed", "queryUVMLegacyDbNotClosed", - "queryUVMLegacyDbClosed", - "completeDownloadClosed", "completeDownloadNotClosed", - "loadPrefsFromContentProviderClosed", - "loadPrefsFromContentProviderNotClosed" + "loadPrefsFromContentProviderNotClosed", }; assertThat( - "No unexpected errors should be found", inferResults, - containsOnly( + "Results should not contain resource leak errors", + inferResults, + containsExactly( RESOURCE_LEAK, - CursorLeaks, - expectedMethods)); + SOURCE_FILE, + methods + ) + ); } } diff --git a/infer/tests/endtoend/java/infer/FilterInputStreamLeaksTest.java b/infer/tests/endtoend/java/infer/FilterInputStreamLeaksTest.java index 239cfba02..973d55e60 100644 --- a/infer/tests/endtoend/java/infer/FilterInputStreamLeaksTest.java +++ b/infer/tests/endtoend/java/infer/FilterInputStreamLeaksTest.java @@ -6,9 +6,7 @@ package endtoend.java.infer; import static org.hamcrest.MatcherAssert.assertThat; -import static utils.matchers.ResultContainsErrorInMethod.contains; -import static utils.matchers.ResultContainsNoErrorInMethod.doesNotContain; -import static utils.matchers.ResultContainsOnlyTheseErrors.containsOnly; +import static utils.matchers.ResultContainsExactly.containsExactly; import org.junit.BeforeClass; import org.junit.Test; @@ -20,7 +18,7 @@ import utils.InferResults; public class FilterInputStreamLeaksTest { - public static final String FilterInputStreamLeaks = + public static final String SOURCE_FILE = "infer/tests/codetoanalyze/java/infer/FilterInputStreamLeaks.java"; public static final String RESOURCE_LEAK = "RESOURCE_LEAK"; @@ -31,284 +29,33 @@ public class FilterInputStreamLeaksTest { public static void loadResults() throws InterruptedException, IOException { inferResults = InferResults.loadInferResults( FilterInputStreamLeaksTest.class, - FilterInputStreamLeaks); + SOURCE_FILE); } - //BufferedInputStream tests @Test - public void whenInferRunsOnBufferedInputStreamNotClosedAfterThenRLIsFound() + public void test() throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "bufferedInputStreamNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnBufferedInputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "bufferedInputStreamClosedAfterReset")); - } - - //CheckedInputStream tests - @Test - public void whenInferRunsOnCheckedInputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "checkedInputStreamNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnCheckedInputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "checkedInputStreamClosedAfterSkip")); - } - - //CipherInputStream tests - @Test - public void whenInferRunsOnCipherInputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "cipherInputStreamNotClosedAfterSkip" - ) - ); - } - - @Test - public void whenInferRunsOnCipherInputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "cipherInputStreamClosedAfterRead")); - } - - //DataInputStream tests - @Test - public void whenInferRunsOnDataInputStreamNotClosedAfterReadThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "dataInputStreamNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnDataInputStreamClosedAfterReadThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "dataInputStreamClosedAfterReadBoolean")); - } - - //DeflaterInputStream tests - @Test - public void whenInferRunsOnDeflaterInputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "deflaterInputStreamNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnDeflaterInputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "deflaterInputStreamClosedAfterReset")); - } - - //GZipInputStream tests - @Test - public void whenInferRunsOnGzipInputStreamNotClosedAfterReadThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "gzipInputStreamNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnGzipInputStreamClosedAfterReadThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "gzipInputStreamClosedAfterRead")); - } - - //DigestInputStream tests - @Test - public void whenInferRunsOnDigestInputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "digestInputStreamNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnDigestInputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "digestInputStreamClosedAfterRead")); - } - - //InflaterInputStream tests - @Test - public void whenInferRunsOnInflaterInputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "inflaterInputStreamNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnInflaterInputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "inflaterInputStreamClosedAfterAvailable")); - } - - //PushbackInputStream tests - @Test - public void whenInferRunsOnInPushbackInputStreamNotClosedThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "pushbackInputStreamNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnPushbackInputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterInputStreamLeaks, - "pushbackInputStreamClosedAfterReset")); - } - - @Test - public void whenInferRunsThenOnlyTheExpectedErrorsAreFound() - throws InterruptedException, IOException, InferException { - String[] expectedMethods = { + String[] methods = { "bufferedInputStreamNotClosedAfterRead", - "bufferedInputStreamClosedAfterReset", "checkedInputStreamNotClosedAfterRead", - "checkedInputStreamClosedAfterSkip", "cipherInputStreamNotClosedAfterSkip", - "cipherInputStreamClosedAfterRead", "dataInputStreamNotClosedAfterRead", - "dataInputStreamClosedAfterReadBoolean", "deflaterInputStreamNotClosedAfterRead", - "deflaterInputStreamClosedAfterReset", "gzipInputStreamNotClosedAfterRead", - "gzipInputStreamClosedAfterRead", + "gzipInputStreamNotClosedAfterRead", "digestInputStreamNotClosedAfterRead", - "digestInputStreamClosedAfterRead", "inflaterInputStreamNotClosedAfterRead", - "inflaterInputStreamClosedAfterAvailable", - "progressMonitorInputStreamNotClosedAfterRead", - "progressMonitorInputStreamClosedAfterSkip", "pushbackInputStreamNotClosedAfterRead", - "pushbackInputStreamClosedAfterReset", - }; assertThat( - "No unexpected errors should be found", inferResults, - containsOnly( + "Results should contain a resource leak error", + inferResults, + containsExactly( RESOURCE_LEAK, - FilterInputStreamLeaks, - expectedMethods)); + SOURCE_FILE, + methods + ) + ); } } diff --git a/infer/tests/endtoend/java/infer/FilterOutputStreamLeaksTest.java b/infer/tests/endtoend/java/infer/FilterOutputStreamLeaksTest.java index 6a8ba3cbf..39dc9b539 100644 --- a/infer/tests/endtoend/java/infer/FilterOutputStreamLeaksTest.java +++ b/infer/tests/endtoend/java/infer/FilterOutputStreamLeaksTest.java @@ -6,9 +6,7 @@ package endtoend.java.infer; import static org.hamcrest.MatcherAssert.assertThat; -import static utils.matchers.ResultContainsErrorInMethod.contains; -import static utils.matchers.ResultContainsNoErrorInMethod.doesNotContain; -import static utils.matchers.ResultContainsOnlyTheseErrors.containsOnly; +import static utils.matchers.ResultContainsExactly.containsExactly; import org.junit.BeforeClass; import org.junit.Test; @@ -20,7 +18,7 @@ import utils.InferResults; public class FilterOutputStreamLeaksTest { - public static final String FilterOutputStreamLeaks = + public static final String SOURCE_FILE = "infer/tests/codetoanalyze/java/infer/FilterOutputStreamLeaks.java"; public static final String RESOURCE_LEAK = "RESOURCE_LEAK"; @@ -31,319 +29,34 @@ public class FilterOutputStreamLeaksTest { public static void loadResults() throws InterruptedException, IOException { inferResults = InferResults.loadInferResults( FilterOutputStreamLeaksTest.class, - FilterOutputStreamLeaks); + SOURCE_FILE); } - //FilterOutputStream tests @Test - public void whenInferRunsOnFilterOutputStreamNotClosedAfterThenRLIsFound() + public void test() throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "filterOutputStreamNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnFilterOutputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "filterOutputStreamClosedAfterWrite" - ) - ); - } - - //DataOutputStream tests - @Test - public void whenInferRunsOnDataOutputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "dataOutputStreamNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnDataOutputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "dataOutputStreamClosedAfterWrite" - ) - ); - } - - //BufferedOutputStream tests - @Test - public void whenInferRunsOnBufferedOutputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "bufferedOutputStreamNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnBufferedOutputStreamClosedAftThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "bufferedOutputStreamClosedAfterWrite" - ) - ); - } - - //CheckedOutputStream tests - @Test - public void whenInferRunsOnCheckedOutputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "checkedOutputStreamNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnCheckedOutputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "checkedOutputStreamClosedAfterWrite")); - } - - //CipherOutputStream tests - @Test - public void whenInferRunsOnCipherOutputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "cipherOutputStreamNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnCipherOutputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "cipherOutputStreamClosedAfterWrite")); - } - - //DeflaterOutputStream tests - @Test - public void whenInferRunsOnDeflaterOutputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "deflaterOutputStreamNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnDeflaterOutputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "deflaterOutputStreamClosedAfterWrite")); - } - - //GZipOutputStream tests - @Test - public void whenInferRunsOnGzipOutputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "gzipOutputStreamNotClosedAfterFlush" - ) - ); - } - - @Test - public void whenInferRunsOnGzipOutputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "gzipOutputStreamClosedAfterWrite")); - } - - //DigestOutputStream tests - @Test - public void whenInferRunsOnDigestOutputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "digestOutputStreamNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnDigestOutputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "digestOutputStreamClosedAfterWrite")); - } - - //InflaterOutputStream tests - @Test - public void whenInferRunsOnInflaterOutputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "inflaterOutputStreamNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnInflaterOutputStreamClosedAfterThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "inflaterOutputStreamClosedAfterWrite")); - } - - //PrintStream tests - @Test - public void whenInferRunsOnPrintStreamNotClosedAfterWriteThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "printStreamNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnPrintStreamClosedAfterWriteThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - FilterOutputStreamLeaks, - "printStreamClosedAfterWrite" - ) - ); - } - - - @Test - public void whenInferRunsOnResourceLeaksThenOnlyTheExpectedErrorsAreFound() - throws InterruptedException, IOException, InferException { - String[] expectedMethods = { + String[] methods = { "filterOutputStreamNotClosedAfterWrite", - "filterOutputStreamClosedAfterWrite", "dataOutputStreamNotClosedAfterWrite", - "dataOutputStreamClosedAfterWrite", "bufferedOutputStreamNotClosedAfterWrite", - "bufferedOutputStreamClosedAfterWrite", "checkedOutputStreamNotClosedAfterWrite", - "checkedOutputStreamClosedAfterWrite", "cipherOutputStreamNotClosedAfterWrite", - "cipherOutputStreamClosedAfterWrite", "deflaterOutputStreamNotClosedAfterWrite", - "deflaterOutputStreamClosedAfterWrite", "digestOutputStreamNotClosedAfterWrite", - "digestOutputStreamClosedAfterWrite", "inflaterOutputStreamNotClosedAfterWrite", - "inflaterOutputStreamClosedAfterWrite", "gzipOutputStreamNotClosedAfterFlush", - "gzipOutputStreamClosedAfterWrite", + "gzipOutputStreamNotClosedAfterFlush", "printStreamNotClosedAfterWrite", - "printStreamClosedAfterWrite", }; assertThat( - "No unexpected errors should be found", inferResults, - containsOnly( + "Results should contain a resource leak error", + inferResults, + containsExactly( RESOURCE_LEAK, - FilterOutputStreamLeaks, - expectedMethods)); + SOURCE_FILE, + methods + ) + ); } } diff --git a/infer/tests/endtoend/java/infer/ReaderLeaksTest.java b/infer/tests/endtoend/java/infer/ReaderLeaksTest.java index cff241182..861c9506e 100644 --- a/infer/tests/endtoend/java/infer/ReaderLeaksTest.java +++ b/infer/tests/endtoend/java/infer/ReaderLeaksTest.java @@ -6,9 +6,7 @@ package endtoend.java.infer; import static org.hamcrest.MatcherAssert.assertThat; -import static utils.matchers.ResultContainsErrorInMethod.contains; -import static utils.matchers.ResultContainsNoErrorInMethod.doesNotContain; -import static utils.matchers.ResultContainsOnlyTheseErrors.containsOnly; +import static utils.matchers.ResultContainsExactly.containsExactly; import org.junit.BeforeClass; import org.junit.Test; @@ -35,265 +33,28 @@ public class ReaderLeaksTest { inferResults = InferResults.loadInferResults(ReaderLeaksTest.class, ReaderLeaks); } - //Reader tests @Test - public void whenInferRunsOnReaderNotClosedAfterReadThenRLIsFound() + public void test() throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ReaderLeaks, - "readerNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnReaderClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ReaderLeaks, - "readerClosed")); - } - - //BufferedReader tests - @Test - public void whenInferRunsOnBufferedReaderNotClosedAfterReadThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ReaderLeaks, - "bufferedReaderNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnBufferedReaderClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ReaderLeaks, - "bufferedReaderClosed")); - } - - //InputStreamReader tests - @Test - public void whenInferRunsOnInputStreamReadNotClosedAfterReadThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ReaderLeaks, - "inputStreamReaderNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnInputStreamReaderClosedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ReaderLeaks, - "inputStreamReaderClosed")); - } - - //FileReader tests - - @Test - public void whenInferRunsOnFileReaderNotClosedAfterReadThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ReaderLeaks, - "fileReaderNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnFileReaderClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ReaderLeaks, - "fileReaderClosed")); - } - - //PushbackReader tests - @Test - public void whenInferRunsOnPushbackReaderNotClosedAfterReadThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ReaderLeaks, - "pushbackReaderNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnPushbackReaderClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ReaderLeaks, - "pushbackReaderClosed")); - } - - //PipedReader tests - @Test - public void whenInferRunsOnPipedReaderNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ReaderLeaks, - "pipedReaderNotClosedAfterConstructedWithWriter" - ) - ); - } - - @Test - public void whenInferRunsOnPipedReaderNotClosedAfterConnectThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ReaderLeaks, - "pipedReaderNotClosedAfterConnect" - ) - ); - } - - @Test - public void whenInferRunsOnPipedReaderClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ReaderLeaks, - "pipedReaderClosed")); - } - - @Test - public void whenInferRunsOnPipedReaderNotConnectedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ReaderLeaks, - "pipedReaderNotConnected")); - } - - /** - * This potential false positive is up for discussion... - */ - @Test - public void whenInferRunsOnPipedReaderFalsePositiveThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ReaderLeaks, - "pipedReaderFalsePositive" - ) - ); - } - - @Test - public void whenInferRunsOnStrictLineReaderClosedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ReaderLeaks, - "strictLineReaderClosed")); - } - - @Test - public void whenInferRunsOnStrictLineReaderLeakThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ReaderLeaks, - "strictLineReaderNoLeak")); - } - - - @Test - public void whenInferRunsOnReaderLeaksThenOnlyTheExpectedErrorsAreFound() - throws InterruptedException, IOException, InferException { - String[] expectedMethods = { + String[] methods = { "readerNotClosedAfterRead", - "readerClosed", "bufferedReaderNotClosedAfterRead", - "bufferedReaderClosed", "inputStreamReaderNotClosedAfterRead", - "inputStreamReaderClosed", "fileReaderNotClosedAfterRead", - "fileReaderClosed", "pushbackReaderNotClosedAfterRead", - "pushbackReaderClosed", "pipedReaderNotClosedAfterConstructedWithWriter", "pipedReaderNotClosedAfterConnect", - "pipedReaderClosed", - "pipedReaderNotConnected", "pipedReaderFalsePositive", - "strictLineReaderClosed", - "strictLineReaderNoLeak" }; assertThat( - "No unexpected errors should be found", inferResults, - containsOnly( + "Results should contain the following resource leak errors", + inferResults, + containsExactly( RESOURCE_LEAK, ReaderLeaks, - expectedMethods)); + methods + ) + ); } } diff --git a/infer/tests/endtoend/java/infer/ResourceLeaksTest.java b/infer/tests/endtoend/java/infer/ResourceLeaksTest.java index ee4a4a833..4067679d1 100644 --- a/infer/tests/endtoend/java/infer/ResourceLeaksTest.java +++ b/infer/tests/endtoend/java/infer/ResourceLeaksTest.java @@ -6,10 +6,8 @@ package endtoend.java.infer; import static org.hamcrest.MatcherAssert.assertThat; -import static utils.matchers.ResultContainsErrorInMethod.contains; -import static utils.matchers.ResultContainsNoErrorInMethod.doesNotContain; import static utils.matchers.ResultContainsNumberOfErrorsInMethod.containsNumberOfErrors; -import static utils.matchers.ResultContainsOnlyTheseErrors.containsOnly; +import static utils.matchers.ResultContainsExactly.containsExactly; import org.junit.BeforeClass; import org.junit.Test; @@ -21,7 +19,7 @@ import utils.InferResults; public class ResourceLeaksTest { - public static final String ResourceLeaks = + public static final String SOURCE_FILE = "infer/tests/codetoanalyze/java/infer/ResourceLeaks.java"; public static final String RESOURCE_LEAK = "RESOURCE_LEAK"; @@ -30,1127 +28,74 @@ public class ResourceLeaksTest { @BeforeClass public static void loadResults() throws InterruptedException, IOException { - inferResults = InferResults.loadInferResults(ResourceLeaksTest.class, ResourceLeaks); + inferResults = InferResults.loadInferResults(ResourceLeaksTest.class, SOURCE_FILE); } - //FileOutputStream tests - - @Test - public void whenInferRunsOnFileOutputStreamNotClosedThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "fileOutputStreamNotClosed" - ) - ); - } - - @Test - public void whenInferRunsOnFileOutputStreamNotClosed2ThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "fileOutputStreamNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnFileOutputStreamClosedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "fileOutputStreamClosed")); - } - - @Test - public void whenInferRunsOnFileOutputStreamTwoLeaksThenTwoLeaksAreFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain 2 resource leak error", - inferResults, - containsNumberOfErrors( - RESOURCE_LEAK, - ResourceLeaks, - "fileOutputStreamTwoLeaks", - 2 - ) - ); - } - - //TwoResource tests - @Test - public void whenInferRunsOnTwoResourcesHeliosFixThenResourceLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "twoResourcesHeliosFix" - ) - ); - } - - @Test - public void whenInferRunsOnTwoResourcesThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "twoResources" - ) - ); - } - - @Test - public void whenInferRunsOnTwoResourcesCommonFixThenResourceLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "twoResourcesCommonFix" - ) - ); - } - - @Test - public void whenInferRunsOnTwoResourcesServerSocketThenResourceLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "twoResourcesServerSocket" - ) - ); - } - - @Test - public void whenInferRunsOnTwoResourcesRandomAccessFileThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "twoResourcesRandomAccessFile" - ) - ); - } - - - @Test - public void whenInferRunsOnTwoResourcesRAFCommonFixThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "twoResourcesRandomAccessFileCommonFix" - ) - ); - } - - //NestedResource tests - - @Test - public void whenInferRunsOnNestedGoodThenResourceLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "nestedGood" - ) - ); - } - - @Test - public void whenInferRunsOnNestedBad1ThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "nestedBad1" - ) - ); - } - - - @Test - public void whenInferRunsOnNestedBad2ThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "nestedBad2" - ) - ); - } - - - @Test - public void whenInferRunsOnObjectInputStreamClosedNestedBadThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "objectInputStreamClosedNestedBad" - ) - ); - } - - @Test - public void whenInferRunsOnObjectOutputStreamClosedNestedBadThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "objectOutputStreamClosedNestedBad" - ) - ); - } - - - //ZipFile tests (JarFile tests also test ZipFiles) - - @Test - public void whenInferRunsOnZipFileLeakExceptionalBranchThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, ResourceLeaks, - "zipFileLeakExceptionalBranch" - ) - ); - } - - @Test - public void whenInferRunsOnzipFileNoLeakThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "jarFileClosed" - ) - ); - } - - //JarFile tests - - @Test - public void whenInferRunsOnJarFileNotClosedThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, ResourceLeaks, - "jarFileNotClosed" - ) - ); - } - - @Test - public void whenInferRunsOnJarFileClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "jarFileClosed" - ) - ); - } - - //FileInputStream tests - @Test - public void whenInferRunsOnFileInputStreamNotClosedAfterReadThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, ResourceLeaks, - "fileInputStreamNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnFileInputStreamClosedThenResourceLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "fileInputStreamClosed" - ) - ); - } - - //PipedInputStream tests - @Test - public void whenInferRunsOnPipedInputStreamNotClosedAftReadThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, ResourceLeaks, - "pipedInputStreamNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnPipedInputStreamClosedThenResourceLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "pipedInputStreamClosed" - ) - ); - } - - //PipedOutputStream tests - @Test - public void whenInferRunsOnPipedOutputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "pipedOutputStreamNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnPipedOutputStreamClosedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "pipedOutputStreamClosed" - ) - ); - } - - //ObjectOutputStream tests - @Test - public void whenInferRunsOnObjectOutputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "objectOutputStreamNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnObjectOutputStreamClosedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "objectOutputStreamClosed" - ) - ); - } - - @Test - public void whenInferRunsOnObjectOutputStreamClosed2ThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "objectOutputStreamClosed2" - ) - ); - } - - //ObjectInputStream tests - @Test - public void whenInferRunsOnObjectInputStreamNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "objectInputStreamNotClosedAfterRead" - ) - ); - } - - @Test - public void whenInferRunsOnObjectInputStreamClosedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "objectInputStreamClosed" - ) - ); - } - - //JarInputStream tests - @Test - public void whenInferRunsJarInputStreamLeakThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "jarInputStreamLeak" - ) - ); - } - - @Test - public void whenInferRunsOnJarInputStreamNoLeakThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "jarInputStreamNoLeak" - ) - ); - } - - @Test - public void whenInferRunsNestedBadJarInputStreamThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "nestedBadJarInputStream" - ) - ); - } - - //JarOutputStream tests - @Test - public void whenInferRunsOnJarOutputStreamLeakThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, ResourceLeaks, - "jarOutputStreamLeak" - ) - ); - } - - @Test - public void whenInferRunsOnJarOutputStreamNoLeakThenResourceLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "jarOutputStreamNoLeak" - ) - ); - } - - - @Test - public void whenInferRunsNestedBadJarOutputStreamThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, ResourceLeaks, - "nestedBadJarOutputStream" - ) - ); - } - - - //Socket tests - @Test - public void whenInferRunsOnSocketNotClosedThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "socketNotClosed" - ) - ); - } - - @Test - public void whenInferRunsOnSocketClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "socketClosed" - ) - ); - } - - @Test - public void whenInferRunsOnSocketInputStreamNotClosedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "socketInputStreamNotClosed" - ) - ); - } - - @Test - public void whenInferRunsOnSocketInputStreamClosedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "socketInputStreamClosed" - ) - ); - } - - @Test - public void whenInferRunsOnSocketOutputStreamNotClosedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "socketOutputStreamNotClosed" - ) - ); - } - - @Test - public void whenInferRunsOnSocketOutputStreamClosedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "socketOutputStreamClosed" - ) - ); - } - - //ServerSocket tests - @Test - public void whenInferRunsOnServerSocketNotClosedThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "serverSocketNotClosed" - ) - ); - } - - @Test - public void whenInferRunsOnServerSocketClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "serverSocketClosed" - ) - ); - } - - @Test - public void whenInferRunsOnServerSocketWithSocketClosedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "serverSocketWithSocketClosed" - ) - ); - } - - //HttpURLConnection - @Test - public void whenInferRunsOnOpenHttpURLConnectionNotDisconnThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, ResourceLeaks, - "openHttpURLConnectionNotDisconnected" - ) - ); - } - - @Test - public void whenInferRunsOnOpenHttpURLConnectionDisconnThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "openHttpURLConnectionDisconnected" - ) - ); - } - - @Test - public void whenInferRunsOnOpenHttpsURLConnectionNotDisconnThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "openHttpsURLConnectionNotDisconnected" - ) - ); - } - - @Test - public void whenInferRunsOnOpenHttpsURLConnectionDisconnThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "openHttpsURLConnectionDisconnected" - ) - ); - } - - //Closeables.close - @Test - public void whenInferRunsOnClosedWithCloseablesThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "closedWithCloseables" - ) - ); - } - - @Test - public void whenInferRunsOnNullClosedWithCloseablesThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "closeNullWithCloseables" - ) - ); - } - - //Closeables.closeQuietly - @Test - public void whenInferRunsOnClosedQuietlyWithCloseablesThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "closedQuietlyWithCloseables" - ) - ); - } - - @Test - public void whenInferRunsOnNullClosedQuietlyWithCloseablesThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "closeNullQuietlyWithCloseables" - ) - ); - } - - // JsonParser - @Test - public void whenInferRunsOnParseFromStringNoLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "parseFromStringAndNotClose" - ) - ); - } - - @Test - public void whenInferRunsOnParseFromInputStreamAndCloseNoLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "parseFromInputStreamAndClose" - ) - ); - } - - @Test - public void whenInferRunsOnParseFromInputStreamAndLeakLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "parseFromInputStreamAndLeak" - ) - ); - } - -// Irritating Installation.java fp that has been banished hopefully forever - - @Test - public void whenInferRunsOnreadInstallationFileBad() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "readInstallationFileBad" - ) - ); - } - - @Test - public void whenInferRunsOnreadInstallationFileGood() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "readInstallationFileGood" - ) - ); - } - - - //URLConnection tests - @Test - public void whenInferRunsOnReadConfigCloseStreamThenLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "readConfigCloseStream" - ) - ); - } - - @Test - public void whenInferRunsOnReadConfigNotCloseStreamThenLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "readConfigNotCloseStream" - ) - ); - } - - @Test - public void whenInferRunsOnReadConfigNotClosedOK() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "readConfigNotClosedOK" - ) - ); - } - - @Test - public void whenInferRunsThemeObtainTypedArrayAndRecycle() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "themeObtainTypedArrayAndRecycle" - ) - ); - } - - @Test - public void whenInferRunsThemeObtainTypedArrayAndLeak() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "themeObtainTypedArrayAndLeak" - ) - ); - } - - @Test - public void whenInferRunsActivityObtainTypedArrayAndRecycle() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "activityObtainTypedArrayAndRecycle" - ) - ); - } - - @Test - public void whenInferRunsActivityObtainTypedArrayAndLeak() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "activityObtainTypedArrayAndLeak" - ) - ); - } - - @Test - public void whenInferRunsContextObtainTypedArrayAndRecycle() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "contextObtainTypedArrayAndRecycle" - ) - ); - } - - @Test - public void whenInferRunsContextObtainTypedArrayAndLeak() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "contextObtainTypedArrayAndLeak" - ) - ); - } - - @Test - public void whenInferRunsOnCopyFileLeak() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "copyFileLeak" - ) - ); - } - - @Test - public void whenInferRunsOnCopyFileClose() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "copyFileClose" - ) - ); - } - - @Test - public void whenInferRunsOnCheckNotNullCauseNoLeakClose() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "checkNotNullCauseNoLeak" - ) - ); - } - - // java.utils.Scanner - @Test - public void whenInferRunsOnScannerNotClosedThenResourceLeakIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - ResourceLeaks, - "scannerNotClosed" - ) - ); - } - @Test - public void whenInferRunsOnScannerClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - ResourceLeaks, - "scannerClosed" - ) - ); - } - - - // remember to put method names here @Test - public void whenInferRunsOnResourceLeaksThenOnlyTheExpectedErrorsAreFound() + public void test() throws InterruptedException, IOException, InferException { - String[] expectedMethods = { + String[] methods = { "fileOutputStreamNotClosed", "fileOutputStreamNotClosedAfterWrite", - "fileOutputStreamClosed", "fileOutputStreamTwoLeaks", "twoResources", - "twoResourcesHeliosFix", - "twoResourcesCommonFix", "twoResourcesServerSocket", "twoResourcesRandomAccessFile", - "twoResourcesRandomAccessFileCommonFix", - "nestedGood", "nestedBad1", "nestedBad2", "objectInputStreamClosedNestedBad", "objectOutputStreamClosedNestedBad", "zipFileLeakExceptionalBranch", - "zipFileNoLeak", "jarFileNotClosed", - "jarFileClosed", "fileInputStreamNotClosedAfterRead", - "fileInputStreamClosed", "pipedInputStreamNotClosedAfterRead", - "pipedInputStreamClosed", "pipedOutputStreamNotClosedAfterWrite", - "pipedOutputStreamClosed", "objectOutputStreamNotClosedAfterWrite", - "objectOutputStreamClosed", "objectInputStreamNotClosedAfterRead", - "objectInputStreamClosed", - "objectInputStreamClosed2", - "jarInputStreamNoLeak", "jarInputStreamLeak", - "jarOutputStreamNoLeak", - "jarOutputStreamLeak", "nestedBadJarInputStream", + "jarOutputStreamLeak", "nestedBadJarOutputStream", "socketNotClosed", - "socketClosed", - "socketInputStreamNotClosed", - "socketInputStreamClosed", - "socketOutputStreamNotClosed", - "socketOutputStreamClosed", "serverSocketNotClosed", - "serverSocketClosed", - "serverSocketWithSocketClosed", - "openHttpURLConnectionDisconnected", "openHttpURLConnectionNotDisconnected", "openHttpsURLConnectionNotDisconnected", - "openHttpsURLConnectionDisconnected", - "closedWithCloseables", - "closedQuietlyWithCloseables", - "parseFromStringAndClose", - "parseFromStringAndLeak", - "parseFromInputStreamAndClose", "parseFromInputStreamAndLeak", - "ignore", - "readInstallationFileGood", "readInstallationFileBad", - "readConfigCloseStream", "readConfigNotCloseStream", - "readConfigNotClosedOK", - "themeObtainTypedArrayAndRecycle", "themeObtainTypedArrayAndLeak", - "activityObtainTypedArrayAndRecycle", "activityObtainTypedArrayAndLeak", - "contextObtainTypedArrayAndRecycle", "contextObtainTypedArrayAndLeak", - "copyFileClose", "copyFileLeak", - "checkNotNullCauseNoLeak", + "copyFileLeak", "scannerNotClosed", - "scannerClosed" }; assertThat( - "No unexpected errors should be found", inferResults, - containsOnly( + "Results should contain the following resource leak errors", + inferResults, + containsExactly( + RESOURCE_LEAK, + SOURCE_FILE, + methods + ) + ); + } + + + @Test + public void whenInferRunsOnFileOutputStreamTwoLeaksThenTwoLeaksAreFound() + throws InterruptedException, IOException, InferException { + assertThat( + "Results should contain 2 resource leak error", + inferResults, + containsNumberOfErrors( RESOURCE_LEAK, - ResourceLeaks, - expectedMethods)); + SOURCE_FILE, + "fileOutputStreamTwoLeaks", + 2 + ) + ); } } diff --git a/infer/tests/endtoend/java/infer/WriterLeaksTest.java b/infer/tests/endtoend/java/infer/WriterLeaksTest.java index bdefceb45..4522c2545 100644 --- a/infer/tests/endtoend/java/infer/WriterLeaksTest.java +++ b/infer/tests/endtoend/java/infer/WriterLeaksTest.java @@ -6,9 +6,7 @@ package endtoend.java.infer; import static org.hamcrest.MatcherAssert.assertThat; -import static utils.matchers.ResultContainsErrorInMethod.contains; -import static utils.matchers.ResultContainsNoErrorInMethod.doesNotContain; -import static utils.matchers.ResultContainsOnlyTheseErrors.containsOnly; +import static utils.matchers.ResultContainsExactly.containsExactly; import org.junit.BeforeClass; import org.junit.Test; @@ -21,7 +19,7 @@ import utils.InferResults; public class WriterLeaksTest { - public static final String WriterLeaks = + public static final String SOURCE_FILE = "infer/tests/codetoanalyze/java/infer/WriterLeaks.java"; public static final String RESOURCE_LEAK = "RESOURCE_LEAK"; @@ -30,235 +28,31 @@ public class WriterLeaksTest { @BeforeClass public static void loadResults() throws InterruptedException, IOException { - inferResults = InferResults.loadInferResults(WriterLeaksTest.class, WriterLeaks); + inferResults = InferResults.loadInferResults(WriterLeaksTest.class, SOURCE_FILE); } - //Writer tests @Test - public void whenInferRunsOnWriterNotClosedAfterWriteThenRLIsFound() + public void test() throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - WriterLeaks, - "writerNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnWriterClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - WriterLeaks, - "writerClosed" - ) - ); - } - - //PrintWriter tests - - @Test - public void whenInferRunsOnPrintWriterNotClosedAfterAppendThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, WriterLeaks, - "printWriterNotClosedAfterAppend" - ) - ); - } - - @Test - public void whenInferRunsOnPrintWriterClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, WriterLeaks, - "printWriterClosed" - ) - ); - } - - //BufferedWriter tests - - @Test - public void whenInferRunsOnBufferedWriterNotClosedAfterWriteThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - WriterLeaks, - "bufferedWriterNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnBufferedWriterClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, WriterLeaks, - "bufferedWriterClosed" - ) - ); - } - - //OutputStreamWriter tests - @Test - public void whenInferRunsOnOutputStreamWriterNotClosedAfterThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - WriterLeaks, - "outputStreamWriterNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnOutputStreamWriterClosedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, WriterLeaks, - "outputStreamWriterClosed" - ) - ); - } - - //FileWriter tests - @Test - public void whenInferRunsOnFileWriterNotClosedAfterWriteThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - WriterLeaks, - "fileWriterNotClosedAfterWrite" - ) - ); - } - - @Test - public void whenInferRunsOnFileWriterClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - WriterLeaks, - "fileWriterClosed" - ) - ); - } - - //PipedWriter tests - @Test - public void whenInferRunsOnPipedWriterNotClosedAfterConstrThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - WriterLeaks, - "pipedWriterNotClosedAfterConstructedWithReader" - ) - ); - } - - @Test - public void whenInferRunsOnPipedWriterNotClosedAfterConnectThenRLIsFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should contain a resource leak error", - inferResults, - contains( - RESOURCE_LEAK, - WriterLeaks, - "pipedWriterNotClosedAfterConnect" - ) - ); - } - - @Test - public void whenInferRunsOnPipedWriterClosedThenResourceLeakIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - WriterLeaks, - "pipedWriterClosed" - ) - ); - } - - @Test - public void whenInferRunsOnPipedWriterNotConnectedThenRLIsNotFound() - throws InterruptedException, IOException, InferException { - assertThat( - "Results should not contain a resource leak error", - inferResults, - doesNotContain( - RESOURCE_LEAK, - WriterLeaks, - "pipedWriterNotConnected" - ) - ); - } - - @Test - public void whenInferRunsOnPrintWriterThenOnlyTheExpectedErrorsAreFound() - throws InterruptedException, IOException, InferException { - String[] expectedMethods = { + String[] methods = { "writerNotClosedAfterWrite", - "writerClosed", "printWriterNotClosedAfterAppend", - "printWriterClosed", "bufferedWriterNotClosedAfterWrite", - "bufferedWriterClosed", "outputStreamWriterNotClosedAfterWrite", - "outputStreamWriterClosed", "fileWriterNotClosedAfterWrite", - "fileWriterClosed", "pipedWriterNotClosedAfterConstructedWithReader", "pipedWriterNotClosedAfterConnect", - "pipedWriterClosed", - "pipedWriterNotConnected", }; assertThat( - "No unexpected errors should be found", inferResults, - containsOnly( + "Results should contain a resource leak error", + inferResults, + containsExactly( RESOURCE_LEAK, - WriterLeaks, - expectedMethods)); + SOURCE_FILE, + methods + ) + ); } }