KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > monitor > StringMonitorTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management.monitor;
10
11 import javax.management.MBeanServer JavaDoc;
12 import javax.management.Notification JavaDoc;
13 import javax.management.NotificationListener JavaDoc;
14 import javax.management.ObjectName JavaDoc;
15 import javax.management.monitor.Monitor JavaDoc;
16 import javax.management.monitor.MonitorNotification JavaDoc;
17 import javax.management.monitor.StringMonitor JavaDoc;
18
19 import test.MutableInteger;
20 import test.MutableObject;
21
22 /**
23  * @version $Revision: 1.8 $
24  */

25 public class StringMonitorTest extends MonitorTestCase
26 {
27    public StringMonitorTest(String JavaDoc name)
28    {
29       super(name);
30    }
31
32    protected Monitor JavaDoc createMonitor()
33    {
34       return new StringMonitor JavaDoc();
35    }
36
37    public void testCorrectInitialization() throws Exception JavaDoc
38    {
39       StringMonitor JavaDoc monitor = (StringMonitor JavaDoc)createMonitor();
40       assertEquals("", monitor.getStringToCompare());
41       assertFalse(monitor.getNotifyDiffer());
42       assertFalse(monitor.getNotifyMatch());
43    }
44
45    public void testSetStringToCompare() throws Exception JavaDoc
46    {
47       StringMonitor JavaDoc monitor = (StringMonitor JavaDoc)createMonitor();
48       try
49       {
50          monitor.setStringToCompare(null);
51          fail();
52       }
53       catch (IllegalArgumentException JavaDoc x)
54       {
55       }
56    }
57
58    /**
59     * The case outlined in the JMX specification
60     */

61    public void testSpecificationCase() throws Exception JavaDoc
62    {
63       ObjectName JavaDoc name = new ObjectName JavaDoc(":mbean=target");
64       ObjectName JavaDoc monitorName = new ObjectName JavaDoc(":monitor=gauge");
65
66       MBeanServer JavaDoc server = newMBeanServer();
67       StringMonitor JavaDoc monitor = (StringMonitor JavaDoc)createMonitor();
68       String JavaDoc reference = "XYZ";
69       monitor.setStringToCompare(reference);
70       monitor.setNotifyMatch(true);
71       monitor.setNotifyDiffer(true);
72       monitor.addObservedObject(name);
73       monitor.setObservedAttribute("String");
74       int period = 1000;
75       monitor.setGranularityPeriod(period);
76       server.registerMBean(monitor, monitorName);
77
78       MonitorTarget target = new MonitorTarget();
79       target.setString(reference);
80       server.registerMBean(target, name);
81
82       final MutableInteger times = new MutableInteger(0);
83       final MutableObject holder = new MutableObject(null);
84       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
85       {
86          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
87          {
88             times.set(times.get() + 1);
89             holder.set(notification);
90          }
91       };
92       server.addNotificationListener(monitorName, listener, null, null);
93
94       monitor.start();
95
96       try
97       {
98          sleep(period * 3);
99          assertEquals(times.get(), 1);
100          MonitorNotification JavaDoc notification = (MonitorNotification JavaDoc)holder.get();
101          assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
102
103          times.set(0);
104          holder.set(null);
105          target.setString("xx");
106
107          sleep(period * 3);
108          assertEquals(times.get(), 1);
109          notification = (MonitorNotification JavaDoc)holder.get();
110          assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
111
112          times.set(0);
113          holder.set(null);
114          target.setString(reference);
115
116          sleep(period * 3);
117          assertEquals(times.get(), 1);
118          notification = (MonitorNotification JavaDoc)holder.get();
119          assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
120
121          times.set(0);
122          holder.set(null);
123          target.setString("yyyy");
124
125          sleep(period * 3);
126          assertEquals(times.get(), 1);
127          notification = (MonitorNotification JavaDoc)holder.get();
128          assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
129
130          times.set(0);
131          holder.set(null);
132          target.setString("zzzzz");
133
134          sleep(period * 3);
135          assertEquals(times.get(), 0);
136          assertNull(holder.get());
137       }
138       finally
139       {
140          monitor.stop();
141       }
142    }
143
144    public interface MonitorTargetMBean
145    {
146       public String JavaDoc getString();
147    }
148
149    public static class MonitorTarget implements MonitorTargetMBean
150    {
151       private String JavaDoc value;
152
153       public String JavaDoc getString()
154       {
155          return value;
156       }
157
158       public void setString(String JavaDoc value)
159       {
160          this.value = value;
161       }
162    }
163 }
164
Popular Tags