Skip to content

Commit

Permalink
testing the testing support :-)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfa1 committed Dec 23, 2023
1 parent e59948c commit 09ce9b9
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static Record of(Key k1, Value v1, Key k2, Value v2) {

private final List<Record.Entry> entries;

private RecordMatcher(List<Record.Entry> entries) {
RecordMatcher(List<Record.Entry> entries) {
this.entries = entries;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* MIT License
*
* Copyright (c) 2018-2023 Davide Angelocola
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package hosh.spi.test.support;

import hosh.spi.ExitStatus;
import org.junit.jupiter.api.Test;

import static hosh.spi.test.support.ExitStatusAssert.assertThat;

@SuppressWarnings("MaskedAssertion")
class ExitStatusAssertTest {

@Test
void error() {
ExitStatus exitStatus = ExitStatus.of(1);
assertThat(exitStatus).isError();
assertThat(exitStatus).hasExitCode(1);
try {
assertThat(exitStatus).isSuccess();
} catch (AssertionError e) {
// all good
}
}

@Test
void success() {
ExitStatus exitStatus = ExitStatus.of(0);
assertThat(exitStatus).isSuccess();
assertThat(exitStatus).hasExitCode(0);
try {
assertThat(exitStatus).isError();
} catch (AssertionError e) {
// all good
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* MIT License
*
* Copyright (c) 2018-2023 Davide Angelocola
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package hosh.spi.test.support;

import hosh.spi.Keys;
import hosh.spi.Record;
import hosh.spi.Records;
import hosh.spi.Values;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class RecordMatcherTest {

@Test
void usage() {
RecordMatcher sut = new RecordMatcher(List.of(new Record.Entry(Keys.NAME, Values.ofText("mario"))));

Record empty = Records.empty();
assertThat(sut.matches(empty)).isFalse();

Record mario = empty.append(Keys.NAME, Values.ofText("mario"));
assertThat(sut.matches(mario)).isTrue();

Record marioAndCount = mario.append(Keys.COUNT, Values.ofNumeric(42));
assertThat(sut.matches(marioAndCount)).isTrue(); // still true, as the required key/value is still there

Record luigi = empty.append(Keys.NAME, Values.ofText("luigi"));
assertThat(sut.matches(luigi)).isFalse(); // not matching the required key/value (mario)
}
}
5 changes: 5 additions & 0 deletions test-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Logger;

/**
* Heavily inspired by JUnit 4.12 TemporaryFolder but with fewer features.
Expand All @@ -42,6 +43,8 @@
*/
public class TemporaryFolder implements Extension, BeforeEachCallback, AfterEachCallback {

private static final Logger LOGGER = Logger.getLogger(TemporaryFolder.class.getName());

private Path folder;

public Path toPath() {
Expand Down Expand Up @@ -98,7 +101,7 @@ private void recursiveDelete(File fileOrDirectory) {
}
boolean deleted = fileOrDirectory.delete();
if (!deleted) {
throw new IllegalStateException("file not deleted: " + fileOrDirectory);
LOGGER.info("not deleted: " + fileOrDirectory);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* MIT License
*
* Copyright (c) 2018-2023 Davide Angelocola
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package hosh.test.support;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;

class TemporaryFolderTest {

@RegisterExtension
final TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
void usage() throws IOException {
// must always provide a good base directory
assertThat(temporaryFolder.toPath())
.isNotNull()
.exists()
.isDirectory()
;

// create a new directory
assertThat(temporaryFolder.newFolder("folder"))
.isNotNull()
.exists()
.isDirectory()
;

// create a new file
assertThat(temporaryFolder.newFile("file.txt"))
.isNotNull()
.exists()
.isEmptyFile()
;

// create a new file under a new directory
assertThat(temporaryFolder.newFile(temporaryFolder.newFolder("sub-folder"), "file.txt"))
.isNotNull()
.exists()
.isEmptyFile()
;

// cleanup
temporaryFolder.afterEach(null);
assertThat(temporaryFolder.toPath()).doesNotExist();
}

}
55 changes: 55 additions & 0 deletions test-support/src/test/java/hosh/test/support/WithExecutorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* MIT License
*
* Copyright (c) 2018-2023 Davide Angelocola
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package hosh.test.support;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.concurrent.ExecutorService;

import static org.mockito.BDDMockito.then;

@ExtendWith(MockitoExtension.class)
class WithExecutorTest {

@Mock
ExecutorService executorService;

@Test
void usage() {
WithExecutor sut = new WithExecutor(executorService);

// submit task
Runnable runnable = () -> System.out.println("test");
sut.submit(runnable);
then(executorService).should().submit(runnable);

// close after usage
sut.afterAll(null);
then(executorService).should().shutdown();
}

}
48 changes: 48 additions & 0 deletions test-support/src/test/java/hosh/test/support/WithThreadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* MIT License
*
* Copyright (c) 2018-2023 Davide Angelocola
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package hosh.test.support;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.assertj.core.api.Assertions.assertThat;

class WithThreadTest {

@RegisterExtension
final WithThread withThread = new WithThread();

@Test
void threadName() {
withThread.renameTo("super mario");
assertThat(withThread.currentName()).isEqualTo("super mario");
}

@Test
void interrupt() {
assertThat(withThread.isInterrupted()).isEqualTo(false);
withThread.interrupt();
assertThat(withThread.isInterrupted()).isEqualTo(true);
}
}
48 changes: 48 additions & 0 deletions test-support/src/test/java/hosh/test/support/WithTimeZoneTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* MIT License
*
* Copyright (c) 2018-2023 Davide Angelocola
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package hosh.test.support;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.util.TimeZone;

import static org.assertj.core.api.Assertions.assertThat;

class WithTimeZoneTest {

@RegisterExtension
final WithTimeZone withTimeZone = new WithTimeZone();

@Test
void lifeCycle() {
TimeZone zurich = TimeZone.getTimeZone("Europe/Zurich");
withTimeZone.changeTo(zurich);
assertThat(TimeZone.getDefault()).isEqualTo(zurich);

TimeZone utc = TimeZone.getTimeZone("UTC");
withTimeZone.changeTo(utc);
assertThat(TimeZone.getDefault()).isEqualTo(utc);
}
}

0 comments on commit 09ce9b9

Please sign in to comment.