001: /**
002: * <copyright>
003: *
004: * Copyright 2001-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.core.property.name;
026:
027: import junit.framework.*;
028:
029: /**
030: * Unit test for class MultiName.
031: */
032: public class MultiNameTest extends TestCase {
033:
034: private SimpleMultiName tst = null;
035: private NoParentMultiName noParent = null;
036: private String componentName = "Child";
037: private String noParentName = "NoParent";
038:
039: public MultiNameTest(String name) {
040: super (name);
041: }
042:
043: protected void setUp() {
044: tst = new SimpleMultiName(componentName);
045: noParent = new NoParentMultiName(noParentName);
046: }
047:
048: protected void tearDown() {
049: }
050:
051: public void testSize() {
052: assertEquals("Test size() [2]", 2, tst.size());
053: assertEquals("Test size() [1]", 1, noParent.size());
054: }
055:
056: public void testGet() {
057: assertEquals("Test get()", new SimpleName("Parent"), tst.get(0));
058: assertEquals("Test get()", new SimpleName(componentName), tst
059: .get(1));
060:
061: // Test Exceptions
062: try {
063: tst.get(15);
064: fail("IllegalArgumentException Expected");
065: } catch (IllegalArgumentException e) {
066: }
067:
068: try {
069: tst.get(-5);
070: fail("IllegalArgumentException Expected");
071: } catch (IllegalArgumentException e) {
072: }
073: }
074:
075: public void testGetPrefix() {
076: assertEquals("Test getPrefix()", new SimpleName("Parent"), tst
077: .getPrefix());
078: assertNull(noParent.getPrefix());
079: }
080:
081: public void testGetPrefixN() {
082:
083: assertEquals("Test getPrefix(int)", new SimpleName("Parent"),
084: tst.getPrefix(0));
085:
086: // Test Exceptions
087: try {
088: tst.getPrefix(15);
089: fail("IllegalArgumentException Expected");
090: } catch (IllegalArgumentException e) {
091: }
092:
093: try {
094: tst.getPrefix(-5);
095: fail("IllegalArgumentException Expected");
096: } catch (IllegalArgumentException e) {
097: }
098: }
099:
100: public void testLast() {
101: assertEquals("Test last()", new SimpleName(componentName), tst
102: .last());
103: }
104:
105: public void testEndsWith() {
106: assertTrue("Test endsWith()", tst.endsWith(new SimpleName(
107: componentName)));
108: }
109:
110: public void testStartsWith() {
111: assertTrue("Test starsWith()", tst.startsWith(new SimpleName(
112: "Parent")));
113: }
114:
115: public void testEquals() {
116: assertTrue("Test equals()", tst.equals(tst));
117: }
118:
119: public void testCompareTo() {
120: assertTrue("Test compareTo() [0]", (tst
121: .compareTo(new SimpleMultiName(componentName)) == 0));
122: SimpleName sn = new SimpleName("Testing");
123: assertTrue("Test compareTo() [>0]", (tst
124: .compareTo(new SimpleMultiName(sn)) > 0));
125: assertTrue("Test compareTo() [<0]", (tst
126: .compareTo(new SimpleName("a")) < 0));
127: }
128:
129: public static Test suite() {
130: return new TestSuite(MultiNameTest.class);
131: }
132:
133: public static void main(String[] args) {
134: junit.textui.TestRunner.run(suite());
135: }
136:
137: public class SimpleMultiName extends MultiName {
138: private SimpleName parent = null;
139:
140: public SimpleMultiName(String name) {
141: super (new SimpleName(name));
142: parent = new SimpleName("Parent");
143: }
144:
145: public SimpleMultiName(SimpleName name) {
146: super (name);
147: parent = new SimpleName("Different Name");
148: }
149:
150: protected CompositeName getParentName() {
151: return parent;
152: }
153: }
154:
155: public class NoParentMultiName extends MultiName {
156: public NoParentMultiName(String name) {
157: super (new SimpleName(name));
158: }
159:
160: protected CompositeName getParentName() {
161: return null;
162: }
163: }
164:
165: }
|