Skip to content

Commit ab33cb4

Browse files
committed
add tests to BadVisibilityOnSharedPrimitiveVariables
1 parent b820400 commit ab33cb4

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/BadVisibilityOnSharedPrimitiveVariablesTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ void failurePath_fieldWithBadVisibility_whenOtherMethodIsSynchronized() {
2727
assertBugInMethodAtLine(BUG_TYPE, "SynchronizedMethodAndBadVisibilityOnField", "shutdown", 17);
2828
}
2929

30+
@Test
31+
void failurePath_fieldWithBadVisibility_whenSetAndGetAreReordered() {
32+
performAnalysis("multithreaded/sharedPrimitiveVariables/FieldWithBadVisibilityReordered.class");
33+
assertBugTypeCount(BUG_TYPE, 1);
34+
assertBugInMethodAtLine(BUG_TYPE, "FieldWithBadVisibilityReordered", "run", 12);
35+
}
36+
37+
@Test
38+
void failurePath_fieldWithBadVisibility_whenClassHasTwoSetters() {
39+
performAnalysis("multithreaded/sharedPrimitiveVariables/FieldWithBadVisibilityTwoSetters.class");
40+
assertBugTypeCount(BUG_TYPE, 2);
41+
assertBugInMethodAtLine(BUG_TYPE, "FieldWithBadVisibilityTwoSetters", "shutdown", 18);
42+
assertBugInMethodAtLine(BUG_TYPE, "FieldWithBadVisibilityTwoSetters", "up", 22);
43+
}
44+
3045
@Test
3146
void failurePath_fieldWithBadVisibility_whenClassExtendsThread() {
3247
performAnalysis("multithreaded/sharedPrimitiveVariables/FieldWithBadVisibilityThread.class");
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package multithreaded.sharedPrimitiveVariables;
2+
3+
public class FieldWithBadVisibilityReordered extends Thread {
4+
private boolean done = false;
5+
6+
public void shutdown() {
7+
done = true;
8+
}
9+
10+
@Override
11+
public void run() {
12+
while (!done) {
13+
try {
14+
Thread.currentThread().sleep(1000);
15+
} catch(InterruptedException ie) {
16+
Thread.currentThread().interrupt();
17+
}
18+
}
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package multithreaded.sharedPrimitiveVariables;
2+
3+
public class FieldWithBadVisibilityTwoSetters extends Thread {
4+
private boolean done = false;
5+
6+
@Override
7+
public void run() {
8+
while (!done) {
9+
try {
10+
Thread.currentThread().sleep(1000);
11+
} catch(InterruptedException ie) {
12+
Thread.currentThread().interrupt();
13+
}
14+
}
15+
}
16+
17+
public void shutdown() {
18+
done = true;
19+
}
20+
21+
public void up() {
22+
done = false;
23+
}
24+
}

0 commit comments

Comments
 (0)