1   package org.apache.maven.surefire.testng.conf;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import java.lang.reflect.Method;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  import org.apache.maven.surefire.booter.ProviderParameterNames;
29  import org.apache.maven.surefire.testset.TestSetFailedException;
30  import org.apache.maven.surefire.util.NestedRuntimeException;
31  
32  import org.testng.TestNG;
33  
34  public abstract class AbstractDirectConfigurator
35      implements Configurator
36  {
37      final Map setters;
38  
39      AbstractDirectConfigurator()
40      {
41          Map options = new HashMap();
42          
43          
44          
45          options.put( "junit", new Setter( "setJUnit", Boolean.class ) );
46          options.put( ProviderParameterNames.THREADCOUNT_PROP, new Setter( "setThreadCount", int.class ) );
47          options.put( "usedefaultlisteners", new Setter( "setUseDefaultListeners", boolean.class ) );
48          this.setters = options;
49      }
50  
51      public void configure( TestNG testng, Map options )
52          throws TestSetFailedException
53      {
54          System.out.println( "\n\n\n\nCONFIGURING TESTNG\n\n\n\n" );
55          
56          final String listeners = (String) options.remove( "listener" );
57          
58          
59          testng.setUseDefaultListeners( false );
60          for ( Iterator it = options.entrySet().iterator(); it.hasNext(); )
61          {
62              Map.Entry entry = (Map.Entry) it.next();
63              String key = (String) entry.getKey();
64              Object val = entry.getValue();
65  
66              Setter setter = (Setter) setters.get( key );
67              if ( setter != null )
68              {
69                  try
70                  {
71                      setter.invoke( testng, val );
72                  }
73                  catch ( Exception ex )
74                  {
75                      throw new NestedRuntimeException( "Cannot set option " + key + " with value " + val, ex );
76                  }
77  
78              }
79          }
80          
81          testng.setListenerClasses( loadListenerClasses( listeners ) );
82      }
83  
84      public static List loadListenerClasses( String listenerClasses )
85          throws TestSetFailedException
86      {
87          if ( listenerClasses == null || "".equals( listenerClasses.trim() ) )
88          {
89              return new ArrayList();
90          }
91  
92          List classes = new ArrayList();
93          String[] classNames = listenerClasses.split( " *, *" );
94          for ( int i = 0; i < classNames.length; i++ )
95          {
96              String className = classNames[i];
97              Class clazz = loadClass( className );
98              classes.add( clazz );
99          }
100 
101         return classes;
102     }
103 
104     public static Class loadClass( String className )
105         throws TestSetFailedException
106     {
107         try
108         {
109             return Class.forName( className );
110         }
111         catch ( Exception ex )
112         {
113             throw new TestSetFailedException( "Cannot find listener class " + className, ex );
114         }
115     }
116 
117     public static final class Setter
118     {
119         private final String setterName;
120 
121         private final Class paramClass;
122 
123         public Setter( String name, Class clazz )
124         {
125             this.setterName = name;
126             this.paramClass = clazz;
127         }
128 
129         public void invoke( Object target, Object value )
130             throws Exception
131         {
132             Method setter = target.getClass().getMethod( this.setterName, new Class[]{ this.paramClass } );
133             if ( setter != null )
134             {
135                 setter.invoke( target, new Object[]{ convertValue( value ) } );
136             }
137         }
138 
139         Object convertValue( Object value )
140         {
141             if ( value == null )
142             {
143                 return value;
144             }
145             if ( this.paramClass.isAssignableFrom( value.getClass() ) )
146             {
147                 return value;
148             }
149 
150             if ( Boolean.class.equals( this.paramClass ) || boolean.class.equals( this.paramClass ) )
151             {
152                 return Boolean.valueOf( value.toString() );
153             }
154             if ( Integer.class.equals( this.paramClass ) || int.class.equals( this.paramClass ) )
155             {
156                 return new Integer( value.toString() );
157             }
158 
159             return value;
160         }
161     }
162 }