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

The following examples show how to use jdk.testlibrary.Asserts#assertFalse() . 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: TestEventMetadata.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void verifyName(String name) {
    System.out.println("Verifying name: " + name);
    Asserts.assertNotEquals(name, null, "Name not allowed to be null");
    Asserts.assertTrue(name.length() > 1, "Name must be at least two characters");
    Asserts.assertTrue(name.length() < 32, "Name should not exceed 32 characters");
    Asserts.assertFalse(isReservedKeyword(name),"Name must not be reserved keyword in the Java language (" + name + ")");
    char firstChar = name.charAt(0);
    Asserts.assertTrue(Character.isAlphabetic(firstChar), "Name must start with a character");
    Asserts.assertTrue(Character.isLowerCase(firstChar), "Name must start with lower case letter");
    Asserts.assertTrue(Character.isJavaIdentifierStart(firstChar), "Not valid first character for Java identifier");
    for (int i = 1; i < name.length(); i++) {
        Asserts.assertTrue(Character.isJavaIdentifierPart(name.charAt(i)), "Not valid character for a Java identifier");
        Asserts.assertTrue(Character.isAlphabetic(name.charAt(i)), "Name must consists of characters, found '" + name.charAt(i) + "'");
    }
    Asserts.assertFalse(name.contains("ID"), "'ID' should not be used in name, consider using 'Id'");
    checkCommonAbbreviations(name);
}
 
Example 2
Source File: TestDestReadOnly.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 {
    Path readOnlyDir = FileHelper.createReadOnlyDir(Paths.get(".", "readonly"));
    if (!FileHelper.isReadOnlyPath(readOnlyDir)) {
        System.out.println("Failed to create read-only dir. Running as root?. Test ignored");
        return;
    }

    Path readOnlyDest = Paths.get(readOnlyDir.toString(), "readonly.jfr");
    Recording r = new Recording();
    r.enable(EventNames.OSInformation);
    r.setToDisk(true);
    verifyException(()->{r.setDestination(readOnlyDest);}, "No exception for setDestination to read-only dir");

    System.out.println("r.getDestination() = " + r.getDestination());

    // Verify that it works if we set destination to a writable dir.
    Path dest = Paths.get(".", "my.jfr");
    r.setDestination(dest);
    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 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: TestHeapSummaryEventConcurrentCMS.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 recording = new Recording();
    recording.enable(EventNames.GarbageCollection).withThreshold(Duration.ofMillis(0));
    recording.enable(EventNames.GCHeapSummary).withThreshold(Duration.ofMillis(0));

    recording.start();
    // Need several GCs to ensure at least one heap summary event from concurrent CMS
    GCHelper.callSystemGc(6, true);
    recording.stop();

    // Remove first and last GCs which can be incomplete
    List<RecordedEvent> events = GCHelper.removeFirstAndLastGC(Events.fromRecording(recording));
    Asserts.assertFalse(events.isEmpty(), "No events found");
    for (RecordedEvent event : events) {
        System.out.println("Event: " + event);
        if (!isCmsGcEvent(event)) {
            continue;
        }
        int gcId = Events.assertField(event, "gcId").getValue();
        verifyHeapSummary(events, gcId, "Before GC");
        verifyHeapSummary(events, gcId, "After GC");
    }
}
 
Example 5
Source File: TestEventMetadata.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void verifyEventType(EventType eventType) {
    System.out.println("Verifying event: " + eventType.getName());
    verifyDescription(eventType.getDescription());
    verifyLabel(eventType.getLabel());
    Asserts.assertNotEquals(eventType.getName(), null, "Name not allowed to be null");
    Asserts.assertTrue(eventType.getName().startsWith("com.oracle.jdk."), "Oracle events must start with com.oracle.jdk");
    String name = eventType.getName().substring("com.oracle.jdk.".length());
    Asserts.assertFalse(isReservedKeyword(name),"Name must not be reserved keyword in the Java language (" + name + ")");
    checkCommonAbbreviations(name);
      char firstChar = name.charAt(0);
    Asserts.assertFalse(name.contains("ID"), "'ID' should not be used in name, consider using 'Id'");
    Asserts.assertTrue(Character.isAlphabetic(firstChar), "Name " + name + " must start with a character");
    Asserts.assertTrue(Character.isUpperCase(firstChar), "Name " + name + " must start with upper case letter");
    for (int i = 0; i < name.length(); i++) {
        char c = name.charAt(i);
        Asserts.assertTrue(Character.isAlphabetic(c) || Character.isDigit(c), "Name " + name + " must consists of characters or numbers, found '" + name.charAt(i) + "'");
    }
}
 
Example 6
Source File: ObjectCountEventVerifier.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void verify(List<RecordedEvent> objectCountEvents) throws Exception {
    Asserts.assertFalse(objectCountEvents.isEmpty(), "Expected at least one object count event");
    // Object count events should be sent only for those classes which instances occupy over 0.5%
    // of the heap. Therefore there can't be more than 200 events
    Asserts.assertLessThanOrEqual(objectCountEvents.size(), 200, "Expected at most 200 object count events");

    HashMap<String, Long> numInstancesOfClass = new HashMap<String, Long>();
    HashMap<String, Long> sizeOfInstances = new HashMap<String, Long>();

    for (RecordedEvent event : objectCountEvents) {
        String className = Events.assertField(event, "objectClass.name").notEmpty().getValue();
        long count = Events.assertField(event, "count").atLeast(0L).getValue();
        long totalSize = Events.assertField(event, "totalSize").atLeast(1L).getValue();
        System.out.println(className);
        numInstancesOfClass.put(className, count);
        sizeOfInstances.put(className, totalSize);
    }
    System.out.println(numInstancesOfClass);
    final String fooArrayName = "[Ljdk/jfr/event/gc/objectcount/Foo;";
    Asserts.assertTrue(numInstancesOfClass.containsKey(fooArrayName), "Expected an event for the Foo array");
    Asserts.assertEquals(sizeOfInstances.get(fooArrayName), expectedFooArraySize(Constants.oneMB), "Wrong size of the Foo array");
}
 
Example 7
Source File: TestDump.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 {
    Recording r = new Recording();
    r.enable(EventNames.OSInformation);
    r.start();
    r.stop();

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

    Asserts.assertTrue(Files.exists(path), "Recording file does not exist: " + path);
    Asserts.assertFalse(RecordingFile.readAllEvents(path).isEmpty(), "No events found");
}
 
Example 8
Source File: TestObjectCountEvent.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 {
    Recording recording = new Recording();
    recording.enable(objectCountEventPath);
    recording.enable(heapSummaryEventPath);

    ObjectCountEventVerifier.createTestData();
    System.gc();
    recording.start();
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    for (RecordedEvent event : events) {
        System.out.println("Event: " + event);
    }

    Optional<RecordedEvent> heapSummaryEvent = events.stream()
                            .filter(e -> Events.isEventType(e, heapSummaryEventPath))
                            .filter(e -> "After GC".equals(Events.assertField(e, "when").getValue()))
                            .findFirst();
    Asserts.assertTrue(heapSummaryEvent.isPresent(), "No heapSummary with cause='After GC'");
    System.out.println("Found heapSummaryEvent: " + heapSummaryEvent.get());
    Events.assertField(heapSummaryEvent.get(), "heapUsed").atLeast(0L).getValue();
    int gcId = Events.assertField(heapSummaryEvent.get(), "gcId").getValue();

    List<RecordedEvent> objCountEvents = events.stream()
                            .filter(e -> Events.isEventType(e, objectCountEventPath))
                            .filter(e -> isGcId(e, gcId))
                            .collect(Collectors.toList());
    Asserts.assertFalse(objCountEvents.isEmpty(), "No objCountEvents for gcId=" + gcId);
    ObjectCountEventVerifier.verify(objCountEvents);
}
 
Example 9
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 10
Source File: GCEventAll.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private Set<Integer> verifyUniqueIds(List<GCHelper.GcBatch> batches) {
    Set<Integer> gcIds = new HashSet<>();
    for (GCHelper.GcBatch batch : batches) {
        Integer gcId = new Integer(batch.getGcId());
        Asserts.assertFalse(gcIds.contains(gcId), "Duplicate gcId: " + gcId);
        gcIds.add(gcId);
    }
    return gcIds;
}
 
Example 11
Source File: Invalid.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 {

        // Normal
        FilePermission fp = new FilePermission("a", "read");

        // Invalid
        FilePermission fp1 = new FilePermission("a\000", "read");
        FilePermission fp2 = new FilePermission("a\000", "read");
        FilePermission fp3 = new FilePermission("b\000", "read");

        // Invalid equals to itself
        Asserts.assertEQ(fp1, fp1);

        // and not equals to anything else, including other invalid ones
        Asserts.assertNE(fp, fp1);
        Asserts.assertNE(fp1, fp);
        Asserts.assertNE(fp1, fp2);
        Asserts.assertNE(fp1, fp3);

        // Invalid implies itself
        Asserts.assertTrue(fp1.implies(fp1));

        // and not implies or implied by anything else, including other
        // invalid ones
        Asserts.assertFalse(fp.implies(fp1));
        Asserts.assertFalse(fp1.implies(fp));
        Asserts.assertFalse(fp1.implies(fp2));
        Asserts.assertFalse(fp1.implies(fp3));
    }
 
Example 12
Source File: TestClassLoadingStatisticsEvent.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static RecordedEvent getCurrentEvent() throws Throwable {
    Recording recording = new Recording();
    recording.enable(EVENT_PATH);
    recording.start();
    recording.stop();
    List<RecordedEvent> events = Events.fromRecording(recording);
    Asserts.assertFalse(events.isEmpty(), "No events in recording");
    RecordedEvent event = events.get(0);
    return event;
}
 
Example 13
Source File: TestJpsSanity.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testJpsUnknownHost() throws Exception {
    String invalidHostName = "Oja781nh2ev7vcvbajdg-Sda1-C";
    OutputAnalyzer output = JpsHelper.jps(invalidHostName);
    Asserts.assertNotEquals(output.getExitValue(), 0, "Exit code shouldn't be 0");
    Asserts.assertFalse(output.getStderr().isEmpty(), "Error output should not be empty");
    output.shouldContain("Unknown host: " + invalidHostName);
}
 
Example 14
Source File: TestStartRecording.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 String recordingName = "TestStartRecording";
    OutputAnalyzer output = StartupHelper.jcmd("JFR.check");
    output.shouldContain(recordingName);

    Path path = Paths.get(".", "my.jfr");
    output = StartupHelper.jcmd("JFR.dump", "name=" + recordingName, "filename=" + path);
    Asserts.assertFalse(RecordingFile.readAllEvents(path).isEmpty(), "No events found");
}
 
Example 15
Source File: TestDumpOnCrash.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void processOutput(OutputAnalyzer output) throws Exception {
    //output.shouldContain("CreateCoredumpOnCrash turned off, no core file dumped");

    final Path jfrEmergencyFilePath = getHsErrJfrPath(output);
    Asserts.assertTrue(Files.exists(jfrEmergencyFilePath), "No emergency jfr recording file " + jfrEmergencyFilePath + " exists");
    Asserts.assertNotEquals(Files.size(jfrEmergencyFilePath), 0L, "File length 0. Should at least be some bytes");
    System.out.printf("File size=%d%n", Files.size(jfrEmergencyFilePath));

    List<RecordedEvent> events = RecordingFile.readAllEvents(jfrEmergencyFilePath);
    Asserts.assertFalse(events.isEmpty(), "No event found");
    System.out.printf("Found event %s%n", events.get(0).getEventType().getName());
}
 
Example 16
Source File: TestJpsSanity.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static void testJpsVersion() throws Exception {
    OutputAnalyzer output = JpsHelper.jps("-version");
    Asserts.assertNotEquals(output.getExitValue(), 0, "Exit code shouldn't be 0");
    Asserts.assertFalse(output.getStderr().isEmpty(), "Error output should not be empty");
    output.shouldContain("illegal argument: -version");
}
 
Example 17
Source File: ObjectCountAfterGCEvent.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void test(String gcName) throws Exception {
    Recording recording = new Recording();
    recording.enable(objectCountEventPath);
    recording.enable(gcEventPath);
    recording.enable(heapSummaryEventPath);

    ObjectCountEventVerifier.createTestData();
    recording.start();
    System.gc();
    System.gc();
    recording.stop();

    System.out.println("gcName=" + gcName);
    for (RecordedEvent event : Events.fromRecording(recording)) {
        System.out.println("Event: " + event);
    }

    List<RecordedEvent> events= Events.fromRecording(recording);
    Optional<RecordedEvent> gcEvent = events.stream()
                            .filter(e -> isMySystemGc(e, gcName))
                            .findFirst();
    Asserts.assertTrue(gcEvent.isPresent(), "No event System.gc event of type " + gcEventPath);
    System.out.println("Found System.gc event: " + gcEvent.get());
    int gcId = Events.assertField(gcEvent.get(), "gcId").getValue();

    List<RecordedEvent> objCountEvents = events.stream()
                            .filter(e -> Events.isEventType(e, objectCountEventPath))
                            .filter(e -> isGcId(e, gcId))
                            .collect(Collectors.toList());
    Asserts.assertFalse(objCountEvents.isEmpty(), "No objCountEvents for gcId=" + gcId);

    Optional<RecordedEvent> heapSummaryEvent = events.stream()
                            .filter(e -> Events.isEventType(e, heapSummaryEventPath))
                            .filter(e -> isGcId(e, gcId))
                            .filter(e -> "After GC".equals(Events.assertField(e, "when").getValue()))
                            .findFirst();
    Asserts.assertTrue(heapSummaryEvent.isPresent(), "No heapSummary for gcId=" + gcId);
    System.out.println("Found heapSummaryEvent: " + heapSummaryEvent.get());

    Events.assertField(heapSummaryEvent.get(), "heapUsed").atLeast(0L).getValue();
    ObjectCountEventVerifier.verify(objCountEvents);
}
 
Example 18
Source File: TestShouldCommit.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void verifyShouldCommitFalse() {
    MyEvent event = new MyEvent();
    event.begin();
    event.end();
    Asserts.assertFalse(event.shouldCommit(), "shouldCommit() expected false");
}
 
Example 19
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 20
Source File: TestHasValue.java    From dragonwell8_jdk 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"));

    }