Skip to content

Added my project #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: exception-handling
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This repro is part of the Serenity Mentoring Programme that I am going through. This programme is run by John Smart https://johnfergusonsmart.com/serenity-bdd-mentoring/. His programme aims to turn manual testers into world class automation engineers. Using Java, Serenity, BDD, Cucumber and Gerkin
26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-core</artifactId>
<version>2.2.9</version>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit</artifactId>
<version>2.2.9</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.16.1</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -70,5 +85,16 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>14</source>
<target>14</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
68 changes: 68 additions & 0 deletions src/main/java/com/serenitydojo/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.serenitydojo;

/**
* A feline creature.
*/
public class Cat extends Pet {

private String favoriteFood;
private int age;

// A very useful field
public static final String CAT_NOISE = "Meow";

public static String usualFood() {
return "Tuna";
}

public Cat(String name, int age) {
super(name);
this.age = age;
this.favoriteFood = usualFood();
}

public Cat(String name, String favoriteFood, int age) {
super(name);
this.favoriteFood = favoriteFood;
this.age = age;
}

public void setFavoriteFood(String favoriteFood) {
this.favoriteFood = favoriteFood;
}

public String getFavoriteFood() {
return favoriteFood;
}

public int getAge() {
return age;
}

@Override
public String makeNoise() {
return CAT_NOISE;
}

@Override
public String play() {
return "plays with string";
}

public void feed(String food) {
System.out.println(getName() + " eats some " + food);
}

public void groom() {
lickPaws();
cleanFur();
}

private void cleanFur() {
System.out.println(getName() + " cleans his fur");
}

private void lickPaws() {
System.out.println(getName() + " licks his paws");
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/serenitydojo/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.serenitydojo;

public class Dog extends Pet {
private String favoriteToy;
private int age;

public Dog(String name, String favoriteToy, int age) {
super(name);
this.favoriteToy = favoriteToy;
this.age = age;
}

@Override
public String play() {
return "plays with bone";
}

public String getFavoriteToy() {
return favoriteToy;
}

public int getAge() {
return age;
}

public String makeNoise() {
return "Woof";
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/serenitydojo/Hampster.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.serenitydojo;

public class Hampster extends Pet {
private String favoriteGame;
private int age;

public Hampster(String name, String favoriteGame, int age) {
super(name);
this.favoriteGame = favoriteGame;
this.age = age;
}

public String getFavoriteGame() {
return favoriteGame;
}

public int getAge() {
return age;
}

@Override
public String play() {
return "runs in wheel";
}

@Override
public String makeNoise() {
return "Squeak";
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/serenitydojo/HelloWorldWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.serenitydojo;

public class HelloWorldWriter {

public void writeHelloWorld(){
System.out.println("Hello World");
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/serenitydojo/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.serenitydojo;

public abstract class Pet {
private String name;
private int age;


public Pet(String name) {
this.name = name;
}
public String getName() {
return name;
}

public String goForWalks() { return "walk walk walk"; }
public abstract String makeNoise();
public abstract String play();
}
19 changes: 19 additions & 0 deletions src/main/java/com/serenitydojo/exceptions/FileLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.serenitydojo.exceptions;

import java.io.IOException;

public class FileLoader {
public String readHelloWorld() throws IOException {
return "";//Files.readString(Paths.get("src/main/resources/hello.txt"));
}

public Boolean fileContainsText(String filename, String expectedText) {
String path = "src/main/resources/" + filename;
return null;// (Files.readString(Paths.get(path)).contains(expectedText));
}

public Boolean fileHasText(String filename, String expectedText) {
String path = "src/main/resources/" + filename;
return null;// (Files.readString(Paths.get(path)).contains(expectedText));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.serenitydojo.exceptions;

public class MissingWelcomeFileException extends RuntimeException {
public MissingWelcomeFileException(String message, Throwable e) {
super(message, e);
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/serenitydojo/exceptions/StringProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.serenitydojo.exceptions;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

public class StringProcessor {

public String showLengthOf(String input) {
int length = (input == null) ? 0 : input.length();
return input + " has a length of " + length;
}


public int getPortOf(String urlAsAString) {
try {
URL url = new URL(urlAsAString);
return url.getDefaultPort();
} catch(MalformedURLException badlyWrittenException) {
throw new TestEnvironmentUnavailableException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.serenitydojo.exceptions;

public class TestEnvironmentUnavailableException extends RuntimeException {

}
5 changes: 5 additions & 0 deletions src/main/java/com/serenitydojo/model/AnimalType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.serenitydojo.model;

public enum AnimalType {
CAT, DOG, HAMSTER, FISH, LLAMA
}
17 changes: 17 additions & 0 deletions src/main/java/com/serenitydojo/model/Feeder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.serenitydojo.model;

public class Feeder {
public FoodType feeds(AnimalType animal, boolean isPremium) {

switch (animal) {
case CAT:
return (isPremium) ? FoodType.SALMON : FoodType.TUNA;
case DOG:
return (isPremium) ? FoodType.DELUXE_DOG_FOOD : FoodType.DOG_FOOD;
case HAMSTER:
return (isPremium) ? FoodType.LETTUCE : FoodType.CABBAGE;
default:
return FoodType.UNKNOWN;
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/serenitydojo/model/FoodType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.serenitydojo.model;

public enum FoodType {
TUNA, CABBAGE, LETTUCE, SALMON, DOG_FOOD, DELUXE_DOG_FOOD, UNKNOWN
}
1 change: 1 addition & 0 deletions src/main/resources/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
12 changes: 12 additions & 0 deletions src/test/java/com/serenitydojo/HelloWorldWriterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.serenitydojo;

import org.junit.Test;

public class HelloWorldWriterTest {
@Test
public void shouldWriteHelloWorldToTheConsole(){
HelloWorldWriter writer = new HelloWorldWriter();
writer.writeHelloWorld();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.serenitydojo.exceptions;

import org.junit.Test;

import java.io.IOException;

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

public class ExceptionHandlingExercises {

// - Handle a declared exception
// - Catch a declared exception and return a value
// - Catch a declared exception and throw a custom exception
// - Custom logic that throws a custom exception when no result is found
// - Using the Finally block to clean up

/**
* Exercise 1 - Handling exceptions
* Uncomment the code in this test and make it work.
* You will need to modify the FileLoader class so that it correctly handles an IOException
*/
@Test
public void workingWithDeclaredExceptions() throws IOException {
FileLoader fileLoader = new FileLoader();
assertThat(fileLoader.readHelloWorld()).isEqualTo("Hello World");
}

/**
* Exercise 2 - Catching exceptions
* Update the fileContainsText() method in the FileLoader class so that it returns false if the file
* does not contain the excepted text, or if the file does not exist.
*/
@Test
public void catchingExceptions() {
FileLoader fileLoader = new FileLoader();
assertThat(fileLoader.fileContainsText("hello.txt","Hello World")).isTrue();
}

@Test
public void catchingExceptionsWhenTheFileDoesNotExist() {
FileLoader fileLoader = new FileLoader();
assertThat(fileLoader.fileContainsText("does-not-exist.txt","Hello World")).isFalse();
}

/**
* Exercise 3 - Throwing custom exceptions
* Create a custom runtime exception called MissingWelcomeFileException,
* and update the fileHasText() method to throw this exception if no matching file is found.
*/
@Test(expected = MissingWelcomeFileException.class)
public void catchingCustomExceptionsWhenTheFileDoesNotExist() {
FileLoader fileLoader = new FileLoader();
assertThat(fileLoader.fileHasText("does-not-exist.txt","Hello World")).isFalse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.serenitydojo.exceptions;

import org.junit.Test;

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

public class WhenWorkingWithExceptions {

@Test
public void shouldShowTheLengthOfAString() {
StringProcessor stringProcessor = new StringProcessor();

String result = stringProcessor.showLengthOf("some string");

assertThat(result).isEqualTo("some string has a length of 11");
}

@Test
public void shouldShowZeroForNullStrings() {

StringProcessor stringProcessor = new StringProcessor();

String result = stringProcessor.showLengthOf(null);

assertThat(result).isEqualTo("null has a length of 0");
}

@Test(expected = TestEnvironmentUnavailableException.class)
public void shouldFindThePort() {
StringProcessor stringProcessor = new StringProcessor();

stringProcessor.getPortOf("A:https://www.google.com");
}
}
1 change: 0 additions & 1 deletion src/test/java/com/serenitydojo/readme.md

This file was deleted.