50113
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, 2018, 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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
package jdk.test.lib.jfr;
|
|
26 |
|
|
27 |
import java.util.Arrays;
|
|
28 |
import java.util.List;
|
|
29 |
import java.util.stream.Collectors;
|
|
30 |
|
|
31 |
import jdk.jfr.ValueDescriptor;
|
|
32 |
import jdk.jfr.consumer.RecordedObject;
|
|
33 |
import jdk.test.lib.Asserts;
|
|
34 |
|
|
35 |
|
|
36 |
public final class EventField {
|
|
37 |
public final RecordedObject event;
|
|
38 |
public final ValueDescriptor desc;
|
|
39 |
|
|
40 |
public EventField(RecordedObject event, ValueDescriptor valueDescriptor) {
|
|
41 |
this.event = event;
|
|
42 |
this.desc = valueDescriptor;
|
|
43 |
}
|
|
44 |
|
|
45 |
@SuppressWarnings("unchecked")
|
|
46 |
public <T extends Comparable<T>> boolean isEqual(T value) {
|
|
47 |
return value == (T)getValue();
|
|
48 |
}
|
|
49 |
|
|
50 |
@SuppressWarnings("unchecked")
|
|
51 |
public <T extends Comparable<T>> EventField equal(T value) {
|
|
52 |
doAssert(()-> Asserts.assertEquals((T)getValue(), value, getErrMsg("Value not equal to " + value)));
|
|
53 |
return this;
|
|
54 |
}
|
|
55 |
|
|
56 |
@SuppressWarnings("unchecked")
|
|
57 |
public <T extends Comparable<T>> EventField notEqual(T value) {
|
|
58 |
doAssert(()-> Asserts.assertNotEquals((T)getValue(), value, getErrMsg("Value equal to " + value)));
|
|
59 |
return this;
|
|
60 |
}
|
|
61 |
|
|
62 |
@SuppressWarnings("unchecked")
|
|
63 |
public <T extends Comparable<T>> EventField above(T value) {
|
|
64 |
doAssert(()-> Asserts.assertGreaterThan((T)getValue(), value, getErrMsg("Value not above " + value)));
|
|
65 |
return this;
|
|
66 |
}
|
|
67 |
|
|
68 |
@SuppressWarnings("unchecked")
|
|
69 |
public <T extends Comparable<T>> EventField below(T value) {
|
|
70 |
doAssert(()-> Asserts.assertLessThan((T)getValue(), value, getErrMsg("Value not below " + value)));
|
|
71 |
return this;
|
|
72 |
}
|
|
73 |
|
|
74 |
@SuppressWarnings("unchecked")
|
|
75 |
public <T extends Comparable<T>> EventField atLeast(T value) {
|
|
76 |
doAssert(()-> Asserts.assertGreaterThanOrEqual((T)getValue(), value, getErrMsg("Value not atLeast" + value)));
|
|
77 |
return this;
|
|
78 |
}
|
|
79 |
|
|
80 |
@SuppressWarnings("unchecked")
|
|
81 |
public <T extends Comparable<T>> EventField atMost(T value) {
|
|
82 |
doAssert(()-> Asserts.assertLessThanOrEqual((T)getValue(), value, getErrMsg("Value not atMost " + value)));
|
|
83 |
return this;
|
|
84 |
}
|
|
85 |
|
|
86 |
public <T extends Comparable<T>> EventField instring(String part) {
|
|
87 |
final String value = getValue();
|
|
88 |
doAssert(()-> Asserts.assertTrue(value.contains(part), getErrMsg("Value does not contain '" + part +"'")));
|
|
89 |
return this;
|
|
90 |
}
|
|
91 |
|
|
92 |
@SuppressWarnings("unchecked")
|
|
93 |
public <T> T getValue() {
|
|
94 |
return (T)event.getValue(desc.getName());
|
|
95 |
}
|
|
96 |
|
|
97 |
public EventField notNull() {
|
|
98 |
doAssert(()-> Asserts.assertNotNull(getValue(), getErrMsg("Field is null")));
|
|
99 |
return this;
|
|
100 |
}
|
|
101 |
|
|
102 |
public EventField isNull() {
|
|
103 |
doAssert(()-> Asserts.assertNull(getValue(), getErrMsg("Field is not null")));
|
|
104 |
return this;
|
|
105 |
}
|
|
106 |
|
|
107 |
public EventField notEmpty() {
|
|
108 |
notNull();
|
|
109 |
final String s = getValue();
|
|
110 |
doAssert(()-> Asserts.assertFalse(s.isEmpty(), getErrMsg("Field is empty")));
|
|
111 |
return this;
|
|
112 |
}
|
|
113 |
|
|
114 |
private void doAssert(AssertFunction f) {
|
|
115 |
try {
|
|
116 |
f.doAssert();
|
|
117 |
} catch (RuntimeException e) {
|
|
118 |
System.out.printf("Error: %s%nFailed event:%n%s%n", e.getMessage(), event.toString());
|
|
119 |
throw e;
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
public EventField containsAny(String... allowed) {
|
|
124 |
final String value = getValue();
|
|
125 |
final List<String> allowedValues = Arrays.asList(allowed);
|
|
126 |
boolean contains = false;
|
|
127 |
for(String allowedValue : allowed) {
|
|
128 |
if (value.contains(allowedValue)) {
|
|
129 |
contains = true;
|
|
130 |
}
|
|
131 |
}
|
|
132 |
if (!contains) {
|
|
133 |
doAssert(()-> Asserts.fail(getErrMsg(String.format("Value not in (%s)",
|
|
134 |
allowedValues.stream().collect(Collectors.joining(", "))))));
|
|
135 |
}
|
|
136 |
return this;
|
|
137 |
}
|
|
138 |
|
|
139 |
private String getErrMsg(String msg) {
|
|
140 |
final String name = desc.getName();
|
|
141 |
final Object value = event.getValue(name);
|
|
142 |
return String.format("%s, field='%s', value='%s'", msg, name, value);
|
|
143 |
}
|
|
144 |
|
|
145 |
@FunctionalInterface
|
|
146 |
public interface AssertFunction {
|
|
147 |
void doAssert();
|
|
148 |
}
|
|
149 |
}
|