Skip to content

Commit

Permalink
Merge pull request #43 from Segelzwerg/FiX_ISSUE_#18
Browse files Browse the repository at this point in the history
ADD: test for negative input in Speed Unit Classes
fix #18
  • Loading branch information
Marcel authored Dec 8, 2019
2 parents af9af84 + a70cba9 commit b33981a
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 108 deletions.
11 changes: 10 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ dependencies {
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit', module: 'junit'
}
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testImplementation 'org.junit.jupiter:junit-jupiter-params'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}

test {
useJUnitPlatform()
}

apply plugin: 'java'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public class KilometerPerHour implements Speed {
private final float speed;

public KilometerPerHour(float speed) {

if (speed < 0) {
throw new IllegalArgumentException("Speed must not be negative.");
}
this.speed = speed;
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/segelzwerg/sporttooolbox/IUnits/Knot.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public class Knot implements Speed {
private final float speed;

public Knot(float speed) {

if (speed < 0) {
throw new IllegalArgumentException("Speed must not be negative.");
}
this.speed = speed;
}

Expand Down
82 changes: 44 additions & 38 deletions src/main/java/segelzwerg/sporttooolbox/IUnits/MeterPerSecond.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,48 @@
@Setter
@EqualsAndHashCode
public class MeterPerSecond implements Speed {
private final float speed;

public MeterPerSecond(float speed) {

this.speed = speed;
}

/**
* Convert to kilometer per hour
* @return speed in kilometer per hour
*/
public Speed toKilometerPerHour() {
return new KilometerPerHour(speed * Speed.METER_PER_SECOND_TO_KILOMETER_PER_HOUR);
}

/**
* Convert to meter per second
* @return speed in meter per second
*/
public Speed toMeterPerSecond() {
return this;
}

/**
* Convert to mile per hour
* @return speed in mile per hour
*/
public Speed toMilePerHour() {
return new MilePerHour(speed * Speed.METER_PER_SECOND_TO_KILOMETER_PER_HOUR / Speed.MILE_PER_HOUR_TO_KILOMETER_PER_HOUR);
}

/**
* Convert to knot
* @return speed in knot
*/
public Speed toKnot() {
return new Knot(speed * Speed.METER_PER_SECOND_TO_KILOMETER_PER_HOUR / Speed.KNOT_TO_KILOMETER_PER_HOUR);
}
private final float speed;

public MeterPerSecond(float speed) {
if (speed < 0) {
throw new IllegalArgumentException("Speed must not be negative.");
}
this.speed = speed;
}

/**
* Convert to kilometer per hour
*
* @return speed in kilometer per hour
*/
public Speed toKilometerPerHour() {
return new KilometerPerHour(speed * Speed.METER_PER_SECOND_TO_KILOMETER_PER_HOUR);
}

/**
* Convert to meter per second
*
* @return speed in meter per second
*/
public Speed toMeterPerSecond() {
return this;
}

/**
* Convert to mile per hour
*
* @return speed in mile per hour
*/
public Speed toMilePerHour() {
return new MilePerHour(speed * Speed.METER_PER_SECOND_TO_KILOMETER_PER_HOUR / Speed.MILE_PER_HOUR_TO_KILOMETER_PER_HOUR);
}

/**
* Convert to knot
*
* @return speed in knot
*/
public Speed toKnot() {
return new Knot(speed * Speed.METER_PER_SECOND_TO_KILOMETER_PER_HOUR / Speed.KNOT_TO_KILOMETER_PER_HOUR);
}
}
82 changes: 44 additions & 38 deletions src/main/java/segelzwerg/sporttooolbox/IUnits/MilePerHour.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,48 @@
@Setter
@EqualsAndHashCode
public class MilePerHour implements Speed {
private final float speed;

public MilePerHour(float speed) {

this.speed = speed;
}

/**
* Convert to kilometer per hour
* @return speed in kilometer per hour
*/
public Speed toKilometerPerHour() {
return new KilometerPerHour(speed * Speed.MILE_PER_HOUR_TO_KILOMETER_PER_HOUR);
}

/**
* Convert to meter per second
* @return speed in meter per second
*/
public Speed toMeterPerSecond() {
return new MeterPerSecond(speed * Speed.MILE_PER_HOUR_TO_KILOMETER_PER_HOUR / Speed.METER_PER_SECOND_TO_KILOMETER_PER_HOUR);
}

/**
* Convert to mile per hour
* @return speed in mile per hour
*/
public Speed toMilePerHour() {
return this;
}

/**
* Convert to knot
* @return speed in knot
*/
public Speed toKnot() {
return new Knot(speed * Speed.MILE_PER_HOUR_TO_KILOMETER_PER_HOUR / Speed.KNOT_TO_KILOMETER_PER_HOUR);
}
private final float speed;

public MilePerHour(float speed) {
if (speed < 0) {
throw new IllegalArgumentException("Speed must not be negative");
}
this.speed = speed;
}

/**
* Convert to kilometer per hour
*
* @return speed in kilometer per hour
*/
public Speed toKilometerPerHour() {
return new KilometerPerHour(speed * Speed.MILE_PER_HOUR_TO_KILOMETER_PER_HOUR);
}

/**
* Convert to meter per second
*
* @return speed in meter per second
*/
public Speed toMeterPerSecond() {
return new MeterPerSecond(speed * Speed.MILE_PER_HOUR_TO_KILOMETER_PER_HOUR / Speed.METER_PER_SECOND_TO_KILOMETER_PER_HOUR);
}

/**
* Convert to mile per hour
*
* @return speed in mile per hour
*/
public Speed toMilePerHour() {
return this;
}

/**
* Convert to knot
*
* @return speed in knot
*/
public Speed toKnot() {
return new Knot(speed * Speed.MILE_PER_HOUR_TO_KILOMETER_PER_HOUR / Speed.KNOT_TO_KILOMETER_PER_HOUR);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package segelzwerg.sporttooolbox.IUnits;

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

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Tests the Distance class
Expand Down Expand Up @@ -62,8 +63,8 @@ public void sixty_kilometer_two_hour_test_speed() {
* Expected: IllegalArgumentException
*/

@Test(expected = IllegalArgumentException.class)
@Test
public void negative_distance() {
new Distance(-1);
assertThrows(IllegalArgumentException.class, () -> new Distance(-1));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package segelzwerg.sporttooolbox.IUnits;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class KilometerPerHourTest {

Expand All @@ -16,7 +17,7 @@ public class KilometerPerHourTest {
* Set up before all tests
* Initialization of static Speed thirtyMetersPerSecond
*/
@BeforeClass
@BeforeAll
public static void setUp() {
thirtyKilometersPerHour = new KilometerPerHour(THIRTY_KM_PER_HOUR);
}
Expand Down Expand Up @@ -73,4 +74,15 @@ public void toKnot() {

assertThat(convertedSpeed.getSpeed(), equalTo(16.198704f));
}

/**
* negative Input
* Speed: -1 km/h
*
* @expected IllegalArgumentException
*/
@Test
public void negativeInput() {
assertThrows(IllegalArgumentException.class, () -> new KilometerPerHour(-1));
}
}
18 changes: 15 additions & 3 deletions src/test/java/segelzwerg/sporttooolbox/IUnits/KnotTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package segelzwerg.sporttooolbox.IUnits;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class KnotTest {
private static final float THIRTY_KNOTS = 30f;
Expand All @@ -15,7 +16,7 @@ public class KnotTest {
* Set up before all tests
* Initialization of static Speed thirtyKnots
*/
@BeforeClass
@BeforeAll
public static void setUp() {
thirtyKnots = new Knot(THIRTY_KNOTS);
}
Expand Down Expand Up @@ -72,4 +73,15 @@ public void toKnot() {

assertThat(convertedSpeed, is(thirtyKnots));
}

/**
* negative Input
* Speed: -1 Knots
*
* @expected IllegalArgumentException
*/
@Test
public void negativeInput() {
assertThrows(IllegalArgumentException.class, () -> new Knot(-1));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package segelzwerg.sporttooolbox.IUnits;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class MeterPerSecondTest {

Expand All @@ -16,7 +17,7 @@ public class MeterPerSecondTest {
* Set up before all tests
* Initialization of static Speed thirtyMetersPerSecond
*/
@BeforeClass
@BeforeAll
public static void setUp() {
thirtyMetersPerSecond = new MeterPerSecond(THIRTY_METERS_PER_SECOND);
}
Expand Down Expand Up @@ -73,4 +74,15 @@ public void toKnot() {

assertThat(convertedSpeed.getSpeed(), equalTo(58.315334F));
}

/**
* negative Input
* Speed: -1 m/s
*
* @expected IllegalArgumentException
*/
@Test
public void negativeInput() {
assertThrows(IllegalArgumentException.class, () -> new MeterPerSecond(-1));
}
}
Loading

0 comments on commit b33981a

Please sign in to comment.