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