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.HashMap;
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.TestListResolver;
33  import org.apache.maven.surefire.testset.TestRequest;
34  import org.apache.maven.surefire.util.RunOrder;
35  
36  import junit.framework.TestCase;
37  
38  /**
39   * @author Kristian Rosenvold
40   */
41  public class SurefireReflectorTest
42      extends TestCase
43  {
44      public void testSetDirectoryScannerParameters()
45          throws Exception
46      {
47          SurefireReflector surefireReflector = getReflector();
48          Object foo = getFoo();
49  
50          DirectoryScannerParameters directoryScannerParameters =
51              new DirectoryScannerParameters( new File( "ABC" ), new ArrayList<String>(), new ArrayList<String>(),
52                                              new ArrayList<String>(), false, "hourly" );
53          surefireReflector.setDirectoryScannerParameters( foo, directoryScannerParameters );
54          assertTrue( isCalled( foo ) );
55  
56      }
57  
58      public void testRunOrderParameters()
59          throws Exception
60      {
61          SurefireReflector surefireReflector = getReflector();
62          Object foo = getFoo();
63  
64          RunOrderParameters runOrderParameters = new RunOrderParameters( RunOrder.DEFAULT, new File( "." ) );
65          surefireReflector.setRunOrderParameters( foo, runOrderParameters );
66          assertTrue( isCalled( foo ) );
67  
68      }
69  
70      public void testTestSuiteDefinition()
71          throws Exception
72      {
73          SurefireReflector surefireReflector = getReflector();
74          Object foo = getFoo();
75  
76          TestRequest testSuiteDefinition =
77              new TestRequest( Arrays.asList( new File( "file1" ), new File( "file2" ) ),
78                               new File( "TestSOurce" ), new TestListResolver( "aUserRequestedTest#aMethodRequested" ) );
79          surefireReflector.setTestSuiteDefinition( foo, testSuiteDefinition );
80          assertTrue( isCalled( foo ) );
81      }
82  
83      public void testProviderProperties()
84          throws Exception
85      {
86          SurefireReflector surefireReflector = getReflector();
87          Object foo = getFoo();
88  
89          surefireReflector.setProviderProperties( foo, new HashMap<String, String>() );
90          assertTrue( isCalled( foo ) );
91      }
92  
93      public void testReporterConfiguration()
94          throws Exception
95      {
96          SurefireReflector surefireReflector = getReflector();
97          Object foo = getFoo();
98  
99          ReporterConfiguration reporterConfiguration = getReporterConfiguration();
100         surefireReflector.setReporterConfigurationAware( foo, reporterConfiguration );
101         assertTrue( isCalled( foo ) );
102     }
103 
104     private ReporterConfiguration getReporterConfiguration()
105     {
106         return new ReporterConfiguration( new File( "CDE" ), true );
107     }
108 
109     public void testTestClassLoaderAware()
110         throws Exception
111     {
112         SurefireReflector surefireReflector = getReflector();
113         Object foo = getFoo();
114 
115         surefireReflector.setTestClassLoader( foo, getClass().getClassLoader() );
116         assertTrue( isCalled( foo ) );
117     }
118 
119     public void testArtifactInfoAware()
120         throws Exception
121     {
122         SurefireReflector surefireReflector = getReflector();
123         Object foo = getFoo();
124 
125         TestArtifactInfo testArtifactInfo = new TestArtifactInfo( "12.3", "test" );
126         surefireReflector.setTestArtifactInfo( foo, testArtifactInfo );
127         assertTrue( isCalled( foo ) );
128     }
129 
130     private SurefireReflector getReflector()
131     {
132         return new SurefireReflector( this.getClass().getClassLoader() );
133     }
134 
135     public Object getFoo()
136     { // Todo: Setup a different classloader so we can really test crossing
137         return new Foo();
138     }
139 
140 
141     private Boolean isCalled( Object foo )
142     {
143         final Method isCalled;
144         try
145         {
146             isCalled = foo.getClass().getMethod( "isCalled" );
147             return (Boolean) isCalled.invoke( foo );
148         }
149         catch ( ReflectiveOperationException e )
150         {
151             throw new RuntimeException( e );
152         }
153     }
154 }