Source Code Cross Referenced for DescriptorSupportTest.java in  » JMX » mx4j » test » javax » management » modelmbean » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » JMX » mx4j » test.javax.management.modelmbean 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) The MX4J Contributors.
003:         * All rights reserved.
004:         *
005:         * This software is distributed under the terms of the MX4J License version 1.0.
006:         * See the terms of the MX4J License in the documentation provided with this software.
007:         */
008:
009:        package test.javax.management.modelmbean;
010:
011:        import java.io.ByteArrayInputStream;
012:        import java.io.ByteArrayOutputStream;
013:        import java.io.ObjectInputStream;
014:        import java.io.ObjectOutputStream;
015:        import java.util.ArrayList;
016:        import java.util.Arrays;
017:        import java.util.HashSet;
018:        import java.util.Iterator;
019:        import java.util.List;
020:        import java.util.Set;
021:        import javax.management.Descriptor;
022:        import javax.management.RuntimeOperationsException;
023:        import javax.management.modelmbean.DescriptorSupport;
024:        import javax.management.modelmbean.XMLParseException;
025:
026:        import test.MX4JTestCase;
027:
028:        /**
029:         * @version $Revision: 1.19 $
030:         */
031:
032:        public class DescriptorSupportTest extends MX4JTestCase {
033:            public DescriptorSupportTest(String s) {
034:                super (s);
035:            }
036:
037:            public void testValid() throws Exception {
038:                // Create a valid Descriptor object
039:                String[] attributes = { "name=mytest", "descriptorType=MBean",
040:                        "role=constructor" };
041:                new DescriptorSupport(attributes);
042:
043:                // Try a different constructor
044:                String[] names = { "name", "descriptorType", "role",
045:                        "persistPolicy", "persistPeriod" };
046:                String[] values = { "mytest", "MBean", "constructor", "Never",
047:                        "1" };
048:                new DescriptorSupport(names, values);
049:
050:                // Create a valid Descriptor object. Check that persistPolicy's value is not case sensitive
051:                names = new String[] { "name", "descriptorType",
052:                        "persistPolicy" };
053:                values = new String[] { "mytest", "MBean", "never" };
054:
055:                new DescriptorSupport(names, values);
056:            }
057:
058:            public void testInvalid() throws Exception {
059:                // Create an invalid Descriptor object. Check persist policy
060:                String[] names = { "name", "descriptorType", "role",
061:                        "persistPolicy" };
062:                String[] values = { "mytest", "MBean", "constructor",
063:                        "Something" };
064:
065:                try {
066:                    new DescriptorSupport(names, values);
067:                    fail("Descriptor support object created with invalid attributes");
068:                } catch (RuntimeOperationsException ex) {
069:                }
070:
071:                // Create an invalid Descriptor object. Check persistPeriod
072:                // Persist period should be bigger or equal than -1
073:                names = new String[] { "name", "descriptorType",
074:                        "persistPolicy", "persistPeriod" };
075:                values = new String[] { "mytest", "MBean", "Never", "-2" };
076:
077:                try {
078:                    new DescriptorSupport(names, values);
079:                    fail("Descriptor support object created with invalid persistPeriod");
080:                } catch (RuntimeOperationsException ex) {
081:                }
082:
083:                // Create an invalid Descriptor object. Check visiblity
084:                // visibility should be between 1 and 4
085:                names = new String[] { "name", "descriptorType", "visibility" };
086:                values = new String[] { "mytest", "MBean", "0" };
087:
088:                try {
089:                    new DescriptorSupport(names, values);
090:                    fail("Descriptor support object created with invalid visiblity");
091:                } catch (RuntimeOperationsException ex) {
092:                }
093:
094:                // Create an invalid Descriptor object. Check visiblity
095:                // visibility should be between 1 and 4
096:                names = new String[] { "name", "descriptorType", "visibility" };
097:                values = new String[] { "mytest", "MBean", "5" };
098:
099:                try {
100:                    new DescriptorSupport(names, values);
101:                    fail("Descriptor support object created with invalid visiblity");
102:                } catch (RuntimeOperationsException ex) {
103:                }
104:            }
105:
106:            public void testIsValid() throws Exception {
107:                // Test for bug #686306
108:                String[] names = { "name", "descriptorType", "persistPolicy",
109:                        "persistPeriod" };
110:                String[] values = { "test", "mbean", "AlwaYs", "-1" };
111:
112:                DescriptorSupport ds = new DescriptorSupport(names, values);
113:
114:                assertTrue(ds.isValid());
115:            }
116:
117:            public void testSeverityField() throws Exception {
118:                // Test for bug #744423 and #775742
119:                String[] names = { "name", "descriptorType", "severity" };
120:                String[] values = { "test", "mbean", "0" };
121:
122:                DescriptorSupport ds = new DescriptorSupport(names, values);
123:                assertTrue(ds.isValid());
124:
125:                names = new String[] { "name", "descriptorType", "severity" };
126:                values = new String[] { "test", "mbean", "6" };
127:
128:                ds = new DescriptorSupport(names, values);
129:                assertTrue(ds.isValid());
130:            }
131:
132:            public void testCaseInsensitiveFieldNames() throws Exception {
133:                String[] fields = { "descriptorType", "myField" };
134:                Object[] values = { "MBean", "top secret" };
135:                DescriptorSupport ds = new DescriptorSupport(fields, values);
136:                assertEquals("Expecting 'descriptorType' value to be 'mbean'",
137:                        (String) ds.getFieldValue("DESCRIPTORTYPE"), "MBean");
138:                assertEquals("Expecting 'myField' value to be 'top secret'",
139:                        (String) ds.getFieldValue("MYfIELD"), "top secret");
140:
141:                fields = new String[] { "name", "descriptorType", "deleteMe" };
142:                values = new String[] { "testCaseInsensitiveFieldNames",
143:                        "MBean", "nothing of consequence" };
144:                ds = new DescriptorSupport(fields, values);
145:                ds.removeField("DELETEmE");
146:                String[] fieldnames = ds.getFields();
147:                List fieldlist = new ArrayList();
148:                for (int i = 0; i < fieldnames.length; i++)
149:                    fieldlist.add(fieldnames[i]);
150:                int fieldcount = fieldnames.length;
151:                assertEquals("Expecting 'deleteMe' to be gone", fieldcount, 2);
152:                assertFalse(fieldlist.contains("deleteme"));
153:            }
154:
155:            public void testCaseInsensitiveFieldValues() throws Exception {
156:                String[] fields = { "descriptorType", "persistPolicy", "log" };
157:                String[] values = { "mBEAN", "oNuPDATE", "TRUE" };
158:                new DescriptorSupport(fields, values);
159:            }
160:
161:            public void testCaseSensitivityPreserved() throws Exception {
162:                String[] names = { "Name", "DescriptorType" };
163:                String[] values = { "test", "mbean" };
164:                DescriptorSupport descriptor = new DescriptorSupport(names,
165:                        values);
166:
167:                // Check case insensitivity on get
168:                String value = (String) descriptor.getFieldValue("name");
169:                assertNotNull(value);
170:                assertEquals(value, values[0]);
171:                value = (String) descriptor.getFieldValue("descriptorType");
172:                assertNotNull(value);
173:                assertEquals(value, values[1]);
174:
175:                // Be sure case is preserved
176:                String[] fieldNames = descriptor.getFieldNames();
177:                assertNotNull(fieldNames);
178:                assertEquals(fieldNames.length, 2);
179:                if (!fieldNames[0].equals(names[0])
180:                        && !fieldNames[0].equals(names[1]))
181:                    fail();
182:                if (!fieldNames[1].equals(names[0])
183:                        && !fieldNames[1].equals(names[1]))
184:                    fail();
185:
186:                // Check serialization works
187:                ByteArrayOutputStream baos = new ByteArrayOutputStream();
188:                ObjectOutputStream oos = new ObjectOutputStream(baos);
189:                oos.writeObject(descriptor);
190:                oos.close();
191:                ByteArrayInputStream bais = new ByteArrayInputStream(baos
192:                        .toByteArray());
193:                ObjectInputStream ois = new ObjectInputStream(bais);
194:                DescriptorSupport newDescriptor = (DescriptorSupport) ois
195:                        .readObject();
196:                value = (String) newDescriptor.getFieldValue("name");
197:                assertNotNull(value);
198:                assertEquals(value, values[0]);
199:                // Be sure case is preserved
200:                fieldNames = newDescriptor.getFieldNames();
201:                assertNotNull(fieldNames);
202:                assertEquals(fieldNames.length, 2);
203:                if (!fieldNames[0].equals(names[0])
204:                        && !fieldNames[0].equals(names[1]))
205:                    fail();
206:                if (!fieldNames[1].equals(names[0])
207:                        && !fieldNames[1].equals(names[1]))
208:                    fail();
209:
210:                // Check that removeField() really removes the field, no matter case sensitivity
211:                descriptor.removeField("name");
212:                value = (String) descriptor.getFieldValue("name");
213:                assertNull(value);
214:                fieldNames = descriptor.getFieldNames();
215:                assertNotNull(fieldNames);
216:                assertEquals(fieldNames.length, 1);
217:                assertEquals(fieldNames[0], names[1]);
218:            }
219:
220:            public void testDefaultFieldValuesSize() throws Exception {
221:                DescriptorSupport ds = new DescriptorSupport();
222:                Object[] fields = ds.getFieldValues(null);
223:                assertTrue("Expecting 0 length array", fields.length == 0);
224:            }
225:
226:            public void testNullDescriptorConstructorParam() throws Exception {
227:                DescriptorSupport ds = new DescriptorSupport(
228:                        (DescriptorSupport) null);
229:                assertNotNull(ds.getFields());
230:                assertEquals("Expecting the descriptor to be empty", ds
231:                        .getFields().length, 0);
232:                assertFalse("Expecting the descriptor to be invalid", ds
233:                        .isValid());
234:            }
235:
236:            public void testNullStringConstructorParam() throws Exception {
237:                try {
238:                    new DescriptorSupport((String) null);
239:                    fail("Expecting RuntimeOperationsException");
240:                } catch (RuntimeOperationsException x) {
241:                    if (!(x.getTargetException() instanceof  IllegalArgumentException))
242:                        fail("Target exception should be IllegalArgumentException");
243:                }
244:            }
245:
246:            public void testNullStringArrayCtorParm() throws Exception {
247:                String[] nullfields = null;
248:                DescriptorSupport ds = new DescriptorSupport(nullfields);
249:                Object[] fields = ds.getFieldValues(null);
250:                assertEquals("Expecting 0 length array", fields.length, 0);
251:            }
252:
253:            public void testToXMLString() throws Exception {
254:                String[] fields = { "wine", "vineyard", "year", "price" };
255:                Object[] values = { "Amarone", "Allegrini", "1996",
256:                        new Integer(90) };
257:                DescriptorSupport ds = new DescriptorSupport(fields, values);
258:                String xml = ds.toXMLString();
259:                assertTrue("Descriptor from XML != Descriptor",
260:                        descriptorsEqual(ds, (new DescriptorSupport(xml))));
261:
262:                fields = new String[] { "wine=Amarone", "vineyard=Allegrini",
263:                        "year=1996" };
264:                ds = new DescriptorSupport(fields);
265:                xml = ds.toXMLString();
266:                assertTrue("Descriptor from XML != Descriptor",
267:                        descriptorsEqual(ds, (new DescriptorSupport(xml))));
268:            }
269:
270:            public void testEmptyDescriptorToXMLString() throws Exception {
271:                DescriptorSupport ds = new DescriptorSupport();
272:                String xml = ds.toXMLString();
273:                assertEquals("Unexpected xml: " + xml, xml,
274:                        "<Descriptor></Descriptor>");
275:            }
276:
277:            public void testGetFieldsUnknownName() {
278:                String[] fields = { "wine", "vineyard", "year" };
279:                String[] values = { "Amarone", "Allegrini", "1996" };
280:                DescriptorSupport ds = new DescriptorSupport(fields, values);
281:                Object fv = ds.getFieldValue("price");
282:                assertNull(fv);
283:                fields = new String[] { "wine", "vineyard", "year", "price" };
284:                Object[] fvs = ds.getFieldValues(fields);
285:                assertEquals("wrong number of values", fvs.length,
286:                        fields.length);
287:                assertNull(fvs[3]);
288:            }
289:
290:            public void testGetFieldsEmptyArray() {
291:                String[] fields = { "wine", "vineyard", "year" };
292:                String[] values = { "Amarone", "Allegrini", "1996" };
293:                DescriptorSupport ds = new DescriptorSupport(fields, values);
294:                Object[] fvs = ds.getFieldValues(new String[0]);
295:                assertEquals("Expecting empty array", fvs.length, 0);
296:            }
297:
298:            public void testRemoveField() {
299:                String[] fields = { "wine", "vineyard", "year", "price" };
300:                Object[] values = { "Amarone", "Allegrini", "1996",
301:                        new Integer(90) };
302:                DescriptorSupport ds = new DescriptorSupport(fields, values);
303:                assertEquals("Expecting " + fields.length + " names", ds
304:                        .getFieldNames().length, fields.length);
305:                ds.removeField("price");
306:                assertEquals("Expecting " + (fields.length - 1) + " names", ds
307:                        .getFieldNames().length, fields.length - 1);
308:            }
309:
310:            public void testRemoveNonexistentFieldDoesntThrow() {
311:                String[] fields = { "wine", "vineyard", "year" };
312:                String[] values = { "Amarone", "Allegrini", "1996" };
313:                DescriptorSupport ds = new DescriptorSupport(fields, values);
314:                ds.removeField("price");
315:            }
316:
317:            public void testRemoveNullField() {
318:                String[] fields = { "wine", "vineyard", "year" };
319:                String[] values = { "Amarone", "Allegrini", "1996" };
320:                DescriptorSupport ds = new DescriptorSupport(fields, values);
321:                ds.removeField(null);
322:            }
323:
324:            public void testXMLStringConstructor() throws Exception {
325:                StringBuffer xmldescriptor = new StringBuffer();
326:                xmldescriptor.append("<Descriptor>");
327:                xmldescriptor.append("<field ");
328:                xmldescriptor.append("name=\"bogus\" ");
329:                xmldescriptor.append("value=\"xyzzy\"");
330:                xmldescriptor.append(">");
331:                xmldescriptor.append("</field>");
332:                xmldescriptor.append("</Descriptor>");
333:                DescriptorSupport ds = new DescriptorSupport(xmldescriptor
334:                        .toString());
335:                String xml = ds.toXMLString();
336:                assertTrue("Descriptor from XML != Descriptor",
337:                        descriptorsEqual(ds, (new DescriptorSupport(xml))));
338:            }
339:
340:            public void testXMLSpecialStringConstructor() throws Exception {
341:                String[] fields = { "name=Lawrence", "nickname=(Larry)" };
342:                DescriptorSupport ds = new DescriptorSupport(fields);
343:                String xml = ds.toXMLString();
344:                DescriptorSupport xmlds = new DescriptorSupport(xml);
345:                assertTrue("Descriptors not equal", descriptorsEqual(ds, xmlds));
346:            }
347:
348:            public void testXMLPrimitiveConstructor() throws Exception {
349:                StringBuffer xmldescriptor = new StringBuffer();
350:                xmldescriptor.append("<Descriptor>");
351:                xmldescriptor.append("<field ");
352:                xmldescriptor.append("name=\"boolean\" ");
353:                xmldescriptor.append("value=\"(java.lang.Boolean/true)\"");
354:                xmldescriptor.append(">");
355:                xmldescriptor.append("</field>");
356:                xmldescriptor.append("<field ");
357:                xmldescriptor.append("name=\"byte\" ");
358:                xmldescriptor.append("value=\"(java.lang.Byte/127)\"");
359:                xmldescriptor.append(">");
360:                xmldescriptor.append("</field>");
361:                xmldescriptor.append("<field ");
362:                xmldescriptor.append("name=\"char\" ");
363:                xmldescriptor.append("value=\"(java.lang.Character/k)\"");
364:                xmldescriptor.append(">");
365:                xmldescriptor.append("</field>");
366:                xmldescriptor.append("<field ");
367:                xmldescriptor.append("name=\"short\" ");
368:                xmldescriptor.append("value=\"(java.lang.Short/4096)\"");
369:                xmldescriptor.append(">");
370:                xmldescriptor.append("</field>");
371:                xmldescriptor.append("<field ");
372:                xmldescriptor.append("name=\"int\" ");
373:                xmldescriptor.append("value=\"(java.lang.Integer/16384)\"");
374:                xmldescriptor.append(">");
375:                xmldescriptor.append("</field>");
376:                xmldescriptor.append("<field ");
377:                xmldescriptor.append("name=\"long\" ");
378:                xmldescriptor.append("value=\"(java.lang.Long/123456789)\"");
379:                xmldescriptor.append(">");
380:                xmldescriptor.append("</field>");
381:                xmldescriptor.append("<field ");
382:                xmldescriptor.append("name=\"float\" ");
383:                xmldescriptor.append("value=\"(java.lang.Float/3.14)\"");
384:                xmldescriptor.append(">");
385:                xmldescriptor.append("</field>");
386:                xmldescriptor.append("<field ");
387:                xmldescriptor.append("name=\"double\" ");
388:                xmldescriptor.append("value=\"(java.lang.Double/3.14e-10)\"");
389:                xmldescriptor.append(">");
390:                xmldescriptor.append("</field>");
391:                xmldescriptor.append("<field ");
392:                xmldescriptor.append("name=\"null\" ");
393:                xmldescriptor.append("value=\"(null)\"");
394:                xmldescriptor.append(">");
395:                xmldescriptor.append("</field>");
396:                xmldescriptor.append("</Descriptor>");
397:                DescriptorSupport ds = new DescriptorSupport(xmldescriptor
398:                        .toString());
399:                String xml = ds.toXMLString();
400:                assertTrue("Descriptor from XML != Descriptor",
401:                        descriptorsEqual(ds, (new DescriptorSupport(xml))));
402:            }
403:
404:            public void testXMLClassConstructor() throws Exception {
405:                StringBuffer xmldescriptor = new StringBuffer();
406:                xmldescriptor.append("<Descriptor>");
407:                xmldescriptor.append("<field ");
408:                xmldescriptor.append("name=\"date\" ");
409:                xmldescriptor
410:                        .append("value=\"(java.net.URL/http://mx4j.sourceforge.net)\"");
411:                xmldescriptor.append(">");
412:                xmldescriptor.append("</field>");
413:                xmldescriptor.append("</Descriptor>");
414:                DescriptorSupport ds = new DescriptorSupport(xmldescriptor
415:                        .toString());
416:                String xml = ds.toXMLString();
417:                assertTrue("Descriptor from XML != Descriptor",
418:                        descriptorsEqual(ds, (new DescriptorSupport(xml))));
419:            }
420:
421:            public void testBogusXMLConstructor() throws Exception {
422:                try {
423:                    new DescriptorSupport(
424:                            "<Descriptor><field name=</Descriptor>");
425:                    fail("Expecting XMLParseException");
426:                } catch (XMLParseException x) {
427:                }
428:            }
429:
430:            public void testBogusXMLValueCtor() throws Exception {
431:                try {
432:                    StringBuffer xmldescriptor = new StringBuffer();
433:                    xmldescriptor.append("<Descriptor>");
434:                    xmldescriptor.append("<field ");
435:                    xmldescriptor.append("name=\"bogus\" ");
436:                    xmldescriptor.append("value=\"(java.lang.Byte/256)\"");
437:                    xmldescriptor.append(">");
438:                    xmldescriptor.append("</field>");
439:                    xmldescriptor.append("</Descriptor>");
440:                    new DescriptorSupport(xmldescriptor.toString());
441:                    fail("Expecting XMLParseException");
442:                } catch (XMLParseException x) {
443:                }
444:            }
445:
446:            private boolean descriptorsEqual(Descriptor done, Descriptor dtwo) {
447:                Set namesone = new HashSet(Arrays.asList(done.getFieldNames()));
448:                Set namestwo = new HashSet(Arrays.asList(dtwo.getFieldNames()));
449:                if (!namesone.equals(namestwo))
450:                    return false;
451:                Iterator i = namesone.iterator();
452:                while (i.hasNext()) {
453:                    String field = (String) i.next();
454:                    Object vone = done.getFieldValue(field);
455:                    Object vtwo = dtwo.getFieldValue(field);
456:                    if ((vone == null && vtwo != null)
457:                            || (vone != null && !vone.equals(vtwo))) {
458:                        return false;
459:                    }
460:                }
461:                return true;
462:            }
463:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.