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