parent
fda373c706
commit
2b7060e917
@ -0,0 +1,82 @@
|
||||
// Copyright (c) 2015-Present Facebook. All rights reserved.
|
||||
|
||||
package codetoanalyze.java.infer;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
|
||||
public class CloseableAsResourceExample {
|
||||
|
||||
class LocalException extends IOException {
|
||||
}
|
||||
|
||||
class SomeResource implements Closeable {
|
||||
|
||||
native boolean isValid();
|
||||
void doSomething() throws LocalException {
|
||||
if (!isValid()) {
|
||||
throw new LocalException();
|
||||
}
|
||||
}
|
||||
public void close() {}
|
||||
}
|
||||
|
||||
void closingCloseable() {
|
||||
SomeResource res = new SomeResource();
|
||||
res.close();
|
||||
}
|
||||
|
||||
void notClosingCloseable() {
|
||||
SomeResource res = new SomeResource();
|
||||
} // should report a resource leak
|
||||
|
||||
void tryWithResource() {
|
||||
try (SomeResource res = new SomeResource()) {
|
||||
try {
|
||||
res.doSomething();
|
||||
} catch (LocalException e) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void withException() throws LocalException {
|
||||
SomeResource res = new SomeResource();
|
||||
res.doSomething();
|
||||
res.close();
|
||||
} // should report a resource leak
|
||||
|
||||
class Res implements Closeable {
|
||||
public Res() {
|
||||
}
|
||||
public void close() {}
|
||||
}
|
||||
|
||||
class Wrapper implements Closeable {
|
||||
Res mR;
|
||||
public Wrapper(Res r) {
|
||||
mR = r;
|
||||
}
|
||||
public void close() {
|
||||
mR.close();
|
||||
}
|
||||
}
|
||||
|
||||
class Sub extends Wrapper {
|
||||
public Sub(Res r) {
|
||||
super(r);
|
||||
}
|
||||
}
|
||||
|
||||
void closingWrapper() {
|
||||
Res r = new Res();
|
||||
Sub s = new Sub(r);
|
||||
s.close();
|
||||
}
|
||||
|
||||
void notClosingWrapper() {
|
||||
Sub s = new Sub(new Res());
|
||||
s.mR.close();
|
||||
} // should report a resource leak
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
// Copyright (c) 2015-Present Facebook. All rights reserved.
|
||||
|
||||
package endtoend.java.infer;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static utils.matchers.ResultContainsExactly.containsExactly;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import utils.InferException;
|
||||
import utils.InferResults;
|
||||
|
||||
public class CloseableAsResourceTest {
|
||||
|
||||
public static final String SOURCE_FILE =
|
||||
"infer/tests/codetoanalyze/java/infer/CloseableAsResourceExample.java";
|
||||
|
||||
public static final String RESOURCE_LEAK = "RESOURCE_LEAK";
|
||||
|
||||
private static InferResults inferResults;
|
||||
|
||||
@BeforeClass
|
||||
public static void loadResults() throws IOException {
|
||||
inferResults = InferResults.loadInferResults(CloseableAsResourceTest.class, SOURCE_FILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test()
|
||||
throws InterruptedException, IOException, InferException {
|
||||
String[] methods = {
|
||||
"withException",
|
||||
"notClosingCloseable",
|
||||
"notClosingWrapper",
|
||||
};
|
||||
assertThat(
|
||||
"Results should not contain resource leak errors",
|
||||
inferResults,
|
||||
containsExactly(
|
||||
RESOURCE_LEAK,
|
||||
SOURCE_FILE,
|
||||
methods
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue