|
| 1 | +/* |
| 2 | + * Copyright 2005-2010 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ws.mock.client; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +import org.springframework.core.io.ByteArrayResource; |
| 22 | +import org.springframework.core.io.ClassPathResource; |
| 23 | +import org.springframework.core.io.Resource; |
| 24 | +import org.springframework.ws.WebServiceMessage; |
| 25 | +import org.springframework.xml.transform.StringSource; |
| 26 | +import org.springframework.xml.validation.XmlValidator; |
| 27 | +import org.springframework.xml.validation.XmlValidatorFactory; |
| 28 | + |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import static org.easymock.EasyMock.*; |
| 33 | + |
| 34 | +public class SchemaValidatingRequestMatcherTest { |
| 35 | + |
| 36 | + private Resource schema2; |
| 37 | + |
| 38 | + private Resource schema1; |
| 39 | + |
| 40 | + private WebServiceMessage message; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setUp() { |
| 44 | + message = createMock(WebServiceMessage.class); |
| 45 | + schema1 = new ClassPathResource("schemaValidatingRequestMatcherTest.xsd", SchemaValidatingRequestMatcherTest.class); |
| 46 | + schema2 = new ByteArrayResource("".getBytes()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void singleSchemaMatch() throws IOException, AssertionError { |
| 51 | + expect(message.getPayloadSource()).andReturn(new StringSource( |
| 52 | + "<test xmlns=\"http://www.example.org/schema\"><number>0</number><text>text</text></test>")); |
| 53 | + |
| 54 | + RequestMatcher requestMatcher = WebServiceMock.validPayload(schema1); |
| 55 | + |
| 56 | + replay(message); |
| 57 | + |
| 58 | + requestMatcher.match(null, message); |
| 59 | + |
| 60 | + verify(message); |
| 61 | + } |
| 62 | + |
| 63 | + @Test(expected = AssertionError.class) |
| 64 | + public void singleSchemaNonMatch() throws IOException, AssertionError { |
| 65 | + expect(message.getPayloadSource()).andReturn(new StringSource( |
| 66 | + "<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>")); |
| 67 | + |
| 68 | + RequestMatcher requestMatcher = WebServiceMock.validPayload(schema1); |
| 69 | + |
| 70 | + replay(message); |
| 71 | + |
| 72 | + requestMatcher.match(null, message); |
| 73 | + |
| 74 | + verify(message); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void multipleSchemaMatch() throws IOException, AssertionError { |
| 79 | + expect(message.getPayloadSource()).andReturn(new StringSource( |
| 80 | + "<test xmlns=\"http://www.example.org/schema\"><number>0</number><text>text</text></test>")); |
| 81 | + |
| 82 | + RequestMatcher requestMatcher = WebServiceMock.validPayload(schema1, schema2); |
| 83 | + |
| 84 | + replay(message); |
| 85 | + |
| 86 | + requestMatcher.match(null, message); |
| 87 | + |
| 88 | + verify(message); |
| 89 | + } |
| 90 | + |
| 91 | + @Test(expected = AssertionError.class) |
| 92 | + public void multipleSchemaNotOk() throws IOException, AssertionError { |
| 93 | + expect(message.getPayloadSource()).andReturn(new StringSource( |
| 94 | + "<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>")); |
| 95 | + |
| 96 | + RequestMatcher requestMatcher = WebServiceMock.validPayload(schema1, schema2); |
| 97 | + |
| 98 | + replay(message); |
| 99 | + |
| 100 | + requestMatcher.match(null, message); |
| 101 | + |
| 102 | + verify(message); |
| 103 | + } |
| 104 | + |
| 105 | + @Test(expected = AssertionError.class) |
| 106 | + public void multipleSchemaDifferentOrderNotOk() throws IOException, AssertionError { |
| 107 | + expect(message.getPayloadSource()).andReturn(new StringSource( |
| 108 | + "<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>")); |
| 109 | + |
| 110 | + RequestMatcher requestMatcher = WebServiceMock.validPayload(schema2, schema1); |
| 111 | + |
| 112 | + replay(message); |
| 113 | + |
| 114 | + requestMatcher.match(null, message); |
| 115 | + |
| 116 | + verify(message); |
| 117 | + } |
| 118 | + |
| 119 | + @Test(expected = AssertionError.class) |
| 120 | + public void xmlValidatorNotOk() throws IOException, AssertionError { |
| 121 | + expect(message.getPayloadSource()).andReturn(new StringSource( |
| 122 | + "<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>")); |
| 123 | + |
| 124 | + XmlValidator validator = XmlValidatorFactory.createValidator(schema1, XmlValidatorFactory.SCHEMA_W3C_XML); |
| 125 | + RequestMatcher requestMatcher = new SchemaValidatingRequestMatcher(validator); |
| 126 | + |
| 127 | + replay(message); |
| 128 | + |
| 129 | + requestMatcher.match(null, message); |
| 130 | + |
| 131 | + verify(message); |
| 132 | + } |
| 133 | +} |
0 commit comments