Command Objekt zum Transformieren von String und ähnlichem in byte[]
authorfelix <felix@a944a559-bf0e-0410-8ddc-85264b264b6c>
Tue, 15 May 2007 18:25:47 +0000 (18:25 +0000)
committerfelix <felix@a944a559-bf0e-0410-8ddc-85264b264b6c>
Tue, 15 May 2007 18:25:47 +0000 (18:25 +0000)
git-svn-id: https://www.internetallee.de/svn/bytewurf@22 a944a559-bf0e-0410-8ddc-85264b264b6c

projekte/netzschalter/src/de/bytewurf/projekte/netzschalter/Command.java [new file with mode: 0644]
projekte/netzschalter/test/de/bytewurf/projekte/netzschalter/CommandTest.java [new file with mode: 0644]

diff --git a/projekte/netzschalter/src/de/bytewurf/projekte/netzschalter/Command.java b/projekte/netzschalter/src/de/bytewurf/projekte/netzschalter/Command.java
new file mode 100644 (file)
index 0000000..859169b
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * 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;
+       }
+}
diff --git a/projekte/netzschalter/test/de/bytewurf/projekte/netzschalter/CommandTest.java b/projekte/netzschalter/test/de/bytewurf/projekte/netzschalter/CommandTest.java
new file mode 100644 (file)
index 0000000..8dcc005
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * 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]);
+       }
+
+}