/* * Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/ package jdk.test.lib.jfr;
/** * Helper class to verify RecordedEvent content
*/ publicclass Events {
publicstatic EventField assertField(RecordedEvent event, String name) {
String[] partNames = name.split("\\.");
RecordedObject struct = event; try { for (int i=0; i<partNames.length; ++i) { final String partName = partNames[i]; finalboolean isLastPart = i == partNames.length - 1;
ValueDescriptor d = getValueDescriptor(struct, partName); if (isLastPart) { returnnew EventField(struct, d);
} else {
assertTrue(struct.getValue(partName) instanceof RecordedObject, "Expected '" + partName + "' to be a struct");
struct = struct.getValue(partName);
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.printf("Failed event:%n%s%n", event.toString());
fail(String.format("Field %s not in event", name)); returnnull;
}
privatestatic RecordedObject getRecordedPackage(final RecordedClass rc) { if (rc == null) { thrownew RuntimeException("RecordedClass must not be null!");
} return rc.getValue("package");
}
privatestatic RecordedObject getRecordedModule(final RecordedObject pkg) { if (pkg == null) { // null package is an unnamed module (null) returnnull;
}
return pkg.getValue("module");
} /** * Validates the recored name field * * @param ro should be a Package or a Module * @param targetName name to match
*/ privatestaticboolean isMatchingTargetName(final RecordedObject ro, final String targetName) { if (ro == null) { return targetName == null;
}
final String recordedName = ro.getValue("name");
if (recordedName == null) { return targetName == null;
}
return recordedName.equals(targetName);
}
publicstaticvoid assertClassPackage(final RecordedClass rc, final String packageNameTarget) { final RecordedObject recordedPackage = getRecordedPackage(rc);
if (recordedPackage == null) { if (packageNameTarget != null) { thrownew RuntimeException("RecordedClass package is null!");
} return;
}
assertTrue(isMatchingTargetName(recordedPackage, packageNameTarget), "mismatched package name! Target is " + packageNameTarget);
}
publicstaticvoid assertClassModule(final RecordedClass rc, final String moduleNameTarget) { final RecordedObject recordedPackage = getRecordedPackage(rc); final RecordedObject recordedModule = getRecordedModule(recordedPackage);
if (recordedModule == null) { if (moduleNameTarget != null) { thrownew RuntimeException("RecordedClass module is null!");
} return;
}
/** * Creates a list of events from a recording. * * @param recording recording, not {@code null} * @return a list, not null * @throws IOException if an event set could not be created due to I/O * errors.
*/ publicstatic List<RecordedEvent> fromRecording(Recording recording) throws IOException { return RecordingFile.readAllEvents(makeCopy(recording));
}
privatestatic Path makeCopy(Recording recording) throws IOException {
Path p = recording.getDestination(); if (p == null) {
File directory = new File("."); // FIXME: Must come up with a way to give human-readable name // this will at least not clash when running parallel.
ProcessHandle h = ProcessHandle.current();
p = new File(directory.getAbsolutePath(), "recording-" + recording.getId() + "-pid" + h.pid() + ".jfr").toPath();
recording.dump(p);
} return p;
}
publicstaticvoid hasAnnotation(ValueDescriptor field, Class<? extends java.lang.annotation.Annotation> annotationClass) throws Exception {
AnnotationElement a = getAnnotation(field, annotationClass); if (a == null) { thrownew Exception("Expected " + annotationClass.getSimpleName() + " on field " + field.getName());
}
}
publicstaticvoid assertAnnotation(ValueDescriptor field, Class<? extends java.lang.annotation.Annotation> annotationClass, String value) throws Exception {
AnnotationElement a = getAnnotation(field, annotationClass);
Object v = a.getValue("value"); if (!v.equals(value)) { thrownew Exception("Expected " + annotationClass.getSimpleName() + " on field " + field.getName() + " to have value " + value + ", but got " + v);
}
}
// candidate for moving into API publicstatic AnnotationElement getAnnotation(ValueDescriptor v, Class<?> clazz) throws Exception { for (AnnotationElement a : v.getAnnotationElements()) { if (a.getTypeName().equals(clazz.getName())) { return a;
}
}
thrownew Exception("Could not find annotation " + clazz.getName());
}
// candidate for moving into API publicstatic AnnotationElement getAnnotationByName(EventType t, String name) throws Exception { for (AnnotationElement a : t.getAnnotationElements()) { if (a.getTypeName().equals(name)) { return a;
}
} thrownew Exception("Could not find annotation '" + name + " in type " + t.getName());
}
// candidate for moving into API publicstatic SettingDescriptor getSetting(EventType type, String name) { for (SettingDescriptor s : type.getSettingDescriptors()) { if (s.getName().equals(name)) { return s;
}
} thrownew IllegalArgumentException("Could not setting with name " + name);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.