KickJava   Java API By Example, From Geeks To Geeks.

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


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.relation.InvalidRelationTypeException JavaDoc;
32 import javax.management.relation.RelationSupport JavaDoc;
33 import javax.management.relation.RelationTypeSupport JavaDoc;
34 import javax.management.relation.RoleInfo JavaDoc;
35 import javax.management.relation.RoleInfoNotFoundException JavaDoc;
36
37 import junit.framework.TestCase;
38
39 /**
40  * Relation Type Support tests
41  *
42  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
43  */

44 public class RelationTypeSupportTestCase
45   extends TestCase
46 {
47
48   // Attributes ----------------------------------------------------------------
49

50   // Constructor ---------------------------------------------------------------
51

52   /**
53    * Construct the test
54    */

55   public RelationTypeSupportTestCase(String JavaDoc s)
56   {
57     super(s);
58   }
59
60   // Tests ---------------------------------------------------------------------
61

62   /**
63    * Basic tests
64    */

65   public void testBasic()
66   {
67     RoleInfo JavaDoc roleInfo1 = null;
68     RoleInfo JavaDoc roleInfo2 = null;
69     RoleInfo JavaDoc[] roleInfos = null;
70     RelationTypeSupport JavaDoc support = null;
71     try
72     {
73       roleInfo1 = new RoleInfo JavaDoc("roleInfo1", RelationSupport JavaDoc.class.getName());
74       roleInfo2 = new RoleInfo JavaDoc("roleInfo2", RelationSupport JavaDoc.class.getName());
75       roleInfos = new RoleInfo JavaDoc[] { roleInfo1, roleInfo2 };
76       support = new RelationTypeSupport JavaDoc("name", roleInfos);
77     }
78     catch (Exception JavaDoc e)
79     {
80       fail(e.toString());
81     }
82
83     // Check the name
84
assertEquals("name", support.getRelationTypeName());
85
86     // Check the roleInfos
87
ArrayList JavaDoc result = (ArrayList JavaDoc) support.getRoleInfos();
88     assertEquals(2, result.size());
89
90     // Check get
91
try
92     {
93       assertEquals(roleInfo1.toString(), support.getRoleInfo("roleInfo1").toString());
94       assertEquals(roleInfo2.toString(), support.getRoleInfo("roleInfo2").toString());
95     }
96     catch (Exception JavaDoc e)
97     {
98       fail(e.toString());
99     }
100
101     // Check the protected constructor
102
MyRelationTypeSupport mySupport = new MyRelationTypeSupport("myName");
103
104     // Did it work?
105
assertEquals("myName", mySupport.getRelationTypeName());
106     result = (ArrayList JavaDoc) mySupport.getRoleInfos();
107     assertEquals(0, result.size());
108
109     // Add a role info
110
try
111     {
112       mySupport.addRoleInfo(roleInfo1);
113       result = (ArrayList JavaDoc) mySupport.getRoleInfos();
114       assertEquals(1, result.size());
115     }
116     catch (Exception JavaDoc e)
117     {
118       fail(e.toString());
119     }
120   }
121
122   /**
123    * Error handling
124    */

125   public void testErrorHandling()
126   {
127     RoleInfo JavaDoc roleInfo1 = null;
128     RoleInfo JavaDoc roleInfo2 = null;
129     RoleInfo JavaDoc[] roleInfos = null;
130     RelationTypeSupport JavaDoc support = null;
131
132     boolean caught = false;
133     try
134     {
135       roleInfo1 = new RoleInfo JavaDoc("roleInfo1", RelationSupport JavaDoc.class.getName());
136       roleInfo2 = new RoleInfo JavaDoc("roleInfo2", RelationSupport JavaDoc.class.getName());
137       roleInfos = new RoleInfo JavaDoc[] { roleInfo1, roleInfo2 };
138       support = new RelationTypeSupport JavaDoc(null, roleInfos);
139     }
140     catch (IllegalArgumentException JavaDoc e)
141     {
142       caught = true;
143     }
144     catch (Exception JavaDoc e)
145     {
146       fail(e.toString());
147     }
148     if (caught == false)
149       fail("Constructor accepts null relation type name");
150
151     caught = false;
152     try
153     {
154       support = new RelationTypeSupport JavaDoc("name", null);
155     }
156     catch (IllegalArgumentException JavaDoc e)
157     {
158       caught = true;
159     }
160     catch (Exception JavaDoc e)
161     {
162       fail(e.toString());
163     }
164     if (caught == false)
165       fail("Constructor accepts null role infos");
166
167     caught = false;
168     try
169     {
170       support = new RelationTypeSupport JavaDoc("name", new RoleInfo JavaDoc[0]);
171     }
172     catch (InvalidRelationTypeException JavaDoc e)
173     {
174       caught = true;
175     }
176     catch (Exception JavaDoc e)
177     {
178       fail(e.toString());
179     }
180     if (caught == false)
181       fail("Constructor accepts no role infos");
182
183     caught = false;
184     try
185     {
186       roleInfo1 = new RoleInfo JavaDoc("roleInfo1", RelationSupport JavaDoc.class.getName());
187       roleInfos = new RoleInfo JavaDoc[] { roleInfo1, null };
188       support = new RelationTypeSupport JavaDoc("name", roleInfos);
189     }
190     catch (InvalidRelationTypeException JavaDoc e)
191     {
192       caught = true;
193     }
194     catch (Exception JavaDoc e)
195     {
196       fail(e.toString());
197     }
198     if (caught == false)
199       fail("Constructor accepts null role");
200
201     caught = false;
202     try
203     {
204       roleInfo1 = new RoleInfo JavaDoc("roleInfo1", RelationSupport JavaDoc.class.getName());
205       roleInfos = new RoleInfo JavaDoc[] { roleInfo1 };
206       support = new RelationTypeSupport JavaDoc("name", roleInfos);
207       support.getRoleInfo(null);
208     }
209     catch (IllegalArgumentException JavaDoc e)
210     {
211       caught = true;
212     }
213     catch (Exception JavaDoc e)
214     {
215       fail(e.toString());
216     }
217     if (caught == false)
218       fail("getRoleInfo allows a null role info name");
219
220     caught = false;
221     try
222     {
223       roleInfo1 = new RoleInfo JavaDoc("roleInfo1", RelationSupport JavaDoc.class.getName());
224       roleInfos = new RoleInfo JavaDoc[] { roleInfo1 };
225       support = new RelationTypeSupport JavaDoc("name", roleInfos);
226       support.getRoleInfo("rubbish");
227     }
228     catch (RoleInfoNotFoundException JavaDoc e)
229     {
230       caught = true;
231     }
232     catch (Exception JavaDoc e)
233     {
234       fail(e.toString());
235     }
236     if (caught == false)
237       fail("getRoleInfo returns a not existent role info");
238
239     // Check the protected addRoleInfo
240
MyRelationTypeSupport mySupport = new MyRelationTypeSupport("myName");
241
242     caught = false;
243     try
244     {
245       mySupport.addRoleInfo(null);
246     }
247     catch (IllegalArgumentException JavaDoc e)
248     {
249       caught = true;
250     }
251     catch (Exception JavaDoc e)
252     {
253       fail(e.toString());
254     }
255     if (caught == false)
256       fail("addRoleInfo accepts null");
257
258     caught = false;
259     try
260     {
261       mySupport.addRoleInfo(roleInfo1);
262       mySupport.addRoleInfo(roleInfo1);
263     }
264     catch (InvalidRelationTypeException JavaDoc e)
265     {
266       caught = true;
267     }
268     catch (Exception JavaDoc e)
269     {
270       fail(e.toString());
271     }
272     if (caught == false)
273       fail("addRoleInfo accepts duplicates role infos");
274   }
275
276   /**
277    * Test serialization.
278    */

279   public void testSerialization()
280   {
281     // Create the relationt type support
282
RoleInfo JavaDoc roleInfo1 = null;
283     RoleInfo JavaDoc roleInfo2 = null;
284     RoleInfo JavaDoc[] roleInfos = null;
285     RelationTypeSupport JavaDoc support = null;
286     try
287     {
288       roleInfo1 = new RoleInfo JavaDoc("roleInfo1", RelationSupport JavaDoc.class.getName());
289       roleInfo2 = new RoleInfo JavaDoc("roleInfo2", RelationSupport JavaDoc.class.getName());
290       roleInfos = new RoleInfo JavaDoc[] { roleInfo1, roleInfo2 };
291       support = new RelationTypeSupport JavaDoc("name", roleInfos);
292     }
293     catch (Exception JavaDoc e)
294     {
295       fail(e.toString());
296     }
297     RelationTypeSupport JavaDoc support2 = null;
298
299     try
300     {
301       // Serialize it
302
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
303       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
304       oos.writeObject(support);
305     
306       // Deserialize it
307
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
308       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
309       support2 = (RelationTypeSupport JavaDoc) ois.readObject();
310     }
311     catch (IOException JavaDoc ioe)
312     {
313       fail(ioe.toString());
314     }
315     catch (ClassNotFoundException JavaDoc cnfe)
316     {
317       fail(cnfe.toString());
318     }
319
320     // Did it work?
321
assertEquals("name", support2.getRelationTypeName());
322     ArrayList JavaDoc result = (ArrayList JavaDoc) support2.getRoleInfos();
323     assertEquals(2, result.size());
324   }
325
326   // Support -------------------------------------------------------------------
327

328   class MyRelationTypeSupport
329     extends RelationTypeSupport JavaDoc
330   {
331     private static final long serialVersionUID = -1;
332
333     public MyRelationTypeSupport(String JavaDoc relationTypeName)
334     {
335       super(relationTypeName);
336     }
337     public void addRoleInfo(RoleInfo JavaDoc roleInfo)
338       throws IllegalArgumentException JavaDoc, InvalidRelationTypeException JavaDoc
339     {
340       super.addRoleInfo(roleInfo);
341     }
342   }
343 }
344
Popular Tags