Neue Klasse HostConfig, die Hostnamen, portA und portB vereint
authorfelix <felix@a944a559-bf0e-0410-8ddc-85264b264b6c>
Sun, 13 May 2007 09:37:29 +0000 (09:37 +0000)
committerfelix <felix@a944a559-bf0e-0410-8ddc-85264b264b6c>
Sun, 13 May 2007 09:37:29 +0000 (09:37 +0000)
git-svn-id: https://www.internetallee.de/svn/bytewurf@18 a944a559-bf0e-0410-8ddc-85264b264b6c

projekte/netzschalter/src/de/bytewurf/projekte/netzschalter/ConfigPanel.java
projekte/netzschalter/src/de/bytewurf/projekte/netzschalter/HostConfig.java [new file with mode: 0644]
projekte/netzschalter/src/de/bytewurf/projekte/netzschalter/Netzdose.java
projekte/netzschalter/test/de/bytewurf/projekte/netzschalter/HostConfigTest.java [new file with mode: 0644]
projekte/netzschalter/test/de/bytewurf/projekte/netzschalter/NetzdosenMockup.java

index 5c34c30..2965f61 100644 (file)
@@ -71,9 +71,11 @@ public class ConfigPanel extends JPanel {
                try {
                        int portANumber = Integer.parseInt(portAField.getText());
                        int portBNumber = Integer.parseInt(portBField.getText());
-                       netzdose.setHostname(hostnameField.getText());
-                       netzdose.setPortA(portANumber);
-                       netzdose.setPortB(portBNumber);
+                       HostConfig hostConfig = new HostConfig();
+                       hostConfig.setHostname(hostnameField.getText());
+                       hostConfig.setPortA(portANumber);
+                       hostConfig.setPortB(portBNumber);
+                       netzdose.setHostConfig(hostConfig);
                } catch (NumberFormatException e) {
                        LogMediator.getInstance().log(
                                        "Kann portnummer nicht parsen." + e.toString());
diff --git a/projekte/netzschalter/src/de/bytewurf/projekte/netzschalter/HostConfig.java b/projekte/netzschalter/src/de/bytewurf/projekte/netzschalter/HostConfig.java
new file mode 100644 (file)
index 0000000..c1c5638
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Created on 13.05.2007
+ *
+ * To change this generated comment go to 
+ * Window>Preferences>Java>Code Generation>Code Template
+ */
+package de.bytewurf.projekte.netzschalter;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+/**
+ * @author felix
+ */
+public class HostConfig {
+       public static final int MIN_PORT_NUMBER = 0;
+       public static final int MAX_PORT_NUMBER = (1<<16);
+       InetAddress host;
+       int portA = 0;
+       int portB = 0;
+       
+       public String getHostname() {
+               if (host == null)
+                       return "localhost";
+               
+               return host.getHostName();
+       }
+       
+       public void setHostname(String hostname) {
+               try {
+                       host = InetAddress.getByName(hostname);
+               } catch (UnknownHostException e) {
+                       throw new IllegalArgumentException("Hostname nicht gültig");
+               }
+       }
+       
+       public int getPortA() {
+               return portA;
+       }
+       
+       public void setPortA(int portA) {
+               if (!isValidPortnumber(portA))
+                       throw new IllegalArgumentException("Port ausserhalb des gültigen Bereichs");
+               this.portA = portA;
+       }
+
+       private boolean isValidPortnumber(int portA) {
+               return ((portA > MIN_PORT_NUMBER) && (portA < MAX_PORT_NUMBER));
+       }
+       public int getPortB() {
+               return portB;
+       }
+       public void setPortB(int portB) {
+               if (!isValidPortnumber(portB))
+                       throw new IllegalArgumentException("Port ausserhalb des gültigen Bereichs");
+               this.portB = portB;
+       }
+       
+}
index d10f01d..49e50fa 100644 (file)
@@ -17,16 +17,14 @@ public interface Netzdose {
        
        public int getNumberOfRelais();
        
-       public void setHostname(String hostname);
-       
        public String getHostname();
        
-       public void setPortA(int portNumber);
-       
        public int getPortA();
        
-       public void setPortB(int portNumber);
-       
        public int getPortB();
+       
+       public void setHostConfig(HostConfig hostConfig);
+       
+       public HostConfig getHostConfig();
 
 }
\ No newline at end of file
diff --git a/projekte/netzschalter/test/de/bytewurf/projekte/netzschalter/HostConfigTest.java b/projekte/netzschalter/test/de/bytewurf/projekte/netzschalter/HostConfigTest.java
new file mode 100644 (file)
index 0000000..0a0b5ed
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * Created on 13.05.2007
+ *
+ * To change this generated comment go to 
+ * Window>Preferences>Java>Code Generation>Code Template
+ */
+package de.bytewurf.projekte.netzschalter;
+
+import java.net.InetAddress;
+
+import junit.framework.TestCase;
+
+/**
+ * @author felix
+ */
+public class HostConfigTest extends TestCase {
+       
+       HostConfig hostConfig;
+       String testHostname;
+
+       protected void setUp() throws Exception {
+               super.setUp();
+               hostConfig = new HostConfig();
+               testHostname = InetAddress.getLocalHost().getHostName();
+       }
+
+       public void testSetHostname() {
+               assertEquals(hostConfig.getHostname(), "localhost");
+               boolean exceptionOccured = false;
+               try {
+                       hostConfig.setHostname("not_a_hostname1732");
+               } catch (RuntimeException e) {
+                       exceptionOccured = true;
+               }
+               assertEquals(true, exceptionOccured);
+               assertEquals(hostConfig.getHostname(), "localhost");
+               
+               hostConfig.setHostname(testHostname);
+               assertEquals(testHostname, hostConfig.getHostname());
+       }
+       
+       public void testSetPortA() {
+               assertEquals(0, hostConfig.getPortA());
+               assertEquals(true, checkExceptionForInvalidPortA(0));
+               assertEquals(0, hostConfig.getPortA());
+               assertEquals(true, checkExceptionForInvalidPortA((1<<16) + 1));
+               assertEquals(0, hostConfig.getPortA());
+               assertEquals(true, checkExceptionForInvalidPortA(1<<16));
+               assertEquals(0, hostConfig.getPortA());
+               hostConfig.setPortA(23);
+               assertEquals(23, hostConfig.getPortA());
+       }
+
+       private boolean checkExceptionForInvalidPortA(int portNumber) {
+               boolean exceptionOccured = false;
+               try {
+                       hostConfig.setPortA(portNumber);
+               } catch (RuntimeException e) {
+                       exceptionOccured = true;
+               }
+                return exceptionOccured;
+       }
+       
+       public void testSetPortB() {
+               assertEquals(0, hostConfig.getPortB());
+               assertEquals(true, checkExceptionForInvalidPortB(0));
+               assertEquals(0, hostConfig.getPortB());
+               assertEquals(true, checkExceptionForInvalidPortB((1<<16) + 1));
+               assertEquals(0, hostConfig.getPortB());
+               assertEquals(true, checkExceptionForInvalidPortB(1<<16));
+               assertEquals(0, hostConfig.getPortB());
+               hostConfig.setPortB(23);
+               assertEquals(23, hostConfig.getPortB());
+       }
+
+       private boolean checkExceptionForInvalidPortB(int portNumber) {
+               boolean exceptionOccured = false;
+               try {
+                       hostConfig.setPortB(portNumber);
+               } catch (RuntimeException e) {
+                       exceptionOccured = true;
+               }
+                return exceptionOccured;
+       }
+}
index 7211eff..8d0c17b 100644 (file)
@@ -12,9 +12,7 @@ package de.bytewurf.projekte.netzschalter;
 public class NetzdosenMockup implements Netzdose {
 
        byte relaisState = 0;
-       String hostname = "";
-       int portA = 0;
-       int portB = 0;
+       HostConfig hostConfig = new HostConfig();
 
        public int getNumberOfRelais() {
                return 8;
@@ -39,27 +37,26 @@ public class NetzdosenMockup implements Netzdose {
        }
 
        public String getHostname() {
-               return hostname;
-       }
-
-       public void setHostname(String hostname) {
-               this.hostname = hostname;
+               return hostConfig.getHostname();
        }
 
        public int getPortA() {
-               return portA;
+               return hostConfig.getPortA();
        }
 
-       public void setPortA(int portA) {
-               this.portA = portA;
+       public int getPortB() {
+               return hostConfig.getPortB();
        }
 
-       public int getPortB() {
-               return portB;
+       public void setHostConfig(HostConfig hostConfig) {
+               if (hostConfig == null)
+                       throw new RuntimeException("Kein HostConfig Objekt übergeben");
+               
+               this.hostConfig = hostConfig;
        }
 
-       public void setPortB(int portB) {
-               this.portB = portB;
+       public HostConfig getHostConfig() {
+               return hostConfig;
        }