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