Java Code Examples for jdk.test.lib.Asserts#assertEquals()

The following examples show how to use jdk.test.lib.Asserts#assertEquals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestName.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventType t = EventType.getEventType(NamedEvent.class);
    ValueDescriptor testField = t.getField("testField");
    SettingDescriptor setting = getSetting(t, "name");
    AnnotationElement a = Events.getAnnotationByName(t, "com.oracle.TestAnnotation");

    // Check that names are overridden
    Asserts.assertNotNull(testField, "Can't find expected field testField");
    Asserts.assertEquals(t.getName(), "com.oracle.TestEvent", "Incorrect name for event");
    Asserts.assertEquals(a.getTypeName(), "com.oracle.TestAnnotation", "Incorrect name for annotation");
    Asserts.assertEquals(a.getTypeName(), "com.oracle.TestAnnotation", "Incorrect name for annotation");
    Asserts.assertEquals(setting.getName(), "name", "Incorrect name for setting");

    // Check that @Name is persisted
    assertAnnotation(t.getAnnotation(Name.class), "@Name should be persisted on event");
    assertAnnotation(testField.getAnnotation(Name.class), "@Name should be persisted on field");
    assertAnnotation(a.getAnnotation(Name.class), "@Name should be persisted on annotations");
    assertAnnotation(setting.getAnnotation(Name.class), "@Name should be persisted on setting");
}
 
Example 2
Source File: JmxHelper.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void verifyEquals(RecordingInfo ri, Recording r) {
    String destination = r.getDestination() != null ? r.getDestination().toString() : null;
    long maxAge = r.getMaxAge() != null ? r.getMaxAge().getSeconds() : 0;
    long duration = r.getDuration() != null ? r.getDuration().getSeconds() : 0;

    Asserts.assertEquals(destination, ri.getDestination(), "Wrong destination");
    Asserts.assertEquals(r.getDumpOnExit(), ri.getDumpOnExit(), "Wrong dumpOnExit");
    Asserts.assertEquals(duration, ri.getDuration(), "Wrong duration");
    Asserts.assertEquals(r.getId(), ri.getId(), "Wrong id");
    Asserts.assertEquals(maxAge, ri.getMaxAge(), "Wrong maxAge");
    Asserts.assertEquals(r.getMaxSize(), ri.getMaxSize(), "Wrong maxSize");
    Asserts.assertEquals(r.getName(), ri.getName(), "Wrong name");
    Asserts.assertEquals(r.getSize(), ri.getSize(), "Wrong size");
    Asserts.assertEquals(toEpochMillis(r.getStartTime()), ri.getStartTime(), "Wrong startTime");
    Asserts.assertEquals(r.getState().toString(), ri.getState(), "Wrong state");
    Asserts.assertEquals(toEpochMillis(r.getStopTime()), ri.getStopTime(), "Wrong stopTime");

    verifyMapEquals(r.getSettings(), ri.getSettings());
}
 
Example 3
Source File: TestRecordedFullStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void checkEvent(RecordedEvent event, int expectedDepth) throws Throwable {
    RecordedStackTrace stacktrace = null;
    try {
        stacktrace = event.getStackTrace();
        List<RecordedFrame> frames = stacktrace.getFrames();
        Asserts.assertEquals(Math.min(MAX_DEPTH, expectedDepth), frames.size(), "Wrong stacktrace depth. Expected:" + expectedDepth);
        List<String> expectedMethods = getExpectedMethods(expectedDepth);
        Asserts.assertEquals(expectedMethods.size(), frames.size(), "Wrong expectedMethods depth. Test error.");

        for (int i = 0; i < frames.size(); ++i) {
            String name = frames.get(i).getMethod().getName();
            String expectedName = expectedMethods.get(i);
            System.out.printf("method[%d]=%s, expected=%s%n", i, name, expectedName);
            Asserts.assertEquals(name, expectedName, "Wrong method name");
        }

        boolean isTruncated = stacktrace.isTruncated();
        boolean isTruncateExpected = expectedDepth > MAX_DEPTH;
        Asserts.assertEquals(isTruncated, isTruncateExpected, "Wrong value for isTruncated. Expected:" + isTruncateExpected);

        String firstMethod = frames.get(frames.size() - 1).getMethod().getName();
        boolean isFullTrace = "run".equals(firstMethod);
        String msg = String.format("Wrong values for isTruncated=%b, isFullTrace=%b", isTruncated, isFullTrace);
        Asserts.assertTrue(isTruncated != isFullTrace, msg);
    } catch (Throwable t) {
        System.out.println(String.format("stacktrace:%n%s", stacktrace));
        throw t;
    }
}
 
Example 4
Source File: TestGetTypeId.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventType type = EventType.getEventType(CustomEvent.class);

    SettingDescriptor plain = Events.getSetting(type, "plain");
    long plainId = plain.getTypeId();
    Asserts.assertGreaterThan(plainId, 0L);

    SettingDescriptor annotatedType = Events.getSetting(type, "annotatedType");
    long annotatedId = annotatedType.getTypeId();
    Asserts.assertGreaterThan(annotatedId, 0L);

    Asserts.assertNotEquals(annotatedId, plainId);

    SettingDescriptor newName = Events.getSetting(type, "newName");
    Asserts.assertEquals(newName.getTypeId(), annotatedId);

    SettingDescriptor overridden = Events.getSetting(type, "overridden");
    Asserts.assertEquals(overridden.getTypeId(), plainId);

    SettingDescriptor protectedBase = Events.getSetting(type, "protectedBase");
    Asserts.assertEquals(protectedBase.getTypeId(), plainId);

    SettingDescriptor publicBase = Events.getSetting(type, "publicBase");
    Asserts.assertEquals(publicBase.getTypeId(), annotatedId);

    SettingDescriptor packageProtectedBase = Events.getSetting(type, "packageProtectedBase");
    Asserts.assertEquals(packageProtectedBase.getTypeId(), plainId);

    CustomEvent.assertOnDisk((x, y) -> Long.compare(x.getTypeId(), y.getTypeId()));
}
 
Example 5
Source File: TestStartMaxAgeSize.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Recording r = StartupHelper.getRecording("TestStartMaxAgeSize");
    CommonHelper.verifyRecordingState(r, RecordingState.RUNNING);
    Asserts.assertEquals(r.getMaxAge(), Duration.ofSeconds(10), "Wrong maxAge");
    Asserts.assertEquals(r.getMaxSize(), 1000000L, "Wrong maxSize");
    r.stop();
    r.close();
}
 
Example 6
Source File: TestShutdownEvent.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void verifyEvents(RecordedEvent event, int exitCode) {
    if (exitCode == 0) {
        System.out.println("Process exited normally with exit code 0, skipping the verification");
        return;
    }
    Events.assertField(event, "reason").equal("Shutdown requested from Java");
    Events.assertEventThread(event);
    Asserts.assertEquals(event.getThread().getJavaName(), "SIG" + signalName + " handler");
}
 
Example 7
Source File: TestThreadCpuTimeEvent.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void testEventAtThreadExit() throws Throwable {
    Recording recording = new Recording();

    recording.enable(EventNames.ThreadCPULoad).withPeriod(Duration.ofHours(10));
    recording.start();

    Duration testRunTime = Duration.ofMillis(eventPeriodMillis * cpuConsumerRunFactor);
    CyclicBarrier barrier = new CyclicBarrier(2);
    CpuConsumingThread thread = new CpuConsumingThread(testRunTime, barrier);

    // Run a single pass
    thread.start();
    barrier.await();
    barrier.await();

    thread.interrupt();
    thread.join();

    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    verifyPerThreadInvariant(events, cpuConsumerThreadName);

    int exitingCount = 0;
    for (RecordedEvent event : events) {
        RecordedThread eventThread = event.getThread();
        if (eventThread.getJavaName().equals(cpuConsumerThreadName)) {
            exitingCount++;
        }
    }
    Asserts.assertEquals(exitingCount, 1);
}
 
Example 8
Source File: JmxHelper.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void verifyMapEquals(Map<String, String> a, Map<String, String> b) {
    try {
        Asserts.assertEquals(a.size(), b.size(), "Wrong number of keys");
        for (String key : a.keySet()) {
            Asserts.assertTrue(a.containsKey(key), "Missing key " + key);
            Asserts.assertEquals(a.get(key), b.get(key), "Wrong values for key " + key);
            //System.out.printf("equal: %s=%s%n", key, a.get(key));
        }
    } catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
        logMap("a", a);
        logMap("b", b);
        throw e;
    }
}
 
Example 9
Source File: TestSetConfiguration.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();
    long recId = bean.newRecording();

    final String configContents =
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" +
    "<configuration version=\"2.0\" label=\"TestName\" description='TestDesc' provider='TestProvider'>\n" +
    "  <event name=\"" + EventNames.ClassLoad +"\">\r" +
    " \t <setting name=\"enabled\" control='class-loading-enabled'>false</setting>\r\n" +
    "    <setting name=\"stackTrace\">true</setting>\t\r\n" +
    "    <setting name=\"threshold\">5 ms</setting> \n" +
    "  </event>  " +
    "  <control>  " +
    "    <flag name=\"class-loading-enabled\" label=\"Class Loading\">false</flag>\n" +
    "  </control>" +
    "</configuration>";

    Map<String, String> expectedSetting = new HashMap<>();
    expectedSetting.put(EventNames.ClassLoad + "#enabled", "false");
    expectedSetting.put(EventNames.ClassLoad + "#stackTrace", "true");
    expectedSetting.put(EventNames.ClassLoad + "#threshold", "5 ms");

    bean.setConfiguration(recId, configContents);
    RecordingInfo jmxRecording = JmxHelper.getJmxRecording(recId);
    Recording javaRecording = JmxHelper.getJavaRecording(recId);
    JmxHelper.verifyEquals(jmxRecording, javaRecording);

    Map<String, String> settings = jmxRecording.getSettings();
    for (String name : expectedSetting.keySet()) {
        String value = settings.remove(name);
        Asserts.assertNotNull(value, "No setting with name " + name);
        Asserts.assertEquals(value, expectedSetting.get(name), "Wrong setting value");
    }
    Asserts.assertTrue(settings.isEmpty(), "Extra settings found " + settings.keySet());
}
 
Example 10
Source File: TestChunkPeriod.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void testBeginChunk() throws IOException {
    Recording r = new Recording();
    r.enable(SimpleEvent.class).with("period", "beginChunk");
    Instant beforeStart = Instant.now().minus(MARGIN_OF_ERROR);
    r.start();
    Instant afterStart = Instant.now().plus(MARGIN_OF_ERROR);
    r.stop();
    List<RecordedEvent> events = Events.fromRecording(r);
    Asserts.assertEquals(events.size(), 1, "Expected one event with beginChunk");
    RecordedEvent event = events.get(0);
    Asserts.assertGreaterThanOrEqual(event.getStartTime(), beforeStart);
    Asserts.assertGreaterThanOrEqual(afterStart, event.getStartTime());
    r.close();
}
 
Example 11
Source File: TestLargeJavaEvent64k.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void verifyEvents(Recording r, int numberOfThreads, int iterations, Map<String, Object> fields) throws IOException {
    List<RecordedEvent> recordedEvents = Events.fromRecording(r);
    Events.hasEvents(recordedEvents);
    int eventCount = 0;
    for (RecordedEvent re : recordedEvents) {
        verify(re, fields);
        eventCount++;
    }
    System.out.println("Number of expected events: " + numberOfThreads * iterations);
    System.out.println("Number of events found: " + eventCount);
    Asserts.assertEquals(numberOfThreads * iterations, eventCount, "Unexpected number of events");
}
 
Example 12
Source File: TestLargeJavaEvent64k.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void verifyEvents(Recording r, int numberOfThreads, int iterations, Map<String, Object> fields) throws IOException {
    List<RecordedEvent> recordedEvents = Events.fromRecording(r);
    Events.hasEvents(recordedEvents);
    int eventCount = 0;
    for (RecordedEvent re : recordedEvents) {
        verify(re, fields);
        eventCount++;
    }
    System.out.println("Number of expected events: " + numberOfThreads * iterations);
    System.out.println("Number of events found: " + eventCount);
    Asserts.assertEquals(numberOfThreads * iterations, eventCount, "Unexpected number of events");
}
 
Example 13
Source File: CPUSetsReader.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void assertParse(String cpuSet, String expectedResult) {
    Asserts.assertEquals(listToString(parseCpuSet(cpuSet)), expectedResult);
}
 
Example 14
Source File: TestReEnableName.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void testRecordingWithEnabledEvent(String eventname, boolean enabled) throws Exception {
    System.out.println("Testing enabled=" + enabled + " for " + eventname);
    final Path pathDisabled = Paths.get(".", "disabled.txt").toAbsolutePath();
    final Path pathEnabled = Paths.get(".", "enabled.txt").toAbsolutePath();

    Recording r = new Recording();
    if (enabled) {
        r.enable(eventname).withoutThreshold().withoutStackTrace();
    } else {
        r.disable(eventname).withoutThreshold().withoutStackTrace();
    }
    r.start();

    // We should only get events for pathEnabled, not for pathDisabled.
    int countExpectedEvents = 0;
    for (int i = 0; i < 10; ++i) {
        if (enabled) {
            Files.write(pathEnabled, "E".getBytes());
            ++countExpectedEvents;
            r.disable(eventname);
        } else {
            Files.write(pathDisabled, "D".getBytes());
            r.enable(eventname);
        }
        enabled = !enabled;
    }
    r.stop();

    int countFoundEvents = 0;
    for (RecordedEvent event : Events.fromRecording(r)) {
        System.out.printf("Event %s%n", event);
        Asserts.assertEquals(eventname, event.getEventType().getName(), "Wrong event type");
        String path = Events.assertField(event, "path").getValue();
        System.out.println(path);
        if (pathEnabled.toString().equals(path)) {
            ++countFoundEvents;
        }
    }
    Asserts.assertGreaterThanOrEqual(countFoundEvents, countExpectedEvents, "Too few events found");

    r.close();
}
 
Example 15
Source File: TestJcmdDumpLimited.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static void testDumpBeginEndRelative() throws IOException {
    Path testBeginEndRelative = Paths.get("testBeginEndRelative.jfr");
    JcmdHelper.jcmd("JFR.dump", "filename=" + testBeginEndRelative.toFile().getAbsolutePath(), "begin=-3h", "end=-0s");
    Asserts.assertEquals(totalSize, Files.size(testBeginEndRelative), "Expected dump with begin=-3h end=0s to contain data from all recordings");
    Files.delete(testBeginEndRelative);
}
 
Example 16
Source File: TestJcmdDumpLimited.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void testDumpBeginEndInstant() throws IOException {
    Path testBeginEnd = Paths.get("testBeginEndInstant.jfr");
    JcmdHelper.jcmd("JFR.dump", "filename=" + testBeginEnd.toFile().getAbsolutePath(), "begin=" + centerLeft, "end=" + centerRight);
    Asserts.assertEquals(centerSize, Files.size(testBeginEnd), "Expected dump with begin=" + centerLeft + " end=" + centerRight + " contain data from the 'center'-recordings");
    Files.delete(testBeginEnd);
}
 
Example 17
Source File: TestThreshold.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventType thresholdEvent = EventType.getEventType(PeriodicEvent.class);
    String defaultValue = Events.getSetting(thresholdEvent,Threshold.NAME).getDefaultValue();
    Asserts.assertEquals(defaultValue, "23 s", "@Threshold(\"23 s\") Should result in threshold '23 s'");
}
 
Example 18
Source File: TestRecordedEvent.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording r = new Recording();
    r.start();
    TestEvent t = new TestEvent();
    t.commit();
    r.stop();
    List<RecordedEvent> events = Events.fromRecording(r);
    Events.hasEvents(events);

    Asserts.assertEquals(events.size(), 1);

    RecordedEvent event = events.get(0);

    List<ValueDescriptor> descriptors = event.getFields();

    System.out.println("Descriptors");
    for (ValueDescriptor descriptor : descriptors) {
        System.out.println(descriptor.getName());
        System.out.println(descriptor.getTypeName());
    }
    System.out.println("Descriptors end");

    Object recordedClass = event.getValue("clzField");
    Asserts.assertTrue(recordedClass instanceof RecordedClass, "Expected Recorded Class got " + recordedClass);

    Object recordedInt = event.getValue("intField");
    Asserts.assertTrue(recordedInt instanceof Integer);

    Object recordedString = event.getValue("stringField");
    System.out.println("recordedString class: " + recordedString.getClass());
    Asserts.assertTrue(recordedString instanceof String);

    Object myClass = event.getValue("myClass");
    Asserts.assertTrue(myClass instanceof RecordedClass, "Expected Recorded Class got " + recordedClass);

    RecordedClass myRecClass = (RecordedClass) myClass;
    Asserts.assertEquals(MyClass.class.getName(), myRecClass.getName(), "Got " + myRecClass.getName());

    Object recordedClassLoader = myRecClass.getValue("classLoader");
    Asserts.assertTrue(recordedClassLoader instanceof RecordedClassLoader, "Expected Recorded ClassLoader got " + recordedClassLoader);

    RecordedClassLoader myRecClassLoader = (RecordedClassLoader)recordedClassLoader;
    ClassLoader cl = MyClass.class.getClassLoader();
    Asserts.assertEquals(cl.getClass().getName(), myRecClassLoader.getType().getName(), "Expected Recorded ClassLoader type to equal loader type");

    Asserts.assertNotNull(myRecClass.getModifiers());
}
 
Example 19
Source File: TestBiasedLockRevocationEvents.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
static void validateStackTrace(RecordedStackTrace stackTrace, String leafMethodName) {
    List<RecordedFrame> frames = stackTrace.getFrames();
    Asserts.assertFalse(frames.isEmpty());
    String name = frames.get(0).getMethod().getName();
    Asserts.assertEquals(name, leafMethodName);
}
 
Example 20
Source File: TestInheritedAnnotations.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static void assertSon(List<RecordedEvent> events) throws Exception {
    String eventName = SonEvent.class.getName();
    Events.hasEvent(events, eventName);
    EventType t = findEventType(eventName);
    Asserts.assertEquals(t.getCategoryNames(), Collections.singletonList(FAMILY_JOHNSON_STRING));
}