View Javadoc
1   /*
2    * Copyright (c) 2012-2013, Dienst Landelijk Gebied - Ministerie van Economische Zaken
3    * 
4    * Gepubliceerd onder de BSD 2-clause licentie, 
5    * zie https://github.com/MinELenI/CBSviewer/blob/master/LICENSE.md voor de volledige licentie.
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   * Test case for {@link nl.mineleni.cbsviewer.servlet.wms.WMSClientServlet}.
30   * 
31   * @author mprins
32   */
33  public class WMSClientServletTest {
34  	/** servlet die we testen. */
35  	private WMSClientServlet servlet;
36  	/** junit mockery. */
37  	private final JUnitRuleMockery mockery = new JUnitRuleMockery();
38  	/** ge-mockte servlet config gebruikt in de test. */
39  	private ServletConfig servletConfig;
40  	/** ge-mockte servlet context gebruikt in de test. */
41  	private ServletContext servletContext;
42  
43  	/**
44  	 * Set up before each test.
45  	 * 
46  	 * @throws Exception
47  	 *             the exception
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  			/** return de mocked servletContext. */
57  			@Override
58  			public ServletContext getServletContext() {
59  				return WMSClientServletTest.this.servletContext;
60  			}
61  
62  			/** return de mocked servletConfig. */
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  	 * Tear down after each test.
82  	 * 
83  	 * @throws Exception
84  	 *             the exception
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  	 * Test methode voor.
97  	 * {@link nl.mineleni.cbsviewer.servlet.wms.WMSClientServlet#init(javax.servlet.ServletConfig)}
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 			// supposed to throw e
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 	 * Test methode voor.
135 	 * {@link nl.mineleni.cbsviewer.servlet.wms.WMSClientServlet#init(javax.servlet.ServletConfig)}
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 				//this.will(returnValue("http://osm.wheregroup.com/cgi-bin/osm_basic.xml?REQUEST=GetCapabilities&SERVICE=WMS&VERSION=1.1.1"));
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 				//this.will(returnValue("Grenzen"));
156 				this.will(returnValue("osm-nb"));
157 				this.oneOf(WMSClientServletTest.this.servletConfig)
158 						.getInitParameter("lufoCapabilitiesURL");
159 				// this.will(returnValue("http://gisdemo2.agro.nl/arcgis/services/Luchtfoto2010/MapServer/WMSServer?REQUEST=GetCapabilities&SERVICE=WMS"));
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 }