Coverage Report - org.apache.maven.surefire.testng.conf.AbstractDirectConfigurator
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDirectConfigurator
0%
0/35
0%
0/10
4,143
AbstractDirectConfigurator$Setter
0%
0/17
0%
0/14
4,143
 
 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 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  0
     {
 41  0
         Map options = new HashMap();
 42  
         // options.put( ProviderParameterNames.TESTNG_GROUPS_PROP, new Setter( "setGroups", String.class ) );
 43  
         // options.put( ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP, new Setter( "setExcludedGroups", String.class
 44  
         // ) );
 45  0
         options.put( "junit", new Setter( "setJUnit", Boolean.class ) );
 46  0
         options.put( ProviderParameterNames.THREADCOUNT_PROP, new Setter( "setThreadCount", int.class ) );
 47  0
         options.put( "usedefaultlisteners", new Setter( "setUseDefaultListeners", boolean.class ) );
 48  0
         this.setters = options;
 49  0
     }
 50  
 
 51  
     public void configure( TestNG testng, Map options )
 52  
         throws TestSetFailedException
 53  
     {
 54  0
         System.out.println( "\n\n\n\nCONFIGURING TESTNG\n\n\n\n" );
 55  
         // kind of ugly, but listeners are configured differently
 56  0
         final String listeners = (String) options.remove( "listener" );
 57  
         // DGF In 4.7, default listeners dump XML files in the surefire-reports directory,
 58  
         // confusing the report plugin.  This was fixed in later versions.
 59  0
         testng.setUseDefaultListeners( false );
 60  0
         for ( Iterator it = options.entrySet().iterator(); it.hasNext(); )
 61  
         {
 62  0
             Map.Entry entry = (Map.Entry) it.next();
 63  0
             String key = (String) entry.getKey();
 64  0
             Object val = entry.getValue();
 65  
 
 66  0
             Setter setter = (Setter) setters.get( key );
 67  0
             if ( setter != null )
 68  
             {
 69  
                 try
 70  
                 {
 71  0
                     setter.invoke( testng, val );
 72  
                 }
 73  0
                 catch ( Exception ex )
 74  
                 {
 75  0
                     throw new NestedRuntimeException( "Cannot set option " + key + " with value " + val, ex );
 76  0
                 }
 77  
 
 78  
             }
 79  0
         }
 80  
         // TODO: we should have the Profile so that we can decide if this is needed or not
 81  0
         testng.setListenerClasses( loadListenerClasses( listeners ) );
 82  0
     }
 83  
 
 84  
     public static List loadListenerClasses( String listenerClasses )
 85  
         throws TestSetFailedException
 86  
     {
 87  0
         if ( listenerClasses == null || "".equals( listenerClasses.trim() ) )
 88  
         {
 89  0
             return new ArrayList();
 90  
         }
 91  
 
 92  0
         List classes = new ArrayList();
 93  0
         String[] classNames = listenerClasses.split( " *, *" );
 94  0
         for ( int i = 0; i < classNames.length; i++ )
 95  
         {
 96  0
             String className = classNames[i];
 97  0
             Class clazz = loadClass( className );
 98  0
             classes.add( clazz );
 99  
         }
 100  
 
 101  0
         return classes;
 102  
     }
 103  
 
 104  
     public static Class loadClass( String className )
 105  
         throws TestSetFailedException
 106  
     {
 107  
         try
 108  
         {
 109  0
             return Class.forName( className );
 110  
         }
 111  0
         catch ( Exception ex )
 112  
         {
 113  0
             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  0
         {
 125  0
             this.setterName = name;
 126  0
             this.paramClass = clazz;
 127  0
         }
 128  
 
 129  
         public void invoke( Object target, Object value )
 130  
             throws Exception
 131  
         {
 132  0
             Method setter = target.getClass().getMethod( this.setterName, new Class[]{ this.paramClass } );
 133  0
             if ( setter != null )
 134  
             {
 135  0
                 setter.invoke( target, new Object[]{ convertValue( value ) } );
 136  
             }
 137  0
         }
 138  
 
 139  
         Object convertValue( Object value )
 140  
         {
 141  0
             if ( value == null )
 142  
             {
 143  0
                 return value;
 144  
             }
 145  0
             if ( this.paramClass.isAssignableFrom( value.getClass() ) )
 146  
             {
 147  0
                 return value;
 148  
             }
 149  
 
 150  0
             if ( Boolean.class.equals( this.paramClass ) || boolean.class.equals( this.paramClass ) )
 151  
             {
 152  0
                 return Boolean.valueOf( value.toString() );
 153  
             }
 154  0
             if ( Integer.class.equals( this.paramClass ) || int.class.equals( this.paramClass ) )
 155  
             {
 156  0
                 return new Integer( value.toString() );
 157  
             }
 158  
 
 159  0
             return value;
 160  
         }
 161  
     }
 162  
 }