Skip to content

Commit

Permalink
DriverExam: formatting, add 1 case, +test
Browse files Browse the repository at this point in the history
Adjust javadoc for `Palindrome` task solution
  • Loading branch information
andrei-punko committed Jun 18, 2024
1 parent 6d7c5ad commit 0f86edf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
62 changes: 37 additions & 25 deletions src/main/java/by/andd3dfx/common/DriverExam.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,56 @@
*/
public class DriverExam {

public static void executeExercise(IExercise exercise) {
try {
exercise.start();
exercise.execute();
} catch (Exception e) {
exercise.markNegativePoints();
} finally {
exercise.end();
public static void executeExercise(IExercise exercise) {
try {
exercise.start();
exercise.execute();
} catch (Exception e) {
markNegativePoints(exercise);
} finally {
exercise.end();
}
}

private static void markNegativePoints(IExercise exercise) {
try {
exercise.markNegativePoints();
} catch (Exception e) {
// do nothing
}
}
}
}

interface IExercise {

void start() throws Exception;
void start() throws Exception;

void execute();
void execute();

void markNegativePoints();
void markNegativePoints();

void end();
void end();
}

class Exercise implements IExercise {

public void start() {
System.out.println("Start");
}
@Override
public void start() {
System.out.println("Start");
}

public void execute() {
System.out.println("Execute");
}
@Override
public void execute() {
System.out.println("Execute");
}

public void markNegativePoints() {
System.out.println("MarkNegativePoints");
}
@Override
public void markNegativePoints() {
System.out.println("MarkNegativePoints");
}

public void end() {
System.out.println("End");
}
@Override
public void end() {
System.out.println("End");
}
}
2 changes: 1 addition & 1 deletion src/main/java/by/andd3dfx/string/Palindrome.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static boolean isPalindrome(String word) {
/**
* A palindrome is a word that reads the same backward or forward. Write a function that checks if a given word is a
* palindrome. Character case should be ignored. For example, isPalindrome("Deleveled") should return true as
* character case should be ignored, resulting in "deleveled", which is a palindrome since it reads the same
* a character case should be ignored, resulting in "deleveled", which is a palindrome since it reads the same
* backward and forward.
*/
public static boolean isPalindromeIgnoreCase(String word) {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/by/andd3dfx/common/DriverExamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,28 @@ public void execute() {
assertThat(exercise.markNegativePointsCalled).isTrue();
assertThat(exercise.endCalled).isTrue();
}

@Test
public void executeExerciseWhenExecuteNMarkNegativePointsFailed() {
var exercise = new CustomExercise() {
@Override
public void execute() {
super.execute();
throw new RuntimeException();
}

@Override
public void markNegativePoints() {
super.markNegativePoints();
throw new RuntimeException();
}
};

DriverExam.executeExercise(exercise);

assertThat(exercise.startCalled).isTrue();
assertThat(exercise.executeCalled).isTrue();
assertThat(exercise.markNegativePointsCalled).isTrue();
assertThat(exercise.endCalled).isTrue();
}
}

0 comments on commit 0f86edf

Please sign in to comment.