Skip to content

Commit a188179

Browse files
mergify[bot]Josh-Matsuokaandrewazores
authored
fix(jmcagent): Move jfrprobes_schema.xsd to correct location, add ProbeValidatorTest (backport #452) (#454)
* fix(jmcagent): Move jfrprobes_schema.xsd to correct location, add ProbeValidatorTest (#452) (cherry picked from commit 6228d49) # Conflicts: # cryostat-core/src/main/resources/io/cryostat/core/agent/jfrprobes_schema.xsd # cryostat-core/src/main/resources/io/cryostat/core/jmcagent/jfrprobes_schema.xsd # src/main/resources/io/cryostat/core/agent/jfrprobes_schema.xsd * fixup! fix(jmcagent): Move jfrprobes_schema.xsd to correct location, add ProbeValidatorTest (#452) --------- Co-authored-by: Joshua Matsuoka <Josh.matsuoka@gmail.com> Co-authored-by: Andrew Azores <aazores@redhat.com>
1 parent 97f1746 commit a188179

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

src/main/resources/io/cryostat/core/agent/jfrprobes_schema.xsd renamed to src/main/resources/io/cryostat/core/jmcagent/jfrprobes_schema.xsd

File renamed without changes.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Copyright The Cryostat Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.cryostat.core.jmcagent;
17+
18+
import java.io.ByteArrayInputStream;
19+
import java.nio.charset.StandardCharsets;
20+
21+
import javax.xml.transform.stream.StreamSource;
22+
23+
import org.junit.jupiter.api.Assertions;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.extension.ExtendWith;
27+
import org.mockito.junit.jupiter.MockitoExtension;
28+
import org.xml.sax.ErrorHandler;
29+
import org.xml.sax.SAXException;
30+
import org.xml.sax.SAXParseException;
31+
32+
@ExtendWith(MockitoExtension.class)
33+
public class ProbeValidatorTest {
34+
35+
final String VALID_XML =
36+
"<jfragent>\n"
37+
+ //
38+
"\t<events>\n"
39+
+ //
40+
"\t\t<event id=\"demo.jfr.test1\">\n"
41+
+ //
42+
"\t\t\t<label>JFR Hello World Event 1 %TEST_NAME%</label>\n"
43+
+ //
44+
"\t\t\t<description>Defined in the xml file and added by the"
45+
+ " agent.</description>\n"
46+
+ //
47+
"\t\t\t<path>demo/jfrhelloworldevent1</path>\n"
48+
+ //
49+
"\t\t\t<stacktrace>true</stacktrace>\n"
50+
+ //
51+
"\t\t\t<class>org.openjdk.jmc.agent.test.InstrumentMe</class>\n"
52+
+ //
53+
"\t\t\t<method>\n"
54+
+ //
55+
"\t\t\t\t<name>printHelloWorldJFR1</name>\n"
56+
+ //
57+
"\t\t\t\t<descriptor>()V</descriptor>\n"
58+
+ //
59+
"\t\t\t</method>\n"
60+
+ //
61+
"\t\t\t<!-- location {ENTRY, EXIT, WRAP}-->\n"
62+
+ //
63+
"\t\t\t<location>WRAP</location>\n"
64+
+ //
65+
"\t\t\t<fields>\n"
66+
+ //
67+
"\t\t\t\t<field>\n"
68+
+ //
69+
"\t\t\t\t\t<name>'InstrumentMe.STATIC_STRING_FIELD'</name>\n"
70+
+ //
71+
"\t\t\t\t\t<description>Capturing outer class field with class name prefixed"
72+
+ " field name</description>\n"
73+
+ //
74+
"\t\t\t\t\t<expression>InstrumentMe.STATIC_STRING_FIELD</expression>\n"
75+
+ //
76+
"\t\t\t\t</field>\n"
77+
+ //
78+
"\t\t\t</fields>\n"
79+
+ //
80+
"\t\t</event>\n"
81+
+ //
82+
"\t</events>\n"
83+
+ //
84+
"</jfragent>";
85+
86+
final String INVALID_XML =
87+
"<jfragent>\n"
88+
+ //
89+
"\t<foo>\n"
90+
+ //
91+
"\t<bar>\n"
92+
+ //
93+
"'This XML is not a valid probe template'\n"
94+
+ //
95+
"\t</bar>\n"
96+
+ //
97+
"\t</foo>\n"
98+
+ //
99+
"\t</jfragent>";
100+
101+
ProbeValidator validator;
102+
103+
@BeforeEach
104+
void setUp() {
105+
validator = new ProbeValidator();
106+
}
107+
108+
@Test
109+
void shouldThrowProbeValidationErrorOnFailure() {
110+
JMCAgentXMLStream stream =
111+
new JMCAgentXMLStream(
112+
new ByteArrayInputStream(INVALID_XML.getBytes(StandardCharsets.UTF_8)));
113+
Assertions.assertThrows(
114+
ProbeValidationException.class,
115+
() -> {
116+
validator.validate(new StreamSource(stream));
117+
});
118+
}
119+
120+
@Test
121+
void shouldSuccessfullyValidateCorrectTemplate() {
122+
JMCAgentXMLStream stream =
123+
new JMCAgentXMLStream(
124+
new ByteArrayInputStream(VALID_XML.getBytes(StandardCharsets.UTF_8)));
125+
try {
126+
validator.validate(new StreamSource(stream));
127+
} catch (Exception e) {
128+
System.out.println(e.toString());
129+
Assertions.fail();
130+
}
131+
}
132+
133+
@Test
134+
void shouldNotAllowOverridingErrorHandler() {
135+
Assertions.assertThrows(
136+
UnsupportedOperationException.class,
137+
() -> {
138+
validator.setErrorHandler(new TestErrorHandler());
139+
});
140+
}
141+
142+
private static class TestErrorHandler implements ErrorHandler {
143+
144+
@Override
145+
public void warning(SAXParseException exception) throws SAXException {
146+
throw new SAXException("We shouldn't have reached here");
147+
}
148+
149+
@Override
150+
public void error(SAXParseException exception) throws SAXException {
151+
throw new SAXException("We shouldn't have reached here");
152+
}
153+
154+
@Override
155+
public void fatalError(SAXParseException exception) throws SAXException {
156+
throw new SAXException("We shouldn't have reached here");
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)