View Javadoc

1   package org.apache.maven.surefire.testng.conf;
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 org.apache.maven.surefire.testset.TestSetFailedException;
23  import org.testng.TestNG;
24  
25  import java.lang.reflect.Method;
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.Map;
30  
31  /**
32   * TestNG configurator for 5.3+ versions. TestNG exposes
33   * a {@link org.testng.TestNG#configure(java.util.Map)} method.
34   * All suppported TestNG options are passed in String format, except
35   * <code>TestNGCommandLineArgs.LISTENER_COMMAND_OPT</code> which is <code>List&gt;Class&lt;</code>,
36   * <code>TestNGCommandLineArgs.JUNIT_DEF_OPT</code> which is a <code>Boolean</code>,
37   * <code>TestNGCommandLineArgs.SKIP_FAILED_INVOCATION_COUNT_OPT</code> which is a <code>Boolean</code>,
38   * <code>TestNGCommandLineArgs.OBJECT_FACTORY_COMMAND_OPT</code> which is a <code>Class</code>,
39   * <code>TestNGCommandLineArgs.REPORTERS_LIST</code> which is a <code>List&gt;ReporterConfig&lt;</code>.
40   *
41   * <p/>
42   * Test classes and/or suite files are not passed along as options parameters, but
43   * configured separately.
44   *
45   * @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a>
46   */
47  public class TestNGMapConfigurator
48      implements Configurator
49  {
50      public void configure( TestNG testng, Map options ) throws TestSetFailedException
51      {
52          Map convertedOptions = new HashMap();
53          for ( Iterator it = options.entrySet().iterator(); it.hasNext(); )
54          {
55              Map.Entry entry = (Map.Entry) it.next();
56              String key = (String) entry.getKey();
57              Object val = entry.getValue();
58              if ( "listener".equals( key ) )
59              {
60                  val = AbstractDirectConfigurator.loadListenerClasses( (String) val );
61              }
62              if ( "objectfactory".equals( key ) )
63              {
64                  val = AbstractDirectConfigurator.loadClass( (String) val );
65              }
66              if ( "reporter".equals( key ) )
67              {
68                  // TODO support multiple reporters?
69                  val = convertReporterConfig( val );
70                  key = "reporterslist";
71  
72              }
73              if ( "junit".equals( key ) )
74              {
75                  val = convert( val, Boolean.class );
76              } else if ( "skipfailedinvocationcounts".equals( key ) )
77              {
78                  val = convert( val, Boolean.class );
79              }
80              else if ( "threadcount".equals( key ) )
81              {
82                  val = convert( val, String.class );
83              }
84              // TODO objectfactory... not even documented, does it work?
85              if ( key.startsWith( "-" ) )
86              {
87                  convertedOptions.put( key, val );
88              }
89              else
90              {
91                  convertedOptions.put( "-" + key, val );
92              }
93          }
94  
95          testng.configure( convertedOptions );
96  
97      }
98  
99      // ReporterConfig only became available in later versions of TestNG
100     private Object convertReporterConfig( Object val )
101     {
102         final String reporterConfigClassName = "org.testng.ReporterConfig";
103         try
104         {
105             Class reporterConfig = Class.forName( reporterConfigClassName );
106             Method deserialize = reporterConfig.getMethod( "deserialize", new Class[] { String.class } );
107             Object rc = deserialize.invoke( null, new Object[] { val } );
108             ArrayList reportersList = new ArrayList();
109             reportersList.add( rc );
110             return reportersList;
111         }
112         catch ( Exception e )
113         {
114             return val;
115         }
116     }
117 
118     protected Object convert( Object val, Class type )
119     {
120         if ( val == null )
121         {
122             return null;
123         }
124         if ( type.isAssignableFrom( val.getClass() ) )
125         {
126             return val;
127         }
128 
129         if ( ( Boolean.class.equals( type ) || boolean.class.equals( type ) ) && String.class.equals( val.getClass() ) )
130         {
131             return Boolean.valueOf( (String) val );
132         }
133 
134         if ( String.class.equals( type ) )
135         {
136             return val.toString();
137         }
138 
139         return val;
140     }
141 }