001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.teststeps.assertions;
014:
015: import java.lang.reflect.Constructor;
016: import java.util.ArrayList;
017: import java.util.Arrays;
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021:
022: import org.apache.log4j.Logger;
023:
024: import com.eviware.soapui.SoapUI;
025: import com.eviware.soapui.config.RequestAssertionConfig;
026: import com.eviware.soapui.impl.wsdl.support.assertions.Assertable;
027: import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
028: import com.eviware.soapui.support.types.StringToStringMap;
029:
030: /**
031: * Registry for WsdlAssertions
032: *
033: * @author Ole.Matzura
034: */
035:
036: public class WsdlAssertionRegistry {
037: private static WsdlAssertionRegistry instance;
038: private Map<String, Class<? extends WsdlMessageAssertion>> availableAssertions = new HashMap<String, Class<? extends WsdlMessageAssertion>>();
039: private StringToStringMap assertionLabels = new StringToStringMap();
040: private final static Logger log = Logger
041: .getLogger(WsdlAssertionRegistry.class);
042:
043: public WsdlAssertionRegistry() {
044: addAssertion(SoapResponseAssertion.ID, "SOAP Response",
045: SoapResponseAssertion.class);
046: addAssertion(SchemaComplianceAssertion.ID, "Schema Compliance",
047: SchemaComplianceAssertion.class);
048: addAssertion(SimpleContainsAssertion.ID, "Contains",
049: SimpleContainsAssertion.class);
050: addAssertion(SimpleNotContainsAssertion.ID, "Not Contains",
051: SimpleNotContainsAssertion.class);
052: addAssertion(XPathContainsAssertion.ID,
053: XPathContainsAssertion.LABEL,
054: XPathContainsAssertion.class);
055: addAssertion(NotSoapFaultAssertion.ID,
056: NotSoapFaultAssertion.LABEL,
057: NotSoapFaultAssertion.class);
058: addAssertion(SoapFaultAssertion.ID, "SOAP Fault",
059: SoapFaultAssertion.class);
060: addAssertion(ResponseSLAAssertion.ID, "Response SLA",
061: ResponseSLAAssertion.class);
062: addAssertion(GroovyScriptAssertion.ID,
063: GroovyScriptAssertion.LABEL,
064: GroovyScriptAssertion.class);
065: }
066:
067: public void addAssertion(String id, String label,
068: Class<? extends WsdlMessageAssertion> assertionClass) {
069: availableAssertions.put(id, assertionClass);
070: assertionLabels.put(label, id);
071: }
072:
073: public static synchronized WsdlAssertionRegistry getInstance() {
074: if (instance == null)
075: instance = new WsdlAssertionRegistry();
076:
077: return instance;
078: }
079:
080: public WsdlMessageAssertion buildAssertion(
081: RequestAssertionConfig config, Assertable request) {
082: try {
083: String type = config.getType();
084: Class<? extends WsdlMessageAssertion> clazz = availableAssertions
085: .get(type);
086: if (clazz == null) {
087: log.error("Missing assertion for type [" + type + "]");
088: } else {
089: Constructor<? extends WsdlMessageAssertion> ctor = clazz
090: .getConstructor(new Class[] {
091: RequestAssertionConfig.class,
092: Assertable.class });
093:
094: return (WsdlMessageAssertion) ctor.newInstance(config,
095: request);
096: }
097: } catch (Exception e) {
098: SoapUI.logError(e);
099: }
100:
101: return null;
102: }
103:
104: public boolean canBuildAssertion(RequestAssertionConfig config) {
105: return availableAssertions.get(config.getType()) != null;
106: }
107:
108: public enum AssertionType {
109: REQUEST, RESPONSE, BOTH
110: };
111:
112: public String getAssertionTypeForName(String name) {
113: return assertionLabels.get(name);
114: }
115:
116: public String[] getAvailableAssertionNames(AssertionType type) {
117: List<String> result = new ArrayList<String>();
118:
119: for (String assertion : assertionLabels.keySet()) {
120: switch (type) {
121: case BOTH: {
122: result.add(assertion);
123: break;
124: }
125: case REQUEST: {
126: String assertionId = assertionLabels.get(assertion);
127: if (Arrays.asList(
128: availableAssertions.get(assertionId)
129: .getInterfaces()).contains(
130: RequestAssertion.class)) {
131: result.add(assertion);
132: }
133: break;
134: }
135:
136: case RESPONSE: {
137: String assertionId = assertionLabels.get(assertion);
138: if (Arrays.asList(
139: availableAssertions.get(assertionId)
140: .getInterfaces()).contains(
141: ResponseAssertion.class)) {
142: result.add(assertion);
143: }
144: break;
145: }
146: }
147: }
148:
149: return result.toArray(new String[result.size()]);
150: }
151:
152: public String getAssertionNameForType(String type) {
153: for (String assertion : assertionLabels.keySet()) {
154: if (assertionLabels.get(assertion).equals(type))
155: return assertion;
156: }
157:
158: return null;
159: }
160: }
|