1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.tools.pamanager;
18
19 import java.io.File;
20 import java.io.FileReader;
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.Locale;
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27 import junit.textui.TestRunner;
28
29 import org.apache.jetspeed.AbstractRequestContextTestCase;
30 import org.apache.jetspeed.om.common.MutableLanguage;
31 import org.apache.jetspeed.om.common.ParameterComposite;
32 import org.apache.jetspeed.om.common.UserAttribute;
33 import org.apache.jetspeed.om.common.portlet.ContentTypeComposite;
34 import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
35 import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
36 import org.apache.jetspeed.om.common.preference.PreferenceComposite;
37 import org.apache.jetspeed.util.DirectoryHelper;
38 import org.apache.jetspeed.util.descriptor.PortletApplicationDescriptor;
39 import org.apache.jetspeed.util.descriptor.PortletApplicationWar;
40 import org.apache.pluto.om.common.DisplayName;
41 import org.apache.pluto.om.common.LanguageSet;
42 import org.apache.pluto.om.common.ParameterSet;
43 import org.apache.pluto.om.common.Preference;
44 import org.apache.pluto.om.common.PreferenceSet;
45 import org.apache.pluto.om.portlet.ContentTypeSet;
46 import org.apache.pluto.om.portlet.PortletDefinition;
47 import org.apache.pluto.om.portlet.PortletDefinitionList;
48 import org.jdom.Document;
49 import org.jdom.Element;
50 import org.jdom.input.SAXBuilder;
51 import org.jdom.xpath.XPath;
52 import org.xml.sax.EntityResolver;
53 import org.xml.sax.InputSource;
54 import org.xml.sax.SAXException;
55
56 /***
57 * TestPortletDescriptor - tests loading the portlet.xml deployment descriptor
58 * into Java objects
59 *
60 * @author <a href="taylor@apache.org">David Sean Taylor</a>
61 *
62 * @version $Id: TestPortletDescriptor.java 517719 2007-03-13 15:05:48Z ate $
63 */
64 public class TestPortletDescriptor extends AbstractRequestContextTestCase
65 {
66 /***
67 * Start the tests.
68 *
69 * @param args the arguments. Not used
70 */
71 public static void main(String args[])
72 {
73 TestRunner.main(new String[] { TestPortletDescriptor.class.getName()});
74 }
75
76 public static Test suite()
77
78 {
79
80
81
82 return new TestSuite(TestPortletDescriptor.class);
83
84 }
85
86
87
88
89
90
91
92
93
94 public void testLoadPortletApplicationTree() throws Exception
95 {
96 System.out.println("Testing loadPortletApplicationTree");
97 PortletApplicationDescriptor pad = new PortletApplicationDescriptor(new FileReader("./test/testdata/deploy/portlet.xml"), "unit-test");
98 MutablePortletApplication app = pad.createPortletApplication();
99 assertNotNull("App is null", app);
100 assertNotNull("Version is null", app.getVersion());
101 assertTrue("Version invalid: " + app.getVersion(), app.getVersion().equals("1.0"));
102 assertNotNull("PA Identifier is null", app.getApplicationIdentifier());
103 assertTrue(
104 "PA Identifier invalid: " + app.getApplicationIdentifier(),
105 app.getApplicationIdentifier().equals("TestRegistry"));
106
107 validateUserInfo(app);
108
109
110 PortletDefinitionList portletsList = app.getPortletDefinitionList();
111 Iterator it = portletsList.iterator();
112 int count = 0;
113 while (it.hasNext())
114 {
115 PortletDefinitionComposite portlet = (PortletDefinitionComposite) it.next();
116 String identifier = portlet.getPortletIdentifier();
117 assertNotNull("Portlet.Identifier is null", identifier);
118 if (identifier.equals("HelloPortlet"))
119 {
120 validateHelloPortlet(portlet);
121 }
122 count++;
123 }
124 assertTrue("Portlet Count != 4, = " + count, count == 4);
125
126 }
127
128 private void validateUserInfo(MutablePortletApplication app)
129 {
130
131 Collection userAttributeSet = app.getUserAttributes();
132 Iterator it = userAttributeSet.iterator();
133 while (it.hasNext())
134 {
135 UserAttribute userAttribute = (UserAttribute) it.next();
136 assertNotNull("User attribute name is null, ", userAttribute.getName());
137 if (userAttribute.getName().equals("user.name.given"))
138 {
139 assertTrue(
140 "User attribute description: " + userAttribute.getDescription(),
141 userAttribute.getDescription().equals("User Given Name"));
142 }
143 if (userAttribute.getName().equals("user.name.family"))
144 {
145 assertTrue(
146 "User attribute description: " + userAttribute.getDescription(),
147 userAttribute.getDescription().equals("User Last Name"));
148 }
149 if (userAttribute.getName().equals("user.home-info.online.email"))
150 {
151 assertTrue(
152 "User attribute description: " + userAttribute.getDescription(),
153 userAttribute.getDescription().equals("User eMail"));
154 }
155 }
156 }
157
158 private void validateHelloPortlet(PortletDefinitionComposite portlet)
159 {
160
161 assertNotNull("Portlet.Name is null", portlet.getName());
162 assertTrue("Portlet.Name invalid: " + portlet.getName(), portlet.getName().equals("HelloPortlet"));
163
164
165 assertNotNull("Portlet.Class is null", portlet.getClassName());
166 assertTrue(
167 "Portlet.Class invalid: " + portlet.getClassName(),
168 portlet.getClassName().equals("org.apache.jetspeed.portlet.helloworld.HelloWorld"));
169
170
171 assertNotNull("Portlet.Expiration is null", portlet.getExpirationCache());
172 assertTrue("Portlet.Expiration invalid: " + portlet.getExpirationCache(), portlet.getExpirationCache().equals("-1"));
173
174
175 DisplayName displayName = portlet.getDisplayName(Locale.ENGLISH);
176 assertNotNull("Display Name is null", displayName);
177 assertTrue(
178 "Portlet.DisplayName invalid: " + displayName.getDisplayName(),
179 displayName.getDisplayName().equals("HelloWorld Portlet Wrapper"));
180
181
182 ParameterSet paramsList = portlet.getInitParameterSet();
183 Iterator it = paramsList.iterator();
184 int count = 0;
185 while (it.hasNext())
186 {
187 ParameterComposite parameter = (ParameterComposite) it.next();
188 assertTrue("InitParam.Name invalid: " + parameter.getName(), parameter.getName().equals("hello"));
189 assertTrue("InitParam.Value invalid: " + parameter.getValue(), parameter.getValue().equals("Hello Portlet"));
190 assertTrue(
191 "InitParam.Description invalid: " + parameter.getDescription(Locale.ENGLISH),
192 parameter.getDescription(Locale.ENGLISH).getDescription().equals("test init param"));
193 count++;
194 }
195 assertTrue("InitParam Count != 1, count = " + count, count == 1);
196
197
198 ContentTypeSet supports = portlet.getContentTypeSet();
199 it = supports.iterator();
200 count = 0;
201 while (it.hasNext())
202 {
203 ContentTypeComposite contentType = (ContentTypeComposite) it.next();
204 assertTrue("MimeType invalid: " + contentType.getContentType(), contentType.getContentType().equals("text/html"));
205
206
207 Iterator modesIterator = contentType.getPortletModes();
208 int modesCount = 0;
209 while (modesIterator.hasNext())
210 {
211 modesIterator.next();
212 modesCount++;
213 }
214 assertTrue("Portlets Modes Count != 3, count = " + count, modesCount == 3);
215
216 count++;
217 }
218 assertTrue("ContentType Count != 1, count = " + count, count == 1);
219
220
221 LanguageSet infos = portlet.getLanguageSet();
222 it = infos.iterator();
223 count = 0;
224 while (it.hasNext())
225 {
226 MutableLanguage info = (MutableLanguage) it.next();
227 assertTrue("PortletInfo.Title invalid: " + info.getTitle(), info.getTitle().equals("HelloWorldTitle"));
228 assertTrue(
229 "PortletInfo.ShortTitle invalid: " + info.getShortTitle(),
230 info.getShortTitle().equals("This is the short title"));
231 Iterator keywords = info.getKeywords();
232 assertNotNull("Keywords cannot be null", keywords);
233 int keywordCount = 0;
234 while (keywords.hasNext())
235 {
236 String keyword = (String) keywords.next();
237 if (keywordCount == 0)
238 {
239 assertTrue("PortletInfo.Keywords invalid: + " + keyword, keyword.equals("Test"));
240 }
241 else
242 {
243 assertTrue("PortletInfo.Keywords invalid: + " + keyword, keyword.equals("David"));
244 }
245 keywordCount++;
246 }
247 assertTrue("Keywords Count != 2, count = " + count, keywordCount == 2);
248
249 count++;
250 }
251 assertTrue("PortletInfo Count != 1, count = " + count, count == 1);
252
253
254 PreferenceSet prefs = portlet.getPreferenceSet();
255 it = prefs.iterator();
256 count = 0;
257 while (it.hasNext())
258 {
259 PreferenceComposite pref = (PreferenceComposite) it.next();
260 assertNotNull("Preference.Name is null", pref.getName());
261 if (pref.getName().equals("time-server"))
262 {
263 assertTrue("Preference.Name invalid: " + pref.getName(), pref.getName().equals("time-server"));
264 assertTrue("Preference.Modifiable invalid: ", pref.isReadOnly() == false);
265 validatePreferences(pref, new String[] { "http://timeserver.myco.com", "http://timeserver.foo.com" });
266 }
267 else
268 {
269 assertTrue("Preference.Name invalid: " + pref.getName(), pref.getName().equals("port"));
270 assertTrue("Preference.Modifiable invalid: ", pref.isReadOnly() == true);
271 validatePreferences(pref, new String[] { "404" });
272 }
273 count++;
274 }
275 assertTrue("PortletPreference Count != 2, count = " + count, count == 2);
276
277 }
278
279 private void validatePreferences(PreferenceComposite pref, String[] expectedValues)
280 {
281 Iterator values = pref.getValues();
282
283 int count = 0;
284 while (values.hasNext())
285 {
286 String value = (String) values.next();
287 assertTrue("Preference.Value invalid: + " + value + "[" + count + "]", value.equals(expectedValues[count]));
288 count++;
289
290 }
291 assertTrue("Value Count != expectedCount, count = " + expectedValues.length, count == (expectedValues.length));
292
293 }
294
295 public void testWritingToDB() throws Exception
296 {
297
298
299 MutablePortletApplication app = portletRegistry.getPortletApplication("HW_App");
300 if (app != null)
301 {
302 portletRegistry.removeApplication(app);
303
304 }
305
306 PortletApplicationDescriptor pad = new PortletApplicationDescriptor(new FileReader("./test/testdata/deploy/portlet2.xml"), "HW_App");
307 app = pad.createPortletApplication();
308
309 app.setName("HW_App");
310
311
312 portletRegistry.registerPortletApplication(app);
313
314
315
316
317 PortletDefinition pd = portletRegistry.getPortletDefinitionByUniqueName("HW_App::PreferencePortlet");
318
319 assertNotNull(pd);
320
321 assertNotNull(pd.getPreferenceSet());
322
323 Preference pref1 = pd.getPreferenceSet().get("pref1");
324
325 assertNotNull(pref1);
326
327 Iterator itr = pref1.getValues();
328 int count = 0;
329 while (itr.hasNext())
330 {
331 count++;
332 System.out.println("Value " + count + "=" + itr.next());
333 }
334
335 assertTrue(count > 0);
336
337
338 pd = portletRegistry.getPortletDefinitionByUniqueName("HW_App::PickANumberPortlet");
339
340 assertNotNull(pd);
341
342
343 portletRegistry.removeApplication(app);
344
345
346 }
347
348 public void testInfusingWebXML() throws Exception
349 {
350 File warFile = new File("./test/testdata/deploy/webapp");
351 PortletApplicationWar paWar = new PortletApplicationWar(new DirectoryHelper(warFile), "unit-test", "/" );
352
353 SAXBuilder builder = new SAXBuilder(false);
354
355
356
357 builder.setEntityResolver(new EntityResolver()
358 {
359 public InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId)
360 throws SAXException, java.io.IOException
361 {
362
363 if (systemId.equals("http://java.sun.com/dtd/web-app_2_3.dtd"))
364 {
365 return new InputSource(PortletApplicationWar.class.getResourceAsStream("web-app_2_3.dtd"));
366 }
367 else
368 return null;
369 }
370 });
371
372 FileReader srcReader = new FileReader("./test/testdata/deploy/webapp/WEB-INF/web.xml");
373 FileReader targetReader = null;
374 Document doc = builder.build(srcReader);
375
376 Element root = doc.getRootElement();
377
378 try
379 {
380 Object jetspeedServlet = XPath.selectSingleNode(root, PortletApplicationWar.JETSPEED_SERVLET_XPATH);
381 Object jetspeedServletMapping = XPath.selectSingleNode(root, PortletApplicationWar.JETSPEED_SERVLET_MAPPING_XPATH);
382
383 assertNull(jetspeedServlet);
384 assertNull(jetspeedServletMapping);
385
386 PortletApplicationWar targetWar = paWar.copyWar("./target/webapp");
387 targetWar.processWebXML();
388
389 targetReader = new FileReader("./target/webapp/WEB-INF/web.xml");
390
391 Document targetDoc = builder.build(targetReader);
392
393 jetspeedServlet = XPath.selectSingleNode(targetDoc, PortletApplicationWar.JETSPEED_SERVLET_XPATH);
394 jetspeedServletMapping = XPath.selectSingleNode(targetDoc, PortletApplicationWar.JETSPEED_SERVLET_MAPPING_XPATH);
395
396
397 assertNotNull(jetspeedServlet);
398 assertNotNull(jetspeedServletMapping);
399
400 }
401 finally
402 {
403 srcReader.close();
404 paWar.close();
405 if (targetReader != null)
406 {
407 targetReader.close();
408 }
409 File warFile2 = new File("./target/webapp");
410 if (warFile2.exists())
411 {
412 DirectoryHelper dirHelper = new DirectoryHelper(warFile2);
413 dirHelper.remove();
414 dirHelper.close();
415 }
416 }
417
418 }
419
420 }