1
2
3
4
5
6
7 package nl.mineleni.cbsviewer.servlet.wms;
8
9 import static nl.mineleni.cbsviewer.servlet.AbstractBaseServlet.USER_ID;
10 import static nl.mineleni.cbsviewer.servlet.AbstractBaseServlet.USER_PASSWORD;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.junit.Assume.assumeNoException;
15
16 import javax.servlet.ServletConfig;
17 import javax.servlet.ServletContext;
18 import javax.servlet.ServletException;
19
20 import nl.mineleni.cbsviewer.util.StringConstants;
21
22 import org.jmock.Expectations;
23 import org.jmock.integration.junit4.JUnitRuleMockery;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27
28
29
30
31
32
33 public class WMSClientServletTest {
34
35 private WMSClientServlet servlet;
36
37 private final JUnitRuleMockery mockery = new JUnitRuleMockery();
38
39 private ServletConfig servletConfig;
40
41 private ServletContext servletContext;
42
43
44
45
46
47
48
49 @SuppressWarnings("serial")
50 @Before
51 public void setUp() throws Exception {
52 this.servletConfig = this.mockery.mock(ServletConfig.class);
53 this.servletContext = this.mockery.mock(ServletContext.class);
54
55 this.servlet = new WMSClientServlet() {
56
57 @Override
58 public ServletContext getServletContext() {
59 return WMSClientServletTest.this.servletContext;
60 }
61
62
63 @Override
64 public ServletConfig getServletConfig() {
65 return WMSClientServletTest.this.servletConfig;
66 }
67 };
68
69 this.mockery.checking(new Expectations() {
70 {
71 this.atLeast(1).of(WMSClientServletTest.this.servletContext)
72 .getRealPath(StringConstants.MAP_CACHE_DIR.code);
73 this.will(returnValue("C:/workspace/CBSviewer/target/"
74 + StringConstants.MAP_CACHE_DIR.code));
75 }
76 });
77
78 }
79
80
81
82
83
84
85
86 @After
87 public void tearDown() throws Exception {
88 try {
89 this.servlet.destroy();
90 } catch (final NullPointerException e) {
91 assertTrue(true);
92 }
93 }
94
95
96
97
98
99
100 @Test
101 public void testInitServletConfigNull() {
102 this.mockery.checking(new Expectations() {
103 {
104 this.ignoring(WMSClientServletTest.this.servletConfig)
105 .getInitParameter(USER_ID);
106 this.ignoring(WMSClientServletTest.this.servletConfig)
107 .getInitParameter(USER_PASSWORD);
108 this.ignoring(WMSClientServletTest.this.servletConfig)
109 .getServletContext();
110 this.atLeast(1).of(WMSClientServletTest.this.servletConfig)
111 .getInitParameter("bgCapabilitiesURL");
112 this.will(returnValue(null));
113 this.atLeast(0).of(WMSClientServletTest.this.servletConfig)
114 .getInitParameter("bgWMSlayers");
115 this.will(returnValue(null));
116 this.atLeast(0).of(WMSClientServletTest.this.servletConfig)
117 .getInitParameter("lufoCapabilitiesURL");
118 this.will(returnValue(null));
119 this.atLeast(0).of(WMSClientServletTest.this.servletConfig)
120 .getInitParameter("lufoWMSlayers");
121 this.will(returnValue(null));
122 }
123 });
124 try {
125
126 this.servlet.init(this.servletConfig);
127 fail("Een overwachte fail conditie is opgetreden.");
128 } catch (final ServletException e) {
129 assertTrue("De verwachte ServletException is opgetreden.", true);
130 }
131 }
132
133
134
135
136
137
138 @Test
139 public void testInitServletConfig() {
140 this.mockery.checking(new Expectations() {
141 {
142 this.oneOf(WMSClientServletTest.this.servletConfig)
143 .getInitParameter(USER_ID);
144 this.will(returnValue("userID"));
145 this.oneOf(WMSClientServletTest.this.servletConfig)
146 .getInitParameter(USER_PASSWORD);
147 this.will(returnValue("passID"));
148
149 this.oneOf(WMSClientServletTest.this.servletConfig)
150 .getInitParameter("bgCapabilitiesURL");
151
152 this.will(returnValue("http://www.openbasiskaart.nl/mapcache/?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetCapabilities"));
153 this.oneOf(WMSClientServletTest.this.servletConfig)
154 .getInitParameter("bgWMSlayers");
155
156 this.will(returnValue("osm-nb"));
157 this.oneOf(WMSClientServletTest.this.servletConfig)
158 .getInitParameter("lufoCapabilitiesURL");
159
160 this.will(returnValue("http://geodata1.nationaalgeoregister.nl/luchtfoto/wms?REQUEST=GetCapabilities"));
161 this.oneOf(WMSClientServletTest.this.servletConfig)
162 .getInitParameter("lufoWMSlayers");
163 this.will(returnValue(""));
164 this.oneOf(WMSClientServletTest.this.servletConfig)
165 .getInitParameter("featureInfoType");
166 this.will(returnValue(""));
167 }
168 });
169
170 try {
171 this.servlet.init(this.servletConfig);
172 assertEquals("userID", this.servlet.getUserID());
173 assertEquals("passID", this.servlet.getPassID());
174 } catch (final SecurityException s) {
175 assumeNoException(s);
176 } catch (final ServletException e) {
177 if (e.getRootCause().getClass()
178 .equals(java.net.ConnectException.class)) {
179 assumeNoException(e);
180 }
181 fail("Servlet Exception voor init() in test setup.(testInitServletConfig) "
182 + e.getLocalizedMessage());
183 }
184 }
185 }