BAEL-1326 2nd attempt (#3281)
* BAEL-973: updated README * BAEL-1069: Updated README * BAEL-817: add README file * BAEL-1084: README update * BAEL-960: Update README * BAEL-1155: updated README * BAEL-1041: updated README * BAEL-973: Updated README * BAEL-1187: updated README * BAEL-1183: Update README * BAEL-1133: Updated README * BAEL-1098: README update * BAEL-719: add README.md * BAEL-1272: README update * BAEL-1272: README update * BAEL-1196: Update README * BAEL-1326: Default and static methods in interfaces
This commit is contained in:
parent
52edd46c0a
commit
314e2148b2
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.defaultstaticinterfacemethods.application;
|
||||
|
||||
import com.baeldung.defaultstaticinterfacemethods.model.Car;
|
||||
import com.baeldung.defaultstaticinterfacemethods.model.Vehicle;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Vehicle car = new Car("BMW");
|
||||
System.out.println(car.getBrand());
|
||||
System.out.println(car.speedUp());
|
||||
System.out.println(car.slowDown());
|
||||
System.out.println(car.turnAlarmOn());
|
||||
System.out.println(car.turnAlarmOff());
|
||||
System.out.println(Vehicle.getHorsePower(2500, 480));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.defaultstaticinterfacemethods.model;
|
||||
|
||||
public interface Alarm {
|
||||
|
||||
default String turnAlarmOn() {
|
||||
return "Turning the alarm on.";
|
||||
}
|
||||
|
||||
default String turnAlarmOff() {
|
||||
return "Turning the alarm off.";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.defaultstaticinterfacemethods.model;
|
||||
|
||||
public class Car implements Vehicle {
|
||||
|
||||
private final String brand;
|
||||
|
||||
public Car(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String speedUp() {
|
||||
return "The car is speeding up.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String slowDown() {
|
||||
return "The car is slowing down.";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.defaultstaticinterfacemethods.model;
|
||||
|
||||
public class Motorbike implements Vehicle {
|
||||
|
||||
private final String brand;
|
||||
|
||||
public Motorbike(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String speedUp() {
|
||||
return "The motorbike is speeding up.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String slowDown() {
|
||||
return "The motorbike is slowing down.";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.defaultstaticinterfacemethods.model;
|
||||
|
||||
public class MultiAlarmCar implements Vehicle, Alarm {
|
||||
|
||||
private final String brand;
|
||||
|
||||
public MultiAlarmCar(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String speedUp() {
|
||||
return "The motorbike is speeding up.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String slowDown() {
|
||||
return "The mootorbike is slowing down.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String turnAlarmOn() {
|
||||
return Vehicle.super.turnAlarmOn() + " " + Alarm.super.turnAlarmOn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String turnAlarmOff() {
|
||||
return Vehicle.super.turnAlarmOff() + " " + Alarm.super.turnAlarmOff();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.defaultstaticinterfacemethods.model;
|
||||
|
||||
public interface Vehicle {
|
||||
|
||||
String getBrand();
|
||||
|
||||
String speedUp();
|
||||
|
||||
String slowDown();
|
||||
|
||||
default String turnAlarmOn() {
|
||||
return "Turning the vehice alarm on.";
|
||||
}
|
||||
|
||||
default String turnAlarmOff() {
|
||||
return "Turning the vehicle alarm off.";
|
||||
}
|
||||
|
||||
static int getHorsePower(int rpm, int torque) {
|
||||
return (rpm * torque) / 5252;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
package com.baeldung.defaultistaticinterfacemethods.test;
|
||||
|
||||
import com.baeldung.defaultstaticinterfacemethods.model.Car;
|
||||
import com.baeldung.defaultstaticinterfacemethods.model.Motorbike;
|
||||
import com.baeldung.defaultstaticinterfacemethods.model.Vehicle;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class StaticDefaulInterfaceMethodUnitTest {
|
||||
|
||||
private static Car car;
|
||||
private static Motorbike motorbike;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpCarInstance() {
|
||||
car = new Car("BMW");
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpMotorbikeInstance() {
|
||||
motorbike = new Motorbike("Yamaha");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCarInstace_whenBrandisBMW_thenOneAssertion() {
|
||||
assertThat(car.getBrand()).isEqualTo("BMW");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCarInstance_whenCallingSpeedUp_thenOneAssertion() {
|
||||
assertThat(car.speedUp()).isEqualTo("The car is speeding up.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCarInstance_whenCallingSlowDown_thenOneAssertion() {
|
||||
assertThat(car.slowDown()).isEqualTo("The car is slowing down.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCarInstance_whenCallingTurnAlarmOn_thenOneAssertion() {
|
||||
assertThat(car.turnAlarmOn()).isEqualTo("Turning the vehice alarm on.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCarInstance_whenCallingTurnAlarmOff_thenOneAssertion() {
|
||||
assertThat(car.turnAlarmOff()).isEqualTo("Turning the vehicle alarm off.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleInterface_whenCallinggetHorsePower_thenOneAssertion() {
|
||||
assertThat(Vehicle.getHorsePower(2500, 480)).isEqualTo(228);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMooorbikeInstace_whenBrandisYamaha_thenOneAssertion() {
|
||||
assertThat(motorbike.getBrand()).isEqualTo("Yamaha");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMotorbikeInstance_whenCallingSpeedUp_thenOneAssertion() {
|
||||
assertThat(motorbike.speedUp()).isEqualTo("The motorbike is speeding up.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMotorbikeInstance_whenCallingSlowDown_thenOneAssertion() {
|
||||
assertThat(motorbike.slowDown()).isEqualTo("The motorbike is slowing down.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMotorbikeInstance_whenCallingTurnAlarmOn_thenOneAssertion() {
|
||||
assertThat(motorbike.turnAlarmOn()).isEqualTo("Turning the vehice alarm on.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMotorbikeInstance_whenCallingTurnAlarmOff_thenOneAssertion() {
|
||||
assertThat(motorbike.turnAlarmOff()).isEqualTo("Turning the vehicle alarm off.");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue