From 25c077dd047953ccf74729674a75c95d8b736c85 Mon Sep 17 00:00:00 2001 From: omenem Date: Tue, 17 Mar 2020 14:09:20 +0100 Subject: [PATCH 1/6] Tests for ButtonDimmer --- .../smarthut/ButtonDimmerTests.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java new file mode 100644 index 0000000..85f4e49 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java @@ -0,0 +1,55 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import static org.junit.jupiter.api.Assertions.*; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +@DisplayName("A Button Dimmer") +public class ButtonDimmerTests { + + ButtonDimmer buttonDimmer; + + @BeforeEach + public void createNewButtonDimmer() { + this.buttonDimmer = new ButtonDimmer(); + } + + @Nested + @DisplayName(" when lights are present") + class lightsPresent { + + @BeforeEach + public void setLights() { + DimmableLight dl; + for (int i = 0; i < 3; i++) { + dl = new DimmableLight(); + dl.setIntensity(10); + ; + buttonDimmer.addDimmableLight(dl); + } + } + + @Test + @DisplayName(" increase the intensity ") + public void increase() { + buttonDimmer.increaseIntensity(); + for (DimmableLight dl : buttonDimmer.getOutputs()) { + assertTrue(dl.getIntensity() > 10); + } + } + + @Test + @DisplayName(" decrease the intensity ") + public void decrease() { + buttonDimmer.decreaseIntensity(); + for (DimmableLight dl : buttonDimmer.getOutputs()) { + assertTrue(dl.getIntensity() < 10); + } + } + } +} From 207e80c5f94bddfe4e01e18d40fb5fddab1fce4b Mon Sep 17 00:00:00 2001 From: omenem Date: Tue, 17 Mar 2020 14:34:17 +0100 Subject: [PATCH 2/6] Tests for DimmableLight --- .../smarthut/DimmableLightTests.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableLightTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableLightTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableLightTests.java new file mode 100644 index 0000000..f54e754 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/DimmableLightTests.java @@ -0,0 +1,106 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import static org.junit.jupiter.api.Assertions.*; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +@DisplayName("A Button Dimmer") +public class DimmableLightTests { + + DimmableLight dimmableLight; + + @BeforeEach + public void createNewButtonDimmer() { + this.dimmableLight = new DimmableLight(); + } + + @Nested + @DisplayName(" when on") + class WhenOn { + + @BeforeEach + public void setLightOn() { + dimmableLight.setOn(true); + } + + @Test + @DisplayName("the isOn method should return true") + public void isOn() { + assertTrue(dimmableLight.isOn()); + } + + @Test + @DisplayName("the intensity should be 100 when the light is turned on") + public void checkIntensityWhenTurnedOn() { + assertEquals(100, dimmableLight.getIntensity()); + } + + @Test + @DisplayName("setting the intensity to a number between 1 and 100") + public void checkIntensityBetweenLimits() { + dimmableLight.setIntensity(50); + assertEquals(50, dimmableLight.getIntensity()); + } + + @Test + @DisplayName("setting the intensity to a number > 100") + public void checkIntensityMoreThanLimits() { + dimmableLight.setIntensity(150); + assertEquals(100, dimmableLight.getIntensity()); + } + + @Test + @DisplayName("setting the intensity to a number < 0") + public void checkIntensityLessThanLimits() { + dimmableLight.setIntensity(-30); + assertEquals(0, dimmableLight.getIntensity()); + } + + @Test + @DisplayName("setting the intensity to a number <= 0 should turn the light off") + public void checkTurnOn() { + dimmableLight.setIntensity(0); + assertFalse(dimmableLight.isOn()); + } + } + + @Nested + @DisplayName(" when off") + class WhenOff { + + @BeforeEach + public void setLightOff() { + dimmableLight.setOn(false); + } + + @Test + @DisplayName("the isOn method should return false") + public void isOn() { + assertFalse(dimmableLight.isOn()); + } + + @Test + @DisplayName("the intensity should be 0 when the light is turned off") + public void checkIntensityWhenTurnedOff() { + assertEquals(0, dimmableLight.getIntensity()); + } + + @Test + @DisplayName("setting the intensity to a number between 1 and 100") + public void checkIntensityBetweenLimits() { + dimmableLight.setIntensity(50); + assertEquals(50, dimmableLight.getIntensity()); + } + + @Test + @DisplayName("setting the intensity to a number > 0 should turn the light on") + public void checkIntensityLessThanLimits() { + dimmableLight.setIntensity(47); + assertEquals(47, dimmableLight.getIntensity()); + } + } +} From fe911d492ff1cfde6b18f3992b2333530ccefbad Mon Sep 17 00:00:00 2001 From: omenem Date: Tue, 17 Mar 2020 15:10:49 +0100 Subject: [PATCH 3/6] small changes --- .../ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java | 7 ++++++- .../inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java index af00025..10f0c5a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java @@ -1,5 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.Inheritance; @@ -15,7 +16,7 @@ public abstract class Dimmer extends InputDevice { } @OneToMany(mappedBy = "dimmer") - private Set lights; + private Set lights = new HashSet<>(); /** * Get the lights connected to this dimmer @@ -26,4 +27,8 @@ public abstract class Dimmer extends InputDevice { public Set getOutputs() { return this.lights; } + + public void addDimmableLight(DimmableLight dimmableLight) { + lights.add(dimmableLight); + } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java index 85f4e49..22b3e0c 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java @@ -20,8 +20,8 @@ public class ButtonDimmerTests { } @Nested - @DisplayName(" when lights are present") - class lightsPresent { + @DisplayName(" when multiple lights are present") + class MultipleLights { @BeforeEach public void setLights() { From 313e6ed88e9d0a0f98ca851bf8b1ecc2a9d23d85 Mon Sep 17 00:00:00 2001 From: omenem Date: Tue, 17 Mar 2020 15:20:49 +0100 Subject: [PATCH 4/6] small changes --- .../java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java index 10f0c5a..e192015 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java @@ -28,6 +28,7 @@ public abstract class Dimmer extends InputDevice { return this.lights; } + /** Add a light to be controller by this dimmer */ public void addDimmableLight(DimmableLight dimmableLight) { lights.add(dimmableLight); } From 5e7bd298deb9611de62888683632588fb2ef5f9a Mon Sep 17 00:00:00 2001 From: Jacob Salvi Date: Tue, 17 Mar 2020 17:44:57 +0100 Subject: [PATCH 5/6] Added some tests for Regular Light and for Switch --- .../smarthut/RegularLightTests.java | 47 +++++++++ .../sa4/sanmarinoes/smarthut/SwitchTests.java | 96 +++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RegularLightTests.java create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RegularLightTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RegularLightTests.java new file mode 100644 index 0000000..c4f4694 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/RegularLightTests.java @@ -0,0 +1,47 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import static org.junit.jupiter.api.Assertions.*; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("A RegularLight") +public class RegularLightTests { + + RegularLight regularLight; + + @BeforeEach + public void createRegularLight() { + this.regularLight = new RegularLight(); + } + + @Test + @DisplayName("State when just created") + public void beginningState() { + assertFalse(regularLight.isOn()); + } + + @Test + @DisplayName("Changing state to on after creating the light") + public void createAndSetOn() { + regularLight.setOn(true); + assertTrue(regularLight.isOn()); + } + + @Test + @DisplayName("Change state of the light to off after creating it") + public void createAndSetOff() { + regularLight.setOn(false); + assertFalse(regularLight.isOn()); + } + + @Test + @DisplayName("Checks whether a turned on light getting turned on is still in the on State") + public void setOn() { + regularLight.setOn(true); + regularLight.setOn(true); + assertTrue(regularLight.isOn()); + } +} diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchTests.java new file mode 100644 index 0000000..9f937d3 --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SwitchTests.java @@ -0,0 +1,96 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import static org.junit.jupiter.api.Assertions.*; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName("A switch") +public class SwitchTests { + + Switch aSwitch; + + @BeforeEach + public void createNewSwitch() { + + this.aSwitch = new Switch(); + RegularLight regularLight = new RegularLight(); + DimmableLight dimmableLight = new DimmableLight(); + SmartPlug smartPlug = new SmartPlug(); + this.aSwitch.getOutputs().add(regularLight); + this.aSwitch.getOutputs().add(dimmableLight); + this.aSwitch.getOutputs().add(smartPlug); + } + + @Test + @DisplayName("check state when switch created") + public void createdSwitch() { + assertFalse(aSwitch.isOn()); + } + + @Test + @DisplayName("Check toggle on a switch in its off state") + public void offToggle() { + aSwitch.toggle(); + assertTrue(aSwitch.isOn()); + } + + @Test + @DisplayName("Checks whether setting on a off switch works as intended") + public void offSetOn() { + aSwitch.setOn(true); + assertTrue(aSwitch.isOn()); + } + + @Test + @DisplayName("Checks whether setting off a on switch works as intended") + public void onSetOff() { + aSwitch.toggle(); + aSwitch.setOn(false); + assertFalse(aSwitch.isOn()); + } + + @Test + @DisplayName("Checks that setting off an off switch results in a off state") + public void offSetOff() { + aSwitch.setOn(false); + assertFalse(aSwitch.isOn()); + } + + @Test + @DisplayName("Checks that setting on an on switch results in a on state") + public void onSetOn() { + aSwitch.toggle(); + aSwitch.setOn(true); + assertTrue(aSwitch.isOn()); + } + + @Test + @DisplayName("Checks wheter toggling a on switch set its state to off") + public void onToggle() { + aSwitch.setOn(true); + aSwitch.toggle(); + assertFalse(aSwitch.isOn()); + } + + @Test + @DisplayName("Checks that toggling on sets all elements of the Set on as well") + public void toggleEffctOnSet() { + aSwitch.toggle(); + for (final Switchable s : aSwitch.getOutputs()) { + assertTrue(s.isOn()); + } + } + + @Test + @DisplayName("Checks that toggling the switch off also sets all elements of its set off") + public void toggleOffEffectOnElementes() { + aSwitch.setOn(true); + aSwitch.toggle(); + for (final Switchable s : aSwitch.getOutputs()) { + assertFalse(s.isOn()); + } + } +} From ce0edd7287fd149a6b4cadc091468e8f8c39ade3 Mon Sep 17 00:00:00 2001 From: omenem Date: Tue, 17 Mar 2020 17:45:50 +0100 Subject: [PATCH 6/6] KnobDimmer Tests --- .../sanmarinoes/smarthut/KnobDimmerTests.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java new file mode 100644 index 0000000..155242d --- /dev/null +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java @@ -0,0 +1,46 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut; + +import static org.junit.jupiter.api.Assertions.*; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +@DisplayName("A Knob Dimmer") +public class KnobDimmerTests { + + KnobDimmer knobDimmer; + + @BeforeEach + public void createNewKnobDimmer() { + this.knobDimmer = new KnobDimmer(); + } + + @Nested + @DisplayName(" when multiple lights are present") + class MultipleLights { + + @BeforeEach + public void setLights() { + DimmableLight dl; + for (int i = 0; i < 3; i++) { + dl = new DimmableLight(); + dl.setIntensity(10); + ; + knobDimmer.addDimmableLight(dl); + } + } + + @Test + @DisplayName(" set the intensity ") + public void increase() { + knobDimmer.setLightIntensity(30); + for (DimmableLight dl : knobDimmer.getOutputs()) { + assertEquals(30, dl.getIntensity()); + } + } + } +}