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

The following examples show how to use jdk.testlibrary.Asserts#assertNotNull() . 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 dragonwell8_jdk 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: TestGetId.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Map<Long, Recording> recordings = new HashMap<>();

    final int iterations = 100;
    for (int i = 0; i < iterations; ++i) {
        Recording newRecording = new Recording();
        System.out.println("created: " + newRecording.getId());
        Recording oldRecording = recordings.get(newRecording.getId());
        Asserts.assertNull(oldRecording, "Duplicate recording ID: " + newRecording.getId());
        recordings.put(newRecording.getId(), newRecording);

        if (i % 3 == 0) {
            Recording closeRecording = recordings.remove(recordings.keySet().iterator().next());
            Asserts.assertNotNull(closeRecording, "Could not find recording in map. Test error.");
            closeRecording.close();
            System.out.println("closed: " + closeRecording.getId());
        }
    }

    for (Recording r : recordings.values()) {
        r.close();
    }
}
 
Example 3
Source File: TestConstructor.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    ValueDescriptor vdSimple = new ValueDescriptor(String.class, "message");
    Asserts.assertNull(vdSimple.getAnnotation(Label.class), "Expected getAnnotation()==null");
    Asserts.assertEquals(0, vdSimple.getAnnotationElements().size(), "Expected getAnnotations().size() == 0");

    // Add labelA and verify we can read it back
    List<AnnotationElement> annos = new ArrayList<>();
    AnnotationElement labelA = new AnnotationElement(Label.class, "labelA");
    annos.add(labelA);
    System.out.println("labelA.getClass()" + labelA.getClass());
    ValueDescriptor vdComplex = new ValueDescriptor(String.class, "message", annos);

    final Label outLabel = vdComplex.getAnnotation(Label.class);
    Asserts.assertFalse(outLabel == null, "getLabel(Label.class) was null");
    System.out.println("outLabel.value() = " + outLabel.value());

    // Get labelA from getAnnotations() list
    Asserts.assertEquals(1, vdComplex.getAnnotationElements().size(), "Expected getAnnotations().size() == 1");
    final AnnotationElement outAnnotation = vdComplex.getAnnotationElements().get(0);
    Asserts.assertNotNull(outAnnotation, "outAnnotation was null");
    System.out.printf("Annotation: %s = %s%n", outAnnotation.getTypeName(), outAnnotation.getValue("value"));
    Asserts.assertEquals(outAnnotation, labelA, "Expected firstAnnotation == labelA");

}
 
Example 4
Source File: TestClassDefineEvent.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 {
    Recording recording = new Recording();
    recording.enable(EVENT_NAME);
    TestClassLoader cl = new TestClassLoader();
    recording.start();
    cl.loadClass(TEST_CLASS_NAME);
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    boolean foundTestClasses = false;
    for (RecordedEvent event : events) {
        System.out.println(event);
        RecordedClass definedClass = event.getValue("definedClass");
        if (TEST_CLASS_NAME.equals(definedClass.getName())) {
            RecordedClassLoader definingClassLoader = definedClass.getClassLoader();
            Asserts.assertNotNull(definingClassLoader, "Defining Class Loader should not be null");
            RecordedClass definingClassLoaderType = definingClassLoader.getType();
            Asserts.assertNotNull(definingClassLoaderType, "The defining Class Loader type should not be null");
            Asserts.assertEquals(cl.getClass().getName(), definingClassLoaderType.getName(),
                "Expected type " + cl.getClass().getName() + ", got type " + definingClassLoaderType.getName());
            /*Asserts.assertEquals(cl.getName(), definingClassLoader.getName(),
                "Defining Class Loader should have the same name as the original class loader");*/
            foundTestClasses = true;
        }
    }
    Asserts.assertTrue(foundTestClasses, "No class define event found for " + TEST_CLASS_NAME);
}
 
Example 5
Source File: TestConfigurationGetContents.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 {
    List<Configuration> predefinedConfigs = Configuration.getConfigurations();

    Asserts.assertNotNull(predefinedConfigs, "List of predefined configs is null");
    Asserts.assertTrue(predefinedConfigs.size() > 0, "List of predefined configs is empty");

    for (Configuration conf : predefinedConfigs) {
        String name = conf.getName();
        System.out.println("Verifying configuration " + name);
        String fpath = JFR_DIR + name + ".jfc";
        String contents = conf.getContents();
        String fileContents = readFile(fpath);
        Asserts.assertEquals(fileContents, contents, "getContents() does not return the actual contents of the file " + fpath);
    }
}
 
Example 6
Source File: GCEventAll.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Verifies field "cause" in garbage_collection event.
 * Only check that at cause is not null and that at least 1 cause is "System.gc()"
 * We might want to check more cause reasons later.
 */
private void verifyCollectionCause(List<GCHelper.GcBatch> batches) {
    int systemGcCount = 0;
    for (GCHelper.GcBatch batch : batches) {
        RecordedEvent endEvent = batch.getEndEvent();
        String cause = Events.assertField(endEvent, "cause").notEmpty().getValue();
        // A System.GC() can be consolidated into a GCLocker GC
        if (cause.equals("System.gc()") || cause.equals("GCLocker Initiated GC")) {
            systemGcCount++;
        }
        Asserts.assertNotNull(batch.getName(), "garbage_collection.name was null");
    }
    final String msg = "No event with cause=System.gc(), collectors(%s, %s)";
    Asserts.assertTrue(systemGcCount > 0, String.format(msg, youngCollector, oldCollector));
}
 
Example 7
Source File: TestTimeScheduleStart.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testScheduledRecordingIsRunning() throws Exception {
    Recording r = new Recording();
    r.scheduleStart(Duration.ofSeconds(2));
    Asserts.assertNotNull(r.getStartTime(), "start time is null after scheduleStart()");
    CommonHelper.waitForRecordingState(r, RecordingState.RUNNING);
    Asserts.assertLessThanOrEqual(r.getStartTime(), Instant.now(), "start time should not exceed current time");
    r.stop();
    r.close();
}
 
Example 8
Source File: TestStartDelay.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Instant testStart = Instant.now();
    System.out.println("Test started at " + testStart);
    Recording r = StartupHelper.getRecording("TestStartDelay");
    CommonHelper.verifyRecordingState(r, RecordingState.DELAYED);
    Asserts.assertNotNull(r.getStartTime(), "Recording start time should not be null for a delayed recording");
    Asserts.assertLessThanOrEqual(r.getStartTime(), testStart.plus(Duration.ofSeconds(5000)), "Recording start time should not exceed test start time + delay");
    Asserts.assertGreaterThanOrEqual(r.getStartTime(), testStart, "Recording start time should not happen before test start time");
    r.close();
}
 
Example 9
Source File: TestRecordedThreadGroupParent.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void assetrThreadGroupParents(ThreadGroup realGroup, RecordedThreadGroup recordedGroup) {
    if (recordedGroup == null && realGroup == null) {
        return; // root
    }
    Asserts.assertNotNull(recordedGroup, "Parent thread group should not be null");
    Asserts.assertEquals(realGroup.getName(), recordedGroup.getName(), "Parent thread group names don't match");
    assetrThreadGroupParents(realGroup.getParent(), recordedGroup.getParent());
}
 
Example 10
Source File: TestRepositoryPath.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        final Path repo = Paths.get(".", "repo");
        System.out.println("dot is " + Paths.get(".").toRealPath());
        Asserts.assertTrue(Files.exists(repo), "Base repo path does not exist: " + repo);
        Path recordingPath = StartupHelper.findRecordingFileInRepo(repo);
        System.out.println("recordingPath: " + recordingPath);
        Asserts.assertNotNull(recordingPath, "Could not find recording file in repository " + repo);
    }
 
Example 11
Source File: TestSetConfiguration.java    From dragonwell8_jdk 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 12
Source File: TestEnoughPermission.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testStream(FlightRecorderMXBean bean, long recId) throws Exception {
    long streamId = bean.openStream(recId, null);
    byte[] buff = bean.readStream(streamId);
    Asserts.assertNotNull(buff, "Stream data was empty");
    while (buff != null) {
        // TODO: write to file and parse.
        System.out.println("buff.length=" + buff.length);
        buff = bean.readStream(streamId);
    }
    bean.closeStream(streamId);
}
 
Example 13
Source File: JcmdAsserts.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void assertStartTimeGreaterOrEqualThanMBeanValue(String name,
        long actualStartTime) throws Exception {
    Recording recording = findRecording(name);
    Asserts.assertNotNull(recording.getStartTime(), "Start time is not set");
    Asserts.assertGreaterThanOrEqual(actualStartTime, recording.getStartTime().toEpochMilli());
}
 
Example 14
Source File: TestEventFactoryRegisterTwice.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 {
    EventFactory factory = EventFactory.create(Collections.emptyList(), Collections.emptyList());

    EventType eventType = factory.getEventType();
    Asserts.assertNotNull(eventType);

    // Now, register the event
    factory.register();

    verifyRegistered(eventType);

    // Now, register the event again
    factory.register();

    verifyRegistered(eventType);
}
 
Example 15
Source File: TestRecordedClassLoader.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 {
    Recording recording = new Recording();
    recording.enable(EVENT_NAME).withoutStackTrace();
    TestClassLoader cl = new TestClassLoader();
    recording.start();
    cl.loadClass(TEST_CLASS_NAME);
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    boolean isDefined = false;
    System.out.println(events);
    for (RecordedEvent event : events) {
        RecordedClass definedClass = event.getValue("definedClass");
        if (TEST_CLASS_NAME.equals(definedClass.getName())) {
            System.out.println(event);

            // get the RecordedClassLoader from the RecordedClass, the "definedClass"
            RecordedClassLoader definingClassLoader = definedClass.getClassLoader();
            Asserts.assertNotNull(definingClassLoader, "Defining Class Loader should not be null");

            // invoke RecordedClassLoader.getType() in order to validate the type of the RecordedClassLoader
            RecordedClass definingClassLoaderType = definingClassLoader.getType();
            Asserts.assertNotNull(definingClassLoaderType, "The defining Class Loader type should not be null");

            // verify matching types
            Asserts.assertEquals(cl.getClass().getName(), definingClassLoaderType.getName(),
                "Expected type " + cl.getClass().getName() + ", got type " + definingClassLoaderType.getName());

            // get a RecordedClassLoader directly from the "definingClassLoader" field as well
            RecordedClassLoader definingClassLoaderFromField = event.getValue("definingClassLoader");
            Asserts.assertNotNull(definingClassLoaderFromField,
                "Defining Class Loader instantatiated from field should not be null");

            // ensure that the class loader instance used in the test actually has a name
            /** unsupport now
            Asserts.assertNotNull(cl.getName(),
                "Expected a valid name for the TestClassLoader");

            // invoke RecordedClassLoader.getName() to get the name of the class loader instance
            Asserts.assertEquals(cl.getName(), definingClassLoader.getName(),
                "Defining Class Loader should have the same name as the original class loader");
            Asserts.assertEquals(definingClassLoaderFromField.getName(), definingClassLoader.getName(),
                "Defining Class Loader representations should have the same class loader name");
            */

            // invoke uniqueID()
            Asserts.assertGreaterThan(definingClassLoader.getId(), 0L, "Invalid id assignment");

            // second order class loader information ("check class loader of the class loader")
            RecordedClassLoader classLoaderOfDefClassLoader = definingClassLoaderType.getClassLoader();
            Asserts.assertNotNull(classLoaderOfDefClassLoader,
                "The class loader for the definining class loader should not be null");
            /** unsupport now
            Asserts.assertEquals(cl.getClass().getClassLoader().getName(), classLoaderOfDefClassLoader.getName(),
                "Expected class loader name " + cl.getClass().getClassLoader().getName() + ", got name " + classLoaderOfDefClassLoader.getName());
            */

            RecordedClass classLoaderOfDefClassLoaderType = classLoaderOfDefClassLoader.getType();
            Asserts.assertNotNull(classLoaderOfDefClassLoaderType,
                "The class loader type for the defining class loader should not be null");
            Asserts.assertEquals(cl.getClass().getClassLoader().getClass().getName(), classLoaderOfDefClassLoaderType.getName(),
                "Expected type " + cl.getClass().getClassLoader().getClass().getName() + ", got type " + classLoaderOfDefClassLoaderType.getName());

            Asserts.assertGreaterThan(definingClassLoader.getId(), classLoaderOfDefClassLoader.getId(),
                "expected id assignment invariant broken for Class Loaders");

            isDefined = true;
        }
    }
    Asserts.assertTrue(isDefined, "No class define event found to verify RecordedClassLoader");
}
 
Example 16
Source File: TestGetFields.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Throwable {
    // Test simple ValueDescriptor
    final ValueDescriptor vdSimple = new ValueDescriptor(int.class, "id");
    Asserts.assertNotNull(vdSimple.getFields(), "getFields() returned null");
    Asserts.assertTrue(vdSimple.getFields().isEmpty(), "getFields() not empty for vdSimple");
}
 
Example 17
Source File: TestStartName.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 {
    Recording r = StartupHelper.getRecording(args[0]);
    Asserts.assertNotNull(r);
}
 
Example 18
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());
}
 
Example 19
Source File: JcmdAsserts.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void assertMaxAgeEqualsMBeanValue(String name, long maxAge)
        throws Exception {
    Recording recording = findRecording(name);
    Asserts.assertNotNull(recording, "No recording found");
    Asserts.assertEquals(maxAge, recording.getMaxAge().toMillis());
}
 
Example 20
Source File: JcmdAsserts.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void assertDurationEqualsMBeanValue(String name,
        long duration) throws Exception {
    Recording recording = findRecording(name);
    Asserts.assertNotNull(recording, "No recording found");
    Asserts.assertEquals(duration, recording.getDuration().toMillis());
}