View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.junit;
20  
21  import java.security.AccessControlException;
22  import java.security.AccessController;
23  import java.security.PrivilegedAction;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import junit.framework.Test;
28  import junit.framework.TestCase;
29  import junit.framework.TestSuite;
30  import org.apache.maven.surefire.api.report.ReportEntry;
31  import org.apache.maven.surefire.api.report.RunListener;
32  import org.apache.maven.surefire.api.report.TestOutputReportEntry;
33  import org.apache.maven.surefire.api.report.TestReportListener;
34  import org.apache.maven.surefire.api.report.TestSetReportEntry;
35  import org.apache.maven.surefire.api.testset.TestSetFailedException;
36  import org.apache.maven.surefire.common.junit3.JUnit3Reflector;
37  
38  import static org.apache.maven.surefire.shared.lang3.JavaVersion.JAVA_17;
39  import static org.apache.maven.surefire.shared.lang3.JavaVersion.JAVA_RECENT;
40  
41  /**
42   *
43   */
44  public class JUnitTestSetTest extends TestCase {
45  
46      public void testExecuteSuiteClass() throws Exception {
47          ClassLoader testClassLoader = this.getClass().getClassLoader();
48          JUnit3Reflector reflector = new JUnit3Reflector(testClassLoader);
49          SuccessListener listener = new SuccessListener();
50          JUnit3Reporter reporter = new JUnit3Reporter(listener);
51          JUnitTestSetExecutor testSet = new JUnitTestSetExecutor(reflector, reporter);
52          testSet.execute(Suite.class, testClassLoader);
53          List<ReportEntry> succeededTests = listener.getSucceededTests();
54          assertEquals(1, succeededTests.size());
55          assertEquals(
56                  "org.apache.maven.surefire.junit.JUnitTestSetTest$AlwaysSucceeds",
57                  succeededTests.get(0).getSourceName());
58          assertEquals("testSuccess", succeededTests.get(0).getName());
59      }
60  
61      public void testSystemManager() {
62          boolean isDeprecated = !JAVA_RECENT.atMost(JAVA_17);
63          Object originalSm = null;
64          try {
65              if (!isDeprecated) {
66                  originalSm = System.getSecurityManager();
67              }
68  
69              JUnit3Provider.setSystemManager("java.lang.SecurityManager");
70  
71              if (isDeprecated) {
72                  fail();
73              }
74  
75              Object sm = System.getSecurityManager();
76              assertNotNull(sm);
77              assertEquals("java.lang.SecurityManager", sm.getClass().getName());
78              assertNotSame(originalSm, sm);
79          } catch (TestSetFailedException e) {
80              if (!isDeprecated) {
81                  fail();
82              }
83          } finally {
84              if (!isDeprecated) {
85                  try {
86                      SecurityManager sm = (SecurityManager) originalSm;
87                      AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
88                          System.setSecurityManager(sm);
89                          return null;
90                      });
91                  } catch (AccessControlException e) {
92                      // ignore
93                  }
94              }
95          }
96      }
97  
98      /**
99       *
100      */
101     public static final class AlwaysSucceeds extends TestCase {
102         public void testSuccess() {
103             assertTrue(true);
104         }
105     }
106 
107     /**
108      *
109      */
110     public static class SuccessListener implements RunListener, TestReportListener<TestOutputReportEntry> {
111 
112         private List<ReportEntry> succeededTests = new ArrayList<>();
113 
114         @Override
115         public void testSetStarting(TestSetReportEntry report) {}
116 
117         @Override
118         public void testSetCompleted(TestSetReportEntry report) {}
119 
120         @Override
121         public void testStarting(ReportEntry report) {}
122 
123         @Override
124         public void testSucceeded(ReportEntry report) {
125             succeededTests.add(report);
126         }
127 
128         @Override
129         public void testAssumptionFailure(ReportEntry report) {
130             throw new IllegalStateException();
131         }
132 
133         @Override
134         public void testError(ReportEntry report) {
135             throw new IllegalStateException();
136         }
137 
138         @Override
139         public void testFailed(ReportEntry report) {
140             throw new IllegalStateException();
141         }
142 
143         @Override
144         public void testSkipped(ReportEntry report) {
145             throw new IllegalStateException();
146         }
147 
148         @Override
149         public void testExecutionSkippedByUser() {}
150 
151         List<ReportEntry> getSucceededTests() {
152             return succeededTests;
153         }
154 
155         @Override
156         public void writeTestOutput(TestOutputReportEntry reportEntry) {}
157 
158         @Override
159         public boolean isDebugEnabled() {
160             return false;
161         }
162 
163         @Override
164         public void debug(String message) {}
165 
166         @Override
167         public boolean isInfoEnabled() {
168             return false;
169         }
170 
171         @Override
172         public void info(String message) {}
173 
174         @Override
175         public boolean isWarnEnabled() {
176             return false;
177         }
178 
179         @Override
180         public void warning(String message) {}
181 
182         @Override
183         public boolean isErrorEnabled() {
184             return false;
185         }
186 
187         @Override
188         public void error(String message) {}
189 
190         @Override
191         public void error(String message, Throwable t) {}
192 
193         @Override
194         public void error(Throwable t) {}
195     }
196 
197     /**
198      *
199      */
200     public static class Suite {
201 
202         public static Test suite() {
203             TestSuite suite = new TestSuite();
204             suite.addTestSuite(AlwaysSucceeds.class);
205             return suite;
206         }
207     }
208 }