001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2007 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: */
026:
027: package org.cougaar.util;
028:
029: import java.lang.annotation.Retention;
030: import java.lang.annotation.RetentionPolicy;
031: import java.util.ArrayList;
032: import java.util.List;
033:
034: import junit.framework.TestCase;
035:
036: import org.cougaar.util.annotations.Cougaar;
037: import org.cougaar.util.annotations.Argument;
038:
039: /**
040: * Test field initialization via annotation metatdata
041: *
042: * @author rshapiro
043: *
044: */
045: public class TestAnnotations extends TestCase {
046: private static final String GROUP_NAME = "GroupX";
047: private static final String PARAMS = "SimpleParam=1, SimpleParam=100, ListParam=a, ListParam=b ";
048:
049: @Retention(RetentionPolicy.RUNTIME)
050: public @interface TestArgGroup {
051: Argument.GroupRole role() default Argument.GroupRole.MEMBER;
052:
053: Argument.GroupIterationPolicy policy() default Argument.GroupIterationPolicy.FIRST_UP;
054: }
055:
056: @Cougaar.ArgGroup(role=Argument.GroupRole.OWNER,name=GROUP_NAME)
057: public List<Arguments> groupOwner;
058:
059: @TestArgGroup(role=Argument.GroupRole.OWNER)
060: public List<Arguments> testArgGroupOwner;
061:
062: @Cougaar.Arg(name="SimpleParam")
063: @TestArgGroup()
064: public int simple;
065:
066: @Cougaar.Arg(name="SimpleDefaultedParam",defaultValue="10")
067: public int simpleDefaulted;
068:
069: @Cougaar.Arg(name="SimpleDefaultedNullParam",defaultValue=Cougaar.NULL_VALUE)
070: public String simpleDefaultedNull;
071:
072: @Cougaar.Arg(name="ListParam")
073: @Cougaar.ArgGroup(name=GROUP_NAME)
074: public List<String> list;
075:
076: @Cougaar.Arg(name="DefaultedListParam",defaultValue="[d,e]")
077: public List<String> defaultedList;
078:
079: @Cougaar.Arg(name="DefaultedListNullParam",defaultValue=Cougaar.NULL_VALUE)
080: @Cougaar.ArgGroup(name=GROUP_NAME)
081: public List<String> defaultedListNull;
082:
083: private void setAll(Arguments args) {
084: try {
085: new Argument(args).setAllFields(this );
086: } catch (Exception e) {
087: e.printStackTrace();
088: fail(e.getMessage());
089: }
090: }
091:
092: private void setGroup(List<Arguments> owner, int index) {
093: try {
094: new Argument(owner.get(index)).setFields(this );
095: } catch (Exception e) {
096: e.printStackTrace();
097: fail(e.getMessage());
098: }
099: }
100:
101: protected void setUp() {
102: simple = -1;
103: simpleDefaulted = -1;
104: simpleDefaultedNull = "simpleDefaultedNull";
105: list = null;
106: defaultedList = null;
107: defaultedListNull = new ArrayList<String>();
108: testArgGroupOwner = null;
109: groupOwner = null;
110: }
111:
112: public void test_values() {
113: assertEquals(simple, -1);
114: assertNull(list);
115: Arguments arguments = new Arguments(PARAMS);
116: setAll(arguments);
117: assertEquals(simple, 1);
118: assertNotNull(list);
119: assertEquals(list.size(), 2);
120: assertEquals(list.get(0), "a");
121: assertEquals(list.get(1), "b");
122: }
123:
124: public void test_defaults() {
125: assertEquals(simpleDefaulted, -1);
126: assertEquals(simpleDefaultedNull, "simpleDefaultedNull");
127: assertNull(defaultedList);
128: assertNotNull(defaultedListNull);
129: Arguments arguments = new Arguments(PARAMS);
130: setAll(arguments);
131: assertEquals(simpleDefaulted, 10);
132: assertNull(simpleDefaultedNull);
133: assertNotNull(defaultedList);
134: assertEquals(defaultedList.size(), 2);
135: assertEquals(defaultedList.get(0), "d");
136: assertEquals(defaultedList.get(1), "e");
137: assertNull(defaultedListNull);
138: }
139:
140: public void test_groups() {
141: assertNull(testArgGroupOwner);
142: assertNull(groupOwner);
143: assertNull(list);
144: Arguments arguments = new Arguments(PARAMS);
145: setAll(arguments);
146: assertNotNull(testArgGroupOwner);
147: assertEquals(testArgGroupOwner.size(), 2);
148: assertNotNull(groupOwner);
149: assertEquals(groupOwner.size(), 2);
150: setGroup(testArgGroupOwner, 1);
151: assertEquals(simple, 100);
152: setGroup(testArgGroupOwner, 0);
153: assertEquals(simple, 1);
154:
155: assertNotNull(list);
156: assertEquals(list.size(), 2);
157: setGroup(groupOwner, 1);
158: assertEquals(list.size(), 1);
159: assertEquals(list.get(0), "b");
160: }
161: }
|