1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.directory.server.integ;
20
21
22 import java.lang.reflect.Method;
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 import static org.apache.directory.server.integ.state.TestServerContext.test;
28
29 import org.apache.directory.server.core.integ.Level;
30 import org.junit.internal.runners.InitializationError;
31 import org.junit.internal.runners.JUnit4ClassRunner;
32 import org.junit.runner.Description;
33 import org.junit.runner.notification.Failure;
34 import org.junit.runner.notification.RunNotifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42
43
44
45 public class SiRunner extends JUnit4ClassRunner
46 {
47 private static final Logger LOG = LoggerFactory.getLogger( SiRunner.class );
48 private SiSuite suite;
49 private InheritableServerSettings settings;
50
51
52 public SiRunner( Class<?> clazz ) throws InitializationError
53 {
54 super( clazz );
55 }
56
57
58 protected InheritableServerSettings getSettings()
59 {
60 if ( settings != null )
61 {
62 return settings;
63 }
64
65 if ( suite == null )
66 {
67 settings = new InheritableServerSettings( getDescription(), null );
68 }
69
70 return settings;
71 }
72
73
74 @Override
75 public void run( final RunNotifier notifier )
76 {
77 super.run( notifier );
78 Level cleanupLevel = getSettings().getCleanupLevel();
79
80 if ( cleanupLevel == Level.CLASS )
81 {
82 try
83 {
84 shutdown();
85 cleanup();
86 destroy();
87 }
88 catch ( Exception e )
89 {
90 LOG.error( "Encountered exception while trying to cleanup after test class: "
91 + this.getDescription().getDisplayName(), e );
92 notifier.fireTestFailure( new Failure( getDescription(), e ) );
93 }
94 }
95 }
96
97
98 @Override
99 protected void invokeTestMethod( Method method, final RunNotifier notifier )
100 {
101 LOG.debug( "About to invoke test method {}", method.getName() );
102 Description description = methodDescription( method );
103 test( getTestClass(), wrapMethod( method ), notifier, new InheritableServerSettings( description, getSettings() ) );
104
105 Level cleanupLevel = getSettings().getCleanupLevel();
106
107 if ( cleanupLevel == Level.METHOD )
108 {
109 try
110 {
111 shutdown();
112 cleanup();
113 destroy();
114 }
115 catch ( Exception e )
116 {
117 LOG.error( "Encountered exception while trying to cleanup after test class: "
118 + this.getDescription().getDisplayName(), e );
119 notifier.fireTestFailure( new Failure( getDescription(), e ) );
120 }
121 }
122 }
123
124
125 public void setSuite( SiSuite suite )
126 {
127 this.suite = suite;
128 this.settings = new InheritableServerSettings( getDescription(), suite.getSettings() );
129 }
130
131
132 public SiSuite getSuite()
133 {
134 return suite;
135 }
136 }