KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > modelmbean > DescriptorTEST


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.modelmbean;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import junit.framework.TestCase;
37
38 import javax.management.RuntimeOperationsException JavaDoc;
39 import javax.management.modelmbean.DescriptorSupport JavaDoc;
40
41 /**
42  * Tests the standard required <tt>DescriptorSupport</tt> implementation.
43  *
44  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
45  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
46  * @version $Revision: 37459 $
47  */

48 public class DescriptorTEST
49    extends TestCase
50 {
51    public DescriptorTEST(String JavaDoc s)
52    {
53       super(s);
54    }
55
56    // Tests ------------------------------------------------------------
57

58    public void testDefaultConstructor()
59       throws Exception JavaDoc
60    {
61       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc();
62       assertTrue("Should be empty", descriptor.getFields().length == 0);
63       assertFalse("Should not be valid", descriptor.isValid());
64    }
65
66    public void testSizeConstructor()
67       throws Exception JavaDoc
68    {
69       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc(100);
70       assertTrue("Should be empty", descriptor.getFields().length == 0);
71       assertFalse("Should not be valid", descriptor.isValid());
72
73       boolean caught = false;
74       try
75       {
76          descriptor = new DescriptorSupport JavaDoc(-1);
77       }
78       catch (RuntimeOperationsException JavaDoc e)
79       {
80          caught = true;
81       }
82       assertTrue("Expected an exception for a negative size", caught);
83    }
84
85    public void testCopyConstructor()
86       throws Exception JavaDoc
87    {
88       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc((DescriptorSupport JavaDoc) null);
89       assertTrue("Should be empty", descriptor.getFields().length == 0);
90       assertFalse("Should not be valid", descriptor.isValid());
91
92       descriptor = new DescriptorSupport JavaDoc(descriptor);
93       assertTrue("Should be empty", descriptor.getFields().length == 0);
94       assertFalse("Should not be valid", descriptor.isValid());
95
96       descriptor.setField("name", "testName");
97       descriptor.setField("descriptorType", "testType");
98       descriptor = new DescriptorSupport JavaDoc(descriptor);
99       assertTrue("Should be two fields", descriptor.getFields().length == 2);
100       assertEquals("testName", descriptor.getFieldValue("name"));
101       assertEquals("testType", descriptor.getFieldValue("descriptorType"));
102       assertTrue("Should be valid", descriptor.isValid());
103    }
104
105    public void testNamesValuesConstructor()
106       throws Exception JavaDoc
107    {
108       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType" };
109       Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType" };
110       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc(names, values);
111       assertTrue("Should be two fields", descriptor.getFields().length == 2);
112       assertEquals("testName", descriptor.getFieldValue("name"));
113       assertEquals("testType", descriptor.getFieldValue("descriptorType"));
114       assertTrue("Should be valid", descriptor.isValid());
115
116       descriptor = new DescriptorSupport JavaDoc(new String JavaDoc[0], new Object JavaDoc[0]);
117       assertTrue("Should be empty", descriptor.getFields().length == 0);
118       assertFalse("Should not be valid", descriptor.isValid());
119
120       boolean caught = false;
121       try
122       {
123          descriptor = new DescriptorSupport JavaDoc(null, null);
124       }
125       catch (RuntimeOperationsException JavaDoc e)
126       {
127          caught = true;
128       }
129       assertTrue("Expected an exception for null names and values", caught);
130
131       caught = false;
132       try
133       {
134          descriptor = new DescriptorSupport JavaDoc(null, values);
135       }
136       catch (RuntimeOperationsException JavaDoc e)
137       {
138          caught = true;
139       }
140       assertTrue("Expected an exception for null names", caught);
141
142       caught = false;
143       try
144       {
145          descriptor = new DescriptorSupport JavaDoc(names, null);
146       }
147       catch (RuntimeOperationsException JavaDoc e)
148       {
149          caught = true;
150       }
151       assertTrue("Expected an exception for null values", caught);
152
153       Object JavaDoc[] tooManyValues = new Object JavaDoc[] { "testName", "testType", "tooMany" };
154       caught = false;
155       try
156       {
157          descriptor = new DescriptorSupport JavaDoc(names, tooManyValues);
158       }
159       catch (RuntimeOperationsException JavaDoc e)
160       {
161          caught = true;
162       }
163       assertTrue("Expected an exception for too many values", caught);
164
165       Object JavaDoc[] tooFewValues = new Object JavaDoc[] { "testName" };
166       caught = false;
167       try
168       {
169          descriptor = new DescriptorSupport JavaDoc(names, tooFewValues);
170       }
171       catch (RuntimeOperationsException JavaDoc e)
172       {
173          caught = true;
174       }
175       assertTrue("Expected an exception for too few values", caught);
176
177       String JavaDoc[] nullName = new String JavaDoc[] { "name", null };
178       caught = false;
179       try
180       {
181          descriptor = new DescriptorSupport JavaDoc(nullName, values);
182       }
183       catch (RuntimeOperationsException JavaDoc e)
184       {
185          caught = true;
186       }
187       assertTrue("Expected an exception for null name", caught);
188
189       String JavaDoc[] emptyName = new String JavaDoc[] { "name", "" };
190       caught = false;
191       try
192       {
193          descriptor = new DescriptorSupport JavaDoc(emptyName, values);
194       }
195       catch (RuntimeOperationsException JavaDoc e)
196       {
197          caught = true;
198       }
199       assertTrue("Expected an exception for empty name ", caught);
200
201       // This is legal?
202
String JavaDoc[] notEmptyName = new String JavaDoc[] { "name", " " };
203       descriptor = new DescriptorSupport JavaDoc(notEmptyName, values);
204       assertTrue("Should be two fields", descriptor.getFields().length == 2);
205       assertEquals("testName", descriptor.getFieldValue("name"));
206       assertEquals("testType", descriptor.getFieldValue(" "));
207       assertFalse("Should not be valid", descriptor.isValid());
208
209       names = new String JavaDoc[] { "name", "descriptorType", "another" };
210       values = new Object JavaDoc[] { "testName", "testType", null };
211       descriptor = new DescriptorSupport JavaDoc(names, values);
212       assertTrue("Should be three fields", descriptor.getFields().length == 3);
213       assertEquals("testName", descriptor.getFieldValue("name"));
214       assertEquals("testType", descriptor.getFieldValue("descriptorType"));
215       assertTrue("another should be null", descriptor.getFieldValue("another") == null);
216       assertTrue("Should be valid", descriptor.isValid());
217    }
218
219    public void testNameEqualsValueConstructor()
220       throws Exception JavaDoc
221    {
222       String JavaDoc[] fields = new String JavaDoc[] { "name=testName", "descriptorType=testType" };
223       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc(fields);
224       assertTrue("Should be two fields", descriptor.getFields().length == 2);
225       assertEquals("testName", descriptor.getFieldValue("name"));
226       assertEquals("testType", descriptor.getFieldValue("descriptorType"));
227       assertTrue("Should be valid", descriptor.isValid());
228
229       descriptor = new DescriptorSupport JavaDoc((String JavaDoc[]) null);
230       assertTrue("Should be empty", descriptor.getFields().length == 0);
231       assertFalse("Should not be valid", descriptor.isValid());
232
233       descriptor = new DescriptorSupport JavaDoc(new String JavaDoc[0]);
234       assertTrue("Should be empty", descriptor.getFields().length == 0);
235       assertFalse("Should not be valid", descriptor.isValid());
236
237       String JavaDoc[] nullName = new String JavaDoc[] { "name=testName", "=rubbish" };
238       boolean caught = false;
239       try
240       {
241          descriptor = new DescriptorSupport JavaDoc(nullName);
242       }
243       catch (RuntimeOperationsException JavaDoc e)
244       {
245          caught = true;
246       }
247       assertTrue("Expected an exception for '=rubbish'", caught);
248
249       // This is legal?
250
String JavaDoc[] notEmptyName = new String JavaDoc[] { "name=testName", " =rubbish" };
251       descriptor = new DescriptorSupport JavaDoc(notEmptyName);
252       assertTrue("Should be two fields", descriptor.getFields().length == 2);
253       assertEquals("testName", descriptor.getFieldValue("name"));
254       assertEquals("rubbish", descriptor.getFieldValue(" "));
255       assertFalse("Should not be valid", descriptor.isValid());
256    }
257
258    public void testNameEqualsValueConstructorError()
259       throws Exception JavaDoc
260    {
261       try
262       {
263          String JavaDoc[] fields = new String JavaDoc[] { "name=testName", "descriptorType=testType", "another=" };
264          DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc(fields);
265          assertTrue("Should be three fields", descriptor.getFields().length == 3);
266          assertEquals("testName", descriptor.getFieldValue("name"));
267          assertEquals("testType", descriptor.getFieldValue("descriptorType"));
268          assertTrue("another should be null", descriptor.getFieldValue("another") == null);
269          assertTrue("Should be valid", descriptor.isValid());
270       }
271       catch (Exception JavaDoc e)
272       {
273          fail("FAILS IN RI: 'another=' should be valid according to the javadoc " + e.toString());
274       }
275    }
276
277    public void testGetFieldValue()
278       throws Exception JavaDoc
279    {
280       String JavaDoc[] fields = new String JavaDoc[] { "name=testName", "descriptorType=testType" };
281       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc(fields);
282       assertEquals("testName", descriptor.getFieldValue("name"));
283       assertTrue("Field names are not case sensitive", "testName".equals(descriptor.getFieldValue("NAME")));
284       assertTrue("Non existent field should be null", descriptor.getFieldValue("nonExistent") == null);
285
286       boolean caught = false;
287       try
288       {
289          descriptor.getFieldValue(null);
290       }
291       catch (RuntimeOperationsException JavaDoc e)
292       {
293          caught = true;
294       }
295       assertTrue("Expected an exception for null name", caught);
296
297       caught = false;
298       try
299       {
300          descriptor.getFieldValue("");
301       }
302       catch (RuntimeOperationsException JavaDoc e)
303       {
304          caught = true;
305       }
306       assertTrue("Expected an exception for empty name", caught);
307
308       // This is legal?
309
assertTrue("Blank field name is allowed", descriptor.getFieldValue(" ") == null);
310    }
311
312    public void testSetFieldValue()
313       throws Exception JavaDoc
314    {
315       String JavaDoc[] fields = new String JavaDoc[] { "name=testName", "descriptorType=testType" };
316       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc(fields);
317       assertEquals("testName", descriptor.getFieldValue("name"));
318       descriptor.setField("name", "newName");
319       assertEquals("newName", descriptor.getFieldValue("name"));
320       descriptor.setField("NAME", "newNAME");
321       assertEquals("newNAME", descriptor.getFieldValue("name"));
322
323       boolean caught = false;
324       try
325       {
326          descriptor.setField(null, "null");
327       }
328       catch (RuntimeOperationsException JavaDoc e)
329       {
330          caught = true;
331       }
332       assertTrue("Expected an exception for null name", caught);
333
334       caught = false;
335       try
336       {
337          descriptor.setField("", "empty");
338       }
339       catch (RuntimeOperationsException JavaDoc e)
340       {
341          caught = true;
342       }
343       assertTrue("Expected an exception for empty name", caught);
344
345       // This is legal?
346
descriptor.setField(" ", "blank");
347       assertEquals("blank", descriptor.getFieldValue(" "));
348
349       descriptor.setField("newField", "newValue");
350       assertEquals("newValue", descriptor.getFieldValue("newField"));
351    }
352
353    public void testGetFields()
354       throws Exception JavaDoc
355    {
356       String JavaDoc[] fields = new String JavaDoc[] { "name=testName", "descriptorType=testType" };
357       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc(fields);
358       String JavaDoc[] getFields = descriptor.getFields();
359       compareFields(fields, getFields);
360    }
361
362    public void testGetFieldNames()
363       throws Exception JavaDoc
364    {
365       String JavaDoc[] fields = new String JavaDoc[] { "name=testName", "descriptorType=testType" };
366       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc(fields);
367       String JavaDoc[] names = descriptor.getFieldNames();
368       compareFieldNames(fields, names);
369    }
370
371    public void testGetFieldValues()
372       throws Exception JavaDoc
373    {
374       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType" };
375       String JavaDoc[] values = new String JavaDoc[] { "testName", "testType" };
376       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc(names, values);
377
378       Object JavaDoc[] result = descriptor.getFieldValues(null);
379       compareFieldValuesUnsorted(values, result);
380
381       result = descriptor.getFieldValues(new String JavaDoc[0]);
382       compareFieldValues(new Object JavaDoc[0], result);
383
384       result = descriptor.getFieldValues(names);
385       compareFieldValues(values, result);
386
387       result = descriptor.getFieldValues(new String JavaDoc[] { "name" });
388       compareFieldValues(new Object JavaDoc[] { "testName" }, result);
389
390       result = descriptor.getFieldValues(new String JavaDoc[] { "descriptorType", "name" });
391       compareFieldValues(new Object JavaDoc[] { "testType", "testName" }, result);
392
393       result = descriptor.getFieldValues(new String JavaDoc[] { "NAME" });
394       compareFieldValues(new Object JavaDoc[] { "testName" }, result);
395
396       result = descriptor.getFieldValues(new String JavaDoc[] { null });
397       compareFieldValues(new Object JavaDoc[] { null }, result);
398
399       result = descriptor.getFieldValues(new String JavaDoc[] { "" });
400       compareFieldValues(new Object JavaDoc[] { null }, result);
401    }
402
403    public void testSetFieldValues()
404       throws Exception JavaDoc
405    {
406       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc();
407       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType" };
408       Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType" };
409       descriptor.setFields(names, values);
410       assertTrue("Should be two fields", descriptor.getFields().length == 2);
411       assertEquals("testName", descriptor.getFieldValue("name"));
412       assertEquals("testType", descriptor.getFieldValue("descriptorType"));
413       assertTrue("Should be valid", descriptor.isValid());
414
415       descriptor = new DescriptorSupport JavaDoc();
416       descriptor.setFields(new String JavaDoc[0], new Object JavaDoc[0]);
417       assertTrue("Should be empty", descriptor.getFields().length == 0);
418       assertFalse("Should not be valid", descriptor.isValid());
419
420       boolean caught = false;
421       try
422       {
423          descriptor = new DescriptorSupport JavaDoc();
424          descriptor.setFields(null, null);
425       }
426       catch (RuntimeOperationsException JavaDoc e)
427       {
428          caught = true;
429       }
430       assertTrue("Expected an exception for null names and values", caught);
431
432       caught = false;
433       try
434       {
435          descriptor = new DescriptorSupport JavaDoc();
436          descriptor.setFields(null, values);
437       }
438       catch (RuntimeOperationsException JavaDoc e)
439       {
440          caught = true;
441       }
442       assertTrue("Expected an exception for null names", caught);
443
444       caught = false;
445       try
446       {
447          descriptor = new DescriptorSupport JavaDoc();
448          descriptor.setFields(names, null);
449       }
450       catch (RuntimeOperationsException JavaDoc e)
451       {
452          caught = true;
453       }
454       assertTrue("Expected an exception for null values", caught);
455
456       Object JavaDoc[] tooManyValues = new Object JavaDoc[] { "testName", "testType", "tooMany" };
457       caught = false;
458       try
459       {
460          descriptor = new DescriptorSupport JavaDoc();
461          descriptor.setFields(names, tooManyValues);
462       }
463       catch (RuntimeOperationsException JavaDoc e)
464       {
465          caught = true;
466       }
467       assertTrue("Expected an exception for too many values", caught);
468
469       Object JavaDoc[] tooFewValues = new Object JavaDoc[] { "testName" };
470       caught = false;
471       try
472       {
473          descriptor = new DescriptorSupport JavaDoc();
474          descriptor.setFields(names, tooFewValues);
475       }
476       catch (RuntimeOperationsException JavaDoc e)
477       {
478          caught = true;
479       }
480       assertTrue("Expected an exception for too few values", caught);
481
482       String JavaDoc[] nullName = new String JavaDoc[] { "name", null };
483       caught = false;
484       try
485       {
486          descriptor = new DescriptorSupport JavaDoc();
487          descriptor.setFields(nullName, values);
488       }
489       catch (RuntimeOperationsException JavaDoc e)
490       {
491          caught = true;
492       }
493       assertTrue("Expected an exception for null name", caught);
494
495       String JavaDoc[] emptyName = new String JavaDoc[] { "name", "" };
496       caught = false;
497       try
498       {
499          descriptor = new DescriptorSupport JavaDoc();
500          descriptor.setFields(emptyName, values);
501       }
502       catch (RuntimeOperationsException JavaDoc e)
503       {
504          caught = true;
505       }
506       assertTrue("Expected an exception for empty name ", caught);
507
508       // This is legal?
509
String JavaDoc[] notEmptyName = new String JavaDoc[] { "name", " " };
510       descriptor = new DescriptorSupport JavaDoc();
511       descriptor.setFields(notEmptyName, values);
512       assertTrue("Should be two fields", descriptor.getFields().length == 2);
513       assertEquals("testName", descriptor.getFieldValue("name"));
514       assertEquals("testType", descriptor.getFieldValue(" "));
515       assertFalse("Should not be valid", descriptor.isValid());
516
517       names = new String JavaDoc[] { "name", "descriptorType", "another" };
518       values = new Object JavaDoc[] { "testName", "testType", null };
519       descriptor = new DescriptorSupport JavaDoc();
520       descriptor.setFields(names, values);
521       assertTrue("Should be three fields", descriptor.getFields().length == 3);
522       assertEquals("testName", descriptor.getFieldValue("name"));
523       assertEquals("testType", descriptor.getFieldValue("descriptorType"));
524       assertTrue("another should be null", descriptor.getFieldValue("another") == null);
525       assertTrue("Should be valid", descriptor.isValid());
526    }
527
528    public void testClone()
529       throws Exception JavaDoc
530    {
531       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType" };
532       Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType" };
533       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc(names, values);
534
535       DescriptorSupport JavaDoc clone = (DescriptorSupport JavaDoc) descriptor.clone();
536       compareFields(descriptor.getFields(), clone.getFields());
537    }
538
539    public void testRemove()
540       throws Exception JavaDoc
541    {
542       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType" };
543       Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType" };
544       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc(names, values);
545
546       descriptor.removeField("name");
547       assertTrue("Should be one field", descriptor.getFields().length == 1);
548       assertTrue("name should not be present", descriptor.getFieldValue("name") == null);
549       assertEquals("testType", descriptor.getFieldValue("descriptorType"));
550
551       descriptor = new DescriptorSupport JavaDoc(names, values);
552       descriptor.removeField("NAME");
553       assertTrue("Should be one field", descriptor.getFields().length == 1);
554       assertTrue("name should not be present", descriptor.getFieldValue("name") == null);
555       assertEquals("testType", descriptor.getFieldValue("descriptorType"));
556
557       descriptor = new DescriptorSupport JavaDoc(names, values);
558       descriptor.removeField("notPresent");
559       assertTrue("Should be two fields", descriptor.getFields().length == 2);
560       assertEquals("testName", descriptor.getFieldValue("name"));
561       assertEquals("testType", descriptor.getFieldValue("descriptorType"));
562
563       descriptor.removeField(null);
564       assertTrue("Should be two fields", descriptor.getFields().length == 2);
565       assertEquals("testName", descriptor.getFieldValue("name"));
566       assertEquals("testType", descriptor.getFieldValue("descriptorType"));
567
568       descriptor.removeField("");
569       assertTrue("Should be two fields", descriptor.getFields().length == 2);
570       assertEquals("testName", descriptor.getFieldValue("name"));
571       assertEquals("testType", descriptor.getFieldValue("descriptorType"));
572    }
573
574    public void testIsValidMandatory()
575       throws Exception JavaDoc
576    {
577       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType" };
578       Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType" };
579       validate(names, values, true);
580
581       names = new String JavaDoc[] { "name", "descriptorType" };
582       values = new Object JavaDoc[] { null, "testType" };
583       validate(names, values, false);
584
585       names = new String JavaDoc[] { "name", "descriptorType" };
586       values = new Object JavaDoc[] { "", "testType" };
587       validate(names, values, false);
588
589       names = new String JavaDoc[] { "name", "descriptorType" };
590       values = new Object JavaDoc[] { "testName", null };
591       validate(names, values, false);
592
593       names = new String JavaDoc[] { "name", "descriptorType" };
594       values = new Object JavaDoc[] { "testName", "" };
595       validate(names, values, false);
596    }
597
598    public void testIsValidRole()
599       throws Exception JavaDoc
600    {
601       doTestString("role", "operation");
602    }
603
604    public void testIsValidGetMethod()
605       throws Exception JavaDoc
606    {
607       doTestString("getMethod", "getSomething");
608    }
609
610    public void testIsValidSetMethod()
611       throws Exception JavaDoc
612    {
613       doTestString("setMethod", "setSomething");
614    }
615
616    public void testIsValidPersistPeriod()
617       throws Exception JavaDoc
618    {
619       doTestInteger("persistPeriod");
620    }
621
622    public void testIsValidCurrencyTimeLimit()
623       throws Exception JavaDoc
624    {
625       doTestInteger("currencyTimeLimit");
626    }
627
628    public void testIsValidLastUpdateTimeStamp()
629       throws Exception JavaDoc
630    {
631       doTestInteger("lastUpdatedTimeStamp");
632    }
633
634    public void testIsValidLog()
635       throws Exception JavaDoc
636    {
637       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType", "log" };
638       Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType", "true" };
639       validate(names, values, true);
640
641       names = new String JavaDoc[] { "name", "descriptorType", "log" };
642       values = new Object JavaDoc[] { "testName", "testType", "false" };
643       validate(names, values, true);
644
645       names = new String JavaDoc[] { "name", "descriptorType", "log" };
646       values = new Object JavaDoc[] { "testName", "testType", "t" };
647       validate(names, values, true);
648
649       names = new String JavaDoc[] { "name", "descriptorType", "log" };
650       values = new Object JavaDoc[] { "testName", "testType", "f" };
651       validate(names, values, true);
652
653       names = new String JavaDoc[] { "name", "descriptorType", "log" };
654       values = new Object JavaDoc[] { "testName", "testType", "TRUE" };
655       validate(names, values, true);
656
657       names = new String JavaDoc[] { "name", "descriptorType", "log" };
658       values = new Object JavaDoc[] { "testName", "testType", "FALSE" };
659       validate(names, values, true);
660
661       names = new String JavaDoc[] { "name", "descriptorType", "log" };
662       values = new Object JavaDoc[] { "testName", "testType", "T" };
663       validate(names, values, true);
664
665       names = new String JavaDoc[] { "name", "descriptorType", "log" };
666       values = new Object JavaDoc[] { "testName", "testType", "F" };
667       validate(names, values, true);
668
669       names = new String JavaDoc[] { "name", "descriptorType", "log" };
670       values = new Object JavaDoc[] { "testName", "testType", null };
671       validate(names, values, false);
672
673       names = new String JavaDoc[] { "name", "descriptorType", "log" };
674       values = new Object JavaDoc[] { "testName", "testType", "" };
675       validate(names, values, false);
676
677       names = new String JavaDoc[] { "name", "descriptorType", "log" };
678       values = new Object JavaDoc[] { "testName", "testType", "rubbish" };
679       validate(names, values, false);
680    }
681
682    public void testIsValidVisibility()
683       throws Exception JavaDoc
684    {
685       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
686       Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType", "1" };
687       validate(names, values, true);
688
689       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
690       values = new Object JavaDoc[] { "testName", "testType", "2" };
691       validate(names, values, true);
692
693       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
694       values = new Object JavaDoc[] { "testName", "testType", "3" };
695       validate(names, values, true);
696
697       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
698       values = new Object JavaDoc[] { "testName", "testType", "4" };
699       validate(names, values, true);
700
701       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
702       values = new Object JavaDoc[] { "testName", "testType", "0" };
703       validate(names, values, false);
704
705       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
706       values = new Object JavaDoc[] { "testName", "testType", "5" };
707       validate(names, values, false);
708
709       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
710       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(1) };
711       validate(names, values, true);
712
713       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
714       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(2) };
715       validate(names, values, true);
716
717       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
718       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(3) };
719       validate(names, values, true);
720
721       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
722       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(4) };
723       validate(names, values, true);
724
725       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
726       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(0) };
727       validate(names, values, false);
728
729       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
730       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(6) };
731       validate(names, values, false);
732
733       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
734       values = new Object JavaDoc[] { "testName", "testType", null };
735       validate(names, values, false);
736
737       names = new String JavaDoc[] { "name", "descriptorType", "visibility" };
738       values = new Object JavaDoc[] { "testName", "testType", "" };
739       validate(names, values, false);
740    }
741
742    public void testIsValidSeverity()
743       throws Exception JavaDoc
744    {
745       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType", "severity" };
746       Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType", "1" };
747       validate(names, values, true);
748
749       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
750       values = new Object JavaDoc[] { "testName", "testType", "2" };
751       validate(names, values, true);
752
753       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
754       values = new Object JavaDoc[] { "testName", "testType", "3" };
755       validate(names, values, true);
756
757       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
758       values = new Object JavaDoc[] { "testName", "testType", "4" };
759       validate(names, values, true);
760
761       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
762       values = new Object JavaDoc[] { "testName", "testType", "5" };
763       validate(names, values, true);
764
765       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
766       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(1) };
767       validate(names, values, true);
768
769       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
770       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(2) };
771       validate(names, values, true);
772
773       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
774       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(3) };
775       validate(names, values, true);
776
777       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
778       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(4) };
779       validate(names, values, true);
780
781       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
782       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(5) };
783       validate(names, values, true);
784
785       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
786       values = new Object JavaDoc[] { "testName", "testType", null };
787       validate(names, values, false);
788
789       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
790       values = new Object JavaDoc[] { "testName", "testType", "" };
791       validate(names, values, false);
792
793       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
794       values = new Object JavaDoc[] { "testName", "testType", "0" };
795       validate(names, values, false);
796
797       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
798       values = new Object JavaDoc[] { "testName", "testType", "7" };
799       validate(names, values, false);
800
801       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
802       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(0) };
803       validate(names, values, false);
804
805       names = new String JavaDoc[] { "name", "descriptorType", "severity" };
806       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(7) };
807       validate(names, values, false);
808    }
809
810    public void testIsValidError()
811       throws Exception JavaDoc
812    {
813       try
814       {
815          String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType", "severity" };
816          Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType", "6" };
817          validate(names, values, true);
818
819          names = new String JavaDoc[] { "name", "descriptorType", "severity" };
820          values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(6) };
821          validate(names, values, true);
822       }
823       catch (Exception JavaDoc e)
824       {
825          fail("FAILS IN RI: javadoc and spec are inconsistent on whether severity=6 is valid");
826       }
827    }
828
829    public void testIsValidPersistPolicy()
830       throws Exception JavaDoc
831    {
832       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType", "persistPolicy" };
833       Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType", "onUpdate" };
834       validate(names, values, true);
835
836       names = new String JavaDoc[] { "name", "descriptorType", "persistPolicy" };
837       values = new Object JavaDoc[] { "testName", "testType", "noMoreOftenThan" };
838       validate(names, values, true);
839
840       names = new String JavaDoc[] { "name", "descriptorType", "persistPolicy" };
841       values = new Object JavaDoc[] { "testName", "testType", "never" };
842       validate(names, values, true);
843
844       names = new String JavaDoc[] { "name", "descriptorType", "persistPolicy" };
845       values = new Object JavaDoc[] { "testName", "testType", "onTimer" };
846       validate(names, values, true);
847
848       names = new String JavaDoc[] { "name", "descriptorType", "persistPolicy" };
849       values = new Object JavaDoc[] { "testName", "testType", null };
850       validate(names, values, false);
851
852       names = new String JavaDoc[] { "name", "descriptorType", "persistPolicy" };
853       values = new Object JavaDoc[] { "testName", "testType", "" };
854       validate(names, values, false);
855
856       names = new String JavaDoc[] { "name", "descriptorType", "persistPolicy" };
857       values = new Object JavaDoc[] { "testName", "testType", "rubbish" };
858       validate(names, values, false);
859    }
860
861    public void testSerialization()
862       throws Exception JavaDoc
863    {
864       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType" };
865       Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType" };
866       DescriptorSupport JavaDoc descriptor = new DescriptorSupport JavaDoc(names, values);
867
868       // Serialize it
869
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
870       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
871       oos.writeObject(descriptor);
872     
873       // Deserialize it
874
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
875       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
876       Object JavaDoc result = ois.readObject();
877
878       compareFields(descriptor.getFields(), ((DescriptorSupport JavaDoc) result).getFields());
879    }
880
881    // Support -------------------------------------------
882

883    private void doTestString(String JavaDoc field, String JavaDoc value)
884       throws Exception JavaDoc
885    {
886       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType", field };
887       Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType", value };
888       validate(names, values, true);
889
890       names = new String JavaDoc[] { "name", "descriptorType", field };
891       values = new Object JavaDoc[] { "testName", "testType", null };
892       validate(names, values, false);
893
894       names = new String JavaDoc[] { "name", "descriptorType", field };
895       values = new Object JavaDoc[] { "testName", "testType", "" };
896       validate(names, values, false);
897    }
898
899    public void doTestInteger(String JavaDoc field)
900    {
901       String JavaDoc[] names = new String JavaDoc[] { "name", "descriptorType", field };
902       Object JavaDoc[] values = new Object JavaDoc[] { "testName", "testType", "0" };
903       validate(names, values, true);
904
905       names = new String JavaDoc[] { "name", "descriptorType", field };
906       values = new Object JavaDoc[] { "testName", "testType", "-1" };
907       validate(names, values, true);
908
909       names = new String JavaDoc[] { "name", "descriptorType", field };
910       values = new Object JavaDoc[] { "testName", "testType", "100" };
911       validate(names, values, true);
912
913       names = new String JavaDoc[] { "name", "descriptorType", field };
914       values = new Object JavaDoc[] { "testName", "testType", "-2" };
915       validate(names, values, false);
916
917       names = new String JavaDoc[] { "name", "descriptorType", field };
918       values = new Object JavaDoc[] { "testName", "testType", null };
919       validate(names, values, false);
920
921       names = new String JavaDoc[] { "name", "descriptorType", field };
922       values = new Object JavaDoc[] { "testName", "testType", "" };
923       validate(names, values, false);
924
925       names = new String JavaDoc[] { "name", "descriptorType", field };
926       values = new Object JavaDoc[] { "testName", "testType", "rubbish" };
927       validate(names, values, false);
928
929       names = new String JavaDoc[] { "name", "descriptorType", field };
930       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(0) };
931       validate(names, values, true);
932
933       names = new String JavaDoc[] { "name", "descriptorType", field };
934       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(-1) };
935       validate(names, values, true);
936
937       names = new String JavaDoc[] { "name", "descriptorType", field };
938       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(100) };
939       validate(names, values, true);
940
941       names = new String JavaDoc[] { "name", "descriptorType", field };
942       values = new Object JavaDoc[] { "testName", "testType", new Integer JavaDoc(-2) };
943       validate(names, values, false);
944    }
945
946    private void validate(String JavaDoc[] names, Object JavaDoc[] values, boolean valid)
947    {
948       DescriptorSupport JavaDoc descriptor = null;
949       RuntimeOperationsException JavaDoc caught = null;
950       boolean descriptorValid = false;
951       try
952       {
953          descriptor = new DescriptorSupport JavaDoc(names, values);
954          descriptorValid = descriptor.isValid();
955       }
956       catch (RuntimeOperationsException JavaDoc e)
957       {
958          caught = e;
959       }
960       if (valid && caught != null)
961          throw caught;
962       assertEquals("Expected "+ valid + " for new Descriptor(String[], String[]) names=" +
963               Arrays.asList(names) + " values=" + Arrays.asList(values), valid, descriptorValid);
964
965       caught = null;
966       descriptorValid = false;
967       try
968       {
969          String JavaDoc[] fields = new String JavaDoc[names.length];
970          for (int i = 0; i < fields.length; i++)
971          {
972             if (values[i] == null)
973                fields[i] = names[i] + "=";
974             else
975                fields[i] = names[i] + "=" + values[i].toString();
976          }
977          descriptor = new DescriptorSupport JavaDoc(names, values);
978          descriptorValid = descriptor.isValid();
979       }
980       catch (RuntimeOperationsException JavaDoc e)
981       {
982          caught = e;
983       }
984       if (valid && caught != null)
985          throw caught;
986       assertEquals("Expected "+ valid + " for new Descriptor(String[], String[]) names=" +
987          Arrays.asList(names) + " values=" + Arrays.asList(values), valid, descriptorValid);
988
989       caught = null;
990       descriptorValid = false;
991       try
992       {
993          descriptor = new DescriptorSupport JavaDoc();
994          for (int i = 0; i < names.length; i++)
995             descriptor.setField(names[i], values[i]);
996          descriptorValid = descriptor.isValid();
997       }
998       catch (RuntimeOperationsException JavaDoc e)
999       {
1000         caught = e;
1001      }
1002      if (valid && caught != null)
1003         throw caught;
1004      assertEquals("Expected "+ valid + " for new Descriptor(String[], String[]) names=" +
1005         Arrays.asList(names) + " values=" + Arrays.asList(values), valid, descriptorValid);
1006
1007      caught = null;
1008      descriptorValid = false;
1009      try
1010      {
1011         descriptor = new DescriptorSupport JavaDoc();
1012         descriptor.setFields(names, values);
1013         descriptorValid = descriptor.isValid();
1014      }
1015      catch (RuntimeOperationsException JavaDoc e)
1016      {
1017         caught = e;
1018      }
1019      if (valid && caught != null)
1020         throw caught;
1021      assertEquals("Expected "+ valid + " for new Descriptor(String[], String[]) names=" +
1022         Arrays.asList(names) + " values=" + Arrays.asList(values), valid, descriptorValid);
1023   }
1024
1025   private void compareFieldNames(String JavaDoc[] one, String JavaDoc[] two)
1026      throws Exception JavaDoc
1027   {
1028      Set JavaDoc setOne = makeMap(one).keySet();
1029      List JavaDoc setTwo = new ArrayList JavaDoc(Arrays.asList(two));
1030      for (Iterator JavaDoc i = setOne.iterator(); i.hasNext(); )
1031      {
1032         Object JavaDoc key = i.next();
1033         if (setTwo.remove(key) == false)
1034            fail("Expected " + Arrays.asList(two) + " to contain field " + key);
1035      }
1036      assertTrue("Didn't expect the following fields " + setTwo, setTwo.isEmpty());
1037   }
1038
1039   private void compareFieldValuesUnsorted(Object JavaDoc[] one, Object JavaDoc[] two)
1040      throws Exception JavaDoc
1041   {
1042      if (one.length != two.length)
1043         fail("Lengths are different original=" + Arrays.asList(one) + " result=" + Arrays.asList(two));
1044
1045      List JavaDoc listOne = Arrays.asList(one);
1046      List JavaDoc listTwo = new ArrayList JavaDoc(Arrays.asList(two));
1047      for (Iterator JavaDoc i = listOne.iterator(); i.hasNext();)
1048      {
1049         Object JavaDoc value = i.next();
1050         if (listTwo.remove(value) == false)
1051            fail("Expected " + two + " to contain " + value);
1052      }
1053      assertTrue("Didn't expect the following fields " + listTwo, listTwo.isEmpty());
1054   }
1055
1056   private void compareFieldValues(Object JavaDoc[] one, Object JavaDoc[] two)
1057      throws Exception JavaDoc
1058   {
1059      if (one.length != two.length)
1060         fail("Lengths are different original=" + Arrays.asList(one) + " result=" + Arrays.asList(two));
1061      for (int i = 0; i < one.length; i++)
1062      {
1063         if (one[i] == null && two[i] != null)
1064            fail("For index " + i + " original=" + one[i] + " result=" + two[i]);
1065         else if (one[i] != null && two[i] == null)
1066            fail("For index " + i + " original=" + one[i] + " result=" + two[i]);
1067         else if (one[i] != null && one[i].equals(two[i]) == false)
1068            fail("For index " + i + " original=" + one[i] + " result=" + two[i]);
1069      }
1070   }
1071
1072   private void compareFields(String JavaDoc[] one, String JavaDoc[] two)
1073      throws Exception JavaDoc
1074   {
1075      Map JavaDoc mapOne = makeMap(one);
1076      Map JavaDoc mapTwo = makeMap(two);
1077      for (Iterator JavaDoc i = mapOne.entrySet().iterator(); i.hasNext(); )
1078      {
1079         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
1080         Object JavaDoc key = entry.getKey();
1081         Object JavaDoc value = entry.getValue();
1082         if (value.equals(mapTwo.remove(key)) == false)
1083            fail("Expected " + Arrays.asList(two) + " to contain field " + key);
1084      }
1085      assertTrue("Didn't expect the following fields " + mapTwo, mapTwo.isEmpty());
1086   }
1087
1088   private Map JavaDoc makeMap(String JavaDoc[] fields)
1089      throws Exception JavaDoc
1090   {
1091      HashMap JavaDoc result = new HashMap JavaDoc(fields.length);
1092      for (int i = 0; i < fields.length; i++)
1093      {
1094         int index = fields[i].indexOf("=");
1095         String JavaDoc key = fields[i].substring(0, index);
1096         String JavaDoc value = null;
1097         if (index != fields[i].length()-1)
1098            value = fields[i].substring(index);
1099         result.put(key, value);
1100      }
1101      return result;
1102   }
1103}
1104
Popular Tags