50 import org.openjdk.jmc.rjmx.test.RjmxTestCase;
51 import org.openjdk.jmc.rjmx.test.testutil.TestToolkit;
52
53 public class UpdatePolicyTest extends RjmxTestCase implements IMRIValueListener {
54
55 private static int TEST_TIMEOUT_TIME = 30000;
56 private int m_counter = 0;
57
58 @Test
59 public void testUpdatePolicyLookup() throws Exception {
60 helpUpdatePolicyLookup(getExistingAttribute());
61 }
62
63 @Test
64 public void testNonExistingUpdatePolicyLookup() throws Exception {
65 helpUpdatePolicyLookup(getNonExistingAttribute());
66 }
67
68 private void helpUpdatePolicyLookup(MRI mri) throws Exception {
69 IUpdatePolicy policy = UpdatePolicyToolkit.getUpdatePolicy(getConnectionHandle(), mri);
70 assertNotNull("Policy is null!", policy); //$NON-NLS-1$
71 }
72
73 @Test
74 public void testChangeUpdatePolicy() throws Exception {
75 helpChangeUpdatePolicy(getExistingAttribute());
76 }
77
78 @Test
79 public void testChangeNonExistsingUpdatePolicy() throws Exception {
80 helpChangeUpdatePolicy(getNonExistingAttribute());
81 }
82
83 private void helpChangeUpdatePolicy(MRI mri) throws Exception {
84 IUpdatePolicy oldPolicy = UpdatePolicyToolkit.getUpdatePolicy(getConnectionHandle(), mri);
85 assertNotNull(oldPolicy);
86 int oldUpdateTime = getUpdateTime(getConnectionHandle(), mri);
87 int newUpdateTime = oldUpdateTime * 2;
88 UpdatePolicyToolkit.setUpdatePolicy(getConnectionHandle(), mri,
89 PolicyFactory.createSimpleUpdatePolicy(newUpdateTime));
90 IUpdatePolicy newPolicy = UpdatePolicyToolkit.getUpdatePolicy(getConnectionHandle(), mri);
153 IIntervalUpdatePolicy subscriptionPolicy = (IIntervalUpdatePolicy) getAttributeSubscriptionService()
154 .getMRISubscription(getExistingAttribute()).getUpdatePolicy();
155 int newSubscriptionUpdateTime = subscriptionPolicy.getIntervalTime();
156 assertTrue(newUpdateTime == newSubscriptionUpdateTime);
157 synchronized (this) {
158 int oldCounter = m_counter;
159 for (int i = 0; i < 3; i++) {
160 this.wait(time * 2);
161 // System.out.println("[" + time + "-" + i + "] - " + System.currentTimeMillis());
162 }
163 assertTrue(m_counter > oldCounter);
164 }
165 }
166 UpdatePolicyToolkit.setUpdatePolicy(getConnectionHandle(), getExistingAttribute(), oldUpdatePolicy);
167 } finally {
168 getAttributeSubscriptionService().removeMRIValueListener(this);
169 }
170 }
171
172 private MRI getExistingAttribute() {
173 return new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", "UsedPhysicalMemorySize"); //$NON-NLS-1$ //$NON-NLS-2$
174 }
175
176 private MRI getNonExistingAttribute() {
177 return new MRI(Type.ATTRIBUTE, "this.could.possible.not:really=exist,as=an", "attribute"); //$NON-NLS-1$ //$NON-NLS-2$
178 }
179
180 @Override
181 public void valueChanged(MRIValueEvent event) {
182 synchronized (this) {
183 TestToolkit.println(event);
184 if (event.getValue() != null) {
185 m_counter += 1;
186 notifyAll();
187 }
188 }
189 }
190
191 private static int getUpdateTime(IConnectionHandle handle, MRI attributeDescriptor) {
192 return ((IIntervalUpdatePolicy) UpdatePolicyToolkit.getUpdatePolicy(handle, attributeDescriptor))
193 .getIntervalTime();
194 }
195 }
|
50 import org.openjdk.jmc.rjmx.test.RjmxTestCase;
51 import org.openjdk.jmc.rjmx.test.testutil.TestToolkit;
52
53 public class UpdatePolicyTest extends RjmxTestCase implements IMRIValueListener {
54
55 private static int TEST_TIMEOUT_TIME = 30000;
56 private int m_counter = 0;
57
58 @Test
59 public void testUpdatePolicyLookup() throws Exception {
60 helpUpdatePolicyLookup(getExistingAttribute());
61 }
62
63 @Test
64 public void testNonExistingUpdatePolicyLookup() throws Exception {
65 helpUpdatePolicyLookup(getNonExistingAttribute());
66 }
67
68 private void helpUpdatePolicyLookup(MRI mri) throws Exception {
69 IUpdatePolicy policy = UpdatePolicyToolkit.getUpdatePolicy(getConnectionHandle(), mri);
70 assertNotNull("Policy is null!", policy);
71 }
72
73 @Test
74 public void testChangeUpdatePolicy() throws Exception {
75 helpChangeUpdatePolicy(getExistingAttribute());
76 }
77
78 @Test
79 public void testChangeNonExistsingUpdatePolicy() throws Exception {
80 helpChangeUpdatePolicy(getNonExistingAttribute());
81 }
82
83 private void helpChangeUpdatePolicy(MRI mri) throws Exception {
84 IUpdatePolicy oldPolicy = UpdatePolicyToolkit.getUpdatePolicy(getConnectionHandle(), mri);
85 assertNotNull(oldPolicy);
86 int oldUpdateTime = getUpdateTime(getConnectionHandle(), mri);
87 int newUpdateTime = oldUpdateTime * 2;
88 UpdatePolicyToolkit.setUpdatePolicy(getConnectionHandle(), mri,
89 PolicyFactory.createSimpleUpdatePolicy(newUpdateTime));
90 IUpdatePolicy newPolicy = UpdatePolicyToolkit.getUpdatePolicy(getConnectionHandle(), mri);
153 IIntervalUpdatePolicy subscriptionPolicy = (IIntervalUpdatePolicy) getAttributeSubscriptionService()
154 .getMRISubscription(getExistingAttribute()).getUpdatePolicy();
155 int newSubscriptionUpdateTime = subscriptionPolicy.getIntervalTime();
156 assertTrue(newUpdateTime == newSubscriptionUpdateTime);
157 synchronized (this) {
158 int oldCounter = m_counter;
159 for (int i = 0; i < 3; i++) {
160 this.wait(time * 2);
161 // System.out.println("[" + time + "-" + i + "] - " + System.currentTimeMillis());
162 }
163 assertTrue(m_counter > oldCounter);
164 }
165 }
166 UpdatePolicyToolkit.setUpdatePolicy(getConnectionHandle(), getExistingAttribute(), oldUpdatePolicy);
167 } finally {
168 getAttributeSubscriptionService().removeMRIValueListener(this);
169 }
170 }
171
172 private MRI getExistingAttribute() {
173 return new MRI(Type.ATTRIBUTE, "java.lang:type=OperatingSystem", "UsedPhysicalMemorySize");
174 }
175
176 private MRI getNonExistingAttribute() {
177 return new MRI(Type.ATTRIBUTE, "this.could.possible.not:really=exist,as=an", "attribute");
178 }
179
180 @Override
181 public void valueChanged(MRIValueEvent event) {
182 synchronized (this) {
183 TestToolkit.println(event);
184 if (event.getValue() != null) {
185 m_counter += 1;
186 notifyAll();
187 }
188 }
189 }
190
191 private static int getUpdateTime(IConnectionHandle handle, MRI attributeDescriptor) {
192 return ((IIntervalUpdatePolicy) UpdatePolicyToolkit.getUpdatePolicy(handle, attributeDescriptor))
193 .getIntervalTime();
194 }
195 }
|