1   /*
2    * Copyright 2005-2011 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.test.support.matcher;
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  
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import static org.easymock.EasyMock.*;
31  
32  public class SchemaValidatingMatcherTest {
33  
34      private Resource schema2;
35  
36      private Resource schema1;
37  
38      private WebServiceMessage message;
39  
40      @Before
41      public void setUp() {
42          message = createMock(WebServiceMessage.class);
43          schema1 = new ClassPathResource("schemaValidatingMatcherTest.xsd", SchemaValidatingMatcherTest.class);
44          schema2 = new ByteArrayResource("".getBytes());
45      }
46  
47      @Test
48      public void singleSchemaMatch() throws IOException, AssertionError {
49          expect(message.getPayloadSource()).andReturn(new StringSource(
50                  "<test xmlns=\"http://www.example.org/schema\"><number>0</number><text>text</text></test>"));
51  
52          SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1);
53  
54          replay(message);
55  
56          matcher.match(message);
57  
58          verify(message);
59      }
60  
61      @Test(expected = AssertionError.class)
62      public void singleSchemaNonMatch() throws IOException, AssertionError {
63          expect(message.getPayloadSource()).andReturn(new StringSource(
64                  "<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>")).times(2);
65  
66          SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1);
67  
68          replay(message);
69  
70          matcher.match(message);
71  
72          verify(message);
73      }
74  
75      @Test
76      public void multipleSchemaMatch() throws IOException, AssertionError {
77          expect(message.getPayloadSource()).andReturn(new StringSource(
78                  "<test xmlns=\"http://www.example.org/schema\"><number>0</number><text>text</text></test>"));
79  
80          SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1, schema2);
81  
82          replay(message);
83  
84          matcher.match(message);
85  
86          verify(message);
87      }
88  
89      @Test(expected = AssertionError.class)
90      public void multipleSchemaNotOk() throws IOException, AssertionError {
91          expect(message.getPayloadSource()).andReturn(new StringSource(
92                  "<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>")).times(2);
93  
94          SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1, schema2);
95  
96          replay(message);
97  
98          matcher.match(message);
99  
100         verify(message);
101     }
102 
103     @Test(expected = AssertionError.class)
104     public void multipleSchemaDifferentOrderNotOk() throws IOException, AssertionError {
105         expect(message.getPayloadSource()).andReturn(new StringSource(
106                 "<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>")).times(2);
107 
108         SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema2, schema1);
109 
110         replay(message);
111 
112         matcher.match(message);
113 
114         verify(message);
115     }
116 
117     @Test(expected = AssertionError.class)
118     public void xmlValidatorNotOk() throws IOException, AssertionError {
119         expect(message.getPayloadSource()).andReturn(new StringSource(
120                 "<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>")).times(2);
121 
122         SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1);
123 
124         replay(message);
125 
126         matcher.match(message);
127 
128         verify(message);
129     }
130 }