View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.loadtest.assertions;
14  
15  import java.lang.reflect.Constructor;
16  import java.lang.reflect.InvocationTargetException;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.apache.log4j.Logger;
21  
22  import com.eviware.soapui.config.LoadTestAssertionConfig;
23  import com.eviware.soapui.impl.wsdl.loadtest.LoadTestAssertion;
24  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
25  
26  /***
27   * Registry for available LoadTestAssertions
28   * 
29   * @author Ole.Matzura
30   */
31  
32  public class LoadTestAssertionRegistry
33  {
34     private static LoadTestAssertionRegistry instance;
35  	private Map<String,Class<? extends AbstractLoadTestAssertion> > availableAssertions = new HashMap<String,Class<? extends AbstractLoadTestAssertion> >();
36  	private final static Logger logger = Logger.getLogger( LoadTestAssertionRegistry.class );
37  
38  	public LoadTestAssertionRegistry()
39  	{
40        availableAssertions.put( TestStepAverageAssertion.STEP_AVERAGE_TYPE, TestStepAverageAssertion.class );
41        availableAssertions.put( TestStepTpsAssertion.STEP_TPS_TYPE, TestStepTpsAssertion.class );
42        availableAssertions.put( TestStepMaxAssertion.STEP_MAXIMUM_TYPE, TestStepMaxAssertion.class );
43        availableAssertions.put( TestStepStatusAssertion.STEP_STATUS_TYPE, TestStepStatusAssertion.class );
44        availableAssertions.put( MaxErrorsAssertion.MAX_ERRORS_TYPE, MaxErrorsAssertion.class );
45  	}
46  	
47  	public static synchronized LoadTestAssertionRegistry getInstance()
48  	{
49  		if( instance == null )
50  			instance = new LoadTestAssertionRegistry();
51  		
52  		return instance;
53  	}
54  
55  	public static AbstractLoadTestAssertion buildAssertion(LoadTestAssertionConfig config, WsdlLoadTest loadTest)
56  	{
57  	   try
58  		{
59  			Class<? extends AbstractLoadTestAssertion> clazz = getInstance().availableAssertions.get(config.getType());
60  			Constructor<? extends AbstractLoadTestAssertion> ctor = clazz
61  					.getConstructor(new Class[] { LoadTestAssertionConfig.class, WsdlLoadTest.class  });
62  
63  			return (AbstractLoadTestAssertion) ctor.newInstance(config, loadTest );
64  		}
65  		catch (SecurityException e)
66  		{
67  			e.printStackTrace();
68  		}
69  		catch (NoSuchMethodException e)
70  		{
71  			e.printStackTrace();
72  		}
73  		catch (IllegalArgumentException e)
74  		{
75  			e.printStackTrace();
76  		}
77  		catch (InstantiationException e)
78  		{
79  			e.printStackTrace();
80  		}
81  		catch (IllegalAccessException e)
82  		{
83  			e.printStackTrace();
84  		}
85  		catch (InvocationTargetException e)
86  		{
87  			e.printStackTrace();
88  		}
89  		
90  		return null;
91  	}
92  	
93  	public static LoadTestAssertionConfig createAssertionConfig( String type )
94  	{
95  		LoadTestAssertionConfig config = LoadTestAssertionConfig.Factory.newInstance();
96  		config.setType( type );
97  		return config;
98  	}
99  
100 	public static String[] getAvailableAssertions()
101 	{
102       return  getInstance().availableAssertions.keySet().toArray( 
103       		new String[getInstance().availableAssertions.size()] );
104 	}
105 
106 	public static LoadTestAssertion createAssertion(String type, WsdlLoadTest loadTest )
107 	{
108 		LoadTestAssertionConfig config = createAssertionConfig( type );
109 		return buildAssertion( config, loadTest );
110 	}
111 }