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.TestArtifactInfo;
32  import org.apache.maven.surefire.testset.TestRequest;
33  import org.apache.maven.surefire.util.NestedRuntimeException;
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(), Boolean.FALSE,
51                                              "hourly" );
52          surefireReflector.setDirectoryScannerParameters( foo, directoryScannerParameters );
53          assertTrue( isCalled( foo ).booleanValue() );
54  
55      }
56  
57      public void testTestSuiteDefinition()
58          throws Exception
59      {
60          SurefireReflector surefireReflector = getReflector();
61          Object foo = getFoo();
62  
63          TestRequest testSuiteDefinition =
64              new TestRequest( Arrays.asList( new File[]{ new File( "file1" ), new File( "file2" ) } ),
65                               new File( "TestSOurce" ), "aUserRequestedTest", "aMethodRequested" );
66          surefireReflector.setTestSuiteDefinition( foo, testSuiteDefinition );
67          assertTrue( isCalled( foo ).booleanValue() );
68      }
69  
70      public void testProviderProperties()
71          throws Exception
72      {
73          SurefireReflector surefireReflector = getReflector();
74          Object foo = getFoo();
75  
76          surefireReflector.setProviderProperties( foo, new Properties() );
77          assertTrue( isCalled( foo ).booleanValue() );
78      }
79  
80      public void testReporterConfiguration()
81          throws Exception
82      {
83          SurefireReflector surefireReflector = getReflector();
84          Object foo = getFoo();
85  
86          ReporterConfiguration reporterConfiguration = getReporterConfiguration();
87          surefireReflector.setReporterConfigurationAware( foo, reporterConfiguration );
88          assertTrue( isCalled( foo ).booleanValue() );
89      }
90  
91      private ReporterConfiguration getReporterConfiguration()
92      {
93          return new ReporterConfiguration( new File( "CDE" ), Boolean.TRUE );
94      }
95  
96      public void testTestClassLoaderAware()
97          throws Exception
98      {
99          SurefireReflector surefireReflector = getReflector();
100         Object foo = getFoo();
101 
102         surefireReflector.setTestClassLoader( foo, getClass().getClassLoader(), getClass().getClassLoader() );
103         assertTrue( isCalled( foo ).booleanValue() );
104     }
105 
106     public void testArtifactInfoAware()
107         throws Exception
108     {
109         SurefireReflector surefireReflector = getReflector();
110         Object foo = getFoo();
111 
112         TestArtifactInfo testArtifactInfo = new TestArtifactInfo( "12.3", "test" );
113         surefireReflector.setTestArtifactInfo( foo, testArtifactInfo );
114         assertTrue( isCalled( foo ).booleanValue() );
115     }
116 
117     private SurefireReflector getReflector()
118     {
119         return new SurefireReflector( this.getClass().getClassLoader() );
120     }
121 
122     public Object getFoo()
123     { // Todo: Setup a different classloader so we can really test crossing
124         return new Foo();
125     }
126 
127 
128     private Boolean isCalled( Object foo )
129     {
130         final Method isCalled;
131         try
132         {
133             isCalled = foo.getClass().getMethod( "isCalled", new Class[0] );
134             return (Boolean) isCalled.invoke( foo, new Object[0] );
135         }
136         catch ( IllegalAccessException e )
137         {
138             throw new NestedRuntimeException( e );
139         }
140         catch ( InvocationTargetException e )
141         {
142             throw new NestedRuntimeException( e );
143         }
144         catch ( NoSuchMethodException e )
145         {
146             throw new NestedRuntimeException( e );
147         }
148     }
149 
150 
151 }