[infer][Java] use the containsExactly matcher for resource leak tests

master
jrm 10 years ago
parent a8a8501243
commit 7a767fe900

@ -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
)
);
}
}

@ -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
)
);
}
}

@ -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
)
);
}
}

@ -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
)
);
}
}

File diff suppressed because it is too large Load Diff

@ -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
)
);
}
}

Loading…
Cancel
Save