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 java.io.File;
028: import java.lang.IllegalArgumentException;
029: import java.lang.IndexOutOfBoundsException;
030: import java.util.List;
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034: import org.cougaar.tools.csmart.recipe.RecipeBase;
035: import org.cougaar.tools.csmart.recipe.RecipeComponent;
036: import org.cougaar.tools.csmart.society.SocietyBase;
037: import org.cougaar.tools.csmart.society.SocietyComponent;
038:
039: /**
040: * Unit Test for classExperiment
041: *
042: *
043: * Created: Fri Mar 22 13:30:26 2002
044: *
045: */
046: public class ExperimentTest extends TestCase {
047:
048: private Experiment experiment = null;
049:
050: /**
051: * Creates a new <code>ExperimentTest</code> instance.
052: *
053: * @param name test name
054: */
055: public ExperimentTest(String name) {
056: super (name);
057: }
058:
059: protected void setUp() {
060: experiment = new DBExperiment("testExperiment");
061: }
062:
063: public void testSocietyOperations() {
064: SocietyComponent sc = new SimpleSociety("Testing");
065: assertEquals("Test getSocietyComponentCount", 0, experiment
066: .getSocietyComponentCount());
067:
068: experiment.addSocietyComponent(sc);
069:
070: // Try to add a second one.
071: try {
072: experiment.addSocietyComponent(sc);
073: fail("Expected IllegalArgumentException");
074: } catch (IllegalArgumentException ee) {
075: }
076:
077: assertEquals("Test getSocietyComponentCount", 1, experiment
078: .getSocietyComponentCount());
079: assertEquals("Test getSocietyComponent", sc, experiment
080: .getSocietyComponent());
081: experiment.removeSocietyComponent();
082: assertEquals("Test remove success", 0, experiment
083: .getSocietyComponentCount());
084: }
085:
086: public void testRecipeOperations() {
087: RecipeComponent rc = new SimpleRecipe("Testing");
088: RecipeComponent rc1 = new SimpleRecipe("Testing1");
089: RecipeComponent rc2 = new SimpleRecipe("Testing2");
090:
091: assertEquals("Test getRecipeCount", 0, experiment
092: .getRecipeComponentCount());
093:
094: experiment.addRecipeComponent(rc);
095:
096: // Try to add a second one, of same name.
097: try {
098: experiment.addRecipeComponent(rc);
099: fail("Expected IllegalArgumentException");
100: } catch (IllegalArgumentException ee) {
101: }
102:
103: assertEquals("Test getRecipeCount", 1, experiment
104: .getRecipeComponentCount());
105: assertEquals("Test getRecipe(int)", rc, experiment
106: .getRecipeComponent(0));
107:
108: // Try boundries
109: try {
110: experiment.getRecipeComponent(-1);
111: fail("Expected IndexOutOfBoundsException");
112: } catch (IndexOutOfBoundsException ee) {
113: }
114:
115: try {
116: experiment.getRecipeComponent(5);
117: fail("Expected IndexOutOfBoundsException");
118: } catch (IndexOutOfBoundsException ee) {
119: }
120:
121: experiment.removeRecipeComponent(rc);
122: assertEquals("Test Remove", 0, experiment
123: .getRecipeComponentCount());
124:
125: RecipeComponent[] all = new RecipeComponent[3];
126: all[0] = rc;
127: all[1] = rc1;
128: all[2] = rc2;
129: experiment.setRecipeComponents(all);
130: assertEquals("Test setRecipes", 3, experiment
131: .getRecipeComponentCount());
132:
133: RecipeComponent[] dump = experiment.getRecipeComponents();
134: assertEquals("Test getRecipes 1", all[0], dump[0]);
135: assertEquals("Test getRecipes 2", all[1], dump[1]);
136: assertEquals("Test getRecipes 3", all[2], dump[2]);
137: }
138:
139: public void testGenericComponentOperations() {
140: RecipeComponent rc = new SimpleRecipe("r1");
141: RecipeComponent rc2 = new SimpleRecipe("r2");
142: SocietyComponent sc = new SimpleSociety("s");
143:
144: experiment.addComponent(rc);
145: assertEquals("Test Successful recipe add", 1, experiment
146: .getRecipeComponentCount());
147: experiment.addComponent(sc);
148: assertEquals("Test Successful society add", 1, experiment
149: .getSocietyComponentCount());
150: assertEquals("Test getComponentCount", 2, experiment
151: .getComponentCount());
152: experiment.removeComponent(rc);
153: assertEquals("Test Successful recipe add", 0, experiment
154: .getRecipeComponentCount());
155:
156: }
157:
158: public void testRunInProgress() {
159: experiment.setRunInProgress(true);
160: assertEquals("Test Run In Progress", true, experiment
161: .isRunInProgress());
162: }
163:
164: public void testEditInProgress() {
165: experiment.setEditInProgress(true);
166: assertEquals("Test Edit In Progress", true, experiment
167: .isEditInProgress());
168: }
169:
170: public void testResultDirectory() {
171: experiment.setResultDirectory(new File("Test"));
172: assertEquals("Test ResultDirectory", new File("Test"),
173: experiment.getResultDirectory());
174: }
175:
176: public void testHostComponent() {
177: HostComponent h = experiment.addHost("New Host");
178:
179: // Try to add a second one, of same name.
180: try {
181: experiment.addHost("New Host");
182: fail("addHost: Expected IllegalArgumentException");
183: } catch (IllegalArgumentException ee) {
184: }
185:
186: try {
187: experiment.renameHost(h, "New Host");
188: fail("renameHost: Expected IllegalArgumentException");
189: } catch (IllegalArgumentException ee) {
190: }
191:
192: experiment.renameHost(h, "New Name");
193: HostComponent[] hosts = experiment.getHostComponents();
194: assertEquals("Test get hosts", h, hosts[0]);
195:
196: experiment.removeHost(h);
197: hosts = experiment.getHostComponents();
198: assertEquals("Test Remove Host", 0, hosts.length);
199:
200: }
201:
202: public void testNodeComponent() {
203: NodeComponent n = experiment.addNode("New Node");
204:
205: // Try to add a second one, of same name.
206: try {
207: experiment.addNode("New Node");
208: fail("addNode: Expected IllegalArgumentException");
209: } catch (IllegalArgumentException ee) {
210: }
211:
212: try {
213: experiment.renameNode(n, "New Node");
214: fail("renameNode: Expected IllegalArgumentException");
215: } catch (IllegalArgumentException ee) {
216: }
217:
218: experiment.renameNode(n, "New Name");
219: NodeComponent[] nodes = experiment.getNodeComponents();
220: assertEquals("Test get nodes", n, nodes[0]);
221:
222: experiment.removeNode(n);
223: nodes = experiment.getNodeComponents();
224: assertEquals("Test Remove Node", 0, nodes.length);
225:
226: }
227:
228: /**
229: * @return a <code>TestSuite</code>
230: */
231: public static TestSuite suite() {
232: return new TestSuite(ExperimentTest.class);
233: }
234:
235: /**
236: * Entry point
237: */
238: public static void main(String[] args) {
239: junit.textui.TestRunner.run(suite());
240: }
241:
242: public class SimpleSociety extends SocietyBase {
243:
244: public SimpleSociety(String name) {
245: super (name);
246: }
247:
248: public void initProperties() {
249: }
250: }
251:
252: public class SimpleRecipe extends RecipeBase {
253:
254: public SimpleRecipe(String name) {
255: super (name);
256: }
257:
258: public void initProperties() {
259: }
260: }
261:
262: }// ExperimentTest
|