1 package org.junit.rules;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5
6 import java.util.Arrays;
7 import java.util.Collections;
8 import java.util.List;
9
10 import org.junit.Assert;
11 import org.junit.Rule;
12 import org.junit.Test;
13 import org.junit.runner.Description;
14 import org.junit.runner.JUnitCore;
15 import org.junit.runner.Result;
16 import org.junit.runners.model.Statement;
17
18 public class DisableOnDebugTest {
19 private static final List<String> WITHOUT_DEBUG_ARGUMENTS = Collections
20 .emptyList();
21
22 private static final List<String> PRE_JAVA5_DEBUG_ARGUMENTS = Arrays
23 .asList("-Xdebug",
24 "-Xrunjdwp:transport=dt_socket,server=y,address=8000");
25
26 private static final List<String> PRE_JAVA5_DEBUG_ARGUMENTS_IN_REVERSE_ORDER = Arrays
27 .asList("-Xrunjdwp:transport=dt_socket,server=y,address=8000",
28 "-Xdebug");
29
30 private static final List<String> POST_JAVA5_DEBUG_ARGUMENTS = Arrays
31 .asList("-agentlib:jdwp=transport=dt_socket,server=y,address=8000");
32
33
34
35
36 private static class FailOnExecution implements TestRule {
37
38 public Statement apply(Statement base,
39 Description description) {
40 return new Statement() {
41
42 @Override
43 public void evaluate() throws Throwable {
44 throw new AssertionError();
45 }
46 };
47 }
48
49 }
50
51 public static abstract class AbstractDisableOnDebugTest {
52
53 @Rule
54 public TestRule failOnExecution;
55
56 public AbstractDisableOnDebugTest(List<String> arguments) {
57 this.failOnExecution = new DisableOnDebug(new FailOnExecution(),
58 arguments);
59 }
60
61 @Test
62 public void test() {
63 }
64 }
65
66 public static class PreJava5DebugArgumentsTest extends
67 AbstractDisableOnDebugTest {
68
69 public PreJava5DebugArgumentsTest() {
70 super(PRE_JAVA5_DEBUG_ARGUMENTS);
71 }
72
73 }
74
75 public static class PreJava5DebugArgumentsReversedTest extends
76 AbstractDisableOnDebugTest {
77
78 public PreJava5DebugArgumentsReversedTest() {
79 super(PRE_JAVA5_DEBUG_ARGUMENTS_IN_REVERSE_ORDER);
80 }
81
82 }
83
84 public static class PostJava5DebugArgumentsTest extends
85 AbstractDisableOnDebugTest {
86
87 public PostJava5DebugArgumentsTest() {
88 super(POST_JAVA5_DEBUG_ARGUMENTS);
89 }
90
91 }
92
93 public static class WithoutDebugArgumentsTest extends
94 AbstractDisableOnDebugTest {
95
96 public WithoutDebugArgumentsTest() {
97 super(WITHOUT_DEBUG_ARGUMENTS);
98 }
99
100 }
101
102 @Test
103 public void givenPreJava5DebugArgumentsIsDebuggingShouldReturnTrue() {
104 DisableOnDebug subject = new DisableOnDebug(
105 new FailOnExecution(), PRE_JAVA5_DEBUG_ARGUMENTS);
106 assertTrue("Should be debugging", subject.isDebugging());
107 }
108
109 @Test
110 public void givenPreJava5DebugArgumentsInReverseIsDebuggingShouldReturnTrue() {
111 DisableOnDebug subject = new DisableOnDebug(
112 new FailOnExecution(),
113 PRE_JAVA5_DEBUG_ARGUMENTS_IN_REVERSE_ORDER);
114 assertTrue("Should be debugging", subject.isDebugging());
115 }
116
117 @Test
118 public void givenPostJava5DebugArgumentsIsDebuggingShouldReturnTrue() {
119 DisableOnDebug subject = new DisableOnDebug(
120 new FailOnExecution(), POST_JAVA5_DEBUG_ARGUMENTS);
121 assertTrue("Should be debugging", subject.isDebugging());
122 }
123
124 @Test
125 public void givenArgumentsWithoutDebugFlagsIsDebuggingShouldReturnFalse() {
126 DisableOnDebug subject = new DisableOnDebug(
127 new FailOnExecution(), WITHOUT_DEBUG_ARGUMENTS);
128 Assert.assertFalse("Should not be debugging", subject.isDebugging());
129 }
130
131 @Test
132 public void whenRunWithPreJava5DebugArgumentsTestShouldFail() {
133 JUnitCore core = new JUnitCore();
134 Result result = core.run(PreJava5DebugArgumentsTest.class);
135 assertEquals("Should run the test", 1, result.getRunCount());
136 assertEquals("Test should not have failed", 0, result.getFailureCount());
137 }
138
139 @Test
140 public void whenRunWithPreJava5DebugArgumentsInReverseOrderTestShouldFail() {
141 JUnitCore core = new JUnitCore();
142 Result result = core
143 .run(PreJava5DebugArgumentsReversedTest.class);
144 assertEquals("Should run the test", 1, result.getRunCount());
145 assertEquals("Test should not have failed", 0, result.getFailureCount());
146 }
147
148 @Test
149 public void whenRunWithPostJava5DebugArgumentsTestShouldFail() {
150 JUnitCore core = new JUnitCore();
151 Result result = core.run(PostJava5DebugArgumentsTest.class);
152 assertEquals("Should run the test", 1, result.getRunCount());
153 assertEquals("Test should not have failed", 0, result.getFailureCount());
154 }
155
156 @Test
157 public void whenRunWithoutDebugFlagsTestShouldPass() {
158 JUnitCore core = new JUnitCore();
159 Result result = core.run(WithoutDebugArgumentsTest.class);
160 assertEquals("Should run the test", 1, result.getRunCount());
161 assertEquals("Test should have failed", 1, result.getFailureCount());
162 }
163
164 }