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

The following examples show how to use jdk.testlibrary.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: GetAnnotatedOwnerType.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void testTypeArgument() throws Exception {
    AnnotatedType tt = GetAnnotatedOwnerType.class.getField("typeArgument").getAnnotatedType();
    Asserts.assertEquals(tt.getAnnotation(TA.class).value(), "bad");
    Asserts.assertTrue(tt.getAnnotations().length == 1, "expecting one (1) annotation, got: "
            + tt.getAnnotations().length);

    // make sure inner is correctly annotated
    AnnotatedType inner = ((AnnotatedParameterizedType)tt).getAnnotatedActualTypeArguments()[0];
    Asserts.assertEquals(inner.getAnnotation(TB.class).value(), "tb");
    Asserts.assertTrue(inner.getAnnotations().length == 1, "expecting one (1) annotation, got: "
            + inner.getAnnotations().length);

    // make sure owner is correctly annotated
    AnnotatedType outer = inner.getAnnotatedOwnerType();
    Asserts.assertEquals(outer.getAnnotation(TA.class).value(), "good");
    Asserts.assertTrue(outer.getAnnotations().length == 1, "expecting one (1) annotation, got: "
            + outer.getAnnotations().length);
}
 
Example 2
Source File: TestDumpLongPath.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 {
    Recording r = new Recording();
    final String eventPath = EventNames.OSInformation;
    r.enable(eventPath);
    r.start();
    r.stop();

    Path dir = FileHelper.createLongDir(Paths.get("."));
    Path path = Paths.get(dir.toString(), "my.jfr");
    r.dump(path);
    r.close();

    Asserts.assertTrue(Files.exists(path), "Recording file does not exist: " + path);
    List<RecordedEvent> events = RecordingFile.readAllEvents(path);
    Asserts.assertFalse(events.isEmpty(), "No events found");
    String foundEventPath = events.get(0).getEventType().getName();
    System.out.printf("Found event: %s%n", foundEventPath);
    Asserts.assertEquals(foundEventPath, eventPath, "Wrong event");
}
 
Example 3
Source File: JmxHelper.java    From dragonwell8_jdk 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 4
Source File: HeapSummaryEventAllGcs.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void checkPSYoungStartEnd(RecordedEvent event) {
    long oldEnd = Events.assertField(event, "oldSpace.reservedEnd").getValue();
    long youngStart = Events.assertField(event, "youngSpace.start").getValue();
    long youngEnd = Events.assertField(event, "youngSpace.committedEnd").getValue();
    long edenStart = Events.assertField(event, "edenSpace.start").getValue();
    long edenEnd = Events.assertField(event, "edenSpace.end").getValue();
    long fromStart = Events.assertField(event, "fromSpace.start").getValue();
    long fromEnd = Events.assertField(event, "fromSpace.end").getValue();
    long toStart = Events.assertField(event, "toSpace.start").getValue();
    long toEnd = Events.assertField(event, "toSpace.end").getValue();
    Asserts.assertEquals(oldEnd, youngStart, "Young should start where old ends");
    Asserts.assertEquals(youngStart, edenStart, "Eden should be placed first in young");
    if (fromStart < toStart) {
        // [eden][from][to]
        Asserts.assertGreaterThanOrEqual(fromStart, edenEnd, "From should start after eden");
        Asserts.assertLessThanOrEqual(fromEnd, toStart, "To should start after From");
        Asserts.assertLessThanOrEqual(toEnd, youngEnd, "To should start after From");
    } else {
        // [eden][to][from]
        Asserts.assertGreaterThanOrEqual(toStart, edenEnd, "From should start after eden");
        Asserts.assertLessThanOrEqual(toEnd, fromStart, "To should start after From");
        Asserts.assertLessThanOrEqual(fromEnd, youngEnd, "To should start after From");
    }
}
 
Example 5
Source File: TestDumpState.java    From dragonwell8_jdk 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 6
Source File: TestExceptionEvents.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void checkThrowableEvents(List<RecordedEvent> events, String eventName,
    int excpectedEvents, Class<?> expectedClass, String expectedMessage) throws Exception {
    int count = 0;
    for(RecordedEvent event : events) {
        if (Events.isEventType(event, eventName)) {
            String message = Events.assertField(event, "message").getValue();
            if (expectedMessage.equals(message)) {
                RecordedThread t = event.getThread();
                String threadName = t.getJavaName();
                if (threadName != null && threadName.equals(Thread.currentThread().getName())) {
                    RecordedClass jc = event.getValue("thrownClass");
                    if (jc.getName().equals(expectedClass.getName())) {
                        count++;
                    }
                }
            }
        }
    }
    Asserts.assertEquals(count, excpectedEvents, "Wrong event count for type " + eventName);
}
 
Example 7
Source File: TestRecordedObject.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testUnsigned(RecordedObject event) {
    // Unsigned byte value
    Asserts.assertEquals(event.getByte("unsignedByte"), Byte.MIN_VALUE);
    Asserts.assertEquals(event.getInt("unsignedByte"), Byte.toUnsignedInt(Byte.MIN_VALUE));
    Asserts.assertEquals(event.getLong("unsignedByte"), Byte.toUnsignedLong(Byte.MIN_VALUE));
    Asserts.assertEquals(event.getShort("unsignedByte"), (short)Byte.toUnsignedInt(Byte.MIN_VALUE));

    // Unsigned char, nothing should happen, it is unsigned
    Asserts.assertEquals(event.getChar("unsignedChar"), 'q');
    Asserts.assertEquals(event.getInt("unsignedChar"), (int)'q');
    Asserts.assertEquals(event.getLong("unsignedChar"), (long)'q');

    // Unsigned short
    Asserts.assertEquals(event.getShort("unsignedShort"), Short.MIN_VALUE);
    Asserts.assertEquals(event.getInt("unsignedShort"), Short.toUnsignedInt(Short.MIN_VALUE));
    Asserts.assertEquals(event.getLong("unsignedShort"), Short.toUnsignedLong(Short.MIN_VALUE));

    // Unsigned int
    Asserts.assertEquals(event.getInt("unsignedInt"), Integer.MIN_VALUE);
    Asserts.assertEquals(event.getLong("unsignedInt"), Integer.toUnsignedLong(Integer.MIN_VALUE));

    // Unsigned long, nothing should happen
    Asserts.assertEquals(event.getLong("unsignedLong"), Long.MIN_VALUE);

    // Unsigned float, nothing should happen
    Asserts.assertEquals(event.getFloat("unsignedFloat"), Float.MIN_VALUE);

    // Unsigned double, nothing should happen
    Asserts.assertEquals(event.getDouble("unsignedDouble"), Double.MIN_VALUE);
}
 
Example 8
Source File: SimpleEventHelper.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void verifyEvents(Recording r, int ... ids) throws Exception {
    List<Integer> eventIds = new ArrayList<>();
    for (RecordedEvent event : Events.fromRecording(r)) {
        if (Events.isEventType(event, SimpleEvent.class.getName())) {
            int id = Events.assertField(event, "id").getValue();
            System.out.printf("recording %s: event.id=%d%n", r.getName(), id);
            eventIds.add(id);
        }
    }
    Asserts.assertEquals(eventIds.size(), ids.length, "Wrong number of events");
    for (int i = 0; i < ids.length; ++i) {
        Asserts.assertEquals(eventIds.get(i).intValue(), ids[i], "Wrong id in event");
    }
}
 
Example 9
Source File: TestRecordingFile.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void assertUniqueEventTypes(List<EventType> types) {
    HashSet<Long> ids = new HashSet<>();
    for (EventType type : types) {
        ids.add(type.getId());
    }
    Asserts.assertEquals(types.size(), ids.size(), "Event types repeated. " + types);
}
 
Example 10
Source File: TestSnapshot.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testOngoing(boolean disk) throws IOException {
    FlightRecorder recorder = FlightRecorder.getFlightRecorder();
    try (Recording r = new Recording()) {
        r.setToDisk(disk);
        r.enable(SimpleEvent.class);
        r.start();
        SimpleEvent se = new SimpleEvent();
        se.commit();

        try (Recording snapshot = recorder.takeSnapshot()) {

            Asserts.assertGreaterThan(snapshot.getSize(), 0L);
            Asserts.assertGreaterThanOrEqual(snapshot.getStartTime(), r.getStartTime());
            Asserts.assertGreaterThanOrEqual(snapshot.getStopTime(), r.getStartTime());
            Asserts.assertGreaterThanOrEqual(snapshot.getDuration(), Duration.ZERO);
            assertStaticOptions(snapshot);
            try (InputStream is = snapshot.getStream(null, null)) {
                Asserts.assertNotNull(is);
            }

            List<RecordedEvent> events = Events.fromRecording(snapshot);
            Events.hasEvents(events);
            Asserts.assertEquals(events.size(), 1);
            Asserts.assertEquals(events.get(0).getEventType().getName(), SimpleEvent.class.getName());
        }

        r.stop();
    }
}
 
Example 11
Source File: TestSnapshot.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testEmpty() throws IOException {
    FlightRecorder recorder = FlightRecorder.getFlightRecorder();
    Instant before = Instant.now().minusNanos(1);
    try (Recording snapshot = recorder.takeSnapshot()) {
        Instant after = Instant.now().plusNanos(1);
        Asserts.assertEquals(snapshot.getSize(), 0L);
        Asserts.assertLessThan(before, snapshot.getStartTime());
        Asserts.assertGreaterThan(after, snapshot.getStopTime());
        Asserts.assertEquals(snapshot.getStartTime(), snapshot.getStopTime());
        Asserts.assertEquals(snapshot.getDuration(), Duration.ZERO);
        assertStaticOptions(snapshot);
        Asserts.assertEquals(snapshot.getStream(null, null), null);
    }
}
 
Example 12
Source File: TestVMInfoEvent.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 {
    RuntimeMXBean mbean = ManagementFactory.getRuntimeMXBean();
    Recording recording = new Recording();
    recording.enable(EVENT_NAME);
    recording.start();
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    Events.hasEvents(events);
    for (RecordedEvent event : events) {
        System.out.println("Event:" + event);
        Events.assertField(event, "jvmName").equal(mbean.getVmName());
        String jvmVersion = Events.assertField(event, "jvmVersion").notEmpty().getValue();
        if (!jvmVersion.contains(mbean.getVmVersion())) {
            Asserts.fail(String.format("%s does not contain %s", jvmVersion, mbean.getVmVersion()));
        }

        String jvmArgs = Events.assertField(event, "jvmArguments").notNull().getValue();
        String jvmFlags = Events.assertField(event, "jvmFlags").notNull().getValue();
        String eventArgs = (jvmFlags.trim() + " " + jvmArgs).trim();
        String beanArgs = mbean.getInputArguments().stream().collect(Collectors.joining(" "));
        Asserts.assertEquals(eventArgs, beanArgs, "Wrong inputArgs");

        final String javaCommand = mbean.getSystemProperties().get("sun.java.command");
        Events.assertField(event, "javaArguments").equal(javaCommand);
        Events.assertField(event, "jvmStartTime").equal(mbean.getStartTime());
    }
}
 
Example 13
Source File: TestPrintJSON.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 {

        Path recordingFile = ExecuteHelper.createProfilingRecording().toAbsolutePath();

        OutputAnalyzer output = ExecuteHelper.run("print", "--json", recordingFile.toString());
        String json = output.getStdout();

        // Parse JSON using Nashorn
        String statement = "var jsonObject = " + json;
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("nashorn");
        engine.eval(statement);
        JSObject o = (JSObject) engine.get("jsonObject");
        JSObject recording = (JSObject) o.getMember("recording");
        JSObject events = (JSObject) recording.getMember("events");

        // Verify events are equal
        try (RecordingFile rf = new RecordingFile(recordingFile)) {
            for (Object jsonEvent : events.values()) {
                RecordedEvent recordedEvent = rf.readEvent();
                double typeId = recordedEvent.getEventType().getId();
                String startTime = recordedEvent.getStartTime().toString();
                String duration = recordedEvent.getDuration().toString();
                Asserts.assertEquals(typeId, ((Number) ((JSObject) jsonEvent).getMember("typeId")).doubleValue());
                Asserts.assertEquals(startTime, ((JSObject) jsonEvent).getMember("startTime"));
                Asserts.assertEquals(duration, ((JSObject) jsonEvent).getMember("duration"));
                assertEquals(jsonEvent, recordedEvent);
            }
            Asserts.assertFalse(rf.hasMoreEvents(), "Incorrect number of events");
        }
    }
 
Example 14
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 15
Source File: TestDestInvalid.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 {
    Recording r = new Recording();
    r.enable(EventNames.OSInformation);
    r.setToDisk(true);

    Asserts.assertNull(r.getDestination(), "dest not null by default");

    // Set destination to empty path (same as curr dir, not a file)
    verifyException(()->{r.setDestination(Paths.get(""));}, "No exception for setDestination(\"\")", IOException.class);
    System.out.println("1 destination: " + r.getDestination());
    Asserts.assertNull(r.getDestination(), "default dest not null after failed setDest");

    // Set dest to a valid path. This should be kept when a new setDest fails.
    Path dest = Paths.get(".", "my.jfr");
    r.setDestination(dest);
    System.out.println("2 destination: " + r.getDestination());
    Asserts.assertEquals(dest, r.getDestination(), "Wrong get/set dest");

    // Null is allowed for setDestination()
    r.setDestination(null);
    System.out.println("3 destination: " + r.getDestination());
    Asserts.assertNull(r.getDestination(), "dest not null after setDest(null)");

    // Reset dest to correct value and make ssure it is not overwritten
    r.setDestination(dest);
    System.out.println("4 destination: " + r.getDestination());
    Asserts.assertEquals(dest, r.getDestination(), "Wrong get/set dest");

    // Set destination to an existing dir. Old dest is saved.
    verifyException(()->{r.setDestination(Paths.get("."));}, "No exception for setDestination(.)", IOException.class);
    System.out.println("5 destination: " + r.getDestination());
    Asserts.assertEquals(dest, r.getDestination(), "Wrong get/set dest");

    // Set destination to a non-existing dir. Old dest is saved.
    verifyException(()->{r.setDestination(Paths.get(".", "missingdir", "my.jfr"));}, "No exception for setDestination(dirNotExists)", IOException.class);
    System.out.println("6 destination: " + r.getDestination());
    Asserts.assertEquals(dest, r.getDestination(), "Wrong get/set dest");

    // Verify that it works with the old setDest value.
    r.start();
    r.stop();
    r.close();
    Asserts.assertTrue(Files.exists(dest), "No recording file: " + dest);
    List<RecordedEvent> events = RecordingFile.readAllEvents(dest);
    Asserts.assertFalse(events.isEmpty(), "No event found");
    System.out.printf("Found event %s in %s%n", events.get(0).getEventType().getName(), dest.toString());
}
 
Example 16
Source File: TestEventFactory.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void assertEquals(ValueDescriptor v1, ValueDescriptor expected) {
    Asserts.assertEquals(v1.getName(), expected.getName());
    Asserts.assertEquals(v1.getTypeName(), expected.getTypeName());
    assertAnnotationEquals(v1.getAnnotationElements(), expected.getAnnotationElements());
}
 
Example 17
Source File: TestBiasedLockRevocationEvents.java    From dragonwell8_jdk 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 18
Source File: TestInheritedAnnotations.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void assertNoFather(List<RecordedEvent> events) throws Exception {
    String eventName = FatherEvent.class.getName();
    Events.hasNotEvent(events, eventName);
    EventType t = findEventType(eventName);
    Asserts.assertEquals(t.getCategoryNames(), Collections.singletonList(FAMILY_SMITH));
}
 
Example 19
Source File: TestThreshold.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 {
    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 20
Source File: TestEvacuationInfoEvent.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 {
    final long g1HeapRegionSize = 1024 * 1024;
    Recording recording = new Recording();
    recording.enable(EVENT_INFO_NAME).withThreshold(Duration.ofMillis(0));
    recording.enable(EVENT_FAILED_NAME).withThreshold(Duration.ofMillis(0));
    recording.start();
    allocate();
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    Asserts.assertFalse(events.isEmpty(), "No events found");
    for (RecordedEvent event : events) {
        if (!Events.isEventType(event, EVENT_INFO_NAME)) {
            continue;
        }
        System.out.println("Event: " + event);

        int setRegions = Events.assertField(event, "cSetRegions").atLeast(0).getValue();
        long setUsedAfter = Events.assertField(event, "cSetUsedAfter").atLeast(0L).getValue();
        long setUsedBefore = Events.assertField(event, "cSetUsedBefore").atLeast(setUsedAfter).getValue();
        int allocationRegions = Events.assertField(event, "allocationRegions").atLeast(0).getValue();
        long allocRegionsUsedBefore = Events.assertField(event, "allocRegionsUsedBefore").atLeast(0L).getValue();
        long allocRegionsUsedAfter = Events.assertField(event, "allocRegionsUsedAfter").atLeast(0L).getValue();
        long bytesCopied = Events.assertField(event, "bytesCopied").atLeast(0L).getValue();
        int regionsFreed = Events.assertField(event, "regionsFreed").atLeast(0).getValue();

        Asserts.assertEquals(allocRegionsUsedBefore + bytesCopied, allocRegionsUsedAfter, "allocRegionsUsedBefore + bytesCopied = allocRegionsUsedAfter");
        Asserts.assertGreaterThanOrEqual(setRegions, regionsFreed, "setRegions >= regionsFreed");
        Asserts.assertGreaterThanOrEqual(g1HeapRegionSize * allocationRegions, allocRegionsUsedAfter, "G1HeapRegionSize * allocationRegions >= allocationRegionsUsedAfter");
        Asserts.assertGreaterThanOrEqual(g1HeapRegionSize * setRegions, setUsedAfter, "G1HeapRegionSize * setRegions >= setUsedAfter");
        Asserts.assertGreaterThanOrEqual(g1HeapRegionSize * setRegions, setUsedBefore, "G1HeapRegionSize * setRegions >= setUsedBefore");
        Asserts.assertGreaterThanOrEqual(g1HeapRegionSize, allocRegionsUsedBefore, "G1HeapRegionSize >= allocRegionsUsedBefore");

        int gcId = Events.assertField(event, "gcId").getValue();
        boolean isEvacuationFailed = containsEvacuationFailed(events, gcId);
        if (isEvacuationFailed) {
            Asserts.assertGreaterThan(setUsedAfter, 0L, "EvacuationFailure -> setUsedAfter > 0");
            Asserts.assertGreaterThan(setRegions, regionsFreed, "EvacuationFailure -> setRegions > regionsFreed");
        } else {
            Asserts.assertEquals(setUsedAfter, 0L, "No EvacuationFailure -> setUsedAfter = 0");
            Asserts.assertEquals(setRegions, regionsFreed, "No EvacuationFailure -> setRegions = regionsFreed");
        }
    }
}