From: felix Date: Tue, 15 May 2007 18:25:47 +0000 (+0000) Subject: Command Objekt zum Transformieren von String und ähnlichem in byte[] X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=215bb4f260e332e183d6b0e7e79bf504ddf5d3d4;p=bytewurf.git Command Objekt zum Transformieren von String und ähnlichem in byte[] git-svn-id: https://www.internetallee.de/svn/bytewurf@22 a944a559-bf0e-0410-8ddc-85264b264b6c --- 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 index 0000000..859169b --- /dev/null +++ b/projekte/netzschalter/src/de/bytewurf/projekte/netzschalter/Command.java @@ -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 index 0000000..8dcc005 --- /dev/null +++ b/projekte/netzschalter/test/de/bytewurf/projekte/netzschalter/CommandTest.java @@ -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]); + } + +}