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.lang.reflect.Constructor;
16  import java.util.ArrayList;
17  import java.util.Arrays;
18  import java.util.Collections;
19  import java.util.List;
20  
21  import com.eviware.soapui.config.TestAssertionConfig;
22  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
23  import com.eviware.soapui.impl.wsdl.teststeps.assertions.TestAssertionRegistry.AssertableType;
24  import com.eviware.soapui.model.ModelItem;
25  import com.eviware.soapui.model.testsuite.Assertable;
26  import com.eviware.soapui.model.testsuite.TestAssertion;
27  import com.eviware.soapui.support.ClassUtils;
28  
29  public abstract class AbstractTestAssertionFactory implements TestAssertionFactory
30  {
31  	private final String id;
32  	private final String label;
33  	private final Class<? extends TestAssertion> assertionClass;
34  	private final List<Class<? extends ModelItem>> targetClasses = new ArrayList<Class<? extends ModelItem>>();
35  
36  	public AbstractTestAssertionFactory( String id, String label, Class<? extends TestAssertion> assertionClass )
37  	{
38  		this.id = id;
39  		this.label = label;
40  		this.assertionClass = assertionClass;
41  	}
42  
43  	@SuppressWarnings("unchecked")
44  	public AbstractTestAssertionFactory( String id, String label, Class<? extends TestAssertion> assertionClass,
45  			Class<? extends ModelItem> targetClass )
46  	{
47  		this( id, label, assertionClass, new Class[] { targetClass } );
48  	}
49  
50  	public AbstractTestAssertionFactory( String id, String label, Class<? extends TestAssertion> assertionClass,
51  			Class<? extends ModelItem>[] targetClasses )
52  	{
53  		this.id = id;
54  		this.label = label;
55  		this.assertionClass = assertionClass;
56  		for( Class<? extends ModelItem> clazz : targetClasses )
57  		{
58  			this.targetClasses.add( clazz );
59  		}
60  	}
61  
62  	public String getAssertionId()
63  	{
64  		return id;
65  	}
66  
67  	public String getAssertionLabel()
68  	{
69  		return label;
70  	}
71  
72  	public boolean canAssert( Assertable assertable )
73  	{
74  		List<?> classes = Arrays.asList( assertionClass.getInterfaces() );
75  
76  		List<Class<?>> classList = ClassUtils.getImplementedAndExtendedClasses( assertable );
77  		if( !targetClasses.isEmpty() && Collections.disjoint( classList, targetClasses ) )
78  			return false;
79  
80  		if( assertable.getAssertableType() == AssertableType.BOTH )
81  			return true;
82  
83  		if( assertable.getAssertableType() == AssertableType.REQUEST
84  				&& classes.contains( com.eviware.soapui.model.testsuite.RequestAssertion.class ) )
85  			return true;
86  
87  		else if( assertable.getAssertableType() == AssertableType.RESPONSE
88  				&& classes.contains( com.eviware.soapui.model.testsuite.ResponseAssertion.class ) )
89  			return true;
90  
91  		return false;
92  	}
93  
94  	public TestAssertion buildAssertion( TestAssertionConfig config, Assertable assertable )
95  	{
96  		try
97  		{
98  			Constructor<? extends TestAssertion> ctor = assertionClass.getConstructor( new Class[] {
99  					TestAssertionConfig.class, Assertable.class } );
100 
101 			return ctor.newInstance( config, assertable );
102 		}
103 		catch( Exception e )
104 		{
105 			e.printStackTrace();
106 			return null;
107 		}
108 	}
109 }