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.directory.server.core.integ;
20
21
22 import java.util.List;
23
24 import static org.apache.directory.server.core.integ.state.TestServiceContext.cleanup;
25 import static org.apache.directory.server.core.integ.state.TestServiceContext.destroy;
26 import static org.apache.directory.server.core.integ.state.TestServiceContext.shutdown;
27 import org.junit.internal.requests.IgnoredClassRunner;
28 import org.junit.internal.runners.InitializationError;
29 import org.junit.runner.Runner;
30 import org.junit.runner.notification.Failure;
31 import org.junit.runner.notification.RunNotifier;
32 import org.junit.runners.Suite;
33
34
35 /**
36 * A replacement for standard JUnit 4 suites. Note that this test suite
37 * will not startup an DirectoryService instance but will clean it up if
38 * one remains.
39 *
40 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41 * @version $Rev$, $Date$
42 */
43 public class CiSuite extends Suite
44 {
45 private InheritableSettings settings;
46
47
48 public CiSuite( Class<?> clazz ) throws InitializationError
49 {
50 super( clazz );
51 settings = new InheritableSettings( getDescription() );
52 }
53
54
55 public void addAll( List<? extends Runner> runners )
56 {
57 for ( Runner runner : getRunners() )
58 {
59 if ( runner instanceof CiRunner )
60 {
61 CiRunner cir = ( CiRunner) runner;
62 cir.setSuite( this );
63 }
64 else if ( runner instanceof IgnoredClassRunner )
65 {
66 // allow this one
67 }
68 else
69 {
70 throw new IllegalArgumentException( String.format( "Unexpected runner type \"%s\". " +
71 "Test classes within CiSuites must use CiRunners.", runner ) );
72 }
73 }
74
75 super.addAll( runners );
76 }
77
78
79 public void add( Runner runner )
80 {
81 if ( runner instanceof CiRunner )
82 {
83 CiRunner cir = ( CiRunner) runner;
84 cir.setSuite( this );
85 super.add( runner );
86 }
87 else if ( runner instanceof IgnoredClassRunner )
88 {
89 // allow this one
90 }
91 else
92 {
93 throw new IllegalArgumentException( String.format( "Unexpected runner type \"%s\". " +
94 "Test classes within CiSuites must use CiRunners.", runner ) );
95 }
96 }
97
98
99 @Override
100 public void run( final RunNotifier notifier )
101 {
102 super.run( notifier );
103
104 /*
105 * For any service scope other than test system scope, we must have to
106 * shutdown the sevice and cleanup the working directory. Failures to
107 * do this without exception shows that something is wrong with the
108 * server and so the entire test should be marked as failed. So we
109 * presume that tests have failed in the suite if the fixture is in an
110 * inconsistent state. Who knows if this inconsistent state of the
111 * service could have made it so false results were acquired while
112 * running tests.
113 */
114
115 if ( settings.getCleanupLevel() != Level.SYSTEM )
116 {
117 try
118 {
119 shutdown();
120 cleanup();
121 destroy();
122 }
123 catch ( Exception e )
124 {
125 notifier.fireTestFailure( new Failure( getDescription(), e ) );
126 }
127 }
128 }
129
130
131 public InheritableSettings getSettings()
132 {
133 return settings;
134 }
135 }