Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/management/query/CustomQueryTest.java
41149 views
1
/*
2
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 6692027
27
* @summary Check that custom subclasses of QueryEval can be serialized.
28
* @author Eamonn McManus
29
*/
30
31
import java.io.ByteArrayInputStream;
32
import java.io.ByteArrayOutputStream;
33
import java.io.ObjectInputStream;
34
import java.io.ObjectOutputStream;
35
import java.lang.management.ManagementFactory;
36
import java.util.Set;
37
import java.util.concurrent.atomic.AtomicInteger;
38
import javax.management.MBeanServer;
39
import javax.management.MalformedObjectNameException;
40
import javax.management.ObjectName;
41
import javax.management.QueryEval;
42
import javax.management.QueryExp;
43
44
public class CustomQueryTest {
45
public static interface CountMBean {
46
public int getCount();
47
public void increment();
48
}
49
50
public static class Count implements CountMBean {
51
private AtomicInteger count = new AtomicInteger();
52
53
public int getCount() {
54
return count.get();
55
}
56
57
public void increment() {
58
count.incrementAndGet();
59
}
60
61
}
62
63
public static final ObjectName countName;
64
static {
65
try {
66
countName = new ObjectName("d:type=Count");
67
} catch (MalformedObjectNameException e) {
68
throw new AssertionError(e);
69
}
70
}
71
72
/* A query that calls the increment method of the Count MBean every time
73
* it is evaluated. If there is no ObjectName filter, the query will be
74
* evaluated for every MBean in the MBean Server, so the count will be
75
* incremented by the number of MBeans.
76
*/
77
public static class IncrQuery extends QueryEval implements QueryExp {
78
public boolean apply(ObjectName name) {
79
try {
80
getMBeanServer().invoke(countName, "increment", null, null);
81
return true;
82
} catch (Throwable t) {
83
t.printStackTrace();
84
System.exit(1);
85
throw new AssertionError(); // not reached
86
}
87
}
88
}
89
90
public static void main(String[] args) throws Exception {
91
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
92
boolean isSecondAttempt = false;
93
// The test may fail if some new MBean is registered while the test
94
// is running (e.g. Graal MBean). In this case just retry the test.
95
while (true) {
96
mbs.registerMBean(new Count(), countName);
97
int mbeanCount = mbs.getMBeanCount();
98
QueryExp query = new IncrQuery();
99
Set<ObjectName> names = mbs.queryNames(null, query);
100
assertEquals(mbeanCount, names.size());
101
assertEquals(mbeanCount, mbs.getAttribute(countName, "Count"));
102
ByteArrayOutputStream bout = new ByteArrayOutputStream();
103
ObjectOutputStream oout = new ObjectOutputStream(bout);
104
oout.writeObject(query);
105
oout.close();
106
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
107
ObjectInputStream oin = new ObjectInputStream(bin);
108
query = (QueryExp) oin.readObject();
109
names = mbs.queryNames(null, query);
110
int counterCount = (int)mbs.getAttribute(countName, "Count");
111
if (mbeanCount * 2 == counterCount) {
112
break;
113
}
114
if (isSecondAttempt) {
115
assertEquals(mbeanCount * 2, counterCount);
116
break;
117
}
118
isSecondAttempt = true;
119
System.out.println("New MBean was registered. Retrying...");
120
mbs.unregisterMBean(countName);
121
}
122
}
123
124
private static void assertEquals(Object expected, Object actual)
125
throws Exception {
126
if (!expected.equals(actual)) {
127
String failure = "FAILED: expected " + expected + ", got " + actual;
128
throw new Exception(failure);
129
}
130
}
131
}
132
133