001: /**
002: * <copyright>
003: *
004: * Copyright 2002-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */package org.cougaar.tools.csmart.experiment;
026:
027: import junit.framework.*;
028: import java.io.ByteArrayOutputStream;
029:
030: import org.cougaar.tools.csmart.core.cdata.*;
031: import java.io.IOException;
032: import java.util.Iterator;
033:
034: /**
035: * LeafOnlyConfigWriterTest.java
036: *
037: *
038: * Created: Wed Jan 30 11:46:38 2002
039: *
040: */
041:
042: public class LeafOnlyConfigWriterTest extends TestCase {
043: private LeafOnlyConfigWriter cw = null;
044: private ComponentData society;
045:
046: public LeafOnlyConfigWriterTest(String name) {
047: super (name);
048: }
049:
050: protected void setUp() {
051: society = createComponentData();
052: cw = new LeafOnlyConfigWriter(society);
053: }
054:
055: private ComponentData createComponentData() {
056: ComponentData cd = new GenericComponentData();
057: cd.setType(ComponentData.SOCIETY);
058: cd.setName("TestName");
059: cd.setClassName("TestClass");
060:
061: GenericComponentData node = new GenericComponentData();
062: node.setType(ComponentData.NODE);
063: node.setName("Node");
064: node.setClassName("TestNode");
065: node.addLeafComponent(createDummyLeaf("NodeLeaf"));
066:
067: ComponentData agent1 = new AgentComponentData();
068: agent1.setName("Agent1");
069: agent1.addChild(createPlugin("Plugin1", 4));
070: agent1.addChild(createPlugin("Plugin2", 0));
071: agent1.addChild(createPlugin("Plugin3", 1));
072: agent1.addLeafComponent(createDummyLeaf("Agent1_Leaf"));
073: node.addChild(agent1);
074:
075: AgentComponentData agent2 = new AgentComponentData();
076: agent2.setName("Agent2");
077: agent2.addChild(createPlugin("Plugin1", 0));
078: agent2.addChild(createPlugin("Plugin2", 0));
079: agent2.addChild(createPlugin("Plugin3", 3));
080: node.addChild(agent2);
081:
082: cd.addChild(node);
083:
084: return cd;
085: }
086:
087: private GenericComponentData createPlugin(String name, int leafCnt) {
088: GenericComponentData plugin = new GenericComponentData();
089: plugin.setName(name);
090: plugin.setType(ComponentData.PLUGIN);
091: plugin.setClassName("PluginClass");
092:
093: for (int i = 0; i < leafCnt; i++) {
094: plugin
095: .addLeafComponent(createDummyLeaf(name + "_Leaf_"
096: + i));
097: }
098:
099: return plugin;
100: }
101:
102: private LeafComponentData createDummyLeaf(String name) {
103: GenericLeafComponentData dummy = new GenericLeafComponentData();
104: dummy.setType(LeafComponentData.FILE);
105: dummy.setName(name);
106: dummy.setValue(new String("This is the contents of the " + name
107: + " leaf"));
108: return dummy;
109: }
110:
111: /**
112: * Tests finding the leaf component on a Node.
113: * (Can a node even have a leaf component?)
114: *
115: */
116: // public void testNodeLeaf() {
117: // ByteArrayOutputStream fos = new ByteArrayOutputStream();
118: // try {
119: // cw.writeFile("NodeLeaf_Leaf", fos);
120: // } catch(Exception e) {
121: // fail("Caught Unexpected Exception" + e);
122: // }
123: // String contents = fos.toString();
124: // assertEquals("Test Node Leaf",
125: // "This is the contents of the NodeLeaf_Leaf leaf",
126: // contents);
127: // try {
128: // fos.close();
129: // } catch(IOException ioe) {
130: // fail("Caught Unexpected Excpetion" + ioe);
131: // }
132: // }
133:
134: public void testAgentLeaf() {
135: ByteArrayOutputStream fos = new ByteArrayOutputStream();
136:
137: try {
138: cw.writeFile("Agent1_Leaf", fos);
139: } catch (Exception e) {
140: fail("Caught Unexpected Exception" + e);
141: }
142:
143: String contents = fos.toString();
144: assertEquals("Test Node Leaf",
145: "This is the contents of the Agent1_Leaf leaf",
146: contents);
147:
148: try {
149: fos.close();
150: } catch (IOException ioe) {
151: fail("Caught Unexpected Excpetion" + ioe);
152: }
153:
154: }
155:
156: public void testPluginLeaf() {
157: ByteArrayOutputStream fos = new ByteArrayOutputStream();
158: Iterator iter = cw.getFileNames();
159:
160: try {
161: cw.writeFile("Plugin3_Leaf_1", fos);
162: } catch (Exception e) {
163: fail("Caught Unexpected Exception" + e);
164: }
165:
166: String contents = fos.toString();
167: assertEquals("Test Node Leaf",
168: "This is the contents of the Plugin3_Leaf_1 leaf",
169: contents);
170:
171: try {
172: fos.close();
173: } catch (IOException ioe) {
174: fail("Caught Unexpected Excpetion" + ioe);
175: }
176: }
177:
178: }// LeafOnlyConfigWriterTest
|