@Test @DisplayName("can dump and load resolved projects") void persistResolvedProject() { ResolvedProject project = new ResolvedProject( ProjectCoordinates.from("project", "artifact"), of( ArtifactCoordinates.from("project", "artifact", "v1"), ArtifactCoordinates.from("project", "artifact", "v2")) ); String artifactAsYaml = persister.writeResolvedProject(project); ResolvedProject loadedProject = persister.readResolvedProject(artifactAsYaml); assertThat(loadedProject).isEqualTo(project); // equality is based on coordinates so we have to check versions explicitly assertThat(loadedProject.versions()).isEqualTo(project.versions()); }
@Test @DisplayName("can dump and load resolved artifacts") void persistResolvedArtifact() { ResolvedArtifact artifact = new ResolvedArtifact( ArtifactCoordinates.from("artifact.group", "artifact", "version"), of( ArtifactCoordinates.from("dependee1.group", "dep", "v1"), ArtifactCoordinates.from("dependee2.group", "dependee", "v2")) ); String artifactAsYaml = persister.writeResolvedArtifact(artifact); ResolvedArtifact loadedArtifact = persister.readResolvedArtifact(artifactAsYaml); assertThat(loadedArtifact).isEqualTo(artifact); // equality is based on coordinates so we have to check dependees explicitly assertThat(loadedArtifact.dependees()).isEqualTo(artifact.dependees()); }
@Test @DisplayName("can dump and load analyzed artifacts") void persistAnalyzedArtifact() { AnalyzedArtifact artifact = new AnalyzedArtifact( ArtifactCoordinates.from("artifact.group", "artifact", "version"), of( Violation.buildFor( Type.of("artifact.package", "Class"), of( InternalType.of("sun.misc", "Unsafe", "internal", "JDK-internal"), InternalType.of("sun.misc", "BASE64Encoder", "internal", "JDK-internal"))), Violation.buildFor( Type.of("artifact.package", "Class"), of( InternalType.of("sun.misc", "Unsafe", "internal", "JDK-internal"), InternalType.of("sun.misc", "BASE64Encoder", "internal", "JDK-internal")))) ); String artifactAsYaml = persister.writeAnalyzedArtifact(artifact); AnalyzedArtifact loadedArtifact = persister.readAnalyzedArtifact(artifactAsYaml); assertThat(loadedArtifact).isEqualTo(artifact); // equality is based on coordinates so we have to check violations explicitly assertThat(loadedArtifact.violations()).isEqualTo(artifact.violations()); }
/** Tests to ensure that the {@link QueueElement#clone()} method will clone correctly. */ @Test public void testClone() { final QueueElement elem = new QueueElement(); final QueueElement clone; final SongInfo song = new SongInfo(5); final SongInfo cloneSong; elem.setID(7); elem.setQueueIndex(11); elem.setPlayIndex(13); elem.setSong(song); clone = elem.clone(); assertNotNull(clone); assertTrue(elem != clone); assertEquals(elem.getID(), clone.getID()); assertEquals(elem.getQueueIndex(), clone.getQueueIndex()); assertEquals(elem.getPlayIndex(), clone.getPlayIndex()); cloneSong = clone.getSong(); assertNotNull(cloneSong); assertTrue(song != cloneSong); assertEquals(song.getID(), cloneSong.getID()); }
/** Tests to ensure that the {@link IDSet#size()} returns the set's size correctly. */ @Test public void testSize() { final IDSet<TestIdentifiable> s = new IDSet<>(); assertEquals(0, s.size()); s.add(new TestIdentifiable(3)); assertEquals(1, s.size()); s.add(new TestIdentifiable(7)); assertEquals(2, s.size()); s.add(new TestIdentifiable(11)); assertEquals(3, s.size()); s.clear(); assertEquals(0, s.size()); assertEquals("[]", s.toString()); }
/** Tests to ensure that a queue's play index can be set one of its middle elements */ @Test public void testSetPlayIndex_ValidQueue_MiddleElement() throws IOException { final Library lib = LibraryRESTCalls.getLibrary(); final int songID1 = lib.getSongs().get(0).getID(); final int songID2 = lib.getSongs().get(1).getID(); final int songID3 = lib.getSongs().get(2).getID(); Queue q = QueueRESTCalls.createQueue(); final int qID; queuesToCleanup.add(q); qID = q.getID(); // Add songs to the queue q = QueueRESTCalls.addLast(200, qID, songID1, songID2, songID3); // Verify that the play index can be set to 1 q = QueueRESTCalls.setPlayIndex(qID, 1); assertThat(q.getPlayIndex(), is(equalTo(1))); assertThat(q.getElement(0).getPlayIndex(), is(equalTo(-1))); assertThat(q.getElement(1).getPlayIndex(), is(equalTo(0))); assertThat(q.getElement(2).getPlayIndex(), is(equalTo(1))); }
/** Tests to ensure that the a song can be removed from a queue when it is the only element in the queue. */ @Test public void testRemoveElement_Only() throws IOException { final Library lib = LibraryRESTCalls.getLibrary(); Queue q = QueueRESTCalls.createQueue(); final int qID; queuesToCleanup.add(q); qID = q.getID(); // Add a song to the queue q = QueueRESTCalls.addLast(200, qID, lib.getSongs().get(0).getID()); // Remove the song and verify the returned queue does not contain the song q = QueueRESTCalls.removeElement(qID, 0); assertEquals(0, q.size()); // Get the queue and verify the song has been removed q = QueueRESTCalls.getQueue(qID); assertEquals(0, q.size()); }
/** * Tests to ensure that a {@link NullComparator} can be used to sort a collection that contains {@code null} * elements in descending order. The {@code null}s should be at the beginning of the sorted collection. */ @Test public void testSortDescNullsFirst() { final Integer a = 13; final Integer b = null; final Integer c = 7; final Integer d = null; final Integer e = 11; final List<Integer> list = new ArrayList<>(); list.add(a); list.add(b); list.add(c); list.add(d); list.add(e); Collections.sort(list, new IntegerNullComparator(NullComparator.Order.DESC_NULLS_FIRST)); assertNull(list.get(0)); assertNull(list.get(1)); assertEquals(a, list.get(2)); assertEquals(e, list.get(3)); assertEquals(c, list.get(4)); }
/** Tests to ensure that a queue's play index can be set to its first element */ @Test public void testSetPlayIndex_ValidQueue_FirstElement() throws IOException { final Library lib = LibraryRESTCalls.getLibrary(); final int songID1 = lib.getSongs().get(0).getID(); final int songID2 = lib.getSongs().get(1).getID(); final int songID3 = lib.getSongs().get(2).getID(); Queue q = QueueRESTCalls.createQueue(); final int qID; queuesToCleanup.add(q); qID = q.getID(); // Add songs to the queue q = QueueRESTCalls.addLast(200, qID, songID1, songID2, songID3); // Verify that the play index can be set to 2 q = QueueRESTCalls.setPlayIndex(qID, 0); assertThat(q.getPlayIndex(), is(equalTo(0))); assertThat(q.getElement(0).getPlayIndex(), is(equalTo(0))); assertThat(q.getElement(1).getPlayIndex(), is(equalTo(1))); assertThat(q.getElement(2).getPlayIndex(), is(equalTo(2))); }
/** * Tests to ensure that the queue elements' indices are correct after the {@link Queue#addSongLast(SongInfo)} * function is used to add a song to the queue. */ @Test public void testIndicesAfterAddSongLast() { final Queue queue = new Queue(); final SongInfo a = new SongInfo(5); final SongInfo b = new SongInfo(7); final SongInfo c = new SongInfo(11); queue.addSongLast(a); assertEquals(0, queue.getElement(0).getPlayIndex()); assertEquals(0, queue.getElement(0).getQueueIndex()); queue.addSongLast(b); assertEquals(0, queue.getElement(0).getPlayIndex()); assertEquals(0, queue.getElement(0).getQueueIndex()); assertEquals(1, queue.getElement(1).getPlayIndex()); assertEquals(1, queue.getElement(1).getQueueIndex()); queue.addSongLast(c); assertEquals(0, queue.getElement(0).getPlayIndex()); assertEquals(0, queue.getElement(0).getQueueIndex()); assertEquals(1, queue.getElement(1).getPlayIndex()); assertEquals(1, queue.getElement(1).getQueueIndex()); assertEquals(2, queue.getElement(2).getPlayIndex()); assertEquals(2, queue.getElement(2).getQueueIndex()); }
/** Tests to ensure that the {@link IDSet#clear()} function empties the set. */ @Test @SuppressWarnings("unused") public void testClear() { final IDSet<TestIdentifiable> s = new IDSet<>(); int count = 0; s.add(new TestIdentifiable(3)); s.add(new TestIdentifiable(7)); s.add(new TestIdentifiable(11)); assertNotEquals(0, s.size()); assertNotEquals("[]", s.toString()); s.clear(); assertEquals(0, s.size()); for(TestIdentifiable i : s) { count++; } assertEquals(0, count); assertEquals("[]", s.toString()); }
@Test @DisplayName("Given a valid kubernetes service we provide a valid config service info") public void testCreateServiceInfo() throws Exception { Service service = new Service(); service.setMetadata(getObjectMeta()); service.setSpec(getServiceSpec()); ConfigServerServiceInfo serviceInfo = configServerServiceInfoCreator.createServiceInfo(service); assertNotNull(serviceInfo); assertEquals("http://config-service:8080/", serviceInfo.getUri()); }
@Test @DisplayName("Given a kubernetes service without ports we launch a NPE") public void testCreateServiceInfo_ServiceNoPorts() throws Exception { Service service = new Service(); service.setMetadata(getObjectMeta()); expectThrows(NullPointerException.class, () -> configServerServiceInfoCreator.createServiceInfo(service)); }
@Test @DisplayName("Given a kubernetes service without metadata we launch a NPE") public void testCreateServiceInfo_ServiceNoMetadata() throws Exception { Service service = new Service(); service.setSpec(getServiceSpec()); expectThrows(NullPointerException.class, () -> configServerServiceInfoCreator.createServiceInfo(service)); }
@Test @DisplayName("Given the service file, when we instantiate the content then we get a KubernetesServiceInfoCreator") public void testKubernetesServiceInfoCreatorFromServiceFile() throws Exception { InputStream resourceAsStream = this.getClass().getResourceAsStream( "/META-INF/services/org.springframework.cloud.kubernetes.connector.KubernetesServiceInfoCreator"); byte[] bytes = new byte[resourceAsStream.available()]; resourceAsStream.read(bytes); Class<?> aClass = Class.forName(new String(bytes)); Object o = aClass.newInstance(); assertTrue(o instanceof KubernetesServiceInfoCreator); }
@Test void mutatestSimpleCodeEndExecutesTest() throws IOException, VerificationException { prepare("/simple"); verifier.executeGoal("org.pitest:pitest-maven:mutationCoverage"); verifier.verifyTextInLog("Ran 1 tests"); verifier.verifyTextInLog("Generated 1 mutations Killed 1"); }
@Test void executes() { final ProductionCode p = new ProductionCode(); p.calculateOutput(); assertTrue(p.ran); }
@Test void doesNotExecute() { final ProductionCode p = new ProductionCode(); p.x = 4; p.calculateOutput(); assertFalse(p.ran); }
@Test void findsNoTestsOnNonTestClass() { assertThat(testUnitFinder.findTestUnits(JUnit5Configuration.class), is(empty())); assertTrue( testUnitFinder.findTestUnits(JUnit5Configuration.class).isEmpty()); }
@Test void findsTestsOnJUnit5TestClass() { assertThat(testUnitFinder.findTestUnits(DummyTestClass.class), is(not(empty()))); assertFalse( testUnitFinder.findTestUnits(DummyTestClass.class).isEmpty()); }
@Test void findsAllAtTestMethodsAsTestUnits() { assertThat(testUnitFinder.findTestUnits(DummyTestClass.class), hasSize(2)); assertEquals( 2, configuration.testUnitFinder().findTestUnits(DummyTestClass.class).size()); }
@Test void findsCorrectTestUnitNames() { final List<String> testsNames = configuration.testUnitFinder().findTestUnits(DummyTestClass.class).stream() .map(TestUnit::getDescription) .map(Description::getName) .collect(Collectors.toList()); assertThat(testsNames, containsInAnyOrder( "publicTestMethod", "packagePrivateTestMethod" )); }
@Test void findsCorrectTestClass() { final List<String> testsNames = configuration.testUnitFinder().findTestUnits(DummyTestClass.class).stream() .map(TestUnit::getDescription) .map(Description::getQualifiedName) .collect(Collectors.toList()); assertThat(testsNames, containsInAnyOrder( "net.cyphoria.pitest.junit5.JUnit5ConfigurationTest$DummyTestClass.publicTestMethod", "net.cyphoria.pitest.junit5.JUnit5ConfigurationTest$DummyTestClass.packagePrivateTestMethod" )); }
@Test @Disabled void findsSingleTestUnitWithBeforeAll() { assertThat(testUnitFinder.findTestUnits(TestClazzWithStaticSetup.class), hasSize(1)); assertEquals( 1, configuration.testUnitFinder().findTestUnits(TestClazzWithStaticSetup.class).size()); }
@Test void shouldCreateAConfigurationThatFindsJUnitTestsWhenJUnit5OnClassPath() { putJUnit5OnClasspath(); final TestUnitFinder finder = factory.createTestFrameworkConfiguration(groupConfig, source).testUnitFinder(); assertFalse(finder.findTestUnits(JUnit5TestPluginFactoryTest.class).isEmpty()); }
@Test @Disabled void shouldThrowPitErrorWhenNoJunit5OnClassPath() { final PitHelpError error = expectThrows( PitHelpError.class, () -> factory.createTestFrameworkConfiguration(groupConfig, source) ); assertEquals(new PitHelpError(Help.NO_TEST_LIBRARY).getMessage(), error.getMessage()); }
@Test void assumeThat_trueAndFalse() { assumingThat(true, () -> executedTestMethodCount++); assumingThat(false, () -> { String message = "If you can see this, 'assumeFalse(true)' passed, which it obviously shouldn't."; throw new AssertionError(message); }); }
@Test public void boringAssertions() { String mango = "Mango"; // as usual: expected, actual assertEquals("Mango", mango); assertNotEquals("Banana", mango); assertSame(mango, mango); assertNotSame(new String(mango), mango); assertNull(null); assertNotNull(mango); assertFalse(false); assertTrue(true); }
@Test public void interestingAssertions() { String mango = "Mango"; // message comes last assertEquals("Mango", mango, "Y U no equal?!"); // message can be created lazily assertEquals("Mango", mango, () -> "Expensive string, creation deferred until needed."); // for 'assert[True|False]' it is possible to directly test a supplier that exists somewhere in the code BooleanSupplier existingBooleanSupplier = () -> true; assertTrue(existingBooleanSupplier); }
@Test public void exceptionAssertions() { IOException exception = expectThrows( IOException.class, () -> { throw new IOException("Something bad happened"); }); assertTrue(exception.getMessage().contains("Something bad")); }
@Test public void groupedAssertions() { try { assertAll("Multiplication", () -> assertEquals(15, 3 * 5, "3 x 5 = 15"), // this fails on purpose to see what the message looks like () -> assertEquals(15, 5 + 3, "5 x 3 = 15") ); } catch (AssertionError e) { if (CATCH_GROUP_ASSERTION_FAILURE_MESSAGE) print("Assertion failed - message starts in next line:\n" + e.getMessage()); else throw e; } }
@Test void fromDependees_artifactHasUnknownDependenciesAndDependeesWithoutInternalDependencies() { MarkTransitiveInternalDependencies marker = fromDependees( MarkInternalDependencies.UNKNOWN, on(UNKNOWN, NONE)); assertThat(marker).isSameAs(UNKNOWN); }
@Test void fromDependees_artifactHasUnknownDependenciesAndDependeesWithIndirectDependencies() { MarkTransitiveInternalDependencies marker = fromDependees( MarkInternalDependencies.UNKNOWN, on(UNKNOWN, INDIRECT, NONE)); assertThat(marker).isSameAs(INDIRECT); }
@Test void fromDependees_artifactHasUnknownDependenciesAndDependeesWithDirectDependencies() { MarkTransitiveInternalDependencies marker = fromDependees( MarkInternalDependencies.UNKNOWN, on(UNKNOWN, INDIRECT, DIRECT, NONE)); assertThat(marker).isSameAs(INDIRECT); }
@Test void fromDependees_artifactHasNoDependenciesAndDependeesWithUnknownDependencies() { MarkTransitiveInternalDependencies marker = fromDependees( MarkInternalDependencies.NONE, on(NONE, UNKNOWN)); assertThat(marker).isSameAs(UNKNOWN); }
@Test void fromDependees_artifactHasNoDependenciesAndDependeesWithNoDependencies() { MarkTransitiveInternalDependencies marker = fromDependees( MarkInternalDependencies.NONE, on(NONE)); assertThat(marker).isSameAs(NONE); }
@Test void fromDependees_artifactHasNoDependenciesAndDependeesWithIndirectDependencies() { MarkTransitiveInternalDependencies marker = fromDependees( MarkInternalDependencies.NONE, on(UNKNOWN, INDIRECT, NONE)); assertThat(marker).isSameAs(INDIRECT); }
@Test void fromDependees_artifactHasNoDependenciesAndDependeesWithDirectDependencies() { MarkTransitiveInternalDependencies marker = fromDependees( MarkInternalDependencies.NONE, on(UNKNOWN, INDIRECT, DIRECT, NONE)); assertThat(marker).isSameAs(INDIRECT); }
@Test @DisplayName("with null channel, throws NPE") void decoratedChannelNull_throwsException() { assertThatThrownBy( () -> new ReplayingTaskChannelDecorator<>(null, emptySet(), emptySet(), emptySet())) .isInstanceOf(NullPointerException.class) .hasMessageContaining("decoratedChannel"); }
@Test @DisplayName("with null task replay collection, throws NPE") void tasksToReplayNull_throwsException() { assertThatThrownBy( () -> new ReplayingTaskChannelDecorator<>(decoratedChannel, null, emptySet(), emptySet())) .isInstanceOf(NullPointerException.class) .hasMessageContaining("tasksToReplay"); }