001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package test.compliance.relation;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.IOException;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029:
030: import java.util.ArrayList;
031:
032: import javax.management.ObjectName;
033: import javax.management.MalformedObjectNameException;
034: import javax.management.relation.Role;
035:
036: import junit.framework.TestCase;
037:
038: /**
039: * Role tests.<p>
040: *
041: * Test it to death.<p>
042: *
043: * NOTE: The tests use String literals to ensure the comparisons are
044: * not performed on object references.
045: *
046: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
047: */
048: public class RoleTestCase extends TestCase {
049: // Attributes ----------------------------------------------------------------
050:
051: // Constructor ---------------------------------------------------------------
052:
053: /**
054: * Construct the test
055: */
056: public RoleTestCase(String s) {
057: super (s);
058: }
059:
060: // Tests ---------------------------------------------------------------------
061:
062: /**
063: * Basic tests.
064: */
065: public void testBasic() {
066: try {
067: // Create the role
068: ArrayList roleValue = new ArrayList();
069: ObjectName a = new ObjectName(":a=a");
070: ObjectName b = new ObjectName(":b=b");
071: roleValue.add(a);
072: roleValue.add(b);
073: Role role = new Role("RoleName", roleValue);
074:
075: // Check the role name
076: assertEquals("RoleName", role.getRoleName());
077:
078: // Check the role value
079: assertEquals(roleValue, role.getRoleValue());
080:
081: // Change the role name
082: role.setRoleName("Changed");
083: assertEquals("Changed", role.getRoleName());
084:
085: // Change the role value
086: ArrayList roleValue2 = new ArrayList();
087: ObjectName c = new ObjectName(":c=c");
088: ObjectName d = new ObjectName(":d=d");
089: roleValue2.add(c);
090: roleValue2.add(d);
091: role.setRoleValue(roleValue2);
092:
093: // Check the new role value
094: assertEquals(roleValue2, role.getRoleValue());
095: } catch (MalformedObjectNameException mfone) {
096: fail(mfone.toString());
097: }
098: }
099:
100: /**
101: * toString tests.
102: */
103: public void testToString() {
104: try {
105: // Create the role
106: ArrayList roleValue = new ArrayList();
107: ObjectName a = new ObjectName(":a=a");
108: ObjectName b = new ObjectName(":b=b");
109: roleValue.add(a);
110: roleValue.add(b);
111: Role role = new Role("XYZZY", roleValue);
112:
113: // Check the value formatter
114: String result = Role.roleValueToString(roleValue);
115: if (result.lastIndexOf(":a=a") == -1)
116: fail("Missing object name :a=a in roleValueToString");
117: if (result.lastIndexOf(":b=b") == -1)
118: fail("Missing object name :b=b in roleValueToString");
119:
120: // Check the human readable string
121: result = role.toString();
122: if (result.lastIndexOf("XYZZY") == -1)
123: fail("Missing role name in toString");
124: if (result.lastIndexOf(":a=a") == -1)
125: fail("Missing object name :a=a in toString");
126: if (result.lastIndexOf(":b=b") == -1)
127: fail("Missing object name :b=b in toString");
128: } catch (MalformedObjectNameException mfone) {
129: fail(mfone.toString());
130: }
131: }
132:
133: /**
134: * Test Error Handling.
135: */
136: public void testErrorHandling() {
137: try {
138: // Create the role
139: ArrayList roleValue = new ArrayList();
140: ObjectName a = new ObjectName(":a=a");
141: ObjectName b = new ObjectName(":b=b");
142: roleValue.add(a);
143: roleValue.add(b);
144:
145: // Shouldn't allow null for the name in constructor
146: boolean caught = false;
147: try {
148: new Role(null, roleValue);
149: } catch (IllegalArgumentException e) {
150: caught = true;
151: }
152: if (caught == false)
153: fail("Constructor accepts null role name");
154:
155: // Shouldn't allow null for the value in constructor
156: caught = false;
157: try {
158: new Role("RoleName", null);
159: } catch (IllegalArgumentException e) {
160: caught = true;
161: }
162: if (caught == false)
163: fail("Constructor accepts null role value");
164:
165: Role role = new Role("RoleName", roleValue);
166:
167: // Shouldn't allow null for name
168: caught = false;
169: try {
170: role.setRoleName(null);
171: } catch (IllegalArgumentException e) {
172: caught = true;
173: }
174: if (caught == false)
175: fail("setRoleName accepts null");
176:
177: // Shouldn't allow null for value
178: caught = false;
179: try {
180: role.setRoleValue(null);
181: } catch (IllegalArgumentException e) {
182: caught = true;
183: }
184: if (caught == false)
185: fail("setRoleValue accepts null");
186:
187: // Shouldn't allow null for value
188: caught = false;
189: try {
190: Role.roleValueToString(null);
191: } catch (IllegalArgumentException e) {
192: caught = true;
193: }
194: if (caught == false)
195: fail("roleValueToString accepts null");
196: } catch (MalformedObjectNameException mfone) {
197: fail(mfone.toString());
198: }
199: }
200:
201: /**
202: * Test clone.
203: */
204: public void testClone() {
205: // Create the role
206: ArrayList roleValue = new ArrayList();
207: try {
208: roleValue.add(new ObjectName(":a=a"));
209: roleValue.add(new ObjectName(":b=b"));
210: } catch (MalformedObjectNameException mfone) {
211: fail(mfone.toString());
212: }
213: Role role = new Role("RoleName", roleValue);
214: Role role2 = (Role) role.clone();
215:
216: // Did it work?
217: assertEquals(role.getRoleName(), role2.getRoleName());
218: assertEquals(role.getRoleValue(), role2.getRoleValue());
219: if (role.getRoleValue() == role2.getRoleValue())
220: fail("Role.clone() didn't clone");
221: }
222:
223: /**
224: * Test serialization.
225: */
226: public void testSerialization() {
227: // Create the role
228: ArrayList roleValue = new ArrayList();
229: try {
230: roleValue.add(new ObjectName(":a=a"));
231: roleValue.add(new ObjectName(":b=b"));
232: } catch (MalformedObjectNameException mfone) {
233: fail(mfone.toString());
234: }
235: Role role = new Role("RoleName", roleValue);
236: Role role2 = null;
237:
238: try {
239: // Serialize it
240: ByteArrayOutputStream baos = new ByteArrayOutputStream();
241: ObjectOutputStream oos = new ObjectOutputStream(baos);
242: oos.writeObject(role);
243:
244: // Deserialize it
245: ByteArrayInputStream bais = new ByteArrayInputStream(baos
246: .toByteArray());
247: ObjectInputStream ois = new ObjectInputStream(bais);
248: role2 = (Role) ois.readObject();
249: } catch (IOException ioe) {
250: fail(ioe.toString());
251: } catch (ClassNotFoundException cnfe) {
252: fail(cnfe.toString());
253: }
254:
255: // Did it work?
256: assertEquals(role.getRoleName(), role2.getRoleName());
257: assertEquals(role.getRoleValue(), role2.getRoleValue());
258: }
259: }
|