Java Code Examples for jdk.testlibrary.Asserts#assertTrue()
The following examples show how to use
jdk.testlibrary.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: TestGetDuration.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
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 2
Source File: TestEventMetadata.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
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 3
Source File: TestEventMetadata.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
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 4
Source File: TestStreamClosed.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
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 5
Source File: SHA512.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws Exception { MessageDigest md; // Test vectors obtained from // http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_224.pdf md = MessageDigest.getInstance("SHA-512/224"); Asserts.assertTrue(Arrays.equals(md.digest("abc".getBytes()), xeh("4634270F 707B6A54 DAAE7530 460842E2 0E37ED26 5CEEE9A4 3E8924AA"))); Asserts.assertTrue(Arrays.equals(md.digest(( "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu").getBytes()), xeh("23FEC5BB 94D60B23 30819264 0B0C4533 35D66473 4FE40E72 68674AF9"))); // Test vectors obtained from // http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_256.pdf md = MessageDigest.getInstance("SHA-512/256"); Asserts.assertTrue(Arrays.equals(md.digest("abc".getBytes()), xeh("53048E26 81941EF9 9B2E29B7 6B4C7DAB E4C2D0C6 34FC6D46 E0E2F131 07E7AF23"))); Asserts.assertTrue(Arrays.equals(md.digest(( "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu").getBytes()), xeh("3928E184 FB8690F8 40DA3988 121D31BE 65CB9D3E F83EE614 6FEAC861 E19B563A"))); }
Example 6
Source File: RefStatEvent.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
private static void verifyRefStatEvents(List<RecordedEvent> events, int gcId) { List<String> expectedTypes = Arrays.asList("Soft reference", "Weak reference", "Final reference", "Phantom reference"); List<String> actualTypes = new ArrayList<>(); try { for (RecordedEvent event : events) { if (!Events.isEventType(event, refStatsEventPath)) { continue; } Events.assertField(event, "count").atLeast(0L); if (Events.assertField(event, "gcId").isEqual(gcId)) { actualTypes.add(Events.assertField(event, "type").notEmpty().getValue()); } } Asserts.assertEquals(actualTypes.size(), expectedTypes.size(), "Wrong number of refStat events"); Asserts.assertTrue(expectedTypes.containsAll(actualTypes), "Found unknown refStat types"); Asserts.assertTrue(actualTypes.containsAll(expectedTypes), "Missning refStat types"); } catch (Exception e) { System.out.println("Expected refStatTypes: " + expectedTypes.stream().collect(Collectors.joining(", "))); System.out.println("Got refStatTypes: " + actualTypes.stream().collect(Collectors.joining(", "))); throw e; } }
Example 7
Source File: TestDumpState.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
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 8
Source File: TestDumpLongPath.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
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 9
Source File: GetAnnotatedOwnerType.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public static void testRaw() throws Exception { Field f = GetAnnotatedOwnerType.class.getField("rawField"); // make sure inner is correctly annotated AnnotatedType inner = f.getAnnotatedType(); Asserts.assertEquals(inner.getAnnotation(TB.class).value(), "raw"); Asserts.assertTrue(inner.getAnnotations().length == 1, "expecting one (1) annotation, got: " + inner.getAnnotations().length); // make sure owner is correctly annotated, on the correct type AnnotatedType outer = inner.getAnnotatedOwnerType(); Asserts.assertEquals(outer.getType(), ((Class<?>)f.getGenericType()).getEnclosingClass()); Asserts.assertEquals(outer.getAnnotation(TA.class).value(), "raw"); Asserts.assertTrue(outer.getAnnotations().length == 1, "expecting one (1) annotation, got: " + outer.getAnnotations().length); }
Example 10
Source File: TestEnoughPermission.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
private static long testRecording(FlightRecorderMXBean bean) throws Exception { System.out.println("A"); long recId = bean.newRecording(); System.out.println("B"); bean.setPredefinedConfiguration(recId, "profile"); System.out.println("C"); bean.startRecording(recId); System.out.println("D"); Asserts.assertTrue(bean.getRecordings().stream().anyMatch(r -> { return r.getId() == recId; }), "recId not found"); System.out.println("E"); bean.stopRecording(recId); final Path path = Paths.get(".", String.format("rec%d.jfr", recId)); bean.copyTo(recId, path.toString()); //EventSet events = EventSet.fromFile(path); return recId; }
Example 11
Source File: GCEventAll.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private int getLastGcId(List<RecordedEvent> events) { int lastGcId = -1; for (RecordedEvent event : events) { if (GCHelper.isGcEvent(event)) { int gcId = GCHelper.getGcId(event); if (gcId > lastGcId) { lastGcId = gcId; } } } Asserts.assertTrue(lastGcId != -1, "No gcId found"); return lastGcId; }
Example 12
Source File: TestIsInitialized.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Throwable { Asserts.assertFalse(FlightRecorder.isInitialized()); FlightRecorder.addListener(new FlightRecorderListener() { public void recorderInitialized(FlightRecorder recorder) { Asserts.assertTrue(FlightRecorder.isInitialized()); } }); FlightRecorder.getFlightRecorder(); Asserts.assertTrue(FlightRecorder.isInitialized()); }
Example 13
Source File: TestGetAnnotations.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private static void assertAnnotation(List<AnnotationElement> annos, String name, Object expectedValue) { for (AnnotationElement a : annos) { if (a.getTypeName().equals(name)) { Object value = a.getValue("value"); Asserts.assertTrue(Objects.deepEquals(value, expectedValue), "Found annotation " + name + " but value was "+ value +" but expected " + expectedValue); } } }
Example 14
Source File: TestEnableName.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Throwable { Recording r = new Recording(); final String eventType = EventNames.FileWrite; r.enable(eventType).withoutStackTrace(); r.start(); Files.write(Paths.get(".", "dummy.txt"), "DummyFile".getBytes()); r.stop(); Iterator<RecordedEvent> iterator = Events.fromRecording(r).iterator(); Asserts.assertTrue(iterator.hasNext(), "No events found"); System.out.printf("Event:%n%s%n", iterator.next()); r.close(); }
Example 15
Source File: TestJcmdStartStopDefault.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private static String parseRecordingName(OutputAnalyzer output) { // Expected output: // Started recording recording-1. No limit (duration/maxsize/maxage) in use. // Use JFR.dump name=recording-1 filename=FILEPATH to copy recording data to file. String stdout = output.getStdout(); Pattern p = Pattern.compile(".*Use JFR.dump name=(\\S+).*", Pattern.DOTALL); Matcher m = p.matcher(stdout); Asserts.assertTrue(m.matches(), "Could not parse recording name"); String name = m.group(1); System.out.println("Recording name=" + name); return name; }
Example 16
Source File: GCEventAll.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
/** * Verifies that the collector name in initial configuration matches the name in garbage configuration event. * If the names are not equal, then we check if this is an expected collector override. * For example, if old collector in initial config is "G1Old" we allow both event "G1Old" and "SerialOld". */ private void verifyCollectorNames(List<GCHelper.GcBatch> batches) { for (GCHelper.GcBatch batch : batches) { String name = batch.getName(); Asserts.assertNotNull(name, "garbage_collection.name was null"); boolean isYoung = batch.isYoungCollection(); String expectedName = isYoung ? youngCollector : oldCollector; if (!expectedName.equals(name)) { // Collector names not equal. Check if the collector has been overridden by an expected collector. String overrideKey = expectedName + "." + name; boolean isOverride = GCHelper.collectorOverrides.contains(overrideKey); Asserts.assertTrue(isOverride, String.format("Unexpected event name(%s) for collectors(%s, %s)", name, youngCollector, oldCollector)); } } }
Example 17
Source File: Invalid.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
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 18
Source File: TestRecordedFullStackTrace.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private static void checkEvent(RecordedEvent event, int expectedDepth) throws Throwable { RecordedStackTrace stacktrace = null; try { stacktrace = event.getStackTrace(); List<RecordedFrame> frames = stacktrace.getFrames(); Asserts.assertEquals(Math.min(MAX_DEPTH, expectedDepth), frames.size(), "Wrong stacktrace depth. Expected:" + expectedDepth); List<String> expectedMethods = getExpectedMethods(expectedDepth); Asserts.assertEquals(expectedMethods.size(), frames.size(), "Wrong expectedMethods depth. Test error."); for (int i = 0; i < frames.size(); ++i) { String name = frames.get(i).getMethod().getName(); String expectedName = expectedMethods.get(i); System.out.printf("method[%d]=%s, expected=%s%n", i, name, expectedName); Asserts.assertEquals(name, expectedName, "Wrong method name"); } boolean isTruncated = stacktrace.isTruncated(); boolean isTruncateExpected = expectedDepth > MAX_DEPTH; Asserts.assertEquals(isTruncated, isTruncateExpected, "Wrong value for isTruncated. Expected:" + isTruncateExpected); String firstMethod = frames.get(frames.size() - 1).getMethod().getName(); boolean isFullTrace = "run".equals(firstMethod); String msg = String.format("Wrong values for isTruncated=%b, isFullTrace=%b", isTruncated, isFullTrace); Asserts.assertTrue(isTruncated != isFullTrace, msg); } catch (Throwable t) { System.out.println(String.format("stacktrace:%n%s", stacktrace)); throw t; } }
Example 19
Source File: TestIsAvailable.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { boolean expected = Boolean.parseBoolean(args[0]); System.out.println("Expected: " + expected); Asserts.assertTrue(expected == FlightRecorder.isAvailable()); }
Example 20
Source File: TestOwnCommit.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Throwable { Recording r = new Recording(); r.enable(MyEvent.class); r.start(); MyEvent event = new MyEvent(); event.begin(); event.begin = 10; event.duration = Instant.now(); MyEvent.startTime = 20; event.shouldCommit = "shouldCommit"; MyEvent.set = 30; event.end(); event.commit(); // Verify that our methods have not been removed by transformation. int id = 0; event.begin(++id); Asserts.assertEquals(id, staticTestValue, "EventWithBegin failed to set value"); event.end(++id); Asserts.assertEquals(id, staticTestValue, "EventWithEnd failed to set value"); event.commit(++id); Asserts.assertEquals(id, staticTestValue, "EventWithCommit failed to set value"); event.shouldCommit(++id); Asserts.assertEquals(id, staticTestValue, "EventWithShouldCommit failed to set value"); event.set(++id); Asserts.assertEquals(id, staticTestValue, "EventWithSet failed to set value"); r.stop(); Iterator<RecordedEvent> it = Events.fromRecording(r).iterator(); Asserts.assertTrue(it.hasNext(), "No events"); RecordedEvent recordedEvent = it.next(); Asserts.assertTrue(Events.isEventType(recordedEvent, MyEvent.class.getName())); Events.assertField(recordedEvent, "begin").equal(10L); Events.assertField(recordedEvent, "shouldCommit").equal("shouldCommit"); Events.assertField(recordedEvent, "startTime"); Events.assertField(recordedEvent, "duration"); Asserts.assertNull(recordedEvent.getEventType().getField("set")); // static not supported Asserts.assertFalse(it.hasNext(), "More than 1 event"); r.close(); }