001: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.layout.simple;
007:
008: import junit.framework.*;
009:
010: import org.jasig.portal.UserProfile;
011: import org.jasig.portal.PortalException;
012: import org.jasig.portal.layout.IUserLayout;
013: import org.jasig.portal.layout.IUserLayoutStore;
014: import org.jasig.portal.layout.LayoutEvent;
015: import org.jasig.portal.layout.LayoutEventListener;
016: import org.jasig.portal.layout.LayoutMoveEvent;
017: import org.jasig.portal.layout.SingleDocumentUserLayoutStoreMock;
018: import org.jasig.portal.layout.node.IUserLayoutNodeDescription;
019: import org.jasig.portal.layout.node.UserLayoutChannelDescription;
020: import org.jasig.portal.layout.node.UserLayoutFolderDescription;
021: import org.jasig.portal.layout.simple.SimpleUserLayoutManager;
022: import org.jasig.portal.security.IPerson;
023: import org.jasig.portal.security.PersonFactory;
024:
025: import java.io.*;
026: import java.util.Collections;
027: import java.util.Enumeration;
028: import java.util.List;
029:
030: import org.w3c.dom.Document;
031: import org.xml.sax.EntityResolver;
032: import org.xml.sax.InputSource;
033:
034: /*
035: * TODO: This testcase is SERIOUSLY BROKEN. It is testing against a sample layout
036: * XML that is not of the structure that is actually used in uPortal, failing to reflect
037: * a change to place a root <folder/> element under the root <layout/> element,
038: * making the root <layout/> element a mere decorator, that happened around
039: * uPortal 2.2.
040: */
041:
042: /**
043: * JUnit testcase for SimpleUserLayoutManager.
044: * @version $Revision: 35742 $ $Date: 2005-05-03 13:55:29 -0700 (Tue, 03 May 2005) $
045: */
046: public class SimpleUserLayoutManagerTest extends TestCase implements
047: LayoutEventListener {
048: Document sampleUserLayout = null;
049: IUserLayoutStore uls = null;
050: IPerson p = null;
051: SimpleUserLayoutManager man = null;
052: protected final static String SAMPLE_LAYOUT_FILENAME = "userLayout.xml";
053:
054: boolean nodeAdded, nodeDeleted, nodeMoved, nodeUpdated,
055: layoutSaved, layoutLoaded;
056: LayoutEvent lastEvent;
057:
058: public SimpleUserLayoutManagerTest(String s) {
059: super (s);
060: }
061:
062: protected void setUp() throws Exception {
063: UserLayoutDTDResolver er = new UserLayoutDTDResolver();
064: // read in the layout DOM
065: // note that we really do need to have a DOM structure here in order to introduce
066: // persistent changes on the level of userLayout.
067:
068: //org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser();
069: javax.xml.parsers.DocumentBuilderFactory pf = javax.xml.parsers.DocumentBuilderFactory
070: .newInstance();
071: pf.setValidating(true);
072: javax.xml.parsers.DocumentBuilder parser = pf
073: .newDocumentBuilder();
074: //org.apache.xerces.jaxp.DOMParser parser = new org.apache.xerces.parsers.DOMParser ();
075: //parser.setFeature ("http://apache.org/xml/features/validation/dynamic", true);
076: parser.setEntityResolver(er);
077: parser
078: .setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
079: this .sampleUserLayout = parser
080: .parse(new org.xml.sax.InputSource(this .getClass()
081: .getResourceAsStream(SAMPLE_LAYOUT_FILENAME)));
082:
083: p = PersonFactory.createPerson();
084:
085: assertTrue(sampleUserLayout != null);
086: uls = new SingleDocumentUserLayoutStoreMock(sampleUserLayout);
087: man = new SimpleUserLayoutManager(p, new UserProfile(), uls);
088:
089: // clear event-related markers
090: nodeAdded = nodeDeleted = nodeMoved = nodeUpdated = layoutSaved = layoutLoaded = false;
091: lastEvent = null;
092: man.addLayoutEventListener(this );
093: man.loadUserLayout();
094:
095: }
096:
097: protected class UserLayoutDTDResolver implements EntityResolver {
098: final static String dtdName = "userLayout.dtd";
099:
100: /**
101: * Sets up a new input source based on the dtd specified in the xml document
102: * @param publicId the public ID
103: * @param systemId the system ID
104: * @return an input source based on the dtd specified in the xml document
105: */
106: public InputSource resolveEntity(String publicId,
107: String systemId) {
108: InputStream inStream = null;
109: InputSource inSrc = null;
110:
111: if (systemId != null) {
112: if (dtdName != null && systemId.indexOf(dtdName) != -1) {
113: try {
114: inStream = getClass().getResourceAsStream(
115: dtdName);
116: } catch (Exception e) {
117: e.printStackTrace();
118: return null;
119: }
120: }
121: inSrc = new InputSource(inStream);
122: }
123:
124: return inSrc;
125: }
126: }
127:
128: public void testGetNode() throws Exception {
129:
130: // test load event
131: {
132: assertTrue("layoutLoad event receive", layoutLoaded);
133: }
134:
135: // get a folder
136: {
137: String id = "s4";
138: IUserLayoutNodeDescription node = man.getNode(id);
139: assertTrue(node != null);
140: if (node != null) {
141: // make up a fake node, with the same id
142: UserLayoutFolderDescription ts = new UserLayoutFolderDescription();
143: ts.setId(node.getId());
144: ts.setName("Main");
145: ts.setHidden(false);
146: ts.setImmutable(true);
147: ts.setUnremovable(true);
148: ts
149: .setFolderType(UserLayoutFolderDescription.REGULAR_TYPE);
150: assertEquals(ts, node);
151: }
152: }
153:
154: // get a channel
155: {
156: String id = "n6";
157: IUserLayoutNodeDescription node = man.getNode(id);
158: assertTrue(node != null);
159: if (node != null) {
160: // make up a fake node, with the same id
161: UserLayoutChannelDescription ts = new UserLayoutChannelDescription();
162: ts.setId(node.getId());
163: ts.setName("uPortal-Powered Sites");
164: ts.setTitle("uPortal-Powered Sites");
165: ts.setHidden(false);
166: ts.setImmutable(true);
167: ts.setUnremovable(true);
168: ts
169: .setClassName("org.jasig.portal.channels.CGenericXSLT");
170: ts.setFunctionalName("");
171: ts.setChannelPublishId("5");
172: ts.setChannelTypeId("0");
173: ts.setDescription("uPortal Demos channel");
174: ts.setEditable(false);
175: ts.setHasAbout(false);
176: ts.setHasHelp(false);
177: ts.setTimeout(10000);
178:
179: ts
180: .setParameterValue("xmlUri",
181: "http://www.interactivebusiness.com/publish/jasigPortalDemos.rss");
182: ts.setParameterValue("sslUri",
183: "CGenericXSLT/RSS/RSS-0_9x.ssl");
184:
185: assertEquals(ts, node);
186: }
187: }
188: }
189:
190: public void testAddChannel() throws Exception {
191: // testing by doing add and subsequent get
192:
193: // add a channel
194: UserLayoutChannelDescription nchan = new UserLayoutChannelDescription();
195: nchan.setName("Newly added channel");
196: nchan.setTitle("Newly added channel");
197: nchan.setHidden(false);
198: nchan.setImmutable(true);
199: nchan.setUnremovable(true);
200: nchan.setClassName("org.jasig.portal.channels.CGenericXSLT");
201: nchan.setFunctionalName("");
202: nchan.setChannelPublishId("5");
203: nchan.setChannelTypeId("0");
204: nchan.setDescription("some example channel");
205: nchan.setEditable(false);
206: nchan.setHasAbout(false);
207: nchan.setHasHelp(false);
208: nchan.setTimeout(10000);
209:
210: nchan
211: .setParameterValue("xmlUri",
212: "http://www.interactivebusiness.com/publish/jasigPortalDemos.rss");
213: nchan.setParameterValue("sslUri",
214: "CGenericXSLT/RSS/RSS-0_9x.ssl");
215:
216: String parentId = "s7";
217: String siblingId = "n8";
218: assertTrue("can a node be added ?", man.canAddNode(nchan,
219: parentId, siblingId));
220:
221: UserLayoutChannelDescription rchan = (UserLayoutChannelDescription) man
222: .addNode(nchan, parentId, siblingId);
223: // System.out.println("Layout\n"+XML.serializeNode(man.getUserLayoutDOM()));
224: assertTrue("addNode() returned null", rchan != null);
225: nchan.setId(rchan.getId());
226: assertEquals("comparing the original and addNode() result: ",
227: nchan, rchan);
228:
229: // do a get
230: IUserLayoutNodeDescription gnode = man.getNode(nchan.getId());
231: assertEquals("comparing the original and getNode() result: ",
232: nchan, gnode);
233: assertEquals("parentId is the specified attachment point",
234: parentId, man.getParentId(nchan.getId()));
235: assertEquals("siblingId is the specified next sibling",
236: siblingId, man.getNextSiblingId(nchan.getId()));
237:
238: assertTrue("nodeAdded event received", nodeAdded);
239: assertTrue("LayoutEvent is not null", lastEvent != null);
240: assertEquals("LayoutEvent nodeId", nchan.getId(), lastEvent
241: .getNodeDescription().getId());
242:
243: }
244:
245: public void testAddFolder() throws Exception {
246: // testing by doing add and subsequent get
247:
248: // add a channel
249: UserLayoutFolderDescription nfold = new UserLayoutFolderDescription();
250: nfold.setName("Newly added folder");
251: nfold.setHidden(false);
252: nfold.setImmutable(true);
253: nfold.setUnremovable(true);
254: nfold.setFolderType(UserLayoutFolderDescription.REGULAR_TYPE);
255:
256: String parentId = "s7";
257: assertTrue("can a node be added ?", man.canAddNode(nfold,
258: parentId, null));
259:
260: UserLayoutFolderDescription rfold = (UserLayoutFolderDescription) man
261: .addNode(nfold, parentId, null);
262: // System.out.println("Layout\n"+XML.serializeNode(man.getUserLayoutDOM()));
263: assertTrue("addNode() returned null", rfold != null);
264: nfold.setId(rfold.getId());
265: assertEquals("comparing the original and addNode() result: ",
266: nfold, rfold);
267:
268: // do a get
269: IUserLayoutNodeDescription gnode = man.getNode(nfold.getId());
270: assertEquals("comparing the original and getNode() result: ",
271: nfold, gnode);
272: assertEquals("parentId is the specified attachment point",
273: parentId, man.getParentId(nfold.getId()));
274:
275: assertTrue("nodeAdded event received", nodeAdded);
276: assertTrue("LayoutEvent is not null", lastEvent != null);
277: assertEquals("LayoutEvent nodeId", nfold.getId(), lastEvent
278: .getNodeDescription().getId());
279: }
280:
281: public void testCanMove() throws Exception {
282: // try moving a channel
283: String nodeId = "n8";
284: String targetId = "s10";
285: assertTrue("Can channel \"" + nodeId
286: + "\" be moved to folder \"" + targetId + "\"", man
287: .canMoveNode(nodeId, targetId, null));
288:
289: // try moving into an immutable folder
290: targetId = "s4";
291: assertTrue("Moving channel \"" + nodeId
292: + "\" be moved to an immutable folder \"" + targetId
293: + "\"", !man.canMoveNode(nodeId, targetId, null));
294:
295: // try moving from an immutable folder
296: nodeId = "n6";
297: targetId = "s10";
298: assertTrue(
299: "Can channel \""
300: + nodeId
301: + "\" (from the immutable folder) be moved to folder \""
302: + targetId + "\"", !man.canMoveNode(nodeId,
303: targetId, null));
304:
305: }
306:
307: public void testCanDelete() throws Exception {
308: String nodeId = "s1";
309: assertTrue("Can delete removable node=\"" + nodeId + "\"", man
310: .canDeleteNode(nodeId));
311: nodeId = "n3";
312: assertTrue("Can delete unremovable node=\"" + nodeId + "\"",
313: !man.canDeleteNode(nodeId));
314:
315: }
316:
317: public void testDelete() throws Exception {
318: String nodeId = "s1";
319: String parentId = IUserLayout.ROOT_NODE_NAME;
320: man.deleteNode(nodeId);
321:
322: boolean exception = false;
323: try {
324: IUserLayoutNodeDescription node = man.getNode(nodeId);
325: } catch (PortalException e) {
326: exception = true;
327: }
328:
329: assertTrue(
330: "Thrown an exception when looking for a deleted node or returned a node with no parent.",
331: exception || man.getParentId(nodeId) == null);
332:
333: assertTrue("nodeDeleted event received", nodeDeleted);
334: assertTrue("LayoutEvent is not null", lastEvent != null);
335: assertTrue("LayoutMoveEvent was received",
336: lastEvent instanceof LayoutMoveEvent);
337: LayoutMoveEvent lme = (LayoutMoveEvent) lastEvent;
338: assertEquals("LayoutMoveEvent nodeId", nodeId, lme
339: .getNodeDescription().getId());
340: assertEquals("LayoutMoveEvent oldParentNodeId", parentId, lme
341: .getOldParentNodeId());
342:
343: }
344:
345: public void testMoveChannel() throws Exception {
346: // try moving a channel
347: String nodeId = "n8";
348: String parentId = "s7";
349: String targetId = "s10";
350: assertTrue("Can channel \"" + nodeId
351: + "\" be moved to folder \"" + targetId + "\"", man
352: .canMoveNode(nodeId, targetId, null));
353: man.moveNode(nodeId, targetId, null);
354: assertEquals("New channel attachment point", man
355: .getParentId(nodeId), targetId);
356: assertEquals("Next siblingId", man.getNextSiblingId(nodeId),
357: null);
358:
359: assertTrue("nodeMoved event received", nodeMoved);
360: assertTrue("LayoutEvent is not null", lastEvent != null);
361: assertTrue("LayoutMoveEvent was received",
362: lastEvent instanceof LayoutMoveEvent);
363: LayoutMoveEvent lme = (LayoutMoveEvent) lastEvent;
364: assertEquals("LayoutMoveEvent nodeId", nodeId, lme
365: .getNodeDescription().getId());
366: assertEquals("LayoutMoveEvent oldParentNodeId", parentId, lme
367: .getOldParentNodeId());
368:
369: }
370:
371: public void testUpdateChannel() throws Exception {
372: String channelId = "n8";
373: UserLayoutChannelDescription chan = (UserLayoutChannelDescription) man
374: .getNode(channelId);
375: chan.setName("uPortal other page");
376: chan.setTimeout(10);
377: chan.setTitle("uPortal other title");
378: chan.setHidden(true);
379: chan.setParameterValue("newParameter", "newValue");
380: man.updateNode(chan);
381: // System.out.println("Layout\n"+XML.serializeNode(man.getUserLayoutDOM()));
382:
383: UserLayoutChannelDescription rchan = (UserLayoutChannelDescription) man
384: .getNode(channelId);
385: assertEquals(
386: "Comparing node used to update with the update result: ",
387: chan, rchan);
388:
389: assertTrue("nodeUpdated event received", nodeUpdated);
390: assertTrue("LayoutEvent is not null", lastEvent != null);
391: assertEquals("LayoutEvent nodeId", channelId, lastEvent
392: .getNodeDescription().getId());
393:
394: }
395:
396: public void testUpdateFolder() throws Exception {
397: String folderId = "s1";
398: UserLayoutFolderDescription fold = (UserLayoutFolderDescription) man
399: .getNode(folderId);
400: fold.setName("New name");
401: fold.setHidden(true);
402: fold.setFolderType(UserLayoutFolderDescription.REGULAR_TYPE);
403: // get child list
404: Enumeration ochildren = man.getChildIds(folderId);
405:
406: man.updateNode(fold);
407: // System.out.println("Layout\n"+XML.serializeNode(man.getUserLayoutDOM()));
408:
409: UserLayoutFolderDescription rfold = (UserLayoutFolderDescription) man
410: .getNode(folderId);
411: assertEquals(
412: "Comparing node used to update with the update result: ",
413: fold, rfold);
414:
415: Enumeration nchildren = man.getChildIds(folderId);
416:
417: // convert to List because equals() is well defined on
418: // List whereas it is not well defined on Enumeration
419: List oldChildren = Collections.list(ochildren);
420: List newChildren = Collections.list(nchildren);
421:
422: assertEquals("Comparing child Ids of an updated folder: ",
423: oldChildren, newChildren);
424:
425: assertTrue("nodeUpdated event received", nodeUpdated);
426: assertTrue("LayoutEvent is not null", lastEvent != null);
427: assertEquals("LayoutEvent nodeId", folderId, lastEvent
428: .getNodeDescription().getId());
429:
430: }
431:
432: public void testMoveFolder() throws Exception {
433: // try moving a channel
434: String nodeId = "s10";
435: String targetId = "s7";
436: String nextSiblingId = "n8";
437: man.moveNode(nodeId, targetId, nextSiblingId);
438: assertEquals("New channel attachment point", man
439: .getParentId(nodeId), targetId);
440: assertEquals("Next siblingId", man.getNextSiblingId(nodeId),
441: nextSiblingId);
442: // System.out.println("Layout\n"+XML.serializeNode(man.getUserLayoutDOM()));
443: }
444:
445: protected void assertEquals(IUserLayoutNodeDescription one,
446: IUserLayoutNodeDescription two) {
447: assertEquals("", one, two);
448: }
449:
450: protected void assertEquals(String message,
451: IUserLayoutNodeDescription one,
452: IUserLayoutNodeDescription two) {
453: if (one instanceof UserLayoutFolderDescription) {
454: assertTrue(message + "nodes are of a different type!",
455: two instanceof UserLayoutFolderDescription);
456: } else {
457: assertTrue(message + "nodes are of a different type!",
458: two instanceof UserLayoutChannelDescription);
459: }
460: assertEquals(message + "node attribute \"name\"",
461: one.getName(), two.getName());
462: assertEquals(message + "node attribute \"unremovable\"", one
463: .isUnremovable(), two.isUnremovable());
464: assertEquals(message + "node attribute \"mmutable\"", one
465: .isImmutable(), two.isImmutable());
466: assertEquals(message + "node attribute \"hidden\"", one
467: .isHidden(), two.isHidden());
468: }
469:
470: private void assertEquals(String message,
471: UserLayoutChannelDescription one,
472: UserLayoutChannelDescription two) {
473: assertEquals(message + "channel attribute \"hasAbout\"", one
474: .hasAbout(), two.hasAbout());
475: assertEquals(message + "channel attribute \"hasHelp\"", one
476: .hasHelp(), two.hasHelp());
477: assertEquals(message + "channel attribute \"isEditable\"", one
478: .isEditable(), two.isEditable());
479: assertEquals(message + "channel attribute \"timeout\"", one
480: .getTimeout(), two.getTimeout());
481: assertEquals(message + "channel functional name", one
482: .getFunctionalName(), two.getFunctionalName());
483: assertEquals(message + "channel subscribe id", one
484: .getChannelSubscribeId(), two.getChannelSubscribeId());
485: assertEquals(message + "channel type id", one
486: .getChannelTypeId(), two.getChannelTypeId());
487: assertEquals(message + "channel public id", one
488: .getChannelPublishId(), two.getChannelPublishId());
489: assertEquals(message + "channel class name",
490: one.getClassName(), two.getClassName());
491: assertEquals(message + "channel title", one.getTitle(), two
492: .getTitle());
493: assertEquals(message + "channel description", one
494: .getDescription(), two.getDescription());
495:
496: // compare parameter content
497: for (Enumeration e = one.getParameterNames(); e
498: .hasMoreElements();) {
499: String pName = (String) e.nextElement();
500: assertEquals("channel parameter \"" + pName + "\"", one
501: .getParameterValue(pName), two
502: .getParameterValue(pName));
503: }
504: // other way around
505: for (Enumeration e = two.getParameterNames(); e
506: .hasMoreElements();) {
507: assertTrue("contains parameter", one
508: .containsParameter((String) e.nextElement()));
509: }
510:
511: }
512:
513: private void assertEquals(String message,
514: UserLayoutFolderDescription one,
515: UserLayoutFolderDescription two) {
516: assertEquals(message + "channel folder type", one
517: .getFolderType(), two.getFolderType());
518: }
519:
520: public void testsGetFolderDescription() throws Exception {
521:
522: }
523:
524: public void channelAdded(LayoutEvent ev) {
525: lastEvent = ev;
526: nodeAdded = true;
527: }
528:
529: public void channelUpdated(LayoutEvent ev) {
530: lastEvent = ev;
531: nodeUpdated = true;
532: }
533:
534: public void channelMoved(LayoutMoveEvent ev) {
535: lastEvent = ev;
536: nodeMoved = true;
537: }
538:
539: public void channelDeleted(LayoutMoveEvent ev) {
540: lastEvent = ev;
541: nodeDeleted = true;
542: }
543:
544: public void folderAdded(LayoutEvent ev) {
545: lastEvent = ev;
546: nodeAdded = true;
547: }
548:
549: public void folderUpdated(LayoutEvent ev) {
550: lastEvent = ev;
551: nodeUpdated = true;
552: }
553:
554: public void folderMoved(LayoutMoveEvent ev) {
555: lastEvent = ev;
556: nodeMoved = true;
557: }
558:
559: public void folderDeleted(LayoutMoveEvent ev) {
560: lastEvent = ev;
561: nodeDeleted = true;
562: }
563:
564: public void layoutLoaded() {
565: layoutLoaded = true;
566: }
567:
568: public void layoutSaved() {
569: layoutSaved = true;
570: }
571:
572: }
|