KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webrender > test > ServerMessageTest


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webrender.test;
31
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34
35 import nextapp.echo2.webrender.ServerMessage;
36 import junit.framework.TestCase;
37
38 /**
39  * Unit tests for <code>ServerMessage</code>.
40  */

41 public class ServerMessageTest extends TestCase {
42     
43     /**
44      * Test <code>addLibrary()</code>.
45      */

46     public void testAddLibrary() {
47         NodeList JavaDoc libraryNodeList;
48         ServerMessage message = new ServerMessage();
49         
50         message.addLibrary("service1");
51         libraryNodeList = message.getDocument().getElementsByTagName("library");
52         assertEquals(1, libraryNodeList.getLength());
53         
54         message.addLibrary("service2");
55         libraryNodeList = message.getDocument().getElementsByTagName("library");
56         assertEquals(2, libraryNodeList.getLength());
57         
58         message.addLibrary("service1");
59         libraryNodeList = message.getDocument().getElementsByTagName("library");
60         assertEquals(2, libraryNodeList.getLength());
61     }
62     
63     /**
64      * Test <code>appendPartDirective()</code>.
65      */

66     public void testAppendPartDirective() {
67         ServerMessage message = new ServerMessage();
68         message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, "DomUpdate", "dom-add");
69         message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, "DomUpdate", "dom-remove");
70         message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, "DomUpdate", "dom-add");
71         message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, "SomethingElse", "thing");
72         message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, "DomUpdate", "dom-remove");
73         message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, "DomUpdate", "dom-add");
74         assertEquals(3, message.getDocument().getElementsByTagName("message-part").getLength());
75     }
76
77     /**
78      * Test itemized directive capability.
79      */

80     public void testItemizedDirective() {
81         ServerMessage message = new ServerMessage();
82         String JavaDoc[] keyAttributeNames = new String JavaDoc[]{"alpha", "bravo"};
83         
84         Element JavaDoc directiveElement1 = message.getItemizedDirective(ServerMessage.GROUP_ID_POSTUPDATE, "Processor", "Directive",
85                 keyAttributeNames, new String JavaDoc[]{"alpha1", "bravo1"});
86         assertEquals("Directive", directiveElement1.getNodeName());
87         assertEquals("alpha1", directiveElement1.getAttribute("alpha"));
88         assertEquals("bravo1", directiveElement1.getAttribute("bravo"));
89         
90         Element JavaDoc directiveElement2 = message.getItemizedDirective(ServerMessage.GROUP_ID_POSTUPDATE, "Processor", "Directive",
91                 keyAttributeNames, new String JavaDoc[]{"alpha1", "bravo1"});
92         assertEquals("Directive", directiveElement2.getNodeName());
93         assertEquals("alpha1", directiveElement2.getAttribute("alpha"));
94         assertEquals("bravo1", directiveElement2.getAttribute("bravo"));
95         
96         assertTrue(directiveElement1.equals(directiveElement2));
97
98         Element JavaDoc directiveElement3 = message.getItemizedDirective(ServerMessage.GROUP_ID_POSTUPDATE, "Processor", "Directive",
99                 keyAttributeNames, new String JavaDoc[]{"alpha2", "bravo2"});
100         assertEquals("Directive", directiveElement3.getNodeName());
101         assertEquals("alpha2", directiveElement3.getAttribute("alpha"));
102         assertEquals("bravo2", directiveElement3.getAttribute("bravo"));
103         
104         assertFalse(directiveElement1.equals(directiveElement3));
105         assertFalse(directiveElement2.equals(directiveElement3));
106     }
107     
108     /**
109      * Test group identifiers.
110      */

111     public void testGroupIds() {
112         ServerMessage message = new ServerMessage();
113         Element JavaDoc directiveElement, messagePartGroupElement;
114
115         directiveElement = message.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, "Something", "do-something");
116         messagePartGroupElement = (Element JavaDoc) directiveElement.getParentNode().getParentNode();
117         assertEquals("message-part-group", messagePartGroupElement.getNodeName());
118         assertEquals(ServerMessage.GROUP_ID_UPDATE, messagePartGroupElement.getAttribute("id"));
119         
120         directiveElement = message.appendPartDirective(ServerMessage.GROUP_ID_POSTUPDATE, "Something", "do-something");
121         messagePartGroupElement = (Element JavaDoc) directiveElement.getParentNode().getParentNode();
122         assertEquals("message-part-group", messagePartGroupElement.getNodeName());
123         assertEquals(ServerMessage.GROUP_ID_POSTUPDATE, messagePartGroupElement.getAttribute("id"));
124     }
125 }
126
Popular Tags