--- /dev/null
+/*
+ * Created on 15.05.2007
+ *
+ * To change this generated comment go to
+ * Window>Preferences>Java>Code Generation>Code Template
+ */
+package de.bytewurf.projekte.netzschalter;
+
+/**
+ * @author felix
+ */
+public class Command {
+ byte[] commandBytes;
+
+ Command(String commandString) {
+ String[] commands = commandString.split("[\t\n ,]+", 100);
+ commandBytes = new byte[commands.length];
+ try {
+ for (int i = 0; i < commands.length; i++) {
+ commandBytes[i] = Integer.decode(commands[i]).byteValue();
+ }
+ } catch (NumberFormatException e) {
+ commandBytes = null;
+ throw new RuntimeException("Ungültige Zahlen eingegeben" + e.toString());
+ }
+ }
+
+ public byte[] getCommandBytes() {
+ if (commandBytes == null)
+ throw new RuntimeException("No Command configured yet.");
+ return commandBytes;
+ }
+}
--- /dev/null
+/*
+ * Created on 15.05.2007
+ *
+ * To change this generated comment go to
+ * Window>Preferences>Java>Code Generation>Code Template
+ */
+package de.bytewurf.projekte.netzschalter;
+
+import junit.framework.TestCase;
+
+/**
+ * @author felix
+ */
+public class CommandTest extends TestCase {
+
+ public void testCommand() {
+ assertTrue(checkCommandWithException("Test"));
+ assertTrue(checkCommandWithException("Test"));
+ assertFalse(checkCommandWithException("123"));
+ assertTrue(checkCommandWithException("a b 1 3"));
+ assertFalse(checkCommandWithException("0xa,0x12"));
+ assertFalse(checkCommandWithException("0xa, 0x12"));
+ assertFalse(checkCommandWithException("0xaaa, 0x12"));
+ }
+
+ private boolean checkCommandWithException(String commandText) {
+ boolean exceptionOccured = false;
+ try {
+ new Command(commandText);
+ } catch (Exception e) {
+ exceptionOccured = true;
+ }
+ return exceptionOccured;
+ }
+
+ public void testGetCommandBytes() {
+ Command command;
+ command = new Command("0xab,0x12");
+ byte[] commandBytes = command.getCommandBytes();
+ assertEquals(2, commandBytes.length);
+ assertEquals(Integer.decode("0xab").byteValue(), commandBytes[0]);
+ assertEquals(Integer.decode("0x12").byteValue(), commandBytes[1]);
+
+ command = new Command("10,20 30");
+ commandBytes = command.getCommandBytes();
+ assertEquals(3, commandBytes.length);
+ assertEquals(10, commandBytes[0]);
+ assertEquals(20, commandBytes[1]);
+ assertEquals(30, commandBytes[2]);
+ }
+
+}