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 lice
6   nse agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *     http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  import java.io.File;
24  import java.lang.reflect.InvocationTargetException;
25  import java.lang.reflect.Method;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Properties;
29  import org.apache.maven.surefire.report.ReporterConfiguration;
30  import org.apache.maven.surefire.testset.DirectoryScannerParameters;
31  import org.apache.maven.surefire.testset.RunOrderParameters;
32  import org.apache.maven.surefire.testset.TestArtifactInfo;
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(), new ArrayList(), new ArrayList(),
52                                              Boolean.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" ), "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 Properties() );
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" ), Boolean.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", new Class[0] );
147             return (Boolean) isCalled.invoke( foo );
148         }
149         catch ( IllegalAccessException e )
150         {
151             throw new RuntimeException( e );
152         }
153         catch ( InvocationTargetException e )
154         {
155             throw new RuntimeException( e );
156         }
157         catch ( NoSuchMethodException e )
158         {
159             throw new RuntimeException( e );
160         }
161     }
162 
163 
164 }