Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2008 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans.junit; |
41 |
|
42 |
import java.util.ArrayList; |
43 |
import java.util.Arrays; |
44 |
import java.util.Collections; |
45 |
import java.util.Enumeration; |
46 |
import java.util.List; |
47 |
import junit.framework.Test; |
48 |
import junit.framework.TestCase; |
49 |
import junit.framework.TestResult; |
50 |
import junit.runner.BaseTestRunner; |
51 |
import junit.textui.TestRunner; |
52 |
|
53 |
public class RandomTest extends TestCase { |
54 |
|
55 |
private static List<Integer> runs; |
56 |
|
57 |
public RandomTest(String name) { |
58 |
super(name); |
59 |
} |
60 |
|
61 |
public static class One extends NbTestCase { |
62 |
public One(String n) {super(n);} |
63 |
public void testReliable() {runs.add(10);} |
64 |
@Random public void testUnreliable() {runs.add(11);} |
65 |
} |
66 |
|
67 |
public static class Two extends NbTestCase { |
68 |
public Two(String n) {super(n);} |
69 |
public void testReliable() {runs.add(20);} |
70 |
} |
71 |
|
72 |
@Random public static class Three extends NbTestCase { |
73 |
public Three(String n) {super(n);} |
74 |
public void testUnreliable() {runs.add(30);} |
75 |
} |
76 |
|
77 |
public static class Four extends NbTestCase { |
78 |
public Four(String n) {super(n);} |
79 |
public void testReliable() {runs.add(40);} |
80 |
@Random public void testUnreliable() {runs.add(41);} |
81 |
} |
82 |
|
83 |
@Random public static class Five extends NbTestCase { |
84 |
public Five(String n) {super(n);} |
85 |
public void testUnreliable() {runs.add(50);} |
86 |
} |
87 |
|
88 |
public static class Six { |
89 |
public static Test suite() {return new NbTestSuite(Four.class);} |
90 |
} |
91 |
|
92 |
public static class Seven { |
93 |
public static Test suite() {return new NbTestSuite(Five.class);} |
94 |
} |
95 |
|
96 |
public static class Eight { |
97 |
public static Test suite() { |
98 |
NbTestSuite suite = new NbTestSuite(); |
99 |
suite.addTestSuite(Four.class); |
100 |
suite.addTestSuite(Five.class); |
101 |
return suite; |
102 |
} |
103 |
} |
104 |
|
105 |
private void run(Class... tests) { |
106 |
runs = new ArrayList<Integer>(); |
107 |
BaseTestRunner runner = new TestRunner(); |
108 |
for (Class test : tests) { |
109 |
TestResult result = new TestResult(); |
110 |
//result.addListener(new ResultPrinter(System.err)); |
111 |
runner.getTest(test.getName()).run(result); |
112 |
assertEquals("failures in " + test, Collections.emptyList(), Collections.list((Enumeration<?>) result.failures())); |
113 |
assertEquals("errors in " + test, Collections.emptyList(), Collections.list((Enumeration<?>) result.errors())); |
114 |
} |
115 |
} |
116 |
|
117 |
private void runAll() throws Exception { |
118 |
run(One.class, Two.class, Three.class, Six.class, Seven.class, Eight.class); |
119 |
} |
120 |
|
121 |
public void testRegularMode() throws Exception { |
122 |
runAll(); |
123 |
assertEquals(Arrays.asList(10, 11, 20, 30, 40, 41, 50, 40, 41, 50), runs); |
124 |
} |
125 |
|
126 |
public void testIgnoreRandomFailuresMode() throws Exception { |
127 |
System.setProperty("ignore.random.failures", "true"); |
128 |
runAll(); |
129 |
assertEquals(Arrays.asList(10, 20, 40, 40), runs); |
130 |
} |
131 |
|
132 |
public static class Sub extends Three { |
133 |
public Sub(String n) {super(n);} |
134 |
} |
135 |
|
136 |
public void testHeritability() throws Exception { |
137 |
System.setProperty("ignore.random.failures", "false"); |
138 |
run(Sub.class); |
139 |
assertEquals(Collections.singletonList(30), runs); |
140 |
System.setProperty("ignore.random.failures", "true"); |
141 |
run(Sub.class); |
142 |
assertEquals(Collections.emptyList(), runs); |
143 |
} |
144 |
|
145 |
} |