Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TestNGMapConfigurator |
|
| 7.0;7 |
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>Class<</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>ReporterConfig<</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 | 0 | public class TestNGMapConfigurator |
48 | implements Configurator | |
49 | { | |
50 | public void configure( TestNG testng, Map options ) throws TestSetFailedException | |
51 | { | |
52 | 0 | Map convertedOptions = new HashMap(); |
53 | 0 | for ( Iterator it = options.entrySet().iterator(); it.hasNext(); ) |
54 | { | |
55 | 0 | Map.Entry entry = (Map.Entry) it.next(); |
56 | 0 | String key = (String) entry.getKey(); |
57 | 0 | Object val = entry.getValue(); |
58 | 0 | if ( "listener".equals( key ) ) |
59 | { | |
60 | 0 | val = AbstractDirectConfigurator.loadListenerClasses( (String) val ); |
61 | } | |
62 | 0 | if ( "objectfactory".equals( key ) ) |
63 | { | |
64 | 0 | val = AbstractDirectConfigurator.loadClass( (String) val ); |
65 | } | |
66 | 0 | if ( "reporter".equals( key ) ) |
67 | { | |
68 | // TODO support multiple reporters? | |
69 | 0 | val = convertReporterConfig( val ); |
70 | 0 | key = "reporterslist"; |
71 | ||
72 | } | |
73 | 0 | if ( "junit".equals( key ) ) |
74 | { | |
75 | 0 | val = convert( val, Boolean.class ); |
76 | 0 | } else if ( "skipfailedinvocationcounts".equals( key ) ) |
77 | { | |
78 | 0 | val = convert( val, Boolean.class ); |
79 | } | |
80 | 0 | else if ( "threadcount".equals( key ) ) |
81 | { | |
82 | 0 | val = convert( val, String.class ); |
83 | } | |
84 | // TODO objectfactory... not even documented, does it work? | |
85 | 0 | if ( key.startsWith( "-" ) ) |
86 | { | |
87 | 0 | convertedOptions.put( key, val ); |
88 | } | |
89 | else | |
90 | { | |
91 | 0 | convertedOptions.put( "-" + key, val ); |
92 | } | |
93 | 0 | } |
94 | ||
95 | 0 | testng.configure( convertedOptions ); |
96 | ||
97 | 0 | } |
98 | ||
99 | // ReporterConfig only became available in later versions of TestNG | |
100 | private Object convertReporterConfig( Object val ) | |
101 | { | |
102 | 0 | final String reporterConfigClassName = "org.testng.ReporterConfig"; |
103 | try | |
104 | { | |
105 | 0 | Class reporterConfig = Class.forName( reporterConfigClassName ); |
106 | 0 | Method deserialize = reporterConfig.getMethod( "deserialize", new Class[] { String.class } ); |
107 | 0 | Object rc = deserialize.invoke( null, new Object[] { val } ); |
108 | 0 | ArrayList reportersList = new ArrayList(); |
109 | 0 | reportersList.add( rc ); |
110 | 0 | return reportersList; |
111 | } | |
112 | 0 | catch ( Exception e ) |
113 | { | |
114 | 0 | return val; |
115 | } | |
116 | } | |
117 | ||
118 | protected Object convert( Object val, Class type ) | |
119 | { | |
120 | 0 | if ( val == null ) |
121 | { | |
122 | 0 | return null; |
123 | } | |
124 | 0 | if ( type.isAssignableFrom( val.getClass() ) ) |
125 | { | |
126 | 0 | return val; |
127 | } | |
128 | ||
129 | 0 | if ( ( Boolean.class.equals( type ) || boolean.class.equals( type ) ) && String.class.equals( val.getClass() ) ) |
130 | { | |
131 | 0 | return Boolean.valueOf( (String) val ); |
132 | } | |
133 | ||
134 | 0 | if ( String.class.equals( type ) ) |
135 | { | |
136 | 0 | return val.toString(); |
137 | } | |
138 | ||
139 | 0 | return val; |
140 | } | |
141 | } |