View Javadoc

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