001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webrender.test;
031:
032: import org.w3c.dom.Element;
033: import org.w3c.dom.NodeList;
034:
035: import nextapp.echo2.webrender.ServerMessage;
036: import junit.framework.TestCase;
037:
038: /**
039: * Unit tests for <code>ServerMessage</code>.
040: */
041: public class ServerMessageTest extends TestCase {
042:
043: /**
044: * Test <code>addLibrary()</code>.
045: */
046: public void testAddLibrary() {
047: NodeList libraryNodeList;
048: ServerMessage message = new ServerMessage();
049:
050: message.addLibrary("service1");
051: libraryNodeList = message.getDocument().getElementsByTagName(
052: "library");
053: assertEquals(1, libraryNodeList.getLength());
054:
055: message.addLibrary("service2");
056: libraryNodeList = message.getDocument().getElementsByTagName(
057: "library");
058: assertEquals(2, libraryNodeList.getLength());
059:
060: message.addLibrary("service1");
061: libraryNodeList = message.getDocument().getElementsByTagName(
062: "library");
063: assertEquals(2, libraryNodeList.getLength());
064: }
065:
066: /**
067: * Test <code>appendPartDirective()</code>.
068: */
069: public void testAppendPartDirective() {
070: ServerMessage message = new ServerMessage();
071: message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE,
072: "DomUpdate", "dom-add");
073: message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE,
074: "DomUpdate", "dom-remove");
075: message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE,
076: "DomUpdate", "dom-add");
077: message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE,
078: "SomethingElse", "thing");
079: message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE,
080: "DomUpdate", "dom-remove");
081: message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE,
082: "DomUpdate", "dom-add");
083: assertEquals(3, message.getDocument().getElementsByTagName(
084: "message-part").getLength());
085: }
086:
087: /**
088: * Test itemized directive capability.
089: */
090: public void testItemizedDirective() {
091: ServerMessage message = new ServerMessage();
092: String[] keyAttributeNames = new String[] { "alpha", "bravo" };
093:
094: Element directiveElement1 = message.getItemizedDirective(
095: ServerMessage.GROUP_ID_POSTUPDATE, "Processor",
096: "Directive", keyAttributeNames, new String[] {
097: "alpha1", "bravo1" });
098: assertEquals("Directive", directiveElement1.getNodeName());
099: assertEquals("alpha1", directiveElement1.getAttribute("alpha"));
100: assertEquals("bravo1", directiveElement1.getAttribute("bravo"));
101:
102: Element directiveElement2 = message.getItemizedDirective(
103: ServerMessage.GROUP_ID_POSTUPDATE, "Processor",
104: "Directive", keyAttributeNames, new String[] {
105: "alpha1", "bravo1" });
106: assertEquals("Directive", directiveElement2.getNodeName());
107: assertEquals("alpha1", directiveElement2.getAttribute("alpha"));
108: assertEquals("bravo1", directiveElement2.getAttribute("bravo"));
109:
110: assertTrue(directiveElement1.equals(directiveElement2));
111:
112: Element directiveElement3 = message.getItemizedDirective(
113: ServerMessage.GROUP_ID_POSTUPDATE, "Processor",
114: "Directive", keyAttributeNames, new String[] {
115: "alpha2", "bravo2" });
116: assertEquals("Directive", directiveElement3.getNodeName());
117: assertEquals("alpha2", directiveElement3.getAttribute("alpha"));
118: assertEquals("bravo2", directiveElement3.getAttribute("bravo"));
119:
120: assertFalse(directiveElement1.equals(directiveElement3));
121: assertFalse(directiveElement2.equals(directiveElement3));
122: }
123:
124: /**
125: * Test group identifiers.
126: */
127: public void testGroupIds() {
128: ServerMessage message = new ServerMessage();
129: Element directiveElement, messagePartGroupElement;
130:
131: directiveElement = message.appendPartDirective(
132: ServerMessage.GROUP_ID_UPDATE, "Something",
133: "do-something");
134: messagePartGroupElement = (Element) directiveElement
135: .getParentNode().getParentNode();
136: assertEquals("message-part-group", messagePartGroupElement
137: .getNodeName());
138: assertEquals(ServerMessage.GROUP_ID_UPDATE,
139: messagePartGroupElement.getAttribute("id"));
140:
141: directiveElement = message.appendPartDirective(
142: ServerMessage.GROUP_ID_POSTUPDATE, "Something",
143: "do-something");
144: messagePartGroupElement = (Element) directiveElement
145: .getParentNode().getParentNode();
146: assertEquals("message-part-group", messagePartGroupElement
147: .getNodeName());
148: assertEquals(ServerMessage.GROUP_ID_POSTUPDATE,
149: messagePartGroupElement.getAttribute("id"));
150: }
151: }
|