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

The following examples show how to use jdk.test.lib.Asserts#assertTrue() . 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: TestGetStackTrace.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void assertFrame(RecordedFrame frame) {
    int bci = frame.getBytecodeIndex();
    int line = frame.getLineNumber();
    boolean javaFrame = frame.isJavaFrame();
    RecordedMethod method = frame.getMethod();
    String type = frame.getType();
    System.out.println("*** Frame Info ***");
    System.out.println("bci=" + bci);
    System.out.println("line=" + line);
    System.out.println("type=" + type);
    System.out.println("method=" + method);
    System.out.println("***");
    Asserts.assertTrue(javaFrame, "Only Java frame are currently supported");
    Asserts.assertGreaterThanOrEqual(bci, -1);
    Asserts.assertNotNull(method, "Method should not be null");
}
 
Example 2
Source File: TestDestWithDuration.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Path dest = Paths.get(".", "my.jfr");
    Recording r = new Recording();
    SimpleEventHelper.enable(r, true);
    r.setDestination(dest);
    r.start();
    SimpleEventHelper.createEvent(1);

    // Waiting for recording to auto close after duration
    r.setDuration(Duration.ofSeconds(1));
    System.out.println("Waiting for recording to auto close after duration");
    CommonHelper.waitForRecordingState(r, RecordingState.CLOSED);
    System.out.println("recording state = " + r.getState());
    Asserts.assertEquals(r.getState(), RecordingState.CLOSED, "Recording not closed");

    Asserts.assertTrue(Files.exists(dest), "No recording file: " + dest);
    System.out.printf("Recording file size=%d%n", Files.size(dest));
    Asserts.assertNotEquals(Files.size(dest), 0L, "File length 0. Should at least be some bytes");

    List<RecordedEvent> events = RecordingFile.readAllEvents(dest);
    Asserts.assertFalse(events.isEmpty(), "No event found");
    System.out.printf("Found event %s%n", events.get(0).getEventType().getName());
}
 
Example 3
Source File: TestFieldAccess.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void testHasField(RecordedEvent event) {
    System.out.println(event);
    Asserts.assertTrue(event.hasField("stringField"));
    Asserts.assertTrue(event.hasField("intField"));
    Asserts.assertTrue(event.hasField("longField"));
    Asserts.assertTrue(event.hasField("shortField"));
    Asserts.assertTrue(event.hasField("doubleField"));
    Asserts.assertTrue(event.hasField("floatField"));
    Asserts.assertTrue(event.hasField("threadField"));
    Asserts.assertTrue(event.hasField("classField"));
    Asserts.assertTrue(event.hasField("classField.name"));
    Asserts.assertTrue(event.hasField("eventThread"));
    Asserts.assertTrue(event.hasField("eventThread.group.name"));
    Asserts.assertTrue(event.hasField("startTime"));
    Asserts.assertTrue(event.hasField("stackTrace"));
    Asserts.assertTrue(event.hasField("duration"));
    Asserts.assertFalse(event.hasField("doesNotExist"));
    Asserts.assertFalse(event.hasField("classField.doesNotExist"));
    Asserts.assertFalse(event.hasField(""));
    try {
        event.hasField(null);
    } catch (NullPointerException npe) {
        // as expected
    }
}
 
Example 4
Source File: TestDumpState.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void checkEvents(Recording r, List<Integer> expectedIds) throws Exception {
    Path path = Paths.get(".", String.format("%d.jfr", recordingCounter++));
    r.dump(path);
    Asserts.assertTrue(Files.exists(path), "Recording file does not exist: " + path);

    int index = 0;

    for (RecordedEvent event : RecordingFile.readAllEvents(path)) {
        Events.isEventType(event, SimpleEvent.class.getName());
        Integer id = Events.assertField(event, "id").getValue();
        System.out.println("Got event with id " + id);
        Asserts.assertGreaterThan(expectedIds.size(), index, "Too many Events found");
        Asserts.assertEquals(id, expectedIds.get(index), "Wrong id at index " + index);
        ++index;
    }
    Asserts.assertEquals(index, expectedIds.size(), "Too few Events found");
}
 
Example 5
Source File: TestGetDuration.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void verifyNativeEvents() throws Exception {
    Recording r = new Recording();
    r.enable(EventNames.JVMInformation);
    r.enable(EventNames.ThreadSleep);
    r.start();
    // Should trigger a sleep event even if we
    // have a low resolution clock
    Thread.sleep(200);
    r.stop();
    List<RecordedEvent> recordedEvents = Events.fromRecording(r);
    Events.hasEvents(recordedEvents);
    for (RecordedEvent re : recordedEvents) {
        Asserts.assertEquals(re.getDuration(), Duration.between(re.getStartTime(), re.getEndTime()));
        switch (re.getEventType().getName()) {
            case EventNames.JVMInformation:
                Asserts.assertTrue(re.getDuration().isZero());
                break;
            case EventNames.ThreadSleep:
                Asserts.assertTrue(!re.getDuration().isNegative() && !re.getDuration().isZero());
                break;
            default:
                Asserts.fail("Unexpected event: " + re);
                break;
        }
    }
}
 
Example 6
Source File: TestCopyToRunning.java    From TencentKona-8 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);

    Path path = Paths.get(".", "my.jfr");
    bean.copyTo(recId, path.toString());

    List<RecordedEvent> events = RecordingFile.readAllEvents(path);
    Asserts.assertTrue(events.iterator().hasNext(), "No events found");
    for (RecordedEvent event : events) {
        System.out.println("Event:" + event);
    }

    Recording recording = getRecording(recId);
    Asserts.assertEquals(recording.getState(), RecordingState.RUNNING, "Recording not in state running");
    bean.stopRecording(recId);
    Asserts.assertEquals(recording.getState(), RecordingState.STOPPED, "Recording not in state stopped");
    bean.closeRecording(recId);
    Asserts.assertEquals(recording.getState(), RecordingState.CLOSED, "Recording not in state closed");
}
 
Example 7
Source File: TestDumpOnExit.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void testDumponExit(Supplier<Path> p,String... args) throws Exception, IOException {
    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, args);
    OutputAnalyzer output = ProcessTools.executeProcess(pb);
    System.out.println(output.getOutput());
    output.shouldHaveExitValue(0);
    Path dump = p.get();
    Asserts.assertTrue(Files.isRegularFile(dump), "No recording dumped " + dump);
    System.out.println("Dumped recording size=" + Files.size(dump));
    Asserts.assertFalse(RecordingFile.readAllEvents(dump).isEmpty(), "No events in dump");
}
 
Example 8
Source File: TestCloneRepeat.java    From openjdk-jdk8u 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 9
Source File: InvokeVirtual.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * A caller method, assumed to called "callee"/"calleeNative"
 */
@Override
public void caller() {
    if (nativeCallee) {
        Asserts.assertTrue(calleeNative(1, 2L, 3.0f, 4.0d, "5"), CALL_ERR_MSG);
    } else {
        Asserts.assertTrue(callee(1, 2L, 3.0f, 4.0d, "5"), CALL_ERR_MSG);
    }
}
 
Example 10
Source File: NativeSmallIntCallsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(java.lang.String[] unused) throws Exception {
    // Call through jni
    // JNI makes all results != 0 true for compatibility
    boolean nativeTrueVal = nativeCastToBoolCallTrue();
    Asserts.assertTrue(nativeTrueVal, "trueval is false!");

    boolean nativeFalseVal = nativeCastToBoolCallFalse();
    Asserts.assertTrue(nativeFalseVal, "falseval is false in native!");

    // Call a constructor or method that passes jboolean values into Java from native
    nativeCallBoolConstructor(1, true);
    nativeCallBoolConstructor(0x10, true);
    nativeCallBoolConstructor(0x100, false);
    nativeCallBoolConstructor(0x1000, false);
}
 
Example 11
Source File: MaterializeVirtualObjectTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void check(int iteration) {
    // Materialize virtual objects on last invocation
    if (iteration == COMPILE_THRESHOLD) {
        // get frames and check not-null
        HotSpotStackFrameReference materialized = CompilerToVMHelper.getNextStackFrame(
                /* topmost frame */ null, new ResolvedJavaMethod[]{MATERIALIZED_RESOLVED},
                /* don't skip any */ 0);
        Asserts.assertNotNull(materialized, getName()
                + " : got null frame for materialized method");
        HotSpotStackFrameReference notMaterialized = CompilerToVMHelper.getNextStackFrame(
                /* topmost frame */ null, new ResolvedJavaMethod[]{NOT_MATERIALIZED_RESOLVED},
                /* don't skip any */ 0);
        Asserts.assertNE(materialized, notMaterialized,
                "Got same frame pointer for both tested frames");
        Asserts.assertNotNull(notMaterialized, getName()
                + " : got null frame for not materialized method");
        // check that frames has virtual objects before materialization stage
        Asserts.assertTrue(materialized.hasVirtualObjects(), getName()
                + ": materialized frame has no virtual object before materialization");
        Asserts.assertTrue(notMaterialized.hasVirtualObjects(), getName()
                + ": notMaterialized frame has no virtual object before materialization");
        // materialize
        CompilerToVMHelper.materializeVirtualObjects(materialized, INVALIDATE);
        // check that only not materialized frame has virtual objects
        Asserts.assertFalse(materialized.hasVirtualObjects(), getName()
                + " : materialized has virtual object after materialization");
        Asserts.assertTrue(notMaterialized.hasVirtualObjects(), getName()
                + " : notMaterialized has no virtual object after materialization");
        // check that materialized frame was deoptimized in case invalidate=true
        Asserts.assertEQ(WB.isMethodCompiled(MATERIALIZED_METHOD), !INVALIDATE, getName()
                + " : materialized method has unexpected compiled status");
        // check that not materialized frame wasn't deoptimized
        Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD), getName()
                + " : not materialized method has unexpected compiled status");
    }
}
 
Example 12
Source File: SimpleClassFileLoadHookTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) {
    System.out.println(Foo.getValue());
    System.out.println(Foo.getOtherValue());
    Asserts.assertTrue("YYY".equals(Foo.getValue()) &&
                       "xYYYxx".equals(Foo.getOtherValue()),
                       "SimpleClassFileLoadHook should replace XXX with YYY");
}
 
Example 13
Source File: TestSnapshot.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void assertStaticOptions(Recording snapshot) {
    Asserts.assertTrue(snapshot.getName().startsWith("Snapshot"), "Recording name should begin with 'Snapshot'");
    Asserts.assertEquals(snapshot.getMaxAge(), null);
    Asserts.assertEquals(snapshot.getMaxSize(), 0L);
    Asserts.assertTrue(snapshot.getSettings().isEmpty());
    Asserts.assertEquals(snapshot.getState(), RecordingState.STOPPED);
    Asserts.assertEquals(snapshot.getDumpOnExit(), false);
    Asserts.assertEquals(snapshot.getDestination(), null);
}
 
Example 14
Source File: LockCompilationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected void test() throws Exception {
    checkNotCompiled();

    System.out.println("locking compilation");
    WHITE_BOX.lockCompilation();

    try {
        System.out.println("trying to compile");
        compile();
        // to check if it works correctly w/ safepoints
        System.out.println("going to safepoint");
        WHITE_BOX.fullGC();
        // Sleep a while and then make sure the compile is still waiting
        Thread.sleep(5000);

        Asserts.assertTrue(
                WHITE_BOX.isMethodQueuedForCompilation(method),
                method + " must be in queue");
        Asserts.assertFalse(
                WHITE_BOX.isMethodCompiled(method, false),
                method + " must be not compiled");
        Asserts.assertEQ(
                WHITE_BOX.getMethodCompilationLevel(method, false), 0,
                method + " comp_level must be == 0");
        Asserts.assertFalse(
                WHITE_BOX.isMethodCompiled(method, true),
                method + " must be not osr_compiled");
        Asserts.assertEQ(
                WHITE_BOX.getMethodCompilationLevel(method, true), 0,
                method + " osr_comp_level must be == 0");
    } finally {
        System.out.println("unlocking compilation");
        WHITE_BOX.unlockCompilation();
    }
    waitBackgroundCompilation();
    Asserts.assertFalse(
            WHITE_BOX.isMethodQueuedForCompilation(method),
            method + " must not be in queue");
}
 
Example 15
Source File: TestClassDefineEvent.java    From openjdk-jdk8u 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 16
Source File: TestShouldCommit.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void verifyShouldCommitTrue() {
    MyEvent event = new MyEvent();
    event.begin();
    event.end();
    Asserts.assertTrue(event.shouldCommit(), "shouldCommit() expected true");
}
 
Example 17
Source File: TestIsEnabled.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void assertEnabled(String msg) {
    SimpleEvent event = new SimpleEvent();
    Asserts.assertTrue(event.isEnabled(), msg);
}
 
Example 18
Source File: MaterializeVirtualObjectTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void testFrame(String str, int iteration) {
   Helper helper = new Helper(str);
   testFrame2(str, iteration);
   Asserts.assertTrue((helper.string != null) && (this != null)
           && (helper != null), String.format("%s : some locals are null", getName()));
}
 
Example 19
Source File: TestRecordedClassLoader.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 {
    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;
    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
            //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");
           // 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 20
Source File: TestHasValue.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
private static void testHasValue(Class<? extends java.lang.annotation.Annotation> clz) {

        System.out.println("class=" + clz);

        AnnotationElement a = new AnnotationElement(clz, "value");
        Asserts.assertTrue(a.hasValue("value"));
        Asserts.assertFalse(a.hasValue("nosuchvalue"));

    }