508 lines
19 KiB
Java
508 lines
19 KiB
Java
|
|
/**
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2021 Exercism
|
|
* Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
import java.lang.reflect.Method;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
import java.lang.annotation.Retention;
|
|
import java.lang.annotation.RetentionPolicy;
|
|
import java.lang.annotation.Target;
|
|
import java.lang.annotation.ElementType;
|
|
|
|
public class SplitSecondStopwatchMain {
|
|
|
|
@Test(name = "new stopwatch starts in ready state")
|
|
public void newStopwatchStartsInReadyState() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
assertEquals("ready", stopwatch.state());
|
|
});
|
|
}
|
|
|
|
@Test(name = "new stopwatch's current lap has no elapsed time")
|
|
@Disabled
|
|
public void newStopwatchCurrentLapHasNoElapsedTime() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
assertEquals("00:00:00", stopwatch.currentLap());
|
|
});
|
|
}
|
|
|
|
@Test(name = "new stopwatch's total has no elapsed time")
|
|
@Disabled
|
|
public void newStopwatchTotalHasNoElapsedTime() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
assertEquals("00:00:00", stopwatch.total());
|
|
});
|
|
}
|
|
|
|
@Test(name = "new stopwatch does not have previous laps")
|
|
@Disabled
|
|
public void newStopwatchDoesNotHavePreviousLaps() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
assertTrue(stopwatch.previousLaps().isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "start from ready state changes state to running")
|
|
@Disabled
|
|
public void startFromReadyStateChangesStateToRunning() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
assertEquals("running", stopwatch.state());
|
|
});
|
|
}
|
|
|
|
@Test(name = "start does not change previous laps")
|
|
@Disabled
|
|
public void startDoesNotChangePreviousLaps() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
assertTrue(stopwatch.previousLaps().isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "start initiates time tracking for current lap")
|
|
@Disabled
|
|
public void startInitiatesTimeTrackingForCurrentLap() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:00:05");
|
|
assertEquals("00:00:05", stopwatch.currentLap());
|
|
});
|
|
}
|
|
|
|
@Test(name = "start initiates time tracking for total")
|
|
@Disabled
|
|
public void startInitiatesTimeTrackingForTotal() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:00:23");
|
|
assertEquals("00:00:23", stopwatch.total());
|
|
});
|
|
}
|
|
|
|
@Test(name = "start cannot be called from running state")
|
|
@Disabled
|
|
public void startCannotBeCalledFromRunningState() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
try {
|
|
stopwatch.start();
|
|
throw new AssertionError("Expected IllegalStateException");
|
|
} catch (IllegalStateException e) {
|
|
if (!"cannot start an already running stopwatch".equals(e.getMessage())) {
|
|
throw new AssertionError("Wrong message: " + e.getMessage());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@Test(name = "stop from running state changes state to stopped")
|
|
@Disabled
|
|
public void stopFromRunningStateChangesStateToStopped() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.stop();
|
|
assertEquals("stopped", stopwatch.state());
|
|
});
|
|
}
|
|
|
|
@Test(name = "stop pauses time tracking for current lap")
|
|
@Disabled
|
|
public void stopPausesTimeTrackingForCurrentLap() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:00:05");
|
|
stopwatch.stop();
|
|
stopwatch.advanceTime("00:00:08");
|
|
assertEquals("00:00:05", stopwatch.currentLap());
|
|
});
|
|
}
|
|
|
|
@Test(name = "stop pauses time tracking for total")
|
|
@Disabled
|
|
public void stopPausesTimeTrackingForTotal() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:00:13");
|
|
stopwatch.stop();
|
|
stopwatch.advanceTime("00:00:44");
|
|
assertEquals("00:00:13", stopwatch.total());
|
|
});
|
|
}
|
|
|
|
@Test(name = "stop cannot be called from ready state")
|
|
@Disabled
|
|
public void stopCannotBeCalledFromReadyState() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
try {
|
|
stopwatch.stop();
|
|
throw new AssertionError("Expected IllegalStateException");
|
|
} catch (IllegalStateException e) {
|
|
if (!"cannot stop a stopwatch that is not running".equals(e.getMessage())) {
|
|
throw new AssertionError("Wrong message: " + e.getMessage());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@Test(name = "stop cannot be called from stopped state")
|
|
@Disabled
|
|
public void stopCannotBeCalledFromStoppedState() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.stop();
|
|
try {
|
|
stopwatch.stop();
|
|
throw new AssertionError("Expected IllegalStateException");
|
|
} catch (IllegalStateException e) {
|
|
if (!"cannot stop a stopwatch that is not running".equals(e.getMessage())) {
|
|
throw new AssertionError("Wrong message: " + e.getMessage());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@Test(name = "start from stopped state changes state to running")
|
|
@Disabled
|
|
public void startFromStoppedStateChangesStateToRunning() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.stop();
|
|
stopwatch.start();
|
|
assertEquals("running", stopwatch.state());
|
|
});
|
|
}
|
|
|
|
@Test(name = "start from stopped state resumes time tracking for current lap")
|
|
@Disabled
|
|
public void startFromStoppedStateResumesTimeTrackingForCurrentLap() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:01:20");
|
|
stopwatch.stop();
|
|
stopwatch.advanceTime("00:00:20");
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:00:08");
|
|
assertEquals("00:01:28", stopwatch.currentLap());
|
|
});
|
|
}
|
|
|
|
@Test(name = "start from stopped state resumes time tracking for total")
|
|
@Disabled
|
|
public void startFromStoppedStateResumesTimeTrackingForTotal() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:00:23");
|
|
stopwatch.stop();
|
|
stopwatch.advanceTime("00:00:44");
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:00:09");
|
|
assertEquals("00:00:32", stopwatch.total());
|
|
});
|
|
}
|
|
|
|
@Test(name = "lap adds current lap to previous laps")
|
|
@Disabled
|
|
public void lapAddsCurrentLapToPreviousLaps() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:01:38");
|
|
stopwatch.lap();
|
|
assertTrue(stopwatch.previousLaps().containsAll(List.of("00:01:38")));
|
|
stopwatch.advanceTime("00:00:44");
|
|
stopwatch.lap();
|
|
assertTrue(stopwatch.previousLaps().containsAll(List.of("00:01:38", "00:00:44")));
|
|
});
|
|
}
|
|
|
|
@Test(name = "lap resets current lap and resumes time tracking")
|
|
@Disabled
|
|
public void lapResetsCurrentLapAndResumesTimeTracking() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:08:22");
|
|
stopwatch.lap();
|
|
assertEquals("00:00:00", stopwatch.currentLap());
|
|
stopwatch.advanceTime("00:00:15");
|
|
assertEquals("00:00:15", stopwatch.currentLap());
|
|
});
|
|
}
|
|
|
|
@Test(name = "lap continues time tracking for total")
|
|
@Disabled
|
|
public void lapContinuesTimeTrackingForTotal() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:00:22");
|
|
stopwatch.lap();
|
|
stopwatch.advanceTime("00:00:33");
|
|
assertEquals("00:00:55", stopwatch.total());
|
|
});
|
|
}
|
|
|
|
@Test(name = "lap cannot be called from ready state")
|
|
@Disabled
|
|
public void lapCannotBeCalledFromReadyState() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
try {
|
|
stopwatch.lap();
|
|
throw new AssertionError("Expected IllegalStateException");
|
|
} catch (IllegalStateException e) {
|
|
if (!"cannot lap a stopwatch that is not running".equals(e.getMessage())) {
|
|
throw new AssertionError("Wrong message: " + e.getMessage());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@Test(name = "lap cannot be called from stopped state")
|
|
@Disabled
|
|
public void lapCannotBeCalledFromStoppedState() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.stop();
|
|
try {
|
|
stopwatch.lap();
|
|
throw new AssertionError("Expected IllegalStateException");
|
|
} catch (IllegalStateException e) {
|
|
if (!"cannot lap a stopwatch that is not running".equals(e.getMessage())) {
|
|
throw new AssertionError("Wrong message: " + e.getMessage());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@Test(name = "stop does not change previous laps")
|
|
@Disabled
|
|
public void stopDoesNotChangePreviousLaps() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:11:22");
|
|
stopwatch.lap();
|
|
assertTrue(stopwatch.previousLaps().containsAll(List.of("00:11:22")));
|
|
stopwatch.stop();
|
|
assertTrue(stopwatch.previousLaps().containsAll(List.of("00:11:22")));
|
|
});
|
|
}
|
|
|
|
@Test(name = "reset from stopped state changes state to ready")
|
|
@Disabled
|
|
public void resetFromStoppedStateChangesStateToReady() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.stop();
|
|
stopwatch.reset();
|
|
assertEquals("ready", stopwatch.state());
|
|
});
|
|
}
|
|
|
|
@Test(name = "reset resets current lap")
|
|
@Disabled
|
|
public void resetResetsCurrentLap() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:00:10");
|
|
stopwatch.stop();
|
|
stopwatch.reset();
|
|
assertEquals("00:00:00", stopwatch.currentLap());
|
|
});
|
|
}
|
|
|
|
@Test(name = "reset clears previous laps")
|
|
@Disabled
|
|
public void resetClearsPreviousLaps() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("00:00:10");
|
|
stopwatch.lap();
|
|
stopwatch.advanceTime("00:00:20");
|
|
stopwatch.lap();
|
|
assertTrue(stopwatch.previousLaps().containsAll(List.of("00:00:10", "00:00:20")));
|
|
stopwatch.stop();
|
|
stopwatch.reset();
|
|
assertTrue(stopwatch.previousLaps().isEmpty());
|
|
});
|
|
}
|
|
|
|
@Test(name = "reset cannot be called from ready state")
|
|
@Disabled
|
|
public void resetCannotBeCalledFromReadyState() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
try {
|
|
stopwatch.reset();
|
|
throw new AssertionError("Expected IllegalStateException");
|
|
} catch (IllegalStateException e) {
|
|
if (!"cannot reset a stopwatch that is not stopped".equals(e.getMessage())) {
|
|
throw new AssertionError("Wrong message: " + e.getMessage());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@Test(name = "reset cannot be called from running state")
|
|
@Disabled
|
|
public void resetCannotBeCalledFromRunningState() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
try {
|
|
stopwatch.reset();
|
|
throw new AssertionError("Expected IllegalStateException");
|
|
} catch (IllegalStateException e) {
|
|
if (!"cannot reset a stopwatch that is not stopped".equals(e.getMessage())) {
|
|
throw new AssertionError("Wrong message: " + e.getMessage());
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@Test(name = "supports very long laps")
|
|
@Disabled
|
|
public void supportsVeryLongLaps() {
|
|
runTest(() -> {
|
|
SplitSecondStopwatch stopwatch = new SplitSecondStopwatch();
|
|
stopwatch.start();
|
|
stopwatch.advanceTime("01:23:45");
|
|
assertEquals("01:23:45", stopwatch.currentLap());
|
|
stopwatch.lap();
|
|
assertTrue(stopwatch.previousLaps().containsAll(List.of("01:23:45")));
|
|
stopwatch.advanceTime("04:01:40");
|
|
assertEquals("04:01:40", stopwatch.currentLap());
|
|
assertEquals("05:25:25", stopwatch.total());
|
|
stopwatch.lap();
|
|
assertTrue(stopwatch.previousLaps().containsAll(List.of("01:23:45", "04:01:40")));
|
|
stopwatch.advanceTime("08:43:05");
|
|
assertEquals("08:43:05", stopwatch.currentLap());
|
|
assertEquals("14:08:30", stopwatch.total());
|
|
stopwatch.lap();
|
|
assertTrue(stopwatch.previousLaps().containsAll(List.of("01:23:45", "04:01:40", "08:43:05")));
|
|
});
|
|
}
|
|
|
|
private void assertEquals(String expected, String actual) {
|
|
if (!expected.equals(actual)) {
|
|
throw new AssertionError("Expected: " + expected + " but was: " + actual);
|
|
}
|
|
}
|
|
|
|
private void assertTrue(boolean condition) {
|
|
if (!condition) {
|
|
throw new AssertionError("Expected true but was false");
|
|
}
|
|
}
|
|
|
|
private void runTest(Runnable body) {
|
|
try {
|
|
body.run();
|
|
System.out.println("TEST PASSED");
|
|
} catch (Exception | AssertionError t) {
|
|
System.out.println("TEST FAILED: " + t.getMessage());
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
new SplitSecondStopwatchMain().runTests();
|
|
}
|
|
|
|
/**
|
|
* Annotation to mark test methods. The name parameter is used to identify the
|
|
* test in the output.
|
|
*/
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
@Target(ElementType.METHOD)
|
|
private @interface Test {
|
|
String name();
|
|
}
|
|
|
|
/**
|
|
* Annotation to mark test methods as disabled. Disabled tests will not be
|
|
* executed when running the tests.
|
|
*/
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
@Target(ElementType.METHOD)
|
|
private @interface Disabled {
|
|
}
|
|
|
|
private void runTests() {
|
|
Method[] methods = this.getClass().getDeclaredMethods();
|
|
List<Method> allTestMethods = Arrays.asList(methods).stream().filter(m -> m.isAnnotationPresent(Test.class))
|
|
.collect(Collectors.toList());
|
|
List<Method> enabledTestMethods = allTestMethods.stream().filter(m -> !m.isAnnotationPresent(Disabled.class))
|
|
.collect(Collectors.toList());
|
|
System.out
|
|
.println("Running " + enabledTestMethods.size() + " out of " + allTestMethods.size() + " tests... \n");
|
|
for (Method method : enabledTestMethods) {
|
|
try {
|
|
System.out.println("Running test: " + method.getAnnotation(Test.class).name());
|
|
method.invoke(this);
|
|
} catch (Exception e) {
|
|
System.out.println("Test " + method.getAnnotation(Test.class).name()
|
|
+ " failed with an unexpected exception: " + e.getCause());
|
|
}
|
|
}
|
|
System.out.println("Finished running tests.");
|
|
if (allTestMethods.size() != enabledTestMethods.size()) {
|
|
System.out.println("\nThe following tests are disabled and were not executed:");
|
|
for (Method method : allTestMethods) {
|
|
if (method.isAnnotationPresent(Disabled.class)) {
|
|
System.out.println("- " + method.getAnnotation(Test.class).name());
|
|
}
|
|
}
|
|
System.out.println(
|
|
"To enable these tests, remove the @Disabled annotation from the corresponding test methods.");
|
|
}
|
|
}
|
|
}
|