KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > monitor > MonitorTestCase


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.monitor;
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.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.management.Attribute JavaDoc;
33 import javax.management.MBeanNotificationInfo JavaDoc;
34 import javax.management.MBeanServer JavaDoc;
35 import javax.management.MBeanServerFactory JavaDoc;
36 import javax.management.Notification JavaDoc;
37 import javax.management.NotificationListener JavaDoc;
38 import javax.management.ObjectName JavaDoc;
39 import javax.management.monitor.CounterMonitor JavaDoc;
40 import javax.management.monitor.GaugeMonitor JavaDoc;
41 import javax.management.monitor.Monitor JavaDoc;
42 import javax.management.monitor.MonitorNotification JavaDoc;
43 import javax.management.monitor.StringMonitor JavaDoc;
44
45 import junit.framework.AssertionFailedError;
46 import junit.framework.TestCase;
47
48 import org.jboss.test.jmx.compliance.monitor.support.CounterSupport;
49 import org.jboss.test.jmx.compliance.monitor.support.GaugeSupport;
50 import org.jboss.test.jmx.compliance.monitor.support.MonitorSupport;
51 import org.jboss.test.jmx.compliance.monitor.support.StringSupport;
52
53 /**
54  * Monitor Notification Tests
55  *
56  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
57  */

58 public class MonitorTestCase
59    extends TestCase
60    implements NotificationListener JavaDoc
61 {
62    // Constants ---------------------------------------------------------------
63

64    // Attributes --------------------------------------------------------------
65

66    /**
67     * The MBeanServer
68     */

69    MBeanServer JavaDoc server;
70
71    /**
72     * The object name of the monitor service
73     */

74    ObjectName JavaDoc monitorName;
75
76    /**
77     * The monitor
78     */

79    Monitor JavaDoc monitor;
80
81    /**
82     * The object name of the mbean monitored
83     */

84    ObjectName JavaDoc monitoredName;
85
86    /**
87     * The monitored mbean
88     */

89    MonitorSupport monitored;
90
91    /**
92     * The notifications
93     */

94    ArrayList JavaDoc notifications = new ArrayList JavaDoc();
95
96    // Constructor -------------------------------------------------------------
97

98    /**
99     * Construct the test
100     */

101    public MonitorTestCase(String JavaDoc s)
102    {
103       super(s);
104    }
105
106    // Tests -------------------------------------------------------------------
107

108    /**
109     * Test notification types differ
110     */

111    public void testNotificationTypes()
112    {
113       assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR,
114                    "jmx.monitor.error.attribute");
115       assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,
116                    "jmx.monitor.error.type");
117       assertEquals(MonitorNotification.OBSERVED_OBJECT_ERROR,
118                    "jmx.monitor.error.mbean");
119       assertEquals(MonitorNotification.RUNTIME_ERROR,
120                    "jmx.monitor.error.runtime");
121       assertEquals(MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED,
122                    "jmx.monitor.string.differs");
123       assertEquals(MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED,
124                    "jmx.monitor.string.matches");
125       assertEquals(MonitorNotification.THRESHOLD_ERROR,
126                    "jmx.monitor.error.threshold");
127       assertEquals(MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED,
128                    "jmx.monitor.gauge.high");
129       assertEquals(MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED,
130                    "jmx.monitor.gauge.low");
131       assertEquals(MonitorNotification.THRESHOLD_VALUE_EXCEEDED,
132                    "jmx.monitor.counter.threshold");
133    }
134
135    // Counter monitor notification info ---------------------------------------
136

137    /**
138     * Test the notification info of the counter
139     */

140    public void testCounterNotificationInfo()
141       throws Exception JavaDoc
142    {
143       HashSet JavaDoc expected = new HashSet JavaDoc();
144       expected.add(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR);
145       expected.add(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR);
146       expected.add(MonitorNotification.OBSERVED_OBJECT_ERROR);
147       expected.add(MonitorNotification.RUNTIME_ERROR);
148       expected.add(MonitorNotification.THRESHOLD_ERROR);
149       expected.add(MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
150
151       MBeanNotificationInfo JavaDoc[] mbni = new CounterMonitor JavaDoc().getNotificationInfo();
152       checkNotificationInfo("Counter", mbni, expected);
153    }
154
155    // Counter normal no offset no modulus tests -------------------------------
156

157    /**
158     * Test normal counter threshold no offset no modulus
159     */

160    public void testNormalCounterThresholdExceededEarlyNoOffsetNoModulus()
161       throws Exception JavaDoc
162    {
163       initTest();
164       try
165       {
166          initCounterMonitor(true, new Integer JavaDoc(10),
167                             false, new Integer JavaDoc(0), new Integer JavaDoc(0));
168          expectStartMonitor(new Integer JavaDoc(10),
169             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
170       }
171       finally
172       {
173          stopMonitor();
174          MBeanServerFactory.releaseMBeanServer(server);
175       }
176    }
177
178    /**
179     * Test normal counter threshold no offset no modulus
180     */

181    public void testNormalCounterThresholdExceededLateNoOffsetNoModulus()
182       throws Exception JavaDoc
183    {
184       initTest();
185       try
186       {
187          initCounterMonitor(true, new Integer JavaDoc(10),
188                             false, new Integer JavaDoc(0), new Integer JavaDoc(0));
189          dontExpectStartMonitor(new Integer JavaDoc(0));
190          expect(new Integer JavaDoc(10),
191             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
192       }
193       finally
194       {
195          stopMonitor();
196          MBeanServerFactory.releaseMBeanServer(server);
197       }
198    }
199
200    /**
201     * Test normal counter threshold no offset no modulus
202     */

203    public void testNormalCounterThresholdExceededManyNoOffsetNoModulus()
204       throws Exception JavaDoc
205    {
206       initTest();
207       try
208       {
209          initCounterMonitor(true, new Integer JavaDoc(10),
210                             false, new Integer JavaDoc(0), new Integer JavaDoc(0));
211          dontExpectStartMonitor(new Integer JavaDoc(0));
212          expect(new Integer JavaDoc(10),
213             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
214          dontExpect(new Integer JavaDoc(0));
215          expect(new Integer JavaDoc(11),
216             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
217       }
218       finally
219       {
220          stopMonitor();
221          MBeanServerFactory.releaseMBeanServer(server);
222       }
223    }
224
225    /**
226     * Test normal counter threshold no offset no modulus
227     */

228    public void testNormalCounterThresholdNotExceededNoOffsetNoModulus()
229       throws Exception JavaDoc
230    {
231       initTest();
232       try
233       {
234          initCounterMonitor(true, new Integer JavaDoc(10),
235                             false, new Integer JavaDoc(0), new Integer JavaDoc(0));
236          dontExpectStartMonitor(new Integer JavaDoc(0));
237          dontExpect(new Integer JavaDoc(1));
238          dontExpect(new Integer JavaDoc(-1));
239          dontExpect(new Integer JavaDoc(9));
240          expect(new Integer JavaDoc(10),
241             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
242          dontExpect(new Integer JavaDoc(10));
243          dontExpect(new Integer JavaDoc(11));
244          dontExpect(new Integer JavaDoc(9));
245          expect(new Integer JavaDoc(10),
246             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
247          dontExpect(new Integer JavaDoc(9));
248       }
249       finally
250       {
251          stopMonitor();
252          MBeanServerFactory.releaseMBeanServer(server);
253       }
254    }
255
256    /**
257     * Test normal counter threshold no offset no modulus
258     */

259    public void testNormalCounterThresholdExceededNoneNoOffsetNoModulus()
260       throws Exception JavaDoc
261    {
262       initTest();
263       try
264       {
265          initCounterMonitor(false, new Integer JavaDoc(10),
266                             false, new Integer JavaDoc(0), new Integer JavaDoc(0));
267          dontExpectStartMonitor(new Integer JavaDoc(0));
268          dontExpect(new Integer JavaDoc(10));
269          dontExpect(new Integer JavaDoc(0));
270          dontExpect(new Integer JavaDoc(10));
271       }
272       finally
273       {
274          stopMonitor();
275          MBeanServerFactory.releaseMBeanServer(server);
276       }
277    }
278
279    // Counter normal offset no modulus tests ----------------------------------
280

281    /**
282     * Test normal counter threshold offset no modulus
283     */

284    public void testNormalCounterThresholdExceededEarlyOffsetNoModulus()
285       throws Exception JavaDoc
286    {
287       initTest();
288       try
289       {
290          initCounterMonitor(true, new Integer JavaDoc(10),
291                             false, new Integer JavaDoc(10), new Integer JavaDoc(0));
292          expectStartMonitor(new Integer JavaDoc(10),
293             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
294       }
295       finally
296       {
297          stopMonitor();
298          MBeanServerFactory.releaseMBeanServer(server);
299       }
300    }
301
302    /**
303     * Test normal counter threshold offset no modulus
304     */

305    public void testNormalCounterThresholdExceededLateOffsetNoModulus()
306       throws Exception JavaDoc
307    {
308       initTest();
309       try
310       {
311          initCounterMonitor(true, new Integer JavaDoc(10),
312                             false, new Integer JavaDoc(10), new Integer JavaDoc(0));
313          dontExpectStartMonitor(new Integer JavaDoc(0));
314          expect(new Integer JavaDoc(10),
315             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
316       }
317       finally
318       {
319          stopMonitor();
320          MBeanServerFactory.releaseMBeanServer(server);
321       }
322    }
323
324    /**
325     * Test normal counter threshold offset no modulus
326     */

327    public void testNormalCounterThresholdExceededManyOffsetNoModulus()
328       throws Exception JavaDoc
329    {
330       initTest();
331       try
332       {
333          initCounterMonitor(true, new Integer JavaDoc(10),
334                             false, new Integer JavaDoc(10), new Integer JavaDoc(0));
335          dontExpectStartMonitor(new Integer JavaDoc(0));
336          expect(new Integer JavaDoc(10),
337             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
338          dontExpect(new Integer JavaDoc(0));
339          dontExpect(new Integer JavaDoc(11));
340          expect(new Integer JavaDoc(20),
341             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
342          dontExpect(new Integer JavaDoc(29));
343          expect(new Integer JavaDoc(30),
344             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
345          expect(new Integer JavaDoc(40),
346             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
347       }
348       finally
349       {
350          stopMonitor();
351          MBeanServerFactory.releaseMBeanServer(server);
352       }
353    }
354
355    /**
356     * Test normal counter threshold offset no modulus
357     */

358    public void testNormalCounterThresholdNotExceededOffsetNoModulus()
359       throws Exception JavaDoc
360    {
361       initTest();
362       try
363       {
364          initCounterMonitor(true, new Integer JavaDoc(10),
365                             false, new Integer JavaDoc(10), new Integer JavaDoc(0));
366          dontExpectStartMonitor(new Integer JavaDoc(0));
367          dontExpect(new Integer JavaDoc(1));
368          dontExpect(new Integer JavaDoc(-1));
369          dontExpect(new Integer JavaDoc(9));
370          expect(new Integer JavaDoc(10),
371             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
372          dontExpect(new Integer JavaDoc(10));
373          dontExpect(new Integer JavaDoc(11));
374          dontExpect(new Integer JavaDoc(9));
375          expect(new Integer JavaDoc(20),
376             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
377          dontExpect(new Integer JavaDoc(19));
378       }
379       finally
380       {
381          stopMonitor();
382          MBeanServerFactory.releaseMBeanServer(server);
383       }
384    }
385
386    /**
387     * Test normal counter threshold offset no modulus
388     */

389    public void testNormalCounterThresholdExceededNoneOffsetNoModulus()
390       throws Exception JavaDoc
391    {
392       initTest();
393       try
394       {
395          initCounterMonitor(false, new Integer JavaDoc(10),
396                             false, new Integer JavaDoc(10), new Integer JavaDoc(0));
397          dontExpectStartMonitor(new Integer JavaDoc(0));
398          dontExpect(new Integer JavaDoc(10));
399          dontExpect(new Integer JavaDoc(0));
400          dontExpect(new Integer JavaDoc(20));
401       }
402       finally
403       {
404          stopMonitor();
405          MBeanServerFactory.releaseMBeanServer(server);
406       }
407    }
408
409    // Counter normal no offset modulus tests ---------------------------------
410

411    /**
412     * Test normal counter threshold no offset modulus
413     */

414    public void testNormalCounterThresholdExceededEarlyNoOffsetModulus()
415       throws Exception JavaDoc
416    {
417       initTest();
418       try
419       {
420          initCounterMonitor(true, new Integer JavaDoc(10),
421                             false, new Integer JavaDoc(0), new Integer JavaDoc(10));
422          expectStartMonitor(new Integer JavaDoc(10),
423             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
424       }
425       finally
426       {
427          stopMonitor();
428          MBeanServerFactory.releaseMBeanServer(server);
429       }
430    }
431
432    /**
433     * Test normal counter threshold no offset modulus
434     */

435    public void testNormalCounterThresholdExceededLateNoOffsetModulus()
436       throws Exception JavaDoc
437    {
438       initTest();
439       try
440       {
441          initCounterMonitor(true, new Integer JavaDoc(10),
442                             false, new Integer JavaDoc(0), new Integer JavaDoc(10));
443          dontExpectStartMonitor(new Integer JavaDoc(0));
444          expect(new Integer JavaDoc(10),
445             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
446       }
447       finally
448       {
449          stopMonitor();
450          MBeanServerFactory.releaseMBeanServer(server);
451       }
452    }
453
454    /**
455     * Test normal counter threshold no offset modulus
456     */

457    public void testNormalCounterThresholdExceededManyNoOffsetModulus()
458       throws Exception JavaDoc
459    {
460       initTest();
461       try
462       {
463          initCounterMonitor(true, new Integer JavaDoc(10),
464                             false, new Integer JavaDoc(0), new Integer JavaDoc(10));
465          dontExpectStartMonitor(new Integer JavaDoc(0));
466          expect(new Integer JavaDoc(10),
467             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
468          dontExpect(new Integer JavaDoc(0));
469          expect(new Integer JavaDoc(11),
470             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
471          expect(new Integer JavaDoc(12),
472             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
473          dontExpect(new Integer JavaDoc(0));
474          expect(new Integer JavaDoc(10),
475             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
476       }
477       catch (AssertionFailedError e)
478       {
479          fail("FAILS IN RI: Modulus ignored with no offset???");
480       }
481       finally
482       {
483          stopMonitor();
484          MBeanServerFactory.releaseMBeanServer(server);
485       }
486    }
487
488    /**
489     * Test normal counter threshold no offset modulus
490     */

491    public void testNormalCounterThresholdNotExceededNoOffsetModulus()
492       throws Exception JavaDoc
493    {
494       initTest();
495       try
496       {
497          initCounterMonitor(true, new Integer JavaDoc(10),
498                             false, new Integer JavaDoc(0), new Integer JavaDoc(10));
499          dontExpectStartMonitor(new Integer JavaDoc(0));
500          dontExpect(new Integer JavaDoc(1));
501          dontExpect(new Integer JavaDoc(-1));
502          dontExpect(new Integer JavaDoc(9));
503          expect(new Integer JavaDoc(10),
504             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
505          dontExpect(new Integer JavaDoc(9));
506          expect(new Integer JavaDoc(10),
507             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
508          dontExpect(new Integer JavaDoc(9));
509       }
510       finally
511       {
512          stopMonitor();
513          MBeanServerFactory.releaseMBeanServer(server);
514       }
515    }
516
517    /**
518     * Test normal counter threshold no offset modulus
519     */

520    public void testNormalCounterThresholdExceededNoneNoOffsetModulus()
521       throws Exception JavaDoc
522    {
523       initTest();
524       try
525       {
526          initCounterMonitor(false, new Integer JavaDoc(10),
527                             false, new Integer JavaDoc(0), new Integer JavaDoc(10));
528          dontExpectStartMonitor(new Integer JavaDoc(0));
529          dontExpect(new Integer JavaDoc(10));
530          dontExpect(new Integer JavaDoc(0));
531          dontExpect(new Integer JavaDoc(10));
532       }
533       finally
534       {
535          stopMonitor();
536          MBeanServerFactory.releaseMBeanServer(server);
537       }
538    }
539
540    // Counter normal offset modulus tests -------------------------------------
541

542    /**
543     * Test normal counter threshold offset modulus
544     */

545    public void testNormalCounterThresholdExceededEarlyOffsetModulus()
546       throws Exception JavaDoc
547    {
548       initTest();
549       try
550       {
551          initCounterMonitor(true, new Integer JavaDoc(10),
552                             false, new Integer JavaDoc(10), new Integer JavaDoc(10));
553          expectStartMonitor(new Integer JavaDoc(10),
554             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
555       }
556       finally
557       {
558          stopMonitor();
559          MBeanServerFactory.releaseMBeanServer(server);
560       }
561    }
562
563    /**
564     * Test normal counter threshold offset modulus
565     */

566    public void testNormalCounterThresholdExceededLateOffsetModulus()
567       throws Exception JavaDoc
568    {
569       initTest();
570       try
571       {
572          initCounterMonitor(true, new Integer JavaDoc(10),
573                             false, new Integer JavaDoc(10), new Integer JavaDoc(10));
574          dontExpectStartMonitor(new Integer JavaDoc(0));
575          expect(new Integer JavaDoc(10),
576             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
577       }
578       finally
579       {
580          stopMonitor();
581          MBeanServerFactory.releaseMBeanServer(server);
582       }
583    }
584
585    /**
586     * Test normal counter threshold offset modulus
587     */

588    public void testNormalCounterThresholdExceededManyOffsetModulus()
589       throws Exception JavaDoc
590    {
591       initTest();
592       try
593       {
594          initCounterMonitor(true, new Integer JavaDoc(10),
595                             false, new Integer JavaDoc(10), new Integer JavaDoc(20));
596          dontExpectStartMonitor(new Integer JavaDoc(0));
597          expect(new Integer JavaDoc(10),
598             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
599          dontExpect(new Integer JavaDoc(0));
600          dontExpect(new Integer JavaDoc(12));
601          expect(new Integer JavaDoc(20),
602             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
603          if (((CounterMonitor JavaDoc)monitor).getThreshold().equals(new Integer JavaDoc(30)))
604            fail("FAILS IN RI: Threshold 10, Offset 10, Modulus 20 should " +
605                 " never get a threshold of 30");
606          expect(new Integer JavaDoc(10),
607             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
608          dontExpect(new Integer JavaDoc(10));
609          expect(new Integer JavaDoc(20),
610             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
611          expect(new Integer JavaDoc(20),
612             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
613          expect(new Integer JavaDoc(30),
614             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
615       }
616       finally
617       {
618          stopMonitor();
619          MBeanServerFactory.releaseMBeanServer(server);
620       }
621    }
622
623    /**
624     * Test normal counter threshold offset modulus
625     */

626    public void testNormalCounterThresholdNotExceededOffsetModulus()
627       throws Exception JavaDoc
628    {
629       initTest();
630       try
631       {
632          initCounterMonitor(true, new Integer JavaDoc(10),
633                             false, new Integer JavaDoc(10), new Integer JavaDoc(20));
634          dontExpectStartMonitor(new Integer JavaDoc(0));
635          dontExpect(new Integer JavaDoc(1));
636          dontExpect(new Integer JavaDoc(-1));
637          dontExpect(new Integer JavaDoc(9));
638          expect(new Integer JavaDoc(10),
639             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
640          dontExpect(new Integer JavaDoc(10));
641          dontExpect(new Integer JavaDoc(11));
642          dontExpect(new Integer JavaDoc(9));
643          expect(new Integer JavaDoc(20),
644             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
645          if (((CounterMonitor JavaDoc)monitor).getThreshold().equals(new Integer JavaDoc(30)))
646            fail("FAILS IN RI: Threshold 10, Offset 10, Modulus 20 should " +
647                 " never get a threshold of 30");
648          expect(new Integer JavaDoc(19),
649             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
650       }
651       finally
652       {
653          stopMonitor();
654          MBeanServerFactory.releaseMBeanServer(server);
655       }
656    }
657
658    /**
659     * Test normal counter threshold offset modulus
660     */

661    public void testNormalCounterThresholdExceededNoneOffsetModulus()
662       throws Exception JavaDoc
663    {
664       initTest();
665       try
666       {
667          initCounterMonitor(false, new Integer JavaDoc(10),
668                             false, new Integer JavaDoc(10), new Integer JavaDoc(10));
669          dontExpectStartMonitor(new Integer JavaDoc(0));
670          dontExpect(new Integer JavaDoc(10));
671          dontExpect(new Integer JavaDoc(0));
672          dontExpect(new Integer JavaDoc(20));
673       }
674       finally
675       {
676          stopMonitor();
677          MBeanServerFactory.releaseMBeanServer(server);
678       }
679    }
680
681    // Counter difference no offset no modulus tests ---------------------------
682

683    /**
684     * Test difference counter threshold no offset no modulus
685     */

686    public void testDiffCounterThresholdExceededNoOffsetNoModulus()
687       throws Exception JavaDoc
688    {
689       initTest();
690       try
691       {
692          initCounterMonitor(true, new Integer JavaDoc(10),
693                             true, new Integer JavaDoc(0), new Integer JavaDoc(0));
694          dontExpectStartMonitor(new Integer JavaDoc(0));
695          expect(new Integer JavaDoc(10),
696             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
697       }
698       finally
699       {
700          stopMonitor();
701          MBeanServerFactory.releaseMBeanServer(server);
702       }
703    }
704
705    /**
706     * Test difference counter threshold no offset no modulus
707     */

708    public void testDiffCounterThresholdExceededManyNoOffsetNoModulus()
709       throws Exception JavaDoc
710    {
711       initTest();
712       try
713       {
714          initCounterMonitor(true, new Integer JavaDoc(10),
715                             true, new Integer JavaDoc(0), new Integer JavaDoc(0));
716          dontExpectStartMonitor(new Integer JavaDoc(0));
717          expect(new Integer JavaDoc(10),
718             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
719          expectDiff(new Integer JavaDoc(0), new Integer JavaDoc(11),
720             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
721       }
722       finally
723       {
724          stopMonitor();
725          MBeanServerFactory.releaseMBeanServer(server);
726       }
727    }
728
729    /**
730     * Test difference counter threshold no offset no modulus
731     */

732    public void testDiffCounterThresholdNotExceededNoOffsetNoModulus()
733       throws Exception JavaDoc
734    {
735       initTest();
736       try
737       {
738          initCounterMonitor(true, new Integer JavaDoc(10),
739                             true, new Integer JavaDoc(0), new Integer JavaDoc(0));
740          dontExpectStartMonitorDiff(new Integer JavaDoc(0), new Integer JavaDoc(1));
741          dontExpectDiff(new Integer JavaDoc(0), new Integer JavaDoc(9));
742          dontExpectDiff(new Integer JavaDoc(1), new Integer JavaDoc(10));
743          dontExpectDiff(new Integer JavaDoc(9), new Integer JavaDoc(11));
744          dontExpectDiff(new Integer JavaDoc(9), new Integer JavaDoc(0));
745       }
746       finally
747       {
748          stopMonitor();
749          MBeanServerFactory.releaseMBeanServer(server);
750       }
751    }
752
753    /**
754     * Test difference counter threshold no offset no modulus
755     */

756    public void testDiffCounterThresholdExceededNoneNoOffsetNoModulus()
757       throws Exception JavaDoc
758    {
759       initTest();
760       try
761       {
762          initCounterMonitor(false, new Integer JavaDoc(10),
763                             true, new Integer JavaDoc(0), new Integer JavaDoc(0));
764          dontExpectStartMonitorDiff(new Integer JavaDoc(0), new Integer JavaDoc(10));
765          dontExpectDiff(new Integer JavaDoc(0), new Integer JavaDoc(-10));
766          dontExpectDiff(new Integer JavaDoc(0), new Integer JavaDoc(100));
767       }
768       finally
769       {
770          stopMonitor();
771          MBeanServerFactory.releaseMBeanServer(server);
772       }
773    }
774
775    // Counter difference offset no modulus tests ------------------------------
776

777    /**
778     * Test difference counter threshold offset no modulus
779     */

780    public void testDiffCounterThresholdExceededOffsetNoModulus()
781       throws Exception JavaDoc
782    {
783       initTest();
784       try
785       {
786          initCounterMonitor(true, new Integer JavaDoc(10),
787                             true, new Integer JavaDoc(10), new Integer JavaDoc(0));
788          dontExpectStartMonitor(new Integer JavaDoc(0));
789          expect(new Integer JavaDoc(10),
790             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
791       }
792       finally
793       {
794          stopMonitor();
795          MBeanServerFactory.releaseMBeanServer(server);
796       }
797    }
798
799    /**
800     * Test difference counter threshold offset no modulus
801     */

802    public void testDiffCounterThresholdExceededManyOffsetNoModulus()
803       throws Exception JavaDoc
804    {
805       initTest();
806       try
807       {
808          initCounterMonitor(true, new Integer JavaDoc(10),
809                             true, new Integer JavaDoc(10), new Integer JavaDoc(0));
810          dontExpectStartMonitor(new Integer JavaDoc(0));
811          expect(new Integer JavaDoc(10),
812             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
813          expectDiff(new Integer JavaDoc(0), new Integer JavaDoc(10),
814             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
815          expectDiff(new Integer JavaDoc(10), new Integer JavaDoc(30),
816             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
817          expectDiff(new Integer JavaDoc(30), new Integer JavaDoc(60),
818             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
819          expectDiff(new Integer JavaDoc(60), new Integer JavaDoc(100),
820             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
821          expectDiff(new Integer JavaDoc(0), new Integer JavaDoc(10),
822             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
823       }
824       finally
825       {
826          stopMonitor();
827          MBeanServerFactory.releaseMBeanServer(server);
828       }
829    }
830
831    /**
832     * Test difference counter threshold offset no modulus
833     */

834    public void testDiffCounterThresholdNotExceededOffsetNoModulus()
835       throws Exception JavaDoc
836    {
837       initTest();
838       try
839       {
840          initCounterMonitor(true, new Integer JavaDoc(10),
841                             true, new Integer JavaDoc(10), new Integer JavaDoc(0));
842          dontExpectStartMonitorDiff(new Integer JavaDoc(0), new Integer JavaDoc(1));
843          expectDiff(new Integer JavaDoc(0), new Integer JavaDoc(10),
844             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
845          dontExpectDiff(new Integer JavaDoc(10), new Integer JavaDoc(20));
846          expectDiff(new Integer JavaDoc(20), new Integer JavaDoc(40),
847             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
848          dontExpectDiff(new Integer JavaDoc(40), new Integer JavaDoc(69));
849       }
850       finally
851       {
852          stopMonitor();
853          MBeanServerFactory.releaseMBeanServer(server);
854       }
855    }
856
857    /**
858     * Test difference counter threshold offset no modulus
859     */

860    public void testDiffCounterThresholdExceededNoneOffsetNoModulus()
861       throws Exception JavaDoc
862    {
863       initTest();
864       try
865       {
866          initCounterMonitor(false, new Integer JavaDoc(10),
867                             true, new Integer JavaDoc(10), new Integer JavaDoc(0));
868          dontExpectStartMonitorDiff(new Integer JavaDoc(0), new Integer JavaDoc(1));
869          dontExpectDiff(new Integer JavaDoc(0), new Integer JavaDoc(10));
870          dontExpectDiff(new Integer JavaDoc(10), new Integer JavaDoc(20));
871          dontExpectDiff(new Integer JavaDoc(20), new Integer JavaDoc(40));
872          dontExpectDiff(new Integer JavaDoc(40), new Integer JavaDoc(69));
873       }
874       finally
875       {
876          stopMonitor();
877          MBeanServerFactory.releaseMBeanServer(server);
878       }
879    }
880
881    // Counter difference no offset modulus tests ------------------------------
882

883    /**
884     * Test difference counter threshold no offset modulus
885     */

886    public void testDiffCounterThresholdExceededNoOffsetModulus()
887       throws Exception JavaDoc
888    {
889       initTest();
890       try
891       {
892          initCounterMonitor(true, new Integer JavaDoc(10),
893                             true, new Integer JavaDoc(0), new Integer JavaDoc(10));
894          dontExpectStartMonitor(new Integer JavaDoc(0));
895          expect(new Integer JavaDoc(10),
896             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
897       }
898       finally
899       {
900          stopMonitor();
901          MBeanServerFactory.releaseMBeanServer(server);
902       }
903    }
904
905    /**
906     * Test difference counter threshold no offset modulus
907     */

908    public void testDiffCounterThresholdExceededManyNoOffsetModulus()
909       throws Exception JavaDoc
910    {
911       initTest();
912       try
913       {
914          initCounterMonitor(true, new Integer JavaDoc(10),
915                             true, new Integer JavaDoc(0), new Integer JavaDoc(10));
916          dontExpectStartMonitor(new Integer JavaDoc(0));
917          expect(new Integer JavaDoc(10),
918             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
919          expectDiff(new Integer JavaDoc(0), new Integer JavaDoc(11),
920             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
921          expectDiff(new Integer JavaDoc(12), new Integer JavaDoc(22),
922             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
923          expectDiff(new Integer JavaDoc(0), new Integer JavaDoc(10),
924             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
925       }
926       finally
927       {
928          stopMonitor();
929          MBeanServerFactory.releaseMBeanServer(server);
930       }
931    }
932
933    /**
934     * Test difference counter threshold no offset modulus
935     */

936    public void testDiffCounterThresholdNotExceededNoOffsetModulus()
937       throws Exception JavaDoc
938    {
939       initTest();
940       try
941       {
942          initCounterMonitor(true, new Integer JavaDoc(10),
943                             true, new Integer JavaDoc(0), new Integer JavaDoc(10));
944          dontExpectStartMonitorDiff(new Integer JavaDoc(0), new Integer JavaDoc(1));
945          dontExpectDiff(new Integer JavaDoc(0), new Integer JavaDoc(9));
946          dontExpectDiff(new Integer JavaDoc(11), new Integer JavaDoc(20));
947          dontExpectDiffModulus(new Integer JavaDoc(10), new Integer JavaDoc(-3), new Integer JavaDoc(10));
948       }
949       finally
950       {
951          stopMonitor();
952          MBeanServerFactory.releaseMBeanServer(server);
953       }
954    }
955
956    /**
957     * Test difference counter threshold no offset modulus
958     */

959    public void testDiffCounterThresholdExceededNoneNoOffsetModulus()
960       throws Exception JavaDoc
961    {
962       initTest();
963       try
964       {
965          initCounterMonitor(false, new Integer JavaDoc(10),
966                             true, new Integer JavaDoc(0), new Integer JavaDoc(10));
967          dontExpectStartMonitorDiff(new Integer JavaDoc(0), new Integer JavaDoc(10));
968          dontExpectDiffModulus(new Integer JavaDoc(0), new Integer JavaDoc(-10), new Integer JavaDoc(10));
969          dontExpectDiff(new Integer JavaDoc(0), new Integer JavaDoc(100));
970       }
971       finally
972       {
973          stopMonitor();
974          MBeanServerFactory.releaseMBeanServer(server);
975       }
976    }
977
978    // Counter difference offset modulus tests ---------------------------------
979

980    /**
981     * Test difference counter threshold offset modulus
982     */

983    public void testDiffCounterThresholdExceededOffsetModulus()
984       throws Exception JavaDoc
985    {
986       initTest();
987       try
988       {
989          initCounterMonitor(true, new Integer JavaDoc(10),
990                             true, new Integer JavaDoc(10), new Integer JavaDoc(10));
991          dontExpectStartMonitor(new Integer JavaDoc(0));
992          expect(new Integer JavaDoc(10),
993             MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
994       }
995       finally
996       {
997          stopMonitor();
998          MBeanServerFactory.releaseMBeanServer(server);
999       }
1000   }
1001
1002   /**
1003    * Test difference counter threshold offset modulus
1004    */

1005   public void testDiffCounterThresholdExceededManyOffsetModulus()
1006      throws Exception JavaDoc
1007   {
1008      initTest();
1009      try
1010      {
1011         initCounterMonitor(true, new Integer JavaDoc(10),
1012                            true, new Integer JavaDoc(10), new Integer JavaDoc(20));
1013         dontExpectStartMonitor(new Integer JavaDoc(0));
1014         expectDiff(new Integer JavaDoc(0), new Integer JavaDoc(11),
1015            MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
1016         expectDiff(new Integer JavaDoc(11), new Integer JavaDoc(31),
1017            MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
1018         expectDiff(new Integer JavaDoc(0), new Integer JavaDoc(10),
1019            MonitorNotification.THRESHOLD_VALUE_EXCEEDED);
1020      }
1021      finally
1022      {
1023         stopMonitor();
1024         MBeanServerFactory.releaseMBeanServer(server);
1025      }
1026   }
1027
1028   /**
1029    * Test difference counter threshold offset modulus
1030    */

1031   public void testDiffCounterThresholdNotExceededOffsetModulus()
1032      throws Exception JavaDoc
1033   {
1034      initTest();
1035      try
1036      {
1037         initCounterMonitor(true, new Integer JavaDoc(10),
1038                            true, new Integer JavaDoc(10), new Integer JavaDoc(20));
1039         dontExpectStartMonitorDiff(new Integer JavaDoc(0), new Integer JavaDoc(1));
1040         dontExpectDiff(new Integer JavaDoc(1), new Integer JavaDoc(10));
1041         dontExpectDiffModulus(new Integer JavaDoc(10), new Integer JavaDoc(-13), new Integer JavaDoc(20));
1042      }
1043      finally
1044      {
1045         stopMonitor();
1046         MBeanServerFactory.releaseMBeanServer(server);
1047      }
1048   }
1049
1050   /**
1051    * Test difference counter threshold offset modulus
1052    */

1053   public void testDiffCounterThresholdExceededNoneOffsetModulus()
1054      throws Exception JavaDoc
1055   {
1056      initTest();
1057      try
1058      {
1059         initCounterMonitor(false, new Integer JavaDoc(10),
1060                            true, new Integer JavaDoc(10), new Integer JavaDoc(20));
1061         dontExpectStartMonitorDiff(new Integer JavaDoc(0), new Integer JavaDoc(10));
1062         dontExpectDiffModulus(new Integer JavaDoc(0), new Integer JavaDoc(-10), new Integer JavaDoc(20));
1063         dontExpectDiff(new Integer JavaDoc(0), new Integer JavaDoc(100));
1064      }
1065      finally
1066      {
1067         stopMonitor();
1068         MBeanServerFactory.releaseMBeanServer(server);
1069      }
1070   }
1071
1072   // Counter error tests -----------------------------------------------------
1073

1074   /**
1075    * Test invalid attribute
1076    */

1077   public void testCounterInvalidAttribute()
1078      throws Exception JavaDoc
1079   {
1080      initTest();
1081      try
1082      {
1083         initCounterMonitor(true, new Integer JavaDoc(10),
1084                            false, new Integer JavaDoc(10), new Integer JavaDoc(10));
1085         attributeErrorStartMonitor(new Integer JavaDoc(0));
1086      }
1087      finally
1088      {
1089         stopMonitor();
1090         MBeanServerFactory.releaseMBeanServer(server);
1091      }
1092   }
1093
1094   /**
1095    * Test invalid attribute
1096    */

1097   public void testCounterInvalidAttributeNull()
1098      throws Exception JavaDoc
1099   {
1100      initTest();
1101      try
1102      {
1103         initCounterMonitor(true, new Integer JavaDoc(10),
1104                            false, new Integer JavaDoc(10), new Integer JavaDoc(10));
1105         attributeNullStartMonitor(new Integer JavaDoc(0));
1106      }
1107      finally
1108      {
1109         stopMonitor();
1110         MBeanServerFactory.releaseMBeanServer(server);
1111      }
1112   }
1113
1114   /**
1115    * Test invalid attribute type
1116    */

1117   public void testCounterInvalidAttributeType()
1118      throws Exception JavaDoc
1119   {
1120      initTest();
1121      try
1122      {
1123         initCounterMonitor(true, new Integer JavaDoc(10),
1124                            false, new Integer JavaDoc(10), new Integer JavaDoc(10));
1125         attributeTypeStartMonitor(new Integer JavaDoc(0));
1126      }
1127      finally
1128      {
1129         stopMonitor();
1130         MBeanServerFactory.releaseMBeanServer(server);
1131      }
1132   }
1133
1134   /**
1135    * Test write only attribute
1136    */

1137   public void testCounterWriteOnly()
1138      throws Exception JavaDoc
1139   {
1140      initTest();
1141      try
1142      {
1143         initCounterMonitor(true, new Integer JavaDoc(10),
1144                            false, new Integer JavaDoc(10), new Integer JavaDoc(10));
1145         attributeWriteStartMonitor(new Integer JavaDoc(0));
1146      }
1147      finally
1148      {
1149         stopMonitor();
1150         MBeanServerFactory.releaseMBeanServer(server);
1151      }
1152   }
1153
1154   /**
1155    * Test invalid attribute type
1156    */

1157   public void testCounterInvalidObjectName()
1158      throws Exception JavaDoc
1159   {
1160      initTest();
1161      try
1162      {
1163         initCounterMonitor(true, new Integer JavaDoc(10),
1164                            false, new Integer JavaDoc(10), new Integer JavaDoc(10));
1165         objectNameStartMonitor(new Integer JavaDoc(0));
1166      }
1167      finally
1168      {
1169         stopMonitor();
1170         MBeanServerFactory.releaseMBeanServer(server);
1171      }
1172   }
1173
1174   /**
1175    * Test invalid threshold
1176    */

1177   public void testCounterInvalidThreshold()
1178      throws Exception JavaDoc
1179   {
1180      initTest();
1181      try
1182      {
1183         initCounterMonitor(true, new Long JavaDoc(10),
1184                            false, new Integer JavaDoc(10), new Integer JavaDoc(10));
1185         objectNameStartMonitor(new Integer JavaDoc(0));
1186      }
1187      finally
1188      {
1189         stopMonitor();
1190         MBeanServerFactory.releaseMBeanServer(server);
1191      }
1192   }
1193
1194   /**
1195    * Test invalid threshold
1196    */

1197   public void testCounterInvalidOffset()
1198      throws Exception JavaDoc
1199   {
1200      initTest();
1201      try
1202      {
1203         initCounterMonitor(true, new Integer JavaDoc(10),
1204                            false, new Long JavaDoc(10), new Integer JavaDoc(10));
1205         objectNameStartMonitor(new Integer JavaDoc(0));
1206      }
1207      finally
1208      {
1209         stopMonitor();
1210         MBeanServerFactory.releaseMBeanServer(server);
1211      }
1212   }
1213
1214   /**
1215    * Test invalid threshold
1216    */

1217   public void testCounterInvalidModulus()
1218      throws Exception JavaDoc
1219   {
1220      initTest();
1221      try
1222      {
1223         initCounterMonitor(true, new Integer JavaDoc(10),
1224                            false, new Integer JavaDoc(10), new Long JavaDoc(10));
1225         objectNameStartMonitor(new Integer JavaDoc(0));
1226      }
1227      finally
1228      {
1229         stopMonitor();
1230         MBeanServerFactory.releaseMBeanServer(server);
1231      }
1232   }
1233
1234   /**
1235    * Test runtime error
1236    */

1237   public void testCounterRuntimeError()
1238      throws Exception JavaDoc
1239   {
1240      initTest();
1241      try
1242      {
1243         initCounterMonitor(true, new Integer JavaDoc(10),
1244                            false, new Integer JavaDoc(10), new Integer JavaDoc(10));
1245         runtimeErrorStartMonitor(new Integer JavaDoc(0));
1246      }
1247      finally
1248      {
1249         stopMonitor();
1250         MBeanServerFactory.releaseMBeanServer(server);
1251      }
1252   }
1253
1254   // Gauge notification tests ------------------------------------------------
1255

1256   /**
1257    * Test the notification info of the gauge
1258    */

1259   public void testGaugeNotificationInfo()
1260      throws Exception JavaDoc
1261   {
1262      HashSet JavaDoc expected = new HashSet JavaDoc();
1263      expected.add(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR);
1264      expected.add(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR);
1265      expected.add(MonitorNotification.OBSERVED_OBJECT_ERROR);
1266      expected.add(MonitorNotification.RUNTIME_ERROR);
1267      expected.add(MonitorNotification.THRESHOLD_ERROR);
1268      expected.add(MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED);
1269      expected.add(MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED);
1270
1271      MBeanNotificationInfo JavaDoc[] mbni = new GaugeMonitor JavaDoc().getNotificationInfo();
1272      checkNotificationInfo("Gauge", mbni, expected);
1273   }
1274
1275   // Gauge error tests -------------------------------------------------------
1276

1277   /**
1278    * Test invalid attribute
1279    */

1280   public void testGaugeInvalidAttribute()
1281      throws Exception JavaDoc
1282   {
1283      initTest();
1284      try
1285      {
1286         initGaugeMonitor(true, true, new Integer JavaDoc(10), new Integer JavaDoc(0), false);
1287         attributeErrorStartMonitor(new Integer JavaDoc(0));
1288      }
1289      finally
1290      {
1291         stopMonitor();
1292         MBeanServerFactory.releaseMBeanServer(server);
1293      }
1294   }
1295
1296   /**
1297    * Test invalid attribute
1298    */

1299   public void testGaugeInvalidAttributeNull()
1300      throws Exception JavaDoc
1301   {
1302      initTest();
1303      try
1304      {
1305         initGaugeMonitor(true, true, new Integer JavaDoc(10), new Integer JavaDoc(0), false);
1306         attributeNullStartMonitor(new Integer JavaDoc(0));
1307      }
1308      finally
1309      {
1310         stopMonitor();
1311         MBeanServerFactory.releaseMBeanServer(server);
1312      }
1313   }
1314
1315   /**
1316    * Test invalid attribute type
1317    */

1318   public void testGaugeInvalidAttributeType()
1319      throws Exception JavaDoc
1320   {
1321      initTest();
1322      try
1323      {
1324         initGaugeMonitor(true, true, new Integer JavaDoc(10), new Integer JavaDoc(0), false);
1325         attributeTypeStartMonitor(new Integer JavaDoc(0));
1326      }
1327      finally
1328      {
1329         stopMonitor();
1330         MBeanServerFactory.releaseMBeanServer(server);
1331      }
1332   }
1333
1334   /**
1335    * Test write only
1336    */

1337   public void testGaugeWriteOnly()
1338      throws Exception JavaDoc
1339   {
1340      initTest();
1341      try
1342      {
1343         initGaugeMonitor(true, true, new Integer JavaDoc(10), new Integer JavaDoc(0), false);
1344         attributeWriteStartMonitor(new Integer JavaDoc(0));
1345      }
1346      finally
1347      {
1348         stopMonitor();
1349         MBeanServerFactory.releaseMBeanServer(server);
1350      }
1351   }
1352
1353   /**
1354    * Test invalid attribute type
1355    */

1356   public void testGaugeInvalidObjectName()
1357      throws Exception JavaDoc
1358   {
1359      initTest();
1360      try
1361      {
1362         initGaugeMonitor(true, true, new Integer JavaDoc(10), new Integer JavaDoc(0), false);
1363         objectNameStartMonitor(new Integer JavaDoc(0));
1364      }
1365      finally
1366      {
1367         stopMonitor();
1368         MBeanServerFactory.releaseMBeanServer(server);
1369      }
1370   }
1371
1372   /**
1373    * Test invalid threshold
1374    */

1375   public void testGaugeInvalidThreshold()
1376      throws Exception JavaDoc
1377   {
1378      initTest();
1379      try
1380      {
1381         initGaugeMonitor(true, true, new Long JavaDoc(10), new Long JavaDoc(0), false);
1382         objectNameStartMonitor(new Integer JavaDoc(0));
1383      }
1384      finally
1385      {
1386         stopMonitor();
1387         MBeanServerFactory.releaseMBeanServer(server);
1388      }
1389   }
1390
1391   /**
1392    * Test runtime error
1393    */

1394   public void testGaugeRuntimeError()
1395      throws Exception JavaDoc
1396   {
1397      initTest();
1398      try
1399      {
1400         initGaugeMonitor(true, true, new Integer JavaDoc(10), new Integer JavaDoc(0), false);
1401         runtimeErrorStartMonitor(new Integer JavaDoc(0));
1402      }
1403      finally
1404      {
1405         stopMonitor();
1406         MBeanServerFactory.releaseMBeanServer(server);
1407      }
1408   }
1409
1410   // String notification tests -----------------------------------------------
1411

1412   /**
1413    * Test the notification info of the string
1414    */

1415   public void testStringNotificationInfo()
1416      throws Exception JavaDoc
1417   {
1418      HashSet JavaDoc expected = new HashSet JavaDoc();
1419      expected.add(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR);
1420      expected.add(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR);
1421      expected.add(MonitorNotification.OBSERVED_OBJECT_ERROR);
1422      expected.add(MonitorNotification.RUNTIME_ERROR);
1423      expected.add(MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1424      expected.add(MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
1425
1426      MBeanNotificationInfo JavaDoc[] mbni = new StringMonitor JavaDoc().getNotificationInfo();
1427      checkNotificationInfo("String", mbni, expected);
1428   }
1429
1430   // String test -------------------------------------------------------------
1431

1432   /**
1433    * Test a string differs
1434    */

1435   public void testStringDifferEarly()
1436      throws Exception JavaDoc
1437   {
1438      initTest();
1439      try
1440      {
1441         initStringMonitor(true, false, "Hello");
1442         expectStartMonitor("Goodbye",
1443            MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1444      }
1445      finally
1446      {
1447         stopMonitor();
1448         MBeanServerFactory.releaseMBeanServer(server);
1449      }
1450   }
1451
1452   /**
1453    * Test a string differs
1454    */

1455   public void testStringDifferLate()
1456      throws Exception JavaDoc
1457   {
1458      initTest();
1459      try
1460      {
1461         initStringMonitor(true, false, "Hello");
1462         dontExpectStartMonitor("Hello");
1463         expect("Goodbye",
1464            MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1465      }
1466      finally
1467      {
1468         stopMonitor();
1469         MBeanServerFactory.releaseMBeanServer(server);
1470      }
1471   }
1472
1473   /**
1474    * Test a string differs many
1475    */

1476   public void testStringDifferManyEarly()
1477      throws Exception JavaDoc
1478   {
1479      initTest();
1480      try
1481      {
1482         initStringMonitor(true, false, "Hello");
1483         expectStartMonitor("Goodbye",
1484            MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1485         dontExpect("Hello");
1486         expect("Goodbye",
1487            MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1488         dontExpect("Hello");
1489         expect("Goodbye",
1490            MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1491         dontExpect("Hello");
1492      }
1493      finally
1494      {
1495         stopMonitor();
1496         MBeanServerFactory.releaseMBeanServer(server);
1497      }
1498   }
1499
1500   /**
1501    * Test a string differs many
1502    */

1503   public void testStringDifferManyLate()
1504      throws Exception JavaDoc
1505   {
1506      initTest();
1507      try
1508      {
1509         initStringMonitor(true, false, "Hello");
1510         dontExpectStartMonitor("Hello");
1511         expect("Goodbye",
1512            MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1513         dontExpect("Hello");
1514         expect("Goodbye",
1515            MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1516         dontExpect("Hello");
1517         expect("Goodbye",
1518            MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1519         dontExpect("Hello");
1520      }
1521      finally
1522      {
1523         stopMonitor();
1524         MBeanServerFactory.releaseMBeanServer(server);
1525      }
1526   }
1527
1528   /**
1529    * Test a string matches
1530    */

1531   public void testStringMatchEarly()
1532      throws Exception JavaDoc
1533   {
1534      initTest();
1535      try
1536      {
1537         initStringMonitor(false, true, "Hello");
1538         expectStartMonitor("Hello",
1539            MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
1540      }
1541      finally
1542      {
1543         stopMonitor();
1544         MBeanServerFactory.releaseMBeanServer(server);
1545      }
1546   }
1547
1548   /**
1549    * Test a string matches
1550    */

1551   public void testStringMatchLate()
1552      throws Exception JavaDoc
1553   {
1554      initTest();
1555      try
1556      {
1557         initStringMonitor(false, true, "Hello");
1558         dontExpectStartMonitor("Goodbye");
1559         expect("Hello",
1560            MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
1561      }
1562      finally
1563      {
1564         stopMonitor();
1565         MBeanServerFactory.releaseMBeanServer(server);
1566      }
1567   }
1568
1569   /**
1570    * Test a string matches
1571    */

1572   public void testStringMatchManyEarly()
1573      throws Exception JavaDoc
1574   {
1575      initTest();
1576      try
1577      {
1578         initStringMonitor(false, true, "Hello");
1579         expectStartMonitor("Hello",
1580            MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
1581         dontExpect("Goodbye");
1582         expect("Hello",
1583            MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
1584         dontExpect("Goodbye");
1585         expect("Hello",
1586            MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
1587      }
1588      finally
1589      {
1590         stopMonitor();
1591         MBeanServerFactory.releaseMBeanServer(server);
1592      }
1593   }
1594
1595   /**
1596    * Test a string matches
1597    */

1598   public void testStringMatchManyLate()
1599      throws Exception JavaDoc
1600   {
1601      initTest();
1602      try
1603      {
1604         initStringMonitor(false, true, "Hello");
1605         dontExpectStartMonitor("Goodbye");
1606         expect("Hello",
1607            MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
1608         dontExpect("Goodbye");
1609         expect("Hello",
1610            MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
1611         dontExpect("Goodbye");
1612         expect("Hello",
1613            MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
1614      }
1615      finally
1616      {
1617         stopMonitor();
1618         MBeanServerFactory.releaseMBeanServer(server);
1619      }
1620   }
1621
1622   /**
1623    * Test a string differs and matches
1624    */

1625   public void testStringBoth()
1626      throws Exception JavaDoc
1627   {
1628      initTest();
1629      try
1630      {
1631         initStringMonitor(true, true, "Hello");
1632         expectStartMonitor("Goodbye",
1633            MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1634         expect("Hello",
1635            MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
1636      }
1637      finally
1638      {
1639         stopMonitor();
1640         MBeanServerFactory.releaseMBeanServer(server);
1641      }
1642   }
1643
1644   /**
1645    * Test a string differs and matches
1646    */

1647   public void testStringBothMany()
1648      throws Exception JavaDoc
1649   {
1650      initTest();
1651      try
1652      {
1653         initStringMonitor(true, true, "Hello");
1654         expectStartMonitor("Goodbye",
1655            MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1656         expect("Hello",
1657            MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
1658         expect("Goodbye",
1659            MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1660         expect("Hello",
1661            MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
1662         expect("Goodbye",
1663            MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
1664      }
1665      finally
1666      {
1667         stopMonitor();
1668         MBeanServerFactory.releaseMBeanServer(server);
1669      }
1670   }
1671
1672   /**
1673    * Test a string nothing
1674    */

1675   public void testStringNever()
1676      throws Exception JavaDoc
1677   {
1678      initTest();
1679      try
1680      {
1681         initStringMonitor(false, false, "Hello");
1682         dontExpectStartMonitor("Goodbye");
1683         dontExpect("Hello");
1684         dontExpect("Goodbye");
1685      }
1686      finally
1687      {
1688         stopMonitor();
1689         MBeanServerFactory.releaseMBeanServer(server);
1690      }
1691   }
1692
1693   // String error tests ------------------------------------------------------
1694

1695   /**
1696    * Test invalid attribute
1697    */

1698   public void testStringInvalidAttribute()
1699      throws Exception JavaDoc
1700   {
1701      initTest();
1702      try
1703      {
1704         initStringMonitor(true, true, "Hello");
1705         attributeErrorStartMonitor("Goodbye");
1706      }
1707      finally
1708      {
1709         stopMonitor();
1710         MBeanServerFactory.releaseMBeanServer(server);
1711      }
1712   }
1713
1714   /**
1715    * Test invalid attribute
1716    */

1717   public void testStringInvalidAttributeNull()
1718      throws Exception JavaDoc
1719   {
1720      initTest();
1721      try
1722      {
1723         initStringMonitor(true, true, "Hello");
1724         attributeNullStartMonitor("Goodbye");
1725      }
1726      finally
1727      {
1728         stopMonitor();
1729         MBeanServerFactory.releaseMBeanServer(server);
1730      }
1731   }
1732
1733   /**
1734    * Test invalid attribute type
1735    */

1736   public void testStringInvalidAttributeType()
1737      throws Exception JavaDoc
1738   {
1739      initTest();
1740      try
1741      {
1742         initStringMonitor(true, true, "Hello");
1743         attributeTypeStartMonitor("Goodbye");
1744      }
1745      finally
1746      {
1747         stopMonitor();
1748         MBeanServerFactory.releaseMBeanServer(server);
1749      }
1750   }
1751
1752   /**
1753    * Test write only attribute
1754    */

1755   public void testStringWriteOnly()
1756      throws Exception JavaDoc
1757   {
1758      initTest();
1759      try
1760      {
1761         initStringMonitor(true, true, "Hello");
1762         attributeWriteStartMonitor("Goodbye");
1763      }
1764      finally
1765      {
1766         stopMonitor();
1767         MBeanServerFactory.releaseMBeanServer(server);
1768      }
1769   }
1770
1771   /**
1772    * Test invalid attribute type
1773    */

1774   public void testStringInvalidObjectName()
1775      throws Exception JavaDoc
1776   {
1777      initTest();
1778      try
1779      {
1780         initStringMonitor(true, true, "Hello");
1781         objectNameStartMonitor("Goodbye");
1782      }
1783      finally
1784      {
1785         stopMonitor();
1786         MBeanServerFactory.releaseMBeanServer(server);
1787      }
1788   }
1789
1790   /**
1791    * Test runtime error
1792    */

1793   public void testStringRuntimeError()
1794      throws Exception JavaDoc
1795   {
1796      initTest();
1797      try
1798      {
1799         initStringMonitor(true, true, "Hello");
1800         runtimeErrorStartMonitor("Goodbye");
1801      }
1802      finally
1803      {
1804         stopMonitor();
1805         MBeanServerFactory.releaseMBeanServer(server);
1806      }
1807   }
1808
1809   // Support -----------------------------------------------------------------
1810

1811   /**
1812    * Create a counter monitor
1813    */

1814   private void initCounterMonitor(boolean notify, Number JavaDoc threshold,
1815        boolean differenceMode, Number JavaDoc offset, Number JavaDoc modulus)
1816   {
1817      try
1818      {
1819         CounterMonitor JavaDoc counterMonitor = new CounterMonitor JavaDoc();
1820         counterMonitor.setNotify(notify);
1821         counterMonitor.setThreshold(threshold);
1822         counterMonitor.setDifferenceMode(differenceMode);
1823         counterMonitor.setOffset(offset);
1824         counterMonitor.setModulus(modulus);
1825         CounterSupport support = new CounterSupport();
1826         monitor = counterMonitor;
1827         monitored = support;
1828         initMonitor();
1829      }
1830      catch (Exception JavaDoc e)
1831      {
1832         fail(e.toString());
1833      }
1834   }
1835
1836   /**
1837    * Create a gauge monitor
1838    */

1839   private void initGaugeMonitor(boolean notifyHigh, boolean notifyLow,
1840        Number JavaDoc thresholdHigh, Number JavaDoc thresholdLow, boolean differenceMode)
1841   {
1842      try
1843      {
1844         GaugeMonitor JavaDoc gaugeMonitor = new GaugeMonitor JavaDoc();
1845         gaugeMonitor.setNotifyHigh(notifyHigh);
1846         gaugeMonitor.setNotifyLow(notifyLow);
1847         gaugeMonitor.setThresholds(thresholdHigh, thresholdLow);
1848         gaugeMonitor.setDifferenceMode(differenceMode);
1849         GaugeSupport support = new GaugeSupport();
1850         monitor = gaugeMonitor;
1851         monitored = support;
1852         initMonitor();
1853      }
1854      catch (Exception JavaDoc e)
1855      {
1856         fail(e.toString());
1857      }
1858   }
1859
1860   /**
1861    * Create a string monitor
1862    */

1863   private void initStringMonitor(boolean differ, boolean match, String JavaDoc compare)
1864   {
1865      try
1866      {
1867         StringMonitor JavaDoc stringMonitor = new StringMonitor JavaDoc();
1868         stringMonitor.setNotifyDiffer(differ);
1869         stringMonitor.setNotifyMatch(match);
1870         stringMonitor.setStringToCompare(compare);
1871         StringSupport support = new StringSupport();
1872         monitor = stringMonitor;
1873         monitored = support;
1874         initMonitor();
1875      }
1876      catch (Exception JavaDoc e)
1877      {
1878         fail(e.toString());
1879      }
1880   }
1881
1882   /**
1883    * Start a new test
1884    */

1885   private void initTest()
1886   {
1887      notifications.clear();
1888      server = MBeanServerFactory.createMBeanServer();
1889   }
1890
1891   /**
1892    * Create the monitor
1893    */

1894   private void initMonitor() throws Exception JavaDoc
1895   {
1896      monitorName = new ObjectName JavaDoc("test:type=monitor");
1897      monitoredName = new ObjectName JavaDoc("test:type=monitored");
1898      monitor.setObservedObject(monitoredName);
1899      monitor.setObservedAttribute("Value");
1900      monitor.setGranularityPeriod(MonitorSUITE.GRANULARITY_TIME);
1901      server.registerMBean(monitor, monitorName);
1902      server.registerMBean(monitored, monitoredName);
1903      server.addNotificationListener(monitorName, this, null, null);
1904   }
1905
1906   /**
1907    * Stop the monitor
1908    */

1909   private void stopMonitor()
1910   {
1911      if (monitor != null)
1912      {
1913         monitored.end();
1914         monitor.stop();
1915      }
1916   }
1917
1918   /**
1919    * Handle the notification, just add it to the list
1920    */

1921   public void handleNotification(Notification JavaDoc n, Object JavaDoc ignored)
1922   {
1923      synchronized(notifications)
1924      {
1925         notifications.add(n);
1926         notifications.notifyAll();
1927      }
1928   }
1929
1930   /**
1931    * Sync with the notification handler
1932    */

1933   private void sync(boolean getWorks)
1934      throws Exception JavaDoc
1935   {
1936      // Make sure the monitor has got the attribute
1937
if (getWorks)
1938         waitCycle();
1939      else
1940      {
1941         synchronized(notifications)
1942         {
1943            notifications.wait(MonitorSUITE.MAX_WAIT);
1944         }
1945      }
1946   }
1947
1948   /**
1949    * Invalid attribute start monitor
1950    */

1951   private void attributeErrorStartMonitor(Object JavaDoc initial)
1952      throws Exception JavaDoc
1953   {
1954      assertEquals(0, notifications.size());
1955      monitor.setObservedAttribute("rubbish");
1956      monitor.start();
1957      sync(false);
1958      assertEquals(1, notifications.size());
1959      MonitorNotification JavaDoc n = (MonitorNotification JavaDoc) notifications.get(0);
1960      notifications.clear();
1961      assertEquals(monitorName, n.getSource());
1962      assertEquals(monitoredName, n.getObservedObject());
1963      assertEquals("rubbish", n.getObservedAttribute());
1964      assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, n.getType());
1965      n = serializeDeserialize(n);
1966      assertEquals(monitorName, n.getSource());
1967      assertEquals(monitoredName, n.getObservedObject());
1968      assertEquals("rubbish", n.getObservedAttribute());
1969      assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, n.getType());
1970   }
1971
1972   /**
1973    * Invalid attribute null start monitor
1974    */

1975   private void attributeNullStartMonitor(Object JavaDoc initial)
1976      throws Exception JavaDoc
1977   {
1978      assertEquals(0, notifications.size());
1979      monitor.setObservedAttribute("WrongNull");
1980      monitor.start();
1981      sync(false);
1982      assertEquals(1, notifications.size());
1983      MonitorNotification JavaDoc n = (MonitorNotification JavaDoc) notifications.get(0);
1984      notifications.clear();
1985      assertEquals(monitorName, n.getSource());
1986      assertEquals(monitoredName, n.getObservedObject());
1987      assertEquals("WrongNull", n.getObservedAttribute());
1988      assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,
1989                   n.getType());
1990      n = serializeDeserialize(n);
1991      assertEquals(monitorName, n.getSource());
1992      assertEquals(monitoredName, n.getObservedObject());
1993      assertEquals("WrongNull", n.getObservedAttribute());
1994      assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,
1995                   n.getType());
1996   }
1997
1998   /**
1999    * Invalid attribute type start monitor
2000    */

2001   private void attributeTypeStartMonitor(Object JavaDoc initial)
2002      throws Exception JavaDoc
2003   {
2004      assertEquals(0, notifications.size());
2005      monitor.setObservedAttribute("WrongType");
2006      monitor.start();
2007      sync(false);
2008      assertEquals(1, notifications.size());
2009      MonitorNotification JavaDoc n = (MonitorNotification JavaDoc) notifications.get(0);
2010      notifications.clear();
2011      assertEquals(monitorName, n.getSource());
2012      assertEquals(monitoredName, n.getObservedObject());
2013      assertEquals("WrongType", n.getObservedAttribute());
2014      assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,
2015                   n.getType());
2016      n = serializeDeserialize(n);
2017      assertEquals(monitoredName, n.getObservedObject());
2018      assertEquals("WrongType", n.getObservedAttribute());
2019      assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,
2020                   n.getType());
2021   }
2022
2023   /**
2024    * Write only Attribute start monitor
2025    */

2026   private void attributeWriteStartMonitor(Object JavaDoc initial)
2027      throws Exception JavaDoc
2028   {
2029      assertEquals(0, notifications.size());
2030      monitor.setObservedAttribute("WriteOnly");
2031      monitor.start();
2032      sync(false);
2033      assertEquals(1, notifications.size());
2034      MonitorNotification JavaDoc n = (MonitorNotification JavaDoc) notifications.get(0);
2035      notifications.clear();
2036      assertEquals(monitorName, n.getSource());
2037      assertEquals(monitoredName, n.getObservedObject());
2038      assertEquals("WriteOnly", n.getObservedAttribute());
2039      assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR,
2040                   n.getType());
2041      n = serializeDeserialize(n);
2042      assertEquals(monitorName, n.getSource());
2043      assertEquals(monitoredName, n.getObservedObject());
2044      assertEquals("WriteOnly", n.getObservedAttribute());
2045      assertEquals(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR,
2046                   n.getType());
2047   }
2048
2049   /**
2050    * Invalid objectName start monitor
2051    */

2052   private void objectNameStartMonitor(Object JavaDoc initial)
2053      throws Exception JavaDoc
2054   {
2055      assertEquals(0, notifications.size());
2056      monitoredName = new ObjectName JavaDoc("rubbish:type=pants");
2057      monitor.setObservedObject(monitoredName);
2058      monitor.start();
2059      sync(false);
2060      assertEquals(1, notifications.size());
2061      MonitorNotification JavaDoc n = (MonitorNotification JavaDoc) notifications.get(0);
2062      notifications.clear();
2063      assertEquals(monitorName, n.getSource());
2064      assertEquals(monitoredName, n.getObservedObject());
2065      assertEquals("Value", n.getObservedAttribute());
2066      assertEquals(MonitorNotification.OBSERVED_OBJECT_ERROR,
2067                   n.getType());
2068      n = serializeDeserialize(n);
2069      assertEquals(monitorName, n.getSource());
2070      assertEquals(monitoredName, n.getObservedObject());
2071      assertEquals("Value", n.getObservedAttribute());
2072      assertEquals(MonitorNotification.OBSERVED_OBJECT_ERROR,
2073                   n.getType());
2074   }
2075
2076   /**
2077    * runtime error start monitor
2078    */

2079   private void runtimeErrorStartMonitor(Object JavaDoc initial)
2080      throws Exception JavaDoc
2081   {
2082      assertEquals(0, notifications.size());
2083      monitor.setObservedAttribute("WrongException");
2084      monitor.start();
2085      sync(false);
2086      if (notifications.size() != 1)
2087         fail ("FAILS IN RI: Does not notify of error thrown by getter");
2088      assertEquals(1, notifications.size());
2089      MonitorNotification JavaDoc n = (MonitorNotification JavaDoc) notifications.get(0);
2090      notifications.clear();
2091      assertEquals(monitorName, n.getSource());
2092      assertEquals(monitoredName, n.getObservedObject());
2093      assertEquals("WrongException", n.getObservedAttribute());
2094      assertEquals(MonitorNotification.RUNTIME_ERROR,
2095                   n.getType());
2096      n = serializeDeserialize(n);
2097      assertEquals(monitorName, n.getSource());
2098      assertEquals(monitoredName, n.getObservedObject());
2099      assertEquals("WrongException", n.getObservedAttribute());
2100      assertEquals(MonitorNotification.RUNTIME_ERROR,
2101                   n.getType());
2102   }
2103
2104   /**
2105    * Start a monitor expect a notification
2106    */

2107   private void expectStartMonitor(Object JavaDoc expected, String JavaDoc type)
2108      throws Exception JavaDoc
2109   {
2110      assertEquals(0, notifications.size());
2111      monitor.start();
2112      setValue(expected);
2113
2114      // Synchronize the notification
2115
sync(true);
2116
2117      // Check the result
2118
checkGauge(expected);
2119      assertEquals(1, notifications.size());
2120      MonitorNotification JavaDoc n = (MonitorNotification JavaDoc) notifications.get(0);
2121      notifications.clear();
2122      assertEquals(monitorName, n.getSource());
2123      assertEquals(monitoredName, n.getObservedObject());
2124      assertEquals("Value", n.getObservedAttribute());
2125      assertEquals(type, n.getType());
2126      assertEquals(expected, n.getDerivedGauge());
2127      n = serializeDeserialize(n);
2128      assertEquals(monitorName, n.getSource());
2129      assertEquals(monitoredName, n.getObservedObject());
2130      assertEquals("Value", n.getObservedAttribute());
2131      assertEquals(type, n.getType());
2132      assertEquals(expected, n.getDerivedGauge());
2133   }
2134
2135   /**
2136    * Start a monitor, don't expect a notification
2137    */

2138   private void dontExpectStartMonitor(Object JavaDoc initial)
2139      throws Exception JavaDoc
2140   {
2141      assertEquals(0, notifications.size());
2142      monitor.start();
2143      setValue(initial);
2144      waitCycle();
2145      assertEquals(0, notifications.size());
2146      checkGauge(initial);
2147   }
2148
2149   /**
2150    * Start a monitor, don't expect a notification difference
2151    */

2152   private void dontExpectStartMonitorDiff(Object JavaDoc value1, Object JavaDoc value2)
2153      throws Exception JavaDoc
2154   {
2155      monitor.start();
2156      setValue(value1);
2157      waitCycle();
2158      assertEquals(0, notifications.size());
2159      setValue(value2);
2160      waitCycle();
2161      assertEquals(0, notifications.size());
2162      checkGauge(sub(value2, value1));
2163   }
2164
2165   private void setValue(Object JavaDoc value)
2166      throws Exception JavaDoc
2167   {
2168      monitored.lock("set");
2169      server.setAttribute(monitoredName, new Attribute JavaDoc("Value", value));
2170   }
2171
2172   /**
2173    * Expect a notification
2174    */

2175   private void expect(Object JavaDoc expected, Object JavaDoc gauge, String JavaDoc type)
2176      throws Exception JavaDoc
2177   {
2178      assertEquals(0, notifications.size());
2179      setValue(expected);
2180
2181      // Synchronize the notification
2182
sync(true);
2183
2184      // Check the result
2185
checkGauge(gauge);
2186      assertEquals(1, notifications.size());
2187      MonitorNotification JavaDoc n = (MonitorNotification JavaDoc) notifications.get(0);
2188      notifications.clear();
2189      assertEquals(monitorName, n.getSource());
2190      assertEquals(monitoredName, n.getObservedObject());
2191      assertEquals("Value", n.getObservedAttribute());
2192      assertEquals(type, n.getType());
2193      assertEquals(gauge, n.getDerivedGauge());
2194   }
2195
2196   /**
2197    * Expect a notification difference
2198    */

2199   private void expectDiff(Object JavaDoc value1, Object JavaDoc value2, String JavaDoc type)
2200      throws Exception JavaDoc
2201   {
2202      waitCycle();
2203      notifications.clear();
2204      setValue(value1);
2205      waitCycle();
2206      assertEquals(0, notifications.size());
2207      setValue(value2);
2208      sync(true);
2209
2210      // Check the result
2211
checkGauge(sub(value2, value1));
2212      assertEquals(1, notifications.size());
2213      MonitorNotification JavaDoc n = (MonitorNotification JavaDoc) notifications.get(0);
2214      notifications.clear();
2215      assertEquals(monitorName, n.getSource());
2216      assertEquals(monitoredName, n.getObservedObject());
2217      assertEquals("Value", n.getObservedAttribute());
2218      assertEquals(type, n.getType());
2219      assertEquals(sub(value2, value1), n.getDerivedGauge());
2220   }
2221
2222   /**
2223    * Expect a notification
2224    */

2225   private void expect(Object JavaDoc expected, String JavaDoc type)
2226      throws Exception JavaDoc
2227   {
2228      expect(expected, expected, type);
2229   }
2230
2231   /**
2232    * Don't expect a notification
2233    */

2234   private void dontExpect(Object JavaDoc unexpected)
2235      throws Exception JavaDoc
2236   {
2237      assertEquals(0, notifications.size());
2238      setValue(unexpected);
2239      waitCycle();
2240      assertEquals(0, notifications.size());
2241      checkGauge(unexpected);
2242   }
2243
2244   /**
2245    * Don't expect a notification
2246    */

2247   private void dontExpectDiff(Object JavaDoc value1, Object JavaDoc value2)
2248      throws Exception JavaDoc
2249   {
2250      waitCycle();
2251      notifications.clear();
2252      setValue(value1);
2253      waitCycle();
2254      assertEquals(0, notifications.size());
2255      setValue(value2);
2256      waitCycle();
2257      assertEquals(0, notifications.size());
2258      checkGauge(sub(value2, value1));
2259   }
2260
2261   /**
2262    * Don't expect a notification
2263    */

2264   private void dontExpectDiffModulus(Object JavaDoc value1, Object JavaDoc value2, Object JavaDoc value3)
2265      throws Exception JavaDoc
2266   {
2267      waitCycle();
2268      notifications.clear();
2269      setValue(value1);
2270      waitCycle();
2271      assertEquals(0, notifications.size());
2272      setValue(value2);
2273      waitCycle();
2274      assertEquals(0, notifications.size());
2275      checkGauge(add(sub(value2, value1), value3));
2276   }
2277
2278   /**
2279    * Check the gauge
2280    */

2281  private void checkGauge(Object JavaDoc gauge)
2282     throws Exception JavaDoc
2283  {
2284      if (monitor instanceof StringMonitor JavaDoc)
2285         assertEquals(gauge, ((StringMonitor JavaDoc)monitor).getDerivedGauge());
2286      else if (monitor instanceof CounterMonitor JavaDoc)
2287         assertEquals(gauge, ((CounterMonitor JavaDoc)monitor).getDerivedGauge());
2288      else if (monitor instanceof GaugeMonitor JavaDoc)
2289         assertEquals(gauge, ((GaugeMonitor JavaDoc)monitor).getDerivedGauge());
2290      else
2291         fail("You idiot!");
2292  }
2293
2294   /**
2295    * Wait one cycle of the monitor
2296    */

2297   private void waitCycle()
2298   {
2299      monitored.unlock("set");
2300      monitored.lock("set");
2301   }
2302
2303  /**
2304   * Add two numbers.
2305   * @param value1 the first value.
2306   * @param value2 the second value.
2307   * @return value1 + value2 of the correct type
2308   */

2309  private Number JavaDoc add(Object JavaDoc value1, Object JavaDoc value2)
2310  {
2311     if (value1 instanceof Byte JavaDoc)
2312       return new Byte JavaDoc((byte) (((Byte JavaDoc)value1).byteValue() + ((Byte JavaDoc)value2).byteValue()));
2313     if (value1 instanceof Integer JavaDoc)
2314       return new Integer JavaDoc(((Integer JavaDoc)value1).intValue() + ((Integer JavaDoc)value2).intValue());
2315     if (value1 instanceof Short JavaDoc)
2316       return new Short JavaDoc((short) (((Short JavaDoc)value1).shortValue() + ((Short JavaDoc)value2).shortValue()));
2317     if (value1 instanceof Long JavaDoc)
2318       return new Long JavaDoc(((Long JavaDoc)value1).longValue() + ((Long JavaDoc)value2).longValue());
2319     fail("You idiot!");
2320     return null;
2321  }
2322
2323  /**
2324   * Subtract two numbers.
2325   * @param value1 the first value.
2326   * @param value2 the second value.
2327   * @return value1 - value2 of the correct type
2328   */

2329  private Number JavaDoc sub(Object JavaDoc value1, Object JavaDoc value2)
2330  {
2331     if (value1 instanceof Byte JavaDoc)
2332       return new Byte JavaDoc((byte) (((Byte JavaDoc)value1).byteValue() - ((Byte JavaDoc)value2).byteValue()));
2333     if (value1 instanceof Integer JavaDoc)
2334       return new Integer JavaDoc(((Integer JavaDoc)value1).intValue() - ((Integer JavaDoc)value2).intValue());
2335     if (value1 instanceof Short JavaDoc)
2336       return new Short JavaDoc((short) (((Short JavaDoc)value1).shortValue() - ((Short JavaDoc)value2).shortValue()));
2337     if (value1 instanceof Long JavaDoc)
2338       return new Long JavaDoc(((Long JavaDoc)value1).longValue() - ((Long JavaDoc)value2).longValue());
2339     fail("You idiot!");
2340     return null;
2341  }
2342
2343  /**
2344   * Check the notification info is as expected
2345   *
2346   * @param mbni the received notification info
2347   * @param expected the expected notifications
2348   */

2349  private void checkNotificationInfo(String JavaDoc type,
2350                                     MBeanNotificationInfo JavaDoc[] mbni,
2351                                     HashSet JavaDoc expected)
2352  {
2353     String JavaDoc[] types = mbni[0].getNotifTypes();
2354     for (int i = 0; i < types.length; i++)
2355         if (expected.remove(types[i]) == false)
2356            fail(type + ": didn't expect notification type " + types[i]);
2357     Iterator JavaDoc iterator = expected.iterator();
2358     while (iterator.hasNext())
2359         fail(type + ": expected notification type " + iterator.next());
2360  }
2361
2362  /**
2363   * Serialize/Derserialize the notification
2364   *
2365   * @param n the notification
2366   * @return the notification after serialize/deserialze
2367   */

2368  private MonitorNotification JavaDoc serializeDeserialize(MonitorNotification JavaDoc n)
2369     throws Exception JavaDoc
2370  {
2371      // Serialize it
2372
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
2373      ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
2374      oos.writeObject(n);
2375    
2376      // Deserialize it
2377
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
2378      ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bais);
2379      return (MonitorNotification JavaDoc) ois.readObject();
2380  }
2381}
2382
Popular Tags