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