Skip to content
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

Implementing alarm functionality with tests when overriding speed limit #6

Open
wants to merge 8 commits into
base: master
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
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
plugins { id "org.sonarqube" version "2.6.2" apply false }
subprojects {
apply plugin: 'java'
subproject ->
apply plugin: 'java'
apply plugin: 'org.sonarqube'
apply plugin: 'jacoco'

repositories {
jcenter()
Expand Down
23 changes: 13 additions & 10 deletions gradlew
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env sh

##############################################################################
##
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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" "$@"
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,66 @@

import hu.bme.mit.train.interfaces.TrainController;

import java.util.logging.Level;
import java.util.logging.Logger;

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;
private Logger logger = Logger.getLogger(TrainControllerImpl.class.getName());

public TrainControllerImpl() {
thread = new Thread(() -> {
thread.start();
try {
followSpeed();
Thread.sleep(100);
} catch (InterruptedException e) {
logger.log(Level.SEVERE, "Thread Interrupted Exception!", e);
thread.interrupt();
}
});
}

@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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public interface TrainUser {

void overrideJoystickPosition(int joystickPosition);

boolean getAlarmState();

void setAlarmState(boolean alarmState);


}
1 change: 1 addition & 0 deletions train-sensor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ 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);
user.setAlarmState(false);
}
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 12 additions & 0 deletions train-user/src/main/java/hu/bme/mit/train/user/TrainUserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}


}