001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.tools.pamanager;
018:
019: import java.io.File;
020: import java.io.FileReader;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.Locale;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027: import junit.textui.TestRunner;
028:
029: import org.apache.jetspeed.AbstractRequestContextTestCase;
030: import org.apache.jetspeed.om.common.MutableLanguage;
031: import org.apache.jetspeed.om.common.ParameterComposite;
032: import org.apache.jetspeed.om.common.UserAttribute;
033: import org.apache.jetspeed.om.common.portlet.ContentTypeComposite;
034: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
035: import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
036: import org.apache.jetspeed.om.common.preference.PreferenceComposite;
037: import org.apache.jetspeed.util.DirectoryHelper;
038: import org.apache.jetspeed.util.descriptor.PortletApplicationDescriptor;
039: import org.apache.jetspeed.util.descriptor.PortletApplicationWar;
040: import org.apache.pluto.om.common.DisplayName;
041: import org.apache.pluto.om.common.LanguageSet;
042: import org.apache.pluto.om.common.ParameterSet;
043: import org.apache.pluto.om.common.Preference;
044: import org.apache.pluto.om.common.PreferenceSet;
045: import org.apache.pluto.om.portlet.ContentTypeSet;
046: import org.apache.pluto.om.portlet.PortletDefinition;
047: import org.apache.pluto.om.portlet.PortletDefinitionList;
048: import org.jdom.Document;
049: import org.jdom.Element;
050: import org.jdom.input.SAXBuilder;
051: import org.jdom.xpath.XPath;
052: import org.xml.sax.EntityResolver;
053: import org.xml.sax.InputSource;
054: import org.xml.sax.SAXException;
055:
056: /**
057: * TestPortletDescriptor - tests loading the portlet.xml deployment descriptor
058: * into Java objects
059: *
060: * @author <a href="taylor@apache.org">David Sean Taylor</a>
061: *
062: * @version $Id: TestPortletDescriptor.java 517719 2007-03-13 15:05:48Z ate $
063: */
064: public class TestPortletDescriptor extends
065: AbstractRequestContextTestCase {
066: /**
067: * Start the tests.
068: *
069: * @param args the arguments. Not used
070: */
071: public static void main(String args[]) {
072: TestRunner.main(new String[] { TestPortletDescriptor.class
073: .getName() });
074: }
075:
076: public static Test suite()
077:
078: {
079:
080: // All methods starting with "test" will be executed in the test suite.
081:
082: return new TestSuite(TestPortletDescriptor.class);
083:
084: }
085:
086: /*
087: * Overrides the database properties
088: */
089: // public void overrideProperties(Configuration properties)
090: // {
091: // super.overrideProperties(properties);
092: // }
093: public void testLoadPortletApplicationTree() throws Exception {
094: System.out.println("Testing loadPortletApplicationTree");
095: PortletApplicationDescriptor pad = new PortletApplicationDescriptor(
096: new FileReader("./test/testdata/deploy/portlet.xml"),
097: "unit-test");
098: MutablePortletApplication app = pad.createPortletApplication();
099: assertNotNull("App is null", app);
100: assertNotNull("Version is null", app.getVersion());
101: assertTrue("Version invalid: " + app.getVersion(), app
102: .getVersion().equals("1.0"));
103: assertNotNull("PA Identifier is null", app
104: .getApplicationIdentifier());
105: assertTrue("PA Identifier invalid: "
106: + app.getApplicationIdentifier(), app
107: .getApplicationIdentifier().equals("TestRegistry"));
108:
109: validateUserInfo(app);
110:
111: // portlets
112: PortletDefinitionList portletsList = app
113: .getPortletDefinitionList();
114: Iterator it = portletsList.iterator();
115: int count = 0;
116: while (it.hasNext()) {
117: PortletDefinitionComposite portlet = (PortletDefinitionComposite) it
118: .next();
119: String identifier = portlet.getPortletIdentifier();
120: assertNotNull("Portlet.Identifier is null", identifier);
121: if (identifier.equals("HelloPortlet")) {
122: validateHelloPortlet(portlet);
123: }
124: count++;
125: }
126: assertTrue("Portlet Count != 4, = " + count, count == 4);
127:
128: }
129:
130: private void validateUserInfo(MutablePortletApplication app) {
131: // Portlet User Attributes
132: Collection userAttributeSet = app.getUserAttributes();
133: Iterator it = userAttributeSet.iterator();
134: while (it.hasNext()) {
135: UserAttribute userAttribute = (UserAttribute) it.next();
136: assertNotNull("User attribute name is null, ",
137: userAttribute.getName());
138: if (userAttribute.getName().equals("user.name.given")) {
139: assertTrue("User attribute description: "
140: + userAttribute.getDescription(), userAttribute
141: .getDescription().equals("User Given Name"));
142: }
143: if (userAttribute.getName().equals("user.name.family")) {
144: assertTrue("User attribute description: "
145: + userAttribute.getDescription(), userAttribute
146: .getDescription().equals("User Last Name"));
147: }
148: if (userAttribute.getName().equals(
149: "user.home-info.online.email")) {
150: assertTrue("User attribute description: "
151: + userAttribute.getDescription(), userAttribute
152: .getDescription().equals("User eMail"));
153: }
154: }
155: }
156:
157: private void validateHelloPortlet(PortletDefinitionComposite portlet) {
158: // Portlet Name
159: assertNotNull("Portlet.Name is null", portlet.getName());
160: assertTrue("Portlet.Name invalid: " + portlet.getName(),
161: portlet.getName().equals("HelloPortlet"));
162:
163: // Portlet Class
164: assertNotNull("Portlet.Class is null", portlet.getClassName());
165: assertTrue(
166: "Portlet.Class invalid: " + portlet.getClassName(),
167: portlet
168: .getClassName()
169: .equals(
170: "org.apache.jetspeed.portlet.helloworld.HelloWorld"));
171:
172: // Expiration Cache
173: assertNotNull("Portlet.Expiration is null", portlet
174: .getExpirationCache());
175: assertTrue("Portlet.Expiration invalid: "
176: + portlet.getExpirationCache(), portlet
177: .getExpirationCache().equals("-1"));
178:
179: // Display Name
180: DisplayName displayName = portlet
181: .getDisplayName(Locale.ENGLISH);
182: assertNotNull("Display Name is null", displayName);
183: assertTrue("Portlet.DisplayName invalid: "
184: + displayName.getDisplayName(), displayName
185: .getDisplayName().equals("HelloWorld Portlet Wrapper"));
186:
187: // Init Parameters
188: ParameterSet paramsList = portlet.getInitParameterSet();
189: Iterator it = paramsList.iterator();
190: int count = 0;
191: while (it.hasNext()) {
192: ParameterComposite parameter = (ParameterComposite) it
193: .next();
194: assertTrue(
195: "InitParam.Name invalid: " + parameter.getName(),
196: parameter.getName().equals("hello"));
197: assertTrue("InitParam.Value invalid: "
198: + parameter.getValue(), parameter.getValue()
199: .equals("Hello Portlet"));
200: assertTrue("InitParam.Description invalid: "
201: + parameter.getDescription(Locale.ENGLISH),
202: parameter.getDescription(Locale.ENGLISH)
203: .getDescription().equals("test init param"));
204: count++;
205: }
206: assertTrue("InitParam Count != 1, count = " + count, count == 1);
207:
208: // Supports Content Type
209: ContentTypeSet supports = portlet.getContentTypeSet();
210: it = supports.iterator();
211: count = 0;
212: while (it.hasNext()) {
213: ContentTypeComposite contentType = (ContentTypeComposite) it
214: .next();
215: assertTrue("MimeType invalid: "
216: + contentType.getContentType(), contentType
217: .getContentType().equals("text/html"));
218:
219: // Portlet Modes
220: Iterator modesIterator = contentType.getPortletModes();
221: int modesCount = 0;
222: while (modesIterator.hasNext()) {
223: modesIterator.next();
224: modesCount++;
225: }
226: assertTrue("Portlets Modes Count != 3, count = " + count,
227: modesCount == 3);
228:
229: count++;
230: }
231: assertTrue("ContentType Count != 1, count = " + count,
232: count == 1);
233:
234: // Portlet Info
235: LanguageSet infos = portlet.getLanguageSet();
236: it = infos.iterator();
237: count = 0;
238: while (it.hasNext()) {
239: MutableLanguage info = (MutableLanguage) it.next();
240: assertTrue("PortletInfo.Title invalid: " + info.getTitle(),
241: info.getTitle().equals("HelloWorldTitle"));
242: assertTrue("PortletInfo.ShortTitle invalid: "
243: + info.getShortTitle(), info.getShortTitle()
244: .equals("This is the short title"));
245: Iterator keywords = info.getKeywords();
246: assertNotNull("Keywords cannot be null", keywords);
247: int keywordCount = 0;
248: while (keywords.hasNext()) {
249: String keyword = (String) keywords.next();
250: if (keywordCount == 0) {
251: assertTrue("PortletInfo.Keywords invalid: + "
252: + keyword, keyword.equals("Test"));
253: } else {
254: assertTrue("PortletInfo.Keywords invalid: + "
255: + keyword, keyword.equals("David"));
256: }
257: keywordCount++;
258: }
259: assertTrue("Keywords Count != 2, count = " + count,
260: keywordCount == 2);
261:
262: count++;
263: }
264: assertTrue("PortletInfo Count != 1, count = " + count,
265: count == 1);
266:
267: // Portlet Preferences
268: PreferenceSet prefs = portlet.getPreferenceSet();
269: it = prefs.iterator();
270: count = 0;
271: while (it.hasNext()) {
272: PreferenceComposite pref = (PreferenceComposite) it.next();
273: assertNotNull("Preference.Name is null", pref.getName());
274: if (pref.getName().equals("time-server")) {
275: assertTrue(
276: "Preference.Name invalid: " + pref.getName(),
277: pref.getName().equals("time-server"));
278: assertTrue("Preference.Modifiable invalid: ", pref
279: .isReadOnly() == false);
280: validatePreferences(pref, new String[] {
281: "http://timeserver.myco.com",
282: "http://timeserver.foo.com" });
283: } else {
284: assertTrue(
285: "Preference.Name invalid: " + pref.getName(),
286: pref.getName().equals("port"));
287: assertTrue("Preference.Modifiable invalid: ", pref
288: .isReadOnly() == true);
289: validatePreferences(pref, new String[] { "404" });
290: }
291: count++;
292: }
293: assertTrue("PortletPreference Count != 2, count = " + count,
294: count == 2);
295:
296: }
297:
298: private void validatePreferences(PreferenceComposite pref,
299: String[] expectedValues) {
300: Iterator values = pref.getValues();
301:
302: int count = 0;
303: while (values.hasNext()) {
304: String value = (String) values.next();
305: assertTrue("Preference.Value invalid: + " + value + "["
306: + count + "]", value.equals(expectedValues[count]));
307: count++;
308: // System.out.println("value = " + value);
309: }
310: assertTrue("Value Count != expectedCount, count = "
311: + expectedValues.length,
312: count == (expectedValues.length));
313:
314: }
315:
316: public void testWritingToDB() throws Exception {
317:
318: MutablePortletApplication app = portletRegistry
319: .getPortletApplication("HW_App");
320: if (app != null) {
321: portletRegistry.removeApplication(app);
322:
323: }
324:
325: PortletApplicationDescriptor pad = new PortletApplicationDescriptor(
326: new FileReader("./test/testdata/deploy/portlet2.xml"),
327: "HW_App");
328: app = pad.createPortletApplication();
329:
330: app.setName("HW_App");
331:
332: portletRegistry.registerPortletApplication(app);
333:
334: // store.invalidateAll();
335:
336: PortletDefinition pd = portletRegistry
337: .getPortletDefinitionByUniqueName("HW_App::PreferencePortlet");
338:
339: assertNotNull(pd);
340:
341: assertNotNull(pd.getPreferenceSet());
342:
343: Preference pref1 = pd.getPreferenceSet().get("pref1");
344:
345: assertNotNull(pref1);
346:
347: Iterator itr = pref1.getValues();
348: int count = 0;
349: while (itr.hasNext()) {
350: count++;
351: System.out.println("Value " + count + "=" + itr.next());
352: }
353:
354: assertTrue(count > 0);
355:
356: pd = portletRegistry
357: .getPortletDefinitionByUniqueName("HW_App::PickANumberPortlet");
358:
359: assertNotNull(pd);
360:
361: portletRegistry.removeApplication(app);
362:
363: }
364:
365: public void testInfusingWebXML() throws Exception {
366: File warFile = new File("./test/testdata/deploy/webapp");
367: PortletApplicationWar paWar = new PortletApplicationWar(
368: new DirectoryHelper(warFile), "unit-test", "/");
369:
370: SAXBuilder builder = new SAXBuilder(false);
371:
372: // Use the local dtd instead of remote dtd. This
373: // allows to deploy the application offline
374: builder.setEntityResolver(new EntityResolver() {
375: public InputSource resolveEntity(java.lang.String publicId,
376: java.lang.String systemId) throws SAXException,
377: java.io.IOException {
378:
379: if (systemId
380: .equals("http://java.sun.com/dtd/web-app_2_3.dtd")) {
381: return new InputSource(PortletApplicationWar.class
382: .getResourceAsStream("web-app_2_3.dtd"));
383: } else
384: return null;
385: }
386: });
387:
388: FileReader srcReader = new FileReader(
389: "./test/testdata/deploy/webapp/WEB-INF/web.xml");
390: FileReader targetReader = null;
391: Document doc = builder.build(srcReader);
392:
393: Element root = doc.getRootElement();
394:
395: try {
396: Object jetspeedServlet = XPath.selectSingleNode(root,
397: PortletApplicationWar.JETSPEED_SERVLET_XPATH);
398: Object jetspeedServletMapping = XPath
399: .selectSingleNode(
400: root,
401: PortletApplicationWar.JETSPEED_SERVLET_MAPPING_XPATH);
402:
403: assertNull(jetspeedServlet);
404: assertNull(jetspeedServletMapping);
405:
406: PortletApplicationWar targetWar = paWar
407: .copyWar("./target/webapp");
408: targetWar.processWebXML();
409:
410: targetReader = new FileReader(
411: "./target/webapp/WEB-INF/web.xml");
412:
413: Document targetDoc = builder.build(targetReader);
414:
415: jetspeedServlet = XPath.selectSingleNode(targetDoc,
416: PortletApplicationWar.JETSPEED_SERVLET_XPATH);
417: jetspeedServletMapping = XPath
418: .selectSingleNode(
419: targetDoc,
420: PortletApplicationWar.JETSPEED_SERVLET_MAPPING_XPATH);
421:
422: assertNotNull(jetspeedServlet);
423: assertNotNull(jetspeedServletMapping);
424:
425: } finally {
426: srcReader.close();
427: paWar.close();
428: if (targetReader != null) {
429: targetReader.close();
430: }
431: File warFile2 = new File("./target/webapp");
432: if (warFile2.exists()) {
433: DirectoryHelper dirHelper = new DirectoryHelper(
434: warFile2);
435: dirHelper.remove();
436: dirHelper.close();
437: }
438: }
439:
440: }
441:
442: }
|