View Javadoc
1   package org.apache.maven.surefire.booter;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.lang.reflect.InvocationTargetException;
24  import java.lang.reflect.Method;
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.Properties;
28  import org.apache.maven.surefire.report.ReporterConfiguration;
29  import org.apache.maven.surefire.testset.DirectoryScannerParameters;
30  import org.apache.maven.surefire.testset.RunOrderParameters;
31  import org.apache.maven.surefire.testset.TestArtifactInfo;
32  import org.apache.maven.surefire.testset.TestRequest;
33  import org.apache.maven.surefire.util.RunOrder;
34  
35  import junit.framework.TestCase;
36  
37  /**
38   * @author Kristian Rosenvold
39   */
40  public class SurefireReflectorTest
41      extends TestCase
42  {
43      public void testSetDirectoryScannerParameters()
44          throws Exception
45      {
46          SurefireReflector surefireReflector = getReflector();
47          Object foo = getFoo();
48  
49          DirectoryScannerParameters directoryScannerParameters =
50              new DirectoryScannerParameters( new File( "ABC" ), new ArrayList(), new ArrayList(), new ArrayList(),
51                                              Boolean.FALSE, "hourly" );
52          surefireReflector.setDirectoryScannerParameters( foo, directoryScannerParameters );
53          assertTrue( isCalled( foo ) );
54  
55      }
56  
57      public void testRunOrderParameters()
58          throws Exception
59      {
60          SurefireReflector surefireReflector = getReflector();
61          Object foo = getFoo();
62  
63          RunOrderParameters runOrderParameters = new RunOrderParameters( RunOrder.DEFAULT, new File( "." ) );
64          surefireReflector.setRunOrderParameters( foo, runOrderParameters );
65          assertTrue( isCalled( foo ) );
66  
67      }
68  
69      public void testTestSuiteDefinition()
70          throws Exception
71      {
72          SurefireReflector surefireReflector = getReflector();
73          Object foo = getFoo();
74  
75          TestRequest testSuiteDefinition =
76              new TestRequest( Arrays.asList( new File( "file1" ), new File( "file2" ) ),
77                               new File( "TestSOurce" ), "aUserRequestedTest", "aMethodRequested" );
78          surefireReflector.setTestSuiteDefinition( foo, testSuiteDefinition );
79          assertTrue( isCalled( foo ) );
80      }
81  
82      public void testProviderProperties()
83          throws Exception
84      {
85          SurefireReflector surefireReflector = getReflector();
86          Object foo = getFoo();
87  
88          surefireReflector.setProviderProperties( foo, new Properties() );
89          assertTrue( isCalled( foo ) );
90      }
91  
92      public void testReporterConfiguration()
93          throws Exception
94      {
95          SurefireReflector surefireReflector = getReflector();
96          Object foo = getFoo();
97  
98          ReporterConfiguration reporterConfiguration = getReporterConfiguration();
99          surefireReflector.setReporterConfigurationAware( foo, reporterConfiguration );
100         assertTrue( isCalled( foo ) );
101     }
102 
103     private ReporterConfiguration getReporterConfiguration()
104     {
105         return new ReporterConfiguration( new File( "CDE" ), Boolean.TRUE );
106     }
107 
108     public void testTestClassLoaderAware()
109         throws Exception
110     {
111         SurefireReflector surefireReflector = getReflector();
112         Object foo = getFoo();
113 
114         surefireReflector.setTestClassLoader( foo, getClass().getClassLoader() );
115         assertTrue( isCalled( foo ) );
116     }
117 
118     public void testArtifactInfoAware()
119         throws Exception
120     {
121         SurefireReflector surefireReflector = getReflector();
122         Object foo = getFoo();
123 
124         TestArtifactInfo testArtifactInfo = new TestArtifactInfo( "12.3", "test" );
125         surefireReflector.setTestArtifactInfo( foo, testArtifactInfo );
126         assertTrue( isCalled( foo ) );
127     }
128 
129     private SurefireReflector getReflector()
130     {
131         return new SurefireReflector( this.getClass().getClassLoader() );
132     }
133 
134     public Object getFoo()
135     { // Todo: Setup a different classloader so we can really test crossing
136         return new Foo();
137     }
138 
139 
140     private Boolean isCalled( Object foo )
141     {
142         final Method isCalled;
143         try
144         {
145             isCalled = foo.getClass().getMethod( "isCalled", new Class[0] );
146             return (Boolean) isCalled.invoke( foo );
147         }
148         catch ( IllegalAccessException e )
149         {
150             throw new RuntimeException( e );
151         }
152         catch ( InvocationTargetException e )
153         {
154             throw new RuntimeException( e );
155         }
156         catch ( NoSuchMethodException e )
157         {
158             throw new RuntimeException( e );
159         }
160     }
161 
162 
163 }