KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > relation > RoleUnresolvedTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jmx.compliance.relation;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.ObjectInputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.relation.RoleStatus JavaDoc;
34 import javax.management.relation.RoleUnresolved JavaDoc;
35
36 import junit.framework.TestCase;
37
38 /**
39  * Role Unresolved tests.<p>
40  *
41  * Test it to death.<p>
42  *
43  * NOTE: The tests use String literals to ensure the comparisons are
44  * not performed on object references.
45  *
46  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
47  */

48 public class RoleUnresolvedTestCase
49   extends TestCase
50 {
51   // Attributes ----------------------------------------------------------------
52

53   // Constructor ---------------------------------------------------------------
54

55   /**
56    * Construct the test
57    */

58   public RoleUnresolvedTestCase(String JavaDoc s)
59   {
60     super(s);
61   }
62
63   // Tests ---------------------------------------------------------------------
64

65   /**
66    * Basic tests.
67    */

68   public void testBasic()
69   {
70     try
71     {
72       // Create the role Unresolved
73
ArrayList JavaDoc roleValue = new ArrayList JavaDoc();
74       ObjectName JavaDoc a = new ObjectName JavaDoc(":a=a");
75       ObjectName JavaDoc b = new ObjectName JavaDoc(":b=b");
76       roleValue.add(a);
77       roleValue.add(b);
78       RoleUnresolved JavaDoc roleUnresolved = new RoleUnresolved JavaDoc("RoleName", roleValue,
79                                              RoleStatus.NO_ROLE_WITH_NAME);
80
81       // Check the role name
82
assertEquals("RoleName", roleUnresolved.getRoleName());
83
84       // Check the role value
85
assertEquals(roleValue, roleUnresolved.getRoleValue());
86
87       // Check the problem type
88
assertEquals(RoleStatus.NO_ROLE_WITH_NAME, roleUnresolved.getProblemType());
89
90       // Change the role name
91
roleUnresolved.setRoleName("Changed");
92       assertEquals("Changed", roleUnresolved.getRoleName());
93
94       // Change the role value
95
ArrayList JavaDoc roleValue2 = new ArrayList JavaDoc();
96       ObjectName JavaDoc c = new ObjectName JavaDoc(":c=c");
97       ObjectName JavaDoc d = new ObjectName JavaDoc(":d=d");
98       roleValue2.add(c);
99       roleValue2.add(d);
100       roleUnresolved.setRoleValue(roleValue2);
101
102       // Check the new role value
103
assertEquals(roleValue2, roleUnresolved.getRoleValue());
104
105       // Check the problem type
106
roleUnresolved.setProblemType(RoleStatus.ROLE_NOT_READABLE);
107       assertEquals(RoleStatus.ROLE_NOT_READABLE, roleUnresolved.getProblemType());
108     }
109     catch (MalformedObjectNameException JavaDoc mfone)
110     {
111       fail(mfone.toString());
112     }
113   }
114
115   /**
116    * toString tests.
117    */

118   public void testToString()
119   {
120     try
121     {
122       // Create the role
123
ArrayList JavaDoc roleValue = new ArrayList JavaDoc();
124       ObjectName JavaDoc a = new ObjectName JavaDoc(":a=a");
125       ObjectName JavaDoc b = new ObjectName JavaDoc(":b=b");
126       roleValue.add(a);
127       roleValue.add(b);
128       RoleUnresolved JavaDoc roleUnresolved = new RoleUnresolved JavaDoc("XYZZY", roleValue,
129                                              RoleStatus.NO_ROLE_WITH_NAME);
130
131       // Check the human readable string
132
String JavaDoc result = roleUnresolved.toString();
133       if (result.lastIndexOf("XYZZY") == -1)
134         fail("Missing role name in toString");
135       if (result.lastIndexOf(":a=a") == -1)
136         fail("Missing object name :a=a in toString");
137       if (result.lastIndexOf(":b=b") == -1)
138         fail("Missing object name :b=b in toString");
139
140       // TODO How to test the problem type the string isn't specified?
141
}
142     catch (MalformedObjectNameException JavaDoc mfone)
143     {
144       fail(mfone.toString());
145     }
146   }
147
148   /**
149    * Test Error Handling.
150    */

151   public void testErrorHandling()
152   {
153     try
154     {
155       // Create the role
156
ArrayList JavaDoc roleValue = new ArrayList JavaDoc();
157       ObjectName JavaDoc a = new ObjectName JavaDoc(":a=a");
158       ObjectName JavaDoc b = new ObjectName JavaDoc(":b=b");
159       roleValue.add(a);
160       roleValue.add(b);
161
162       // Shouldn't allow null for the name in constructor
163
boolean caught = false;
164       try
165       {
166         new RoleUnresolved JavaDoc(null, roleValue, RoleStatus.NO_ROLE_WITH_NAME);
167       }
168       catch (IllegalArgumentException JavaDoc e)
169       {
170         caught = true;
171       }
172       if (caught == false)
173         fail ("Constructor accepts null role name");
174
175       // Shouldn't allow an invalid problem type
176
caught = false;
177       try
178       {
179         new RoleUnresolved JavaDoc("RoleName", roleValue, -1000);
180       }
181       catch (IllegalArgumentException JavaDoc e)
182       {
183         caught = true;
184       }
185       if (caught == false)
186         fail ("Constructor accepts an invalid problem type");
187
188       RoleUnresolved JavaDoc roleUnresolved = new RoleUnresolved JavaDoc("RoleName", roleValue,
189                                            RoleStatus.NO_ROLE_WITH_NAME);
190
191       // Shouldn't allow null for name
192
caught = false;
193       try
194       {
195         roleUnresolved.setRoleName(null);
196       }
197       catch (IllegalArgumentException JavaDoc e)
198       {
199         caught = true;
200       }
201       if (caught == false)
202         fail ("setRoleName accepts null");
203
204       // Shouldn't allow an invalid problem type
205
caught = false;
206       try
207       {
208         roleUnresolved.setProblemType(-1000);
209       }
210       catch (IllegalArgumentException JavaDoc e)
211       {
212         caught = true;
213       }
214       if (caught == false)
215         fail ("setProblemType accepts an invalid problem type");
216     }
217     catch (MalformedObjectNameException JavaDoc mfone)
218     {
219       fail(mfone.toString());
220     }
221   }
222
223   /**
224    * Test clone.
225    */

226   public void testClone()
227   {
228     // Create the role
229
ArrayList JavaDoc roleValue = new ArrayList JavaDoc();
230     try
231     {
232       roleValue.add(new ObjectName JavaDoc(":a=a"));
233       roleValue.add(new ObjectName JavaDoc(":b=b"));
234     }
235     catch (MalformedObjectNameException JavaDoc mfone)
236     {
237       fail(mfone.toString());
238     }
239     RoleUnresolved JavaDoc roleUnresolved = new RoleUnresolved JavaDoc("RoleName", roleValue,
240                                           RoleStatus.NO_ROLE_WITH_NAME);
241     RoleUnresolved JavaDoc roleUnresolved2 = (RoleUnresolved JavaDoc) roleUnresolved.clone();
242
243     // Did it work?
244
assertEquals(roleUnresolved.getRoleName(), roleUnresolved2.getRoleName());
245     assertEquals(roleUnresolved.getRoleValue(), roleUnresolved2.getRoleValue());
246     assertEquals(roleUnresolved.getProblemType(), roleUnresolved2.getProblemType());
247     if(roleUnresolved.getRoleValue() == roleUnresolved2.getRoleValue())
248       fail("RoleUnresolved.clone() didn't clone");
249   }
250
251   /**
252    * Test serialization.
253    */

254   public void testSerialization()
255   {
256     // Create the role
257
ArrayList JavaDoc roleValue = new ArrayList JavaDoc();
258     try
259     {
260       roleValue.add(new ObjectName JavaDoc(":a=a"));
261       roleValue.add(new ObjectName JavaDoc(":b=b"));
262     }
263     catch (MalformedObjectNameException JavaDoc mfone)
264     {
265       fail(mfone.toString());
266     }
267     RoleUnresolved JavaDoc roleUnresolved = new RoleUnresolved JavaDoc("RoleName", roleValue,
268                                           RoleStatus.NO_ROLE_WITH_NAME);
269     RoleUnresolved JavaDoc roleUnresolved2 = null;
270
271     try
272     {
273       // Serialize it
274
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
275       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
276       oos.writeObject(roleUnresolved);
277     
278       // Deserialize it
279
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
280       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
281       roleUnresolved2 = (RoleUnresolved JavaDoc) ois.readObject();
282     }
283     catch (IOException JavaDoc ioe)
284     {
285       fail(ioe.toString());
286     }
287     catch (ClassNotFoundException JavaDoc cnfe)
288     {
289       fail(cnfe.toString());
290     }
291
292     // Did it work?
293
assertEquals(roleUnresolved.getRoleName(), roleUnresolved2.getRoleName());
294     assertEquals(roleUnresolved.getRoleValue(), roleUnresolved2.getRoleValue());
295   }
296 }
297
Popular Tags