Java Code Examples for jdk.testlibrary.Asserts#fail()

The following examples show how to use jdk.testlibrary.Asserts#fail() . 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: TestRegistered.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    try {
        EventType.getEventType(NotRegisteredByDefaultEvent.class);
        throw new Exception("Should not be able to get event type from unregistered event type");
    } catch (IllegalStateException ise) {
        // as expected
    }
    EventType registered = EventType.getEventType(RegisteredByDefaultEvent.class);
    boolean registeredWorking = false;
    for (EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) {
        if (registered.getId() == type.getId()) {
            registeredWorking = true;
        }
    }
    if (!registeredWorking) {
        Asserts.fail("Default regsitration is not working, can't validate @NoAutoRegistration");
    }
}
 
Example 2
Source File: TestFieldInformation.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    WhiteBox.setWriteAllObjectSamples(true);

    try (Recording recording = new Recording()) {
        recording.enable(EventNames.OldObjectSample).withoutStackTrace().with("cutoff", "infinity");
        recording.start();

        addToTestField();

        recording.stop();

        List<RecordedEvent> events = Events.fromRecording(recording);
        Events.hasEvents(events);
        for (RecordedEvent e : events) {
            if (hasValidField(e)) {
                return;
            }
        }
        System.out.println(events);
        Asserts.fail("Could not find old object with field 'testField'");
    }
}
 
Example 3
Source File: TestJavaMonitorWaitTimeOut.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void assertTimeOutEvent(List<RecordedEvent> events, long timeout, String expectedThreadName) {
    for (RecordedEvent e : events) {
        if (isWaitEvent(e)) {
            Long l = e.getValue("timeout");
            if (l == timeout) {
                RecordedThread notifier = e.getValue("notifier");
                String threadName = null;
                if (notifier != null) {
                    threadName = notifier.getJavaName();
                }
                Asserts.assertEquals(threadName, expectedThreadName, "Invalid thread");
                return;
            }
        }
    }
    Asserts.fail("Could not find event with monitorClass" + Lock.class.getName());
}
 
Example 4
Source File: HeapSummaryEventAllGcs.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean checkCollectors(Recording recording, String expectedYoung, String expectedOld) throws Exception {
    for (RecordedEvent event : Events.fromRecording(recording)) {
        if (Events.isEventType(event, EventNames.GCConfiguration)) {
            final String young = Events.assertField(event, "youngCollector").notEmpty().getValue();
            final String old = Events.assertField(event, "oldCollector").notEmpty().getValue();
            if (young.equals(expectedYoung) && old.equals(expectedOld)) {
                return true;
            }
            // TODO: We treat wrong collector types as an error. Old test only warned. Not sure what is correct.
            Asserts.fail(String.format("Wrong collector types: got('%s','%s'), expected('%s','%s')",
            young, old, expectedYoung, expectedOld));
        }
    }
    Asserts.fail("Missing event type " + EventNames.GCConfiguration);
    return false;
}
 
Example 5
Source File: TestEventTypes.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void assertSame(List<EventTypeInfo> infos, List<EventType> types) {
    List<Long> ids = new ArrayList<>();
    for (EventTypeInfo info : infos) {
        long id = info.getId();
        Asserts.assertFalse(ids.contains(id), "EventTypeInfo.id not unique:" + id);
        ids.add(id);
        boolean isFound = false;
        for (EventType type : types) {
            if (type.getId() == id) {
                assertSame(info, type);
                isFound = true;
                break;
            }
        }
        if (!isFound) {
            String msg = "No EventType for EventTypeInfo";
            System.out.println(msg + ": " + info);
            Asserts.fail(msg);
        }
    }
    Asserts.assertEquals(infos.size(), types.size(), "Number of EventTypeInfos != EventTypes");
}
 
Example 6
Source File: TestStreamClosed.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();

    long recId = bean.newRecording();
    bean.startRecording(recId);
    SimpleEventHelper.createEvent(1);
    bean.stopRecording(recId);

    long streamId = bean.openStream(recId, null);
    bean.closeStream(streamId);
    try {
        bean.readStream(streamId);
        Asserts.fail("No exception whean reading closed stream");
    } catch (IOException e) {
        // Expected exception.
        String msg = e.getMessage().toLowerCase();
        Asserts.assertTrue(msg.contains("stream") && msg.contains("closed"), "No 'stream closed' in " + msg);
    }
    bean.closeRecording(recId);
}
 
Example 7
Source File: JcmdHelper.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void waitUntilRunning(String name) throws Exception {
    long timeoutAt = System.currentTimeMillis() + 10000;
    while (true) {
        OutputAnalyzer output = jcmdCheck(name, false);
        try {
            // The expected output can look like this:
            // Recording: recording=1 name="Recording 1" (running)
            output.shouldMatch("^Recording: recording=\\d+\\s+name=\"" + name
                    + "\".*\\W{1}running\\W{1}");
            return;
        } catch (RuntimeException e) {
            if (System.currentTimeMillis() > timeoutAt) {
                Asserts.fail("Recording not started: " + name);
            }
            Thread.sleep(100);
        }
    }
}
 
Example 8
Source File: TestClinitRegistration.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Test basic registration with or without auto registration
    assertClinitRegister(AutoRegisteredEvent.class, true, false);
    assertClinitRegister(NotAutoRegisterededEvent.class, false, false);
    assertClinitRegister(AutoRegisteredUserClinit.class, true, true);
    assertClinitRegister(NotAutoRegisteredUserClinit.class, false, true);

    // Test complex <clinit>
    assertClinitRegister(ComplexClInit.class, true, true);

    // Test hierarchy
    assertClinitRegister(DerivedClinit.class, true, true);
    if (!isClinitExecuted(Base.class)) {
        Asserts.fail("Expected <clinit> of base class to be executed");
    }

    // Test committed event in <clinit>
    Recording r = new Recording();
    r.start();
    r.enable(EventInClinit.class);
    triggerClinit(EventInClinit.class);
    r.stop();
    hasEvent(r, EventInClinit.class.getName());
}
 
Example 9
Source File: TestCreateConfigFromPath.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testNullReader() throws Exception {
    try {
        Configuration.create((Reader)null);
        Asserts.fail("Exception was not thrown");
    } catch(NullPointerException x) {
    }
}
 
Example 10
Source File: TestEventFactoryRegistration.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void verifyUnregistered(EventType eventType) {
    // Verify the event is not registered
    for (EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) {
        if (eventType.getId() == type.getId()) {
            Asserts.fail("Event is not unregistered");
        }
    }
}
 
Example 11
Source File: TestGetField.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    EventType type = EventType.getEventType(MyEvent.class);

    ValueDescriptor v = type.getField("myByte");
    Asserts.assertNotNull(v, "getFiled(myByte) was null");
    Asserts.assertEquals(v.getTypeName(), "byte", "myByte was not type byte");

    v = type.getField("myInt");
    Asserts.assertNotNull(v, "getFiled(myInt) was null");
    Asserts.assertEquals(v.getTypeName(), "int", "myInt was not type int");

    v = type.getField("myStatic");
    Asserts.assertNull(v, "got static field");

    v = type.getField("notAField");
    Asserts.assertNull(v, "got field that does not exist");

    v = type.getField("");
    Asserts.assertNull(v, "got field for empty name");

    try {
        v = type.getField(null);
        Asserts.fail("No Exception when getField(null)");
    } catch (NullPointerException e) {
        // Expected exception
    }
}
 
Example 12
Source File: TestCopyToRunning.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static Recording getRecording(long recId) {
    for (Recording r : FlightRecorder.getFlightRecorder().getRecordings()) {
        if (r.getId() == recId) {
            return r;
        }
    }
    Asserts.fail("Could not find recording with id " + recId);
    return null;
}
 
Example 13
Source File: TestCloneRepeat.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void verifyEvents(Path path, List<Integer> ids) throws Exception {
    List<RecordedEvent> events = RecordingFile.readAllEvents(path);
    Iterator<RecordedEvent> iterator = events.iterator();
    for (int i=0; i<ids.size(); i++) {
        Asserts.assertTrue(iterator.hasNext(), "Missing event " + ids.get(i));
        EventField idField = Events.assertField(iterator.next(), "id");
        System.out.println("Event.id=" + idField.getValue());
        idField.equal(ids.get(i).intValue());
    }
    if (iterator.hasNext()) {
        Asserts.fail("Got extra event: " + iterator.next());
    }
}
 
Example 14
Source File: JmxHelper.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static Recording getJavaRecording(long recId) {
    for (Recording r : FlightRecorder.getFlightRecorder().getRecordings()) {
        if (r.getId() == recId) {
            return r;
        }
    }
    Asserts.fail("No Recording with id " + recId);
    return null;
}
 
Example 15
Source File: FileHelper.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void verifyRecording(File file) throws Exception {
    Asserts.assertTrue(file.exists(), file.getAbsolutePath() + " does not exist");
    Asserts.assertTrue(file.isFile(), file.getAbsolutePath() + " is not a file");
    Asserts.assertGreaterThan(file.length(), 0L, "Size of recording is 0.");
    List<RecordedEvent> events = RecordingFile.readAllEvents(file.toPath());
    for (RecordedEvent event : events) {
        System.out.printf("First event in recording '%s':%n%s", file.getName(), event);
        return;
    }
    Asserts.fail("No events in file " + file.getName());
}
 
Example 16
Source File: TestMetadataRetention.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static RecordedEvent findChunkRotationEvent(List<RecordedEvent> events) {
    for (RecordedEvent e : events)  {
        if (e.getEventType().getName().equals(ChunkRotation.class.getName())) {
            return e;
        }
    }
    Asserts.fail("Could not find chunk rotation event");
    return null; // Can't happen;
}
 
Example 17
Source File: TestHeapSummaryEventConcurrentCMS.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void verifyHeapSummary(List<RecordedEvent> events, int gcId, String when) {
    for (RecordedEvent event : events) {
        if (!Events.isEventType(event, EventNames.GCHeapSummary)) {
            continue;
        }
        if (gcId == (int)Events.assertField(event, "gcId").getValue() &&
                when.equals(Events.assertField(event, "when").getValue())) {
            System.out.printf("Found " + EventNames.GCHeapSummary + " for id=%d, when=%s%n", gcId, when);
            return;
        }
    }
    Asserts.fail(String.format("No " + EventNames.GCHeapSummary + " for id=%d, when=%s", gcId, when));
}
 
Example 18
Source File: TestName.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static SettingDescriptor getSetting(EventType t, String name) {
    for (SettingDescriptor v : t.getSettingDescriptors()) {
        if (v.getName().equals(name)) {
            return v;
        }
    }
    Asserts.fail("Could not find setting with name " + name);
    return null;
}
 
Example 19
Source File: TestGetSettings.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    EventType eventType = EventType.getEventType(EventWithCustomSettings.class);
    List<SettingDescriptor> settings = eventType.getSettingDescriptors();
    Asserts.assertEquals(settings.size(), 5, "Wrong number of settings");

    // test immutability
    try {
        settings.add(settings.get(0));
        Asserts.fail("Should not be able to modify list returned by getSettings()");
    } catch (UnsupportedOperationException uoe) {
        // OK, as expected
    }
}
 
Example 20
Source File: TestSetConfigurationInvalid.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();
    long recId = bean.newRecording();

    final String correctConfig =
    "<?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");

    // First set a few invalid configs. Should get Exceptions.
    try {
        bean.setConfiguration(recId, null);
        Asserts.fail("Expected NullPointerException");
    } catch (NullPointerException e) {
        // Expected exception
    }

    setInvalidConfig(recId, "Dummy text");
    setInvalidConfig(recId, correctConfig.replace("/event", "event"));
    setInvalidConfig(recId, correctConfig.replace("<control>", ""));

    // Verify that we can set a correct setting after the failed attempts.
    bean.setConfiguration(recId, correctConfig);
    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());
}