View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
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.teststeps.assertions;
14  
15  import java.util.ArrayList;
16  import java.util.HashMap;
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.apache.log4j.Logger;
21  import org.apache.tools.ant.types.selectors.ExtendSelector;
22  
23  import com.eviware.soapui.SoapUI;
24  import com.eviware.soapui.config.TestAssertionConfig;
25  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
26  import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.GroovyScriptAssertion;
27  import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.ResponseSLAAssertion;
28  import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SchemaComplianceAssertion;
29  import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SimpleContainsAssertion;
30  import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SimpleNotContainsAssertion;
31  import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion;
32  import com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XQueryContainsAssertion;
33  import com.eviware.soapui.impl.wsdl.teststeps.assertions.http.HttpDownloadAllResourcesAssertion;
34  import com.eviware.soapui.impl.wsdl.teststeps.assertions.jdbc.JdbcStatusAssertion;
35  import com.eviware.soapui.impl.wsdl.teststeps.assertions.jdbc.JdbcTimeoutAssertion;
36  import com.eviware.soapui.impl.wsdl.teststeps.assertions.jms.JMSStatusAssertion;
37  import com.eviware.soapui.impl.wsdl.teststeps.assertions.jms.JMSTimeoutAssertion;
38  import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.NotSoapFaultAssertion;
39  import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.SoapFaultAssertion;
40  import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.SoapRequestAssertion;
41  import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.SoapResponseAssertion;
42  import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.WSARequestAssertion;
43  import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.WSAResponseAssertion;
44  import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.WSSStatusAssertion;
45  import com.eviware.soapui.model.testsuite.Assertable;
46  import com.eviware.soapui.model.testsuite.TestAssertion;
47  import com.eviware.soapui.support.types.StringToStringMap;
48  
49  /***
50   * Registry for WsdlAssertions
51   * 
52   * @author Ole.Matzura
53   */
54  
55  public class TestAssertionRegistry
56  {
57  	private static TestAssertionRegistry instance;
58  	private Map<String, TestAssertionFactory> availableAssertions = new HashMap<String, TestAssertionFactory>();
59  	private StringToStringMap assertionLabels = new StringToStringMap();
60  	private final static Logger log = Logger.getLogger( TestAssertionRegistry.class );
61  
62  	public TestAssertionRegistry()
63  	{
64  		addAssertion( new SoapResponseAssertion.Factory() );
65  		addAssertion( new SoapRequestAssertion.Factory() );
66  		addAssertion( new SchemaComplianceAssertion.Factory() );
67  		addAssertion( new SimpleContainsAssertion.Factory() );
68  		addAssertion( new SimpleNotContainsAssertion.Factory() );
69  		addAssertion( new XPathContainsAssertion.Factory() );
70  		addAssertion( new NotSoapFaultAssertion.Factory() );
71  		addAssertion( new SoapFaultAssertion.Factory() );
72  		addAssertion( new ResponseSLAAssertion.Factory() );
73  		addAssertion( new GroovyScriptAssertion.Factory() );
74  		addAssertion( new XQueryContainsAssertion.Factory() );
75  		addAssertion( new WSSStatusAssertion.Factory() );
76  		addAssertion( new WSAResponseAssertion.Factory() );
77  		addAssertion( new WSARequestAssertion.Factory() );
78  		addAssertion( new JMSStatusAssertion.Factory() );
79  		addAssertion( new JMSTimeoutAssertion.Factory() );
80  		addAssertion( new JdbcStatusAssertion.Factory() );
81  		addAssertion( new JdbcTimeoutAssertion.Factory() );
82  		addAssertion( new HttpDownloadAllResourcesAssertion.Factory() );
83  	}
84  
85  	public void addAssertion( TestAssertionFactory factory )
86  	{
87  		availableAssertions.put( factory.getAssertionId(), factory );
88  		assertionLabels.put( factory.getAssertionLabel(), factory.getAssertionId() );
89  	}
90  
91  	public static synchronized TestAssertionRegistry getInstance()
92  	{
93  		if( instance == null )
94  			instance = new TestAssertionRegistry();
95  
96  		return instance;
97  	}
98  
99  	public WsdlMessageAssertion buildAssertion( TestAssertionConfig config, Assertable assertable )
100 	{
101 		try
102 		{
103 			String type = config.getType();
104 			TestAssertionFactory factory = availableAssertions.get( type );
105 			if( factory == null )
106 			{
107 				log.error( "Missing assertion for type [" + type + "]" );
108 			}
109 			else
110 			{
111 				return ( WsdlMessageAssertion )factory.buildAssertion( config, assertable );
112 			}
113 		}
114 		catch( Exception e )
115 		{
116 			SoapUI.logError( e );
117 		}
118 
119 		return null;
120 	}
121 	public Class<? extends WsdlMessageAssertion> getAssertionClassType( TestAssertionConfig config )
122 	{
123 		try
124 		{
125 			String type = config.getType();
126 			TestAssertionFactory factory = availableAssertions.get( type );
127 			if( factory == null )
128 			{
129 				log.error( "Missing assertion for type [" + type + "]" );
130 			}
131 			else
132 			{
133 				return factory.getAssertionClassType();
134 			}
135 		}
136 		catch( Exception e )
137 		{
138 			SoapUI.logError( e );
139 		}
140 
141 		return null;
142 	}
143 	public boolean canBuildAssertion( TestAssertionConfig config )
144 	{
145 		return availableAssertions.containsKey( config.getType() );
146 	}
147 
148 	public String getAssertionTypeForName( String name )
149 	{
150 		return assertionLabels.get( name );
151 	}
152 
153 	public enum AssertableType
154 	{
155 		REQUEST, RESPONSE, BOTH
156 	}
157 
158 	public String[] getAvailableAssertionNames( Assertable assertable )
159 	{
160 		List<String> result = new ArrayList<String>();
161 
162 		for( TestAssertionFactory assertion : availableAssertions.values() )
163 		{
164 			if( assertion.canAssert( assertable ) )
165 				result.add( assertion.getAssertionLabel() );
166 		}
167 
168 		return result.toArray( new String[result.size()] );
169 	}
170 
171 	public String getAssertionNameForType( String type )
172 	{
173 		for( String assertion : assertionLabels.keySet() )
174 		{
175 			if( assertionLabels.get( assertion ).equals( type ) )
176 				return assertion;
177 		}
178 
179 		return null;
180 	}
181 
182 	public boolean canAddMultipleAssertions( String name, Assertable assertable )
183 	{
184 		for( int c = 0; c < assertable.getAssertionCount(); c++ )
185 		{
186 			TestAssertion assertion = assertable.getAssertionAt( c );
187 			if( assertion.isAllowMultiple() )
188 				continue;
189 
190 			if( assertion.getClass().equals( availableAssertions.get( getAssertionTypeForName( name ) ) ) )
191 			{
192 				return false;
193 			}
194 		}
195 
196 		return true;
197 	}
198 
199 	public boolean canAddAssertion( WsdlMessageAssertion assertion, Assertable assertable )
200 	{
201 		if( assertion.isAllowMultiple() )
202 			return true;
203 
204 		for( int c = 0; c < assertable.getAssertionCount(); c++ )
205 		{
206 			if( assertion.getClass().equals( assertable.getAssertionAt( c ).getClass() ) )
207 			{
208 				return false;
209 			}
210 		}
211 
212 		return true;
213 	}
214 }