-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
HTMLFeatureInfoOutputFormatTest.java
195 lines (159 loc) · 7.06 KB
/
HTMLFeatureInfoOutputFormatTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* (c) 2014 - 2015 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.wms.featureinfo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.opengis.wfs.FeatureCollectionType;
import net.opengis.wfs.WfsFactory;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.geoserver.catalog.FeatureTypeInfo;
import org.geoserver.catalog.LayerInfo;
import org.geoserver.catalog.NamespaceInfo;
import org.geoserver.catalog.ResourceInfo;
import org.geoserver.catalog.PublishedType;
import org.geoserver.catalog.impl.FeatureTypeInfoImpl;
import org.geoserver.catalog.impl.LayerInfoImpl;
import org.geoserver.catalog.impl.NamespaceInfoImpl;
import org.geoserver.data.test.MockData;
import org.geoserver.ows.Dispatcher;
import org.geoserver.ows.Request;
import org.geoserver.template.GeoServerTemplateLoader;
import org.geoserver.wfs.json.JSONType;
import org.geoserver.wms.GetFeatureInfoRequest;
import org.geoserver.wms.MapLayerInfo;
import org.geoserver.wms.WMSTestSupport;
import org.junit.Before;
import org.junit.Test;
import com.mockrunner.mock.web.MockHttpServletResponse;
import com.mockrunner.mock.web.MockServletContext;
public class HTMLFeatureInfoOutputFormatTest extends WMSTestSupport {
private HTMLFeatureInfoOutputFormat outputFormat;
private FeatureCollectionType fcType;
Map<String, Object> parameters;
GetFeatureInfoRequest getFeatureInfoRequest;
private static final String templateFolder = "/org/geoserver/wms/featureinfo/";
private String currentTemplate;
@Before
public void setUp() throws URISyntaxException, IOException {
outputFormat = new HTMLFeatureInfoOutputFormat(getWMS());
currentTemplate = "test_content.ftl";
// configure template loader
GeoServerTemplateLoader templateLoader = new GeoServerTemplateLoader(
this.getClass(), getDataDirectory()) {
@Override
public Object findTemplateSource(String path) throws IOException {
String templatePath;
if (path.toLowerCase().contains("content")) {
templatePath = currentTemplate;
} else {
templatePath = "empty.ftl";
}
try {
return new File(this.getClass()
.getResource(templateFolder + templatePath).toURI());
} catch (URISyntaxException e) {
return null;
}
}
};
outputFormat.templateLoader = templateLoader;
// test request with some parameters to use in templates
Request request = new Request();
parameters = new HashMap<String, Object>();
parameters.put("LAYER", "testLayer");
Map<String, String> env = new HashMap<String, String>();
env.put("TEST1", "VALUE1");
env.put("TEST2", "VALUE2");
parameters.put("ENV", env);
request.setKvp(parameters);
Dispatcher.REQUEST.set(request);
final FeatureTypeInfo featureType = getFeatureTypeInfo(MockData.PRIMITIVEGEOFEATURE);
fcType = WfsFactory.eINSTANCE.createFeatureCollectionType();
fcType.getFeature().add(featureType.getFeatureSource(null, null).getFeatures());
// fake layer list
List<MapLayerInfo> queryLayers = new ArrayList<MapLayerInfo>();
LayerInfo layerInfo = new LayerInfoImpl();
layerInfo.setType(PublishedType.VECTOR);
ResourceInfo resourceInfo = new FeatureTypeInfoImpl(null);
NamespaceInfo nameSpace = new NamespaceInfoImpl();
nameSpace.setPrefix("topp");
nameSpace.setURI("http://www.topp.org");
resourceInfo.setNamespace(nameSpace);
layerInfo.setResource(resourceInfo);
MapLayerInfo mapLayerInfo = new MapLayerInfo(layerInfo);
queryLayers.add(mapLayerInfo);
getFeatureInfoRequest = new GetFeatureInfoRequest();
getFeatureInfoRequest.setQueryLayers(queryLayers);
}
/**
* Test request values are inserted in processed template
*
* @throws IOException
* @throws URISyntaxException
*/
@Test
public void testRequestParametersAreEvaluatedInTemplate() throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
outputFormat.write(fcType, getFeatureInfoRequest, outStream);
String result = new String(outStream.toByteArray());
assertEquals("VALUE1,VALUE2,testLayer" , result);
}
@Test
public void testEnvironmentVariablesAreEvaluatedInTemplate() throws IOException {
currentTemplate = "test_env_content.ftl";
System.setProperty("TEST_PROPERTY", "MYVALUE");
MockServletContext servletContext = (MockServletContext)applicationContext.getServletContext();
servletContext.setInitParameter("TEST_INIT_PARAM", "MYPARAM");
try {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
outputFormat.write(fcType, getFeatureInfoRequest, outStream);
String result = new String(outStream.toByteArray());
assertEquals("MYVALUE,MYPARAM" , result);
} finally {
System.clearProperty("TEST_PROPERTY");
}
}
/**
* Test that if template asks a request parameter that is not present in request
* an exception is thrown.
*
*/
@Test
public void testErrorWhenRequestParametersAreNotDefined() {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
boolean error = false;
// remove one parameter required in template
parameters.remove("LAYER");
try {
outputFormat.write(fcType, getFeatureInfoRequest, outStream);
} catch (IOException e) {
error = true;
}
assertTrue(error);
}
@Test
public void testHTMLGetFeatureInfoCharset() throws Exception {
String layer = getLayerId(MockData.FORESTS);
String request = "wms?version=1.1.1&bbox=-0.002,-0.002,0.002,0.002&styles=&format=jpeg"
+ "&request=GetFeatureInfo&layers=" + layer + "&query_layers=" + layer
+ "&width=20&height=20&x=10&y=10" + "&info_format=text/html";
MockHttpServletResponse response = getAsServletResponse(request,"");
// MimeType
assertEquals("text/html", response.getContentType());
// Check if the character encoding is the one expected
assertTrue("UTF-8".equals(response.getCharacterEncoding()));
}
}