Browse Source

revised all test for setDevices now testing if every installed devices has the right instance & added productive code

feature-pr-BordComputer-setDevices
Jan Ortner 3 years ago
parent
commit
ab054d5dfe
  1. 9
      src/main/java/BordComputer.java
  2. 27
      src/test/java/BordComputerTest.java

9
src/main/java/BordComputer.java

@ -11,7 +11,7 @@ public class BordComputer {
Device[] installedDevices; Device[] installedDevices;
Device activeDevice = null; Device activeDevice = null;
public void readConfig() {
public void readConfig() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
try (FileReader reader = new FileReader("Geraete.config")) { try (FileReader reader = new FileReader("Geraete.config")) {
Properties properties = new Properties(); Properties properties = new Properties();
@ -20,16 +20,17 @@ public class BordComputer {
Arrays.sort(values); Arrays.sort(values);
deviceNames = new String[values.length]; deviceNames = new String[values.length];
System.arraycopy(values, 0, deviceNames, 0, properties.size()); System.arraycopy(values, 0, deviceNames, 0, properties.size());
installedDevices=new Device[values.length];
installedDevices = new Device[values.length];
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
activeDevice =installedDevices[0];
setDevices();
activeDevice = installedDevices[0];
} }
public void setDevices() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException { public void setDevices() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ClassNotFoundException {
Class<?> c; Class<?> c;
for(int i = 0;i < deviceNames.length; i++) {
for (int i = 0; i < deviceNames.length; i++) {
c = Class.forName(deviceNames[i]); c = Class.forName(deviceNames[i]);
installedDevices[i] = (Device) c.getConstructor().newInstance(); installedDevices[i] = (Device) c.getConstructor().newInstance();
} }

27
src/test/java/BordComputerTest.java

@ -1,5 +1,7 @@
import device.Device; import device.Device;
import device.cdPlayer.CDPlayer;
import device.radioPlayer.RadioPlayer; import device.radioPlayer.RadioPlayer;
import device.usbPlayer.UsbPlayer;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
@ -35,7 +37,7 @@ class BordComputerTest {
} }
} }
static Stream<Arguments> readConfigOptions() {
static Stream<Arguments> readConfigOptions() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
BordComputer bc1 = new BordComputer(); BordComputer bc1 = new BordComputer();
bc1.readConfig(); bc1.readConfig();
@ -50,24 +52,24 @@ class BordComputerTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("setDevicesOptions") @MethodSource("setDevicesOptions")
void setDevicesTest(String testName, BordComputer testBc, String testTyp) {
Device dev = null;
if(testTyp.equals("0")) dev = testBc.installedDevices[0];
if(testTyp.equals("1")) dev = testBc.installedDevices[1];
if(testTyp.equals("2")) dev = testBc.installedDevices[2];
assertThat(dev).describedAs(testName).isNotEqualTo(null);
void setDevicesTest(String testName, BordComputer testBc, String testTyp, Boolean bool) {
Boolean boolActual = null;
if (testTyp.equals("0")) boolActual = (testBc.installedDevices[0] instanceof CDPlayer);
if (testTyp.equals("1")) boolActual = (testBc.installedDevices[1] instanceof RadioPlayer);
if (testTyp.equals("2")) boolActual = (testBc.installedDevices[2] instanceof UsbPlayer);
assertThat(boolActual).describedAs(testName).isEqualTo(bool);
} }
static Stream<Arguments> setDevicesOptions() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { static Stream<Arguments> setDevicesOptions() throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
BordComputer bc = new BordComputer(); BordComputer bc = new BordComputer();
bc.readConfig(); bc.readConfig();
bc.setDevices();
return Stream.of( return Stream.of(
Arguments.of("Test if installedDevices[0] is not null", bc, "0"),
Arguments.of("Test if installedDevices[1] is not null", bc, "1"),
Arguments.of("Test if installedDevices[2] is not null", bc, "2")
Arguments.of("Test if installedDevices[0] is instance of CDPlayer", bc, "0", true),
Arguments.of("Test if installedDevices[1] is instance of RadioPlayer", bc, "1", true),
Arguments.of("Test if installedDevices[2] is instance of UsbPlayer", bc, "2", true)
); );
} }
public int fileReaderCount() { public int fileReaderCount() {
int count = 0; int count = 0;
try (FileReader reader = new FileReader("Geraete.config")) { try (FileReader reader = new FileReader("Geraete.config")) {
@ -76,6 +78,7 @@ class BordComputerTest {
count = properties.size(); count = properties.size();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} return count;
}
return count;
} }
} }
Loading…
Cancel
Save