forked from google/thread-weaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueListTest.java
More file actions
73 lines (60 loc) · 2.19 KB
/
UniqueListTest.java
File metadata and controls
73 lines (60 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Copyright 2009 Weaver authors
*
* This code is part of the Weaver tutorial and may be freely used.
*/
import com.google.testing.threadtester.AnnotatedTestRunner;
import com.google.testing.threadtester.MethodOption;
import com.google.testing.threadtester.ThreadedAfter;
import com.google.testing.threadtester.ThreadedBefore;
import com.google.testing.threadtester.ThreadedMain;
import com.google.testing.threadtester.ThreadedSecondary;
import java.util.HashSet;
import junit.framework.TestCase;
/**
* Unit test for UniqueList. Demonstrates use of
* {@link com.google.testing.threadtester.AnnotatedTestRunner}.
*
* NOTE: This test will fail. It was written to demonstrate a fault in the class
* under test.
*
* @author alasdair.mackintosh@gmail.com (Alasdair Mackintosh)
*/
public class UniqueListTest extends TestCase {
private static final String HELLO = "Hello";
private volatile UniqueList<String> uniqueList;
public void testPutIfAbsent() {
System.out.printf("In testPutIfAbsent\n");
// Create an AnnotatedTestRunner that will run the threaded tests defined in this
// class. We want to test the behaviour of the private method "putIfAbsentInternal" so
// we need to specify it by name using runner.setMethodOption()
AnnotatedTestRunner runner = new AnnotatedTestRunner();
HashSet<String> methods = new HashSet<String>();
runner.setMethodOption(MethodOption.ALL_METHODS, methods);
runner.setDebug(true);
runner.runTests(this.getClass(), UniqueList.class);
}
@ThreadedBefore
public void before() {
// Set up a new UniqueList instance for the test
uniqueList = new UniqueList<String>();
System.out.printf("Created new list\n");
}
@ThreadedMain
public void main() {
// Add a new element to the list in the main test thread
uniqueList.putIfAbsent(HELLO);
}
@ThreadedSecondary
public void secondary() {
// Add a new element to the list in the secondary test thread
uniqueList.putIfAbsent(HELLO);
}
@ThreadedAfter
public void after() {
// If UniqueList is behaving correctly, it should only contain
// a single copy of HELLO
assertEquals(1, uniqueList.size());
assertTrue(uniqueList.contains(HELLO));
}
}