From e9c7eb008151c43cff3d50d9d2ccd0fa3f21f416 Mon Sep 17 00:00:00 2001 From: danielkrausz Date: Fri, 3 Apr 2020 02:22:19 +0200 Subject: [PATCH 1/8] Implement reference speed change by following joystick movements --- .../train/controller/TrainControllerImpl.java | 105 +++++++++--------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java b/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java index a941434..f5df632 100644 --- a/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java +++ b/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java @@ -4,60 +4,61 @@ public class TrainControllerImpl implements TrainController { - private int step = 0; - private int referenceSpeed = 0; - private int speedLimit = 0; - private Thread thread; - - public TrainControllerImpl() { thread = new Thread() { - public void run() { - thread.run(); - try { - followSpeed(); - thread.sleep(200); - } catch(Exception e) { - System.out.println(e.getMessage()); - } - } - }; - } - @Override - public void followSpeed() { - if (referenceSpeed < 0) { - referenceSpeed = 0; - System.out.println("Nullára állt a sebesség"); - } else { - if(referenceSpeed+step > 0) { + private int step = 0; + private int referenceSpeed = 0; + private int speedLimit = 0; + private Thread thread; + + public TrainControllerImpl() { + thread = new Thread() { + public void run() { + thread.start(); + try { + followSpeed(); + thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + + @Override + public void followSpeed() { + if (referenceSpeed < 0) { + referenceSpeed = 0; + } else { + if (referenceSpeed + step > 0) { referenceSpeed += step; } else { - referenceSpeed = 0; + referenceSpeed = 0; } - } - - enforceSpeedLimit(); - } - - @Override - public int getReferenceSpeed() { - return referenceSpeed; - } - - @Override - public void setSpeedLimit(int speedLimit) { - this.speedLimit = speedLimit; - enforceSpeedLimit(); - - } - - private void enforceSpeedLimit() { - if (referenceSpeed > speedLimit) { - referenceSpeed = speedLimit; - } - } - - @Override - public void setJoystickPosition(int joystickPosition) { - this.step = joystickPosition; - } + } + + enforceSpeedLimit(); + } + + @Override + public int getReferenceSpeed() { + return referenceSpeed; + } + + @Override + public void setSpeedLimit(int speedLimit) { + this.speedLimit = speedLimit; + enforceSpeedLimit(); + + } + + private void enforceSpeedLimit() { + if (referenceSpeed > speedLimit) { + referenceSpeed = speedLimit; + } + } + + @Override + public void setJoystickPosition(int joystickPosition) { + this.step = joystickPosition; + } } From 7c654f1d56d451b82c521e3036b8058b2ef690a0 Mon Sep 17 00:00:00 2001 From: danielkrausz Date: Fri, 3 Apr 2020 02:33:06 +0200 Subject: [PATCH 2/8] Fix missing semicolon --- .../java/hu/bme/mit/train/controller/TrainControllerImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java b/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java index f5df632..67e69ee 100644 --- a/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java +++ b/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java @@ -20,7 +20,7 @@ public void run() { e.printStackTrace(); } } - } + }; } @Override From 87100137001b8cd688aef3f8c7ed6d7aa5995e66 Mon Sep 17 00:00:00 2001 From: danielkrausz Date: Fri, 3 Apr 2020 03:42:32 +0200 Subject: [PATCH 3/8] fix errors suggested by sonarqube --- .../train/controller/TrainControllerImpl.java | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java b/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java index 67e69ee..920c6e8 100644 --- a/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java +++ b/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java @@ -1,6 +1,7 @@ package hu.bme.mit.train.controller; import hu.bme.mit.train.interfaces.TrainController; +import java.util.logging.Logger; public class TrainControllerImpl implements TrainController { @@ -8,19 +9,19 @@ public class TrainControllerImpl implements TrainController { private int referenceSpeed = 0; private int speedLimit = 0; private Thread thread; + private System.Logger logger = (System.Logger) Logger.getLogger(TrainControllerImpl.class.getName()); public TrainControllerImpl() { - thread = new Thread() { - public void run() { - thread.start(); - try { - followSpeed(); - thread.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); - } + thread = new Thread(() -> { + thread.start(); + try { + followSpeed(); + Thread.sleep(100); + } catch (InterruptedException e) { + logger.log(System.Logger.Level.ERROR, "Thread Interrupted Exception!", e); + thread.interrupt(); } - }; + }); } @Override From 4da8252ef4613e5992cf2f83d0b08808bcda4e0a Mon Sep 17 00:00:00 2001 From: danielkrausz Date: Fri, 3 Apr 2020 04:13:09 +0200 Subject: [PATCH 4/8] update gradle config with SonarQube --- build.gradle | 5 ++++- gradlew | 23 +++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/build.gradle b/build.gradle index 9461f2b..691ae5e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,8 @@ +plugins { id "org.sonarqube" version "2.6.2" apply false } subprojects { - apply plugin: 'java' + subproject -> + apply plugin: 'java' + apply plugin: 'org.sonarqube' repositories { jcenter() diff --git a/gradlew b/gradlew index 9aa616c..cccdd3d 100755 --- a/gradlew +++ b/gradlew @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh ############################################################################## ## @@ -33,11 +33,11 @@ DEFAULT_JVM_OPTS="" # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" -warn ( ) { +warn () { echo "$*" } -die ( ) { +die () { echo echo "$*" echo @@ -154,16 +154,19 @@ if $cygwin ; then esac fi -# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules -function splitJvmOpts() { - JVM_OPTS=("$@") +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " } -eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS -JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then cd "$(dirname "$0")" fi -exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" +exec "$JAVACMD" "$@" From 5aed1762ede9c9c2d8b005a06e5633a509a789a6 Mon Sep 17 00:00:00 2001 From: danielkrausz Date: Fri, 3 Apr 2020 04:17:29 +0200 Subject: [PATCH 5/8] implement alarm functionality --- .../java/hu/bme/mit/train/interfaces/TrainUser.java | 5 +++++ .../hu/bme/mit/train/sensor/TrainSensorImpl.java | 9 ++++++--- .../java/hu/bme/mit/train/user/TrainUserImpl.java | 12 ++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/train-interfaces/src/main/java/hu/bme/mit/train/interfaces/TrainUser.java b/train-interfaces/src/main/java/hu/bme/mit/train/interfaces/TrainUser.java index 046f6fe..730a184 100644 --- a/train-interfaces/src/main/java/hu/bme/mit/train/interfaces/TrainUser.java +++ b/train-interfaces/src/main/java/hu/bme/mit/train/interfaces/TrainUser.java @@ -8,4 +8,9 @@ public interface TrainUser { void overrideJoystickPosition(int joystickPosition); + boolean getAlarmState(); + + void setAlarmState(boolean alarmState); + + } diff --git a/train-sensor/src/main/java/hu/bme/mit/train/sensor/TrainSensorImpl.java b/train-sensor/src/main/java/hu/bme/mit/train/sensor/TrainSensorImpl.java index 4a640d1..56a60f8 100644 --- a/train-sensor/src/main/java/hu/bme/mit/train/sensor/TrainSensorImpl.java +++ b/train-sensor/src/main/java/hu/bme/mit/train/sensor/TrainSensorImpl.java @@ -22,8 +22,11 @@ public int getSpeedLimit() { @Override public void overrideSpeedLimit(int speedLimit) { - this.speedLimit = speedLimit; - controller.setSpeedLimit(speedLimit); + if (speedLimit < 0 || speedLimit > 500 || speedLimit < controller.getReferenceSpeed() / 2) { + user.setAlarmState(true); + } else { + this.speedLimit = speedLimit; + controller.setSpeedLimit(speedLimit); + } } - } diff --git a/train-user/src/main/java/hu/bme/mit/train/user/TrainUserImpl.java b/train-user/src/main/java/hu/bme/mit/train/user/TrainUserImpl.java index 93d3b2e..8d9f6ef 100644 --- a/train-user/src/main/java/hu/bme/mit/train/user/TrainUserImpl.java +++ b/train-user/src/main/java/hu/bme/mit/train/user/TrainUserImpl.java @@ -7,6 +7,7 @@ public class TrainUserImpl implements TrainUser { private TrainController controller; private int joystickPosition; + private boolean alarmState = false; public TrainUserImpl(TrainController controller) { this.controller = controller; @@ -28,4 +29,15 @@ public void overrideJoystickPosition(int joystickPosition) { controller.setJoystickPosition(joystickPosition); } + @Override + public boolean getAlarmState() { + return this.alarmState; + } + + @Override + public void setAlarmState(boolean alarmState) { + this.alarmState = alarmState; + } + + } From 687c13135670be96cf677996eaf12ed4865f14a2 Mon Sep 17 00:00:00 2001 From: danielkrausz Date: Fri, 3 Apr 2020 05:16:47 +0200 Subject: [PATCH 6/8] fix logger --- .../hu/bme/mit/train/controller/TrainControllerImpl.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java b/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java index 920c6e8..ce29524 100644 --- a/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java +++ b/train-controller/src/main/java/hu/bme/mit/train/controller/TrainControllerImpl.java @@ -1,6 +1,8 @@ package hu.bme.mit.train.controller; import hu.bme.mit.train.interfaces.TrainController; + +import java.util.logging.Level; import java.util.logging.Logger; public class TrainControllerImpl implements TrainController { @@ -9,7 +11,7 @@ public class TrainControllerImpl implements TrainController { private int referenceSpeed = 0; private int speedLimit = 0; private Thread thread; - private System.Logger logger = (System.Logger) Logger.getLogger(TrainControllerImpl.class.getName()); + private Logger logger = Logger.getLogger(TrainControllerImpl.class.getName()); public TrainControllerImpl() { thread = new Thread(() -> { @@ -18,7 +20,7 @@ public TrainControllerImpl() { followSpeed(); Thread.sleep(100); } catch (InterruptedException e) { - logger.log(System.Logger.Level.ERROR, "Thread Interrupted Exception!", e); + logger.log(Level.SEVERE, "Thread Interrupted Exception!", e); thread.interrupt(); } }); From 75a69b7962434090c88b67fb57094a3d17132682 Mon Sep 17 00:00:00 2001 From: danielkrausz Date: Fri, 3 Apr 2020 05:47:26 +0200 Subject: [PATCH 7/8] implement Train Sensor JUnit tests with Mockito, add jacoco dependency to gradle config --- build.gradle | 1 + .../bme/mit/train/sensor/TrainSensorImpl.java | 1 + .../bme/mit/train/sensor/TrainSensorTest.java | 41 +++++++++++++++++-- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 691ae5e..657aa48 100644 --- a/build.gradle +++ b/build.gradle @@ -3,6 +3,7 @@ subprojects { subproject -> apply plugin: 'java' apply plugin: 'org.sonarqube' + apply plugin: 'jacoco' repositories { jcenter() diff --git a/train-sensor/src/main/java/hu/bme/mit/train/sensor/TrainSensorImpl.java b/train-sensor/src/main/java/hu/bme/mit/train/sensor/TrainSensorImpl.java index 56a60f8..27d98ac 100644 --- a/train-sensor/src/main/java/hu/bme/mit/train/sensor/TrainSensorImpl.java +++ b/train-sensor/src/main/java/hu/bme/mit/train/sensor/TrainSensorImpl.java @@ -27,6 +27,7 @@ public void overrideSpeedLimit(int speedLimit) { } else { this.speedLimit = speedLimit; controller.setSpeedLimit(speedLimit); + user.setAlarmState(false); } } } diff --git a/train-sensor/src/test/java/hu/bme/mit/train/sensor/TrainSensorTest.java b/train-sensor/src/test/java/hu/bme/mit/train/sensor/TrainSensorTest.java index ae5dee2..ced3c71 100644 --- a/train-sensor/src/test/java/hu/bme/mit/train/sensor/TrainSensorTest.java +++ b/train-sensor/src/test/java/hu/bme/mit/train/sensor/TrainSensorTest.java @@ -1,19 +1,54 @@ package hu.bme.mit.train.sensor; +import hu.bme.mit.train.interfaces.TrainController; +import hu.bme.mit.train.interfaces.TrainSensor; +import hu.bme.mit.train.interfaces.TrainUser; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.Mock; + import static org.mockito.Mockito.*; public class TrainSensorTest { + @Mock TrainController mockController; + @Mock TrainUser mockUser; + TrainSensor sensor; + // Init test cases with Mockito @Before public void before() { - // TODO Add initializations + mockController = mock(TrainController.class); + mockUser = mock(TrainUser.class); + sensor = new TrainSensorImpl(mockController, mockUser); + } + + // Setting the speedLimit value as valid should be successful + @Test + public void testSuccessfulOverrideSpeedLimit() { + sensor.overrideSpeedLimit(50); + Assert.assertEquals(50, sensor.getSpeedLimit()); + } + + // Setting a negative speedLimit value should call the setAlarmState(true) method + @Test + public void testNegSpeedLimit() { + sensor.overrideSpeedLimit(-10); + verify(mockUser).setAlarmState(true); + } + + // Setting a speedLimit higher than 500 should call the setAlarmState(true) method + @Test + public void testTooHighSpeedLimit() { + sensor.overrideSpeedLimit(1200); + verify(mockUser).setAlarmState(true); } + // Setting a too low speedLimit value compared to the reference speed should call the setAlarmState(true) method @Test - public void ThisIsAnExampleTestStub() { - // TODO Delete this and add test cases based on the issues + public void testTooLowSpeedLimit() { + when(mockController.getReferenceSpeed()).thenReturn(150); + sensor.overrideSpeedLimit(50); + verify(mockUser).setAlarmState(true); } } From 9ef180041b76869f642c51409b891b79227a832b Mon Sep 17 00:00:00 2001 From: danielkrausz Date: Fri, 3 Apr 2020 06:00:47 +0200 Subject: [PATCH 8/8] add mockito config to gradle --- train-sensor/build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/train-sensor/build.gradle b/train-sensor/build.gradle index ff9723e..532d39d 100644 --- a/train-sensor/build.gradle +++ b/train-sensor/build.gradle @@ -4,4 +4,5 @@ dependencies { compile group: 'junit', name: 'junit', version: '4.12' compile 'org.mockito:mockito-core:2.7.16' + compile group: 'org.mockito', name: 'mockito-core', version: '2.22.0' }