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.List; |
46 |
import junit.framework.Test; |
47 |
import junit.framework.TestCase; |
48 |
import junit.framework.TestResult; |
49 |
import junit.runner.BaseTestRunner; |
50 |
import junit.textui.TestRunner; |
51 |
|
52 |
public class RandomTest extends TestCase { |
53 |
|
54 |
private static List<Integer> runs; |
55 |
|
56 |
public RandomTest(String name) { |
57 |
super(name); |
58 |
} |
59 |
|
60 |
public static class One extends NbTestCase { |
61 |
public One(String n) {super(n);} |
62 |
public void testReliable() {runs.add(10);} |
63 |
@Random public void testUnreliable() {runs.add(11);} |
64 |
} |
65 |
|
66 |
public static class Two extends NbTestCase { |
67 |
public Two(String n) {super(n);} |
68 |
public void testReliable() {runs.add(20);} |
69 |
} |
70 |
|
71 |
@Random public static class Three extends NbTestCase { |
72 |
public Three(String n) {super(n);} |
73 |
public void testUnreliable() {runs.add(30);} |
74 |
} |
75 |
|
76 |
public static class Four extends NbTestCase { |
77 |
public Four(String n) {super(n);} |
78 |
public void testReliable() {runs.add(40);} |
79 |
@Random public void testUnreliable() {runs.add(41);} |
80 |
} |
81 |
|
82 |
@Random public static class Five extends NbTestCase { |
83 |
public Five(String n) {super(n);} |
84 |
public void testUnreliable() {runs.add(50);} |
85 |
} |
86 |
|
87 |
public static class Six { |
88 |
public static Test suite() {return new NbTestSuite(Four.class);} |
89 |
} |
90 |
|
91 |
public static class Seven { |
92 |
public static Test suite() {return new NbTestSuite(Five.class);} |
93 |
} |
94 |
|
95 |
public static class Eight { |
96 |
public static Test suite() { |
97 |
NbTestSuite suite = new NbTestSuite(); |
98 |
suite.addTestSuite(Four.class); |
99 |
suite.addTestSuite(Five.class); |
100 |
return suite; |
101 |
} |
102 |
} |
103 |
|
104 |
private void run(Class... tests) { |
105 |
runs = new ArrayList<Integer>(); |
106 |
BaseTestRunner runner = new TestRunner(); |
107 |
for (Class test : tests) { |
108 |
TestResult result = new TestResult(); |
109 |
//result.addListener(new ResultPrinter(System.err)); |
110 |
runner.getTest(test.getName()).run(result); |
111 |
} |
112 |
} |
113 |
|
114 |
private void runAll() throws Exception { |
115 |
run(One.class, Two.class, Three.class, Six.class, Seven.class, Eight.class); |
116 |
} |
117 |
|
118 |
public void testRegularMode() throws Exception { |
119 |
runAll(); |
120 |
assertEquals(Arrays.asList(10, 11, 20, 30, 40, 41, 50, 40, 41, 50), runs); |
121 |
} |
122 |
|
123 |
public void testIgnoreRandomFailuresMode() throws Exception { |
124 |
System.setProperty("ignore.random.failures", "true"); |
125 |
runAll(); |
126 |
assertEquals(Arrays.asList(10, 20, 40, 40), runs); |
127 |
} |
128 |
|
129 |
public static class Sub extends Three { |
130 |
public Sub(String n) {super(n);} |
131 |
} |
132 |
|
133 |
public void testHeritability() throws Exception { |
134 |
System.setProperty("ignore.random.failures", "false"); |
135 |
run(Sub.class); |
136 |
assertEquals(Collections.singletonList(30), runs); |
137 |
System.setProperty("ignore.random.failures", "true"); |
138 |
run(Sub.class); |
139 |
assertEquals(Collections.emptyList(), runs); |
140 |
} |
141 |
|
142 |
} |