From 215bb4f260e332e183d6b0e7e79bf504ddf5d3d4 Mon Sep 17 00:00:00 2001 From: felix Date: Tue, 15 May 2007 18:25:47 +0000 Subject: [PATCH] =?utf8?q?Command=20Objekt=20zum=20Transformieren=20von=20?= =?utf8?q?String=20und=20=C3=A4hnlichem=20in=20byte[]?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: https://www.internetallee.de/svn/bytewurf@22 a944a559-bf0e-0410-8ddc-85264b264b6c --- .../de/bytewurf/projekte/netzschalter/Command.java | 33 ++++++++++++++ .../projekte/netzschalter/CommandTest.java | 52 ++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 projekte/netzschalter/src/de/bytewurf/projekte/netzschalter/Command.java create mode 100644 projekte/netzschalter/test/de/bytewurf/projekte/netzschalter/CommandTest.java 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]); + } + +} -- 2.11.0