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.RoleStatus;
035: import javax.management.relation.RoleUnresolved;
036:
037: import junit.framework.TestCase;
038:
039: /**
040: * Role Unresolved tests.<p>
041: *
042: * Test it to death.<p>
043: *
044: * NOTE: The tests use String literals to ensure the comparisons are
045: * not performed on object references.
046: *
047: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
048: */
049: public class RoleUnresolvedTestCase extends TestCase {
050: // Attributes ----------------------------------------------------------------
051:
052: // Constructor ---------------------------------------------------------------
053:
054: /**
055: * Construct the test
056: */
057: public RoleUnresolvedTestCase(String s) {
058: super (s);
059: }
060:
061: // Tests ---------------------------------------------------------------------
062:
063: /**
064: * Basic tests.
065: */
066: public void testBasic() {
067: try {
068: // Create the role Unresolved
069: ArrayList roleValue = new ArrayList();
070: ObjectName a = new ObjectName(":a=a");
071: ObjectName b = new ObjectName(":b=b");
072: roleValue.add(a);
073: roleValue.add(b);
074: RoleUnresolved roleUnresolved = new RoleUnresolved(
075: "RoleName", roleValue, RoleStatus.NO_ROLE_WITH_NAME);
076:
077: // Check the role name
078: assertEquals("RoleName", roleUnresolved.getRoleName());
079:
080: // Check the role value
081: assertEquals(roleValue, roleUnresolved.getRoleValue());
082:
083: // Check the problem type
084: assertEquals(RoleStatus.NO_ROLE_WITH_NAME, roleUnresolved
085: .getProblemType());
086:
087: // Change the role name
088: roleUnresolved.setRoleName("Changed");
089: assertEquals("Changed", roleUnresolved.getRoleName());
090:
091: // Change the role value
092: ArrayList roleValue2 = new ArrayList();
093: ObjectName c = new ObjectName(":c=c");
094: ObjectName d = new ObjectName(":d=d");
095: roleValue2.add(c);
096: roleValue2.add(d);
097: roleUnresolved.setRoleValue(roleValue2);
098:
099: // Check the new role value
100: assertEquals(roleValue2, roleUnresolved.getRoleValue());
101:
102: // Check the problem type
103: roleUnresolved.setProblemType(RoleStatus.ROLE_NOT_READABLE);
104: assertEquals(RoleStatus.ROLE_NOT_READABLE, roleUnresolved
105: .getProblemType());
106: } catch (MalformedObjectNameException mfone) {
107: fail(mfone.toString());
108: }
109: }
110:
111: /**
112: * toString tests.
113: */
114: public void testToString() {
115: try {
116: // Create the role
117: ArrayList roleValue = new ArrayList();
118: ObjectName a = new ObjectName(":a=a");
119: ObjectName b = new ObjectName(":b=b");
120: roleValue.add(a);
121: roleValue.add(b);
122: RoleUnresolved roleUnresolved = new RoleUnresolved("XYZZY",
123: roleValue, RoleStatus.NO_ROLE_WITH_NAME);
124:
125: // Check the human readable string
126: String result = roleUnresolved.toString();
127: if (result.lastIndexOf("XYZZY") == -1)
128: fail("Missing role name in toString");
129: if (result.lastIndexOf(":a=a") == -1)
130: fail("Missing object name :a=a in toString");
131: if (result.lastIndexOf(":b=b") == -1)
132: fail("Missing object name :b=b in toString");
133:
134: // TODO How to test the problem type the string isn't specified?
135: } catch (MalformedObjectNameException mfone) {
136: fail(mfone.toString());
137: }
138: }
139:
140: /**
141: * Test Error Handling.
142: */
143: public void testErrorHandling() {
144: try {
145: // Create the role
146: ArrayList roleValue = new ArrayList();
147: ObjectName a = new ObjectName(":a=a");
148: ObjectName b = new ObjectName(":b=b");
149: roleValue.add(a);
150: roleValue.add(b);
151:
152: // Shouldn't allow null for the name in constructor
153: boolean caught = false;
154: try {
155: new RoleUnresolved(null, roleValue,
156: RoleStatus.NO_ROLE_WITH_NAME);
157: } catch (IllegalArgumentException e) {
158: caught = true;
159: }
160: if (caught == false)
161: fail("Constructor accepts null role name");
162:
163: // Shouldn't allow an invalid problem type
164: caught = false;
165: try {
166: new RoleUnresolved("RoleName", roleValue, -1000);
167: } catch (IllegalArgumentException e) {
168: caught = true;
169: }
170: if (caught == false)
171: fail("Constructor accepts an invalid problem type");
172:
173: RoleUnresolved roleUnresolved = new RoleUnresolved(
174: "RoleName", roleValue, RoleStatus.NO_ROLE_WITH_NAME);
175:
176: // Shouldn't allow null for name
177: caught = false;
178: try {
179: roleUnresolved.setRoleName(null);
180: } catch (IllegalArgumentException e) {
181: caught = true;
182: }
183: if (caught == false)
184: fail("setRoleName accepts null");
185:
186: // Shouldn't allow an invalid problem type
187: caught = false;
188: try {
189: roleUnresolved.setProblemType(-1000);
190: } catch (IllegalArgumentException e) {
191: caught = true;
192: }
193: if (caught == false)
194: fail("setProblemType accepts an invalid problem type");
195: } catch (MalformedObjectNameException mfone) {
196: fail(mfone.toString());
197: }
198: }
199:
200: /**
201: * Test clone.
202: */
203: public void testClone() {
204: // Create the role
205: ArrayList roleValue = new ArrayList();
206: try {
207: roleValue.add(new ObjectName(":a=a"));
208: roleValue.add(new ObjectName(":b=b"));
209: } catch (MalformedObjectNameException mfone) {
210: fail(mfone.toString());
211: }
212: RoleUnresolved roleUnresolved = new RoleUnresolved("RoleName",
213: roleValue, RoleStatus.NO_ROLE_WITH_NAME);
214: RoleUnresolved roleUnresolved2 = (RoleUnresolved) roleUnresolved
215: .clone();
216:
217: // Did it work?
218: assertEquals(roleUnresolved.getRoleName(), roleUnresolved2
219: .getRoleName());
220: assertEquals(roleUnresolved.getRoleValue(), roleUnresolved2
221: .getRoleValue());
222: assertEquals(roleUnresolved.getProblemType(), roleUnresolved2
223: .getProblemType());
224: if (roleUnresolved.getRoleValue() == roleUnresolved2
225: .getRoleValue())
226: fail("RoleUnresolved.clone() didn't clone");
227: }
228:
229: /**
230: * Test serialization.
231: */
232: public void testSerialization() {
233: // Create the role
234: ArrayList roleValue = new ArrayList();
235: try {
236: roleValue.add(new ObjectName(":a=a"));
237: roleValue.add(new ObjectName(":b=b"));
238: } catch (MalformedObjectNameException mfone) {
239: fail(mfone.toString());
240: }
241: RoleUnresolved roleUnresolved = new RoleUnresolved("RoleName",
242: roleValue, RoleStatus.NO_ROLE_WITH_NAME);
243: RoleUnresolved roleUnresolved2 = null;
244:
245: try {
246: // Serialize it
247: ByteArrayOutputStream baos = new ByteArrayOutputStream();
248: ObjectOutputStream oos = new ObjectOutputStream(baos);
249: oos.writeObject(roleUnresolved);
250:
251: // Deserialize it
252: ByteArrayInputStream bais = new ByteArrayInputStream(baos
253: .toByteArray());
254: ObjectInputStream ois = new ObjectInputStream(bais);
255: roleUnresolved2 = (RoleUnresolved) ois.readObject();
256: } catch (IOException ioe) {
257: fail(ioe.toString());
258: } catch (ClassNotFoundException cnfe) {
259: fail(cnfe.toString());
260: }
261:
262: // Did it work?
263: assertEquals(roleUnresolved.getRoleName(), roleUnresolved2
264: .getRoleName());
265: assertEquals(roleUnresolved.getRoleValue(), roleUnresolved2
266: .getRoleValue());
267: }
268: }
|