1   /*
2    * Copyright 2005-2012 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.server.endpoint.mapping;
18  
19  import java.net.URI;
20  
21  import org.springframework.ws.MockWebServiceMessageFactory;
22  import org.springframework.ws.context.DefaultMessageContext;
23  import org.springframework.ws.context.MessageContext;
24  import org.springframework.ws.transport.WebServiceConnection;
25  import org.springframework.ws.transport.context.DefaultTransportContext;
26  import org.springframework.ws.transport.context.TransportContextHolder;
27  
28  import org.junit.After;
29  import org.junit.Assert;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  import static org.easymock.EasyMock.*;
34  
35  public class UriEndpointMappingTest {
36  
37      private UriEndpointMapping mapping;
38  
39      private MessageContext context;
40  
41      @Before
42      public void setUp() throws Exception {
43          mapping = new UriEndpointMapping();
44          context = new DefaultMessageContext(new MockWebServiceMessageFactory());
45      }
46  
47      @After
48      public void clearContext() {
49          TransportContextHolder.setTransportContext(null);
50      }
51  
52      @Test
53      public void getLookupKeyForMessage() throws Exception {
54          WebServiceConnection connectionMock = createMock(WebServiceConnection.class);
55          TransportContextHolder.setTransportContext(new DefaultTransportContext(connectionMock));
56  
57          URI uri = new URI("jms://exampleQueue");
58          expect(connectionMock.getUri()).andReturn(uri);
59  
60          replay(connectionMock);
61  
62          Assert.assertEquals("Invalid lookup key", uri.toString(), mapping.getLookupKeyForMessage(context));
63  
64          verify(connectionMock);
65      }
66  
67      @Test
68      public void getLookupKeyForMessagePath() throws Exception {
69          mapping.setUsePath(true);
70  
71          WebServiceConnection connectionMock = createMock(WebServiceConnection.class);
72          TransportContextHolder.setTransportContext(new DefaultTransportContext(connectionMock));
73  
74          URI uri = new URI("http://example.com/foo/bar");
75          expect(connectionMock.getUri()).andReturn(uri);
76  
77          replay(connectionMock);
78  
79          Assert.assertEquals("Invalid lookup key", "/foo/bar", mapping.getLookupKeyForMessage(context));
80  
81          verify(connectionMock);
82      }
83  
84      @Test
85      public void testValidateLookupKey() throws Exception {
86          Assert.assertTrue("URI not valid", mapping.validateLookupKey("http://example.com/services"));
87          Assert.assertFalse("URI not valid", mapping.validateLookupKey("some string"));
88      }
89  }