From 56280ba6bef3b5a26fc48fa002cbb8dc04e36db9 Mon Sep 17 00:00:00 2001 From: jomu Date: Wed, 20 Nov 2013 20:56:24 +0000 Subject: [PATCH] added esapi config --- .../shared/util/StringEncodingException.java | 7 ++- .../muehlencord/shared/util/StringUtil.java | 7 +-- .../muehlencord/shared/util/OSUtilTest.java | 39 +++++++++++++++++ .../util/StringEncodingExceptionTest.java | 43 +++++++++++++++++++ .../shared/util/StringUtilTest.java | 11 +++-- 5 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 util/src/test/java/de/muehlencord/shared/util/OSUtilTest.java create mode 100644 util/src/test/java/de/muehlencord/shared/util/StringEncodingExceptionTest.java diff --git a/util/src/main/java/de/muehlencord/shared/util/StringEncodingException.java b/util/src/main/java/de/muehlencord/shared/util/StringEncodingException.java index 984610d..aaa9446 100644 --- a/util/src/main/java/de/muehlencord/shared/util/StringEncodingException.java +++ b/util/src/main/java/de/muehlencord/shared/util/StringEncodingException.java @@ -1,7 +1,3 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ package de.muehlencord.shared.util; /** @@ -9,12 +5,15 @@ package de.muehlencord.shared.util; * @author joern@muehlencord.de */ public class StringEncodingException extends Exception { + + protected final static String DEFAULT_MSG = "An encoding exception occurred"; /** * Creates a new instance of * StringEncodingException without detail message. */ public StringEncodingException() { + super (DEFAULT_MSG); } /** diff --git a/util/src/main/java/de/muehlencord/shared/util/StringUtil.java b/util/src/main/java/de/muehlencord/shared/util/StringUtil.java index 8967665..cf62a32 100644 --- a/util/src/main/java/de/muehlencord/shared/util/StringUtil.java +++ b/util/src/main/java/de/muehlencord/shared/util/StringUtil.java @@ -5,6 +5,7 @@ */ package de.muehlencord.shared.util; +import java.io.UnsupportedEncodingException; import java.text.ParseException; import org.apache.log4j.Logger; @@ -29,7 +30,7 @@ public abstract class StringUtil { try { byte[] b = input.getBytes("UTF-8"); return new String(b, "ISO-8859-1"); - } catch (Exception ex) { + } catch (UnsupportedEncodingException ex) { logger.debug(getStackTraceString(ex)); throw new StringEncodingException("Cannot convert string from UTF-8 to ISO-8859-1. Reason: " + ex.getMessage(), ex); } @@ -44,8 +45,8 @@ public abstract class StringUtil { public static String getStackTraceString(Throwable ex) { String logString = ex.toString() + "\n"; StackTraceElement[] stack = ex.getStackTrace(); - for (int i = 0; i < stack.length; i++) { - logString += (stack[i].toString()) + "\n"; + for (StackTraceElement stack1 : stack) { + logString += (stack1.toString()) + "\n"; } return logString; } diff --git a/util/src/test/java/de/muehlencord/shared/util/OSUtilTest.java b/util/src/test/java/de/muehlencord/shared/util/OSUtilTest.java new file mode 100644 index 0000000..29be0a1 --- /dev/null +++ b/util/src/test/java/de/muehlencord/shared/util/OSUtilTest.java @@ -0,0 +1,39 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package de.muehlencord.shared.util; + +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author jomu + */ +public class OSUtilTest { + + /** + * executes getOS method + */ + @Test + public void testGetOS() { + String osName = System.getProperties().getProperty("os.name").toUpperCase(); + int os = OSUtil.getOperationSystem(); + + switch (osName) { + case "LINUX": + assertTrue(os == OSUtil.OS_LINUX); + break; + case "WINDOWS": + assertTrue(os == OSUtil.OS_WINDOWS); + break; + default: + fail("Unknown / unsupported OS for test case"); + } + + System.out.println(osName); + } + +} diff --git a/util/src/test/java/de/muehlencord/shared/util/StringEncodingExceptionTest.java b/util/src/test/java/de/muehlencord/shared/util/StringEncodingExceptionTest.java new file mode 100644 index 0000000..b06cafa --- /dev/null +++ b/util/src/test/java/de/muehlencord/shared/util/StringEncodingExceptionTest.java @@ -0,0 +1,43 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package de.muehlencord.shared.util; + +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author jomu + */ +public class StringEncodingExceptionTest { + + + @Test + public void testDefaultConstructor() { + StringEncodingException ex = new StringEncodingException(); + assertTrue (ex.getMessage().equals (StringEncodingException.DEFAULT_MSG)); + } + + @Test + public void testMessageConstructor() { + String msg = "Test message"; + StringEncodingException ex = new StringEncodingException(msg); + assertTrue (ex.getMessage().equals (msg)); + } + + + @Test + public void testRootcauseConstructor() { + String msg = "Test message"; + StringEncodingException rootCause = new StringEncodingException(); + StringEncodingException ex = new StringEncodingException(msg, rootCause); + assertTrue (ex.getMessage().equals (msg)); + assertTrue (ex.getCause().equals (rootCause)); + } + + +} diff --git a/util/src/test/java/de/muehlencord/shared/util/StringUtilTest.java b/util/src/test/java/de/muehlencord/shared/util/StringUtilTest.java index 89e9255..ccdedb4 100644 --- a/util/src/test/java/de/muehlencord/shared/util/StringUtilTest.java +++ b/util/src/test/java/de/muehlencord/shared/util/StringUtilTest.java @@ -1,4 +1,3 @@ - package de.muehlencord.shared.util; import java.io.IOException; @@ -8,16 +7,16 @@ import org.junit.Test; /** * Basic StringUtilTests + * * @author joern@muehlencord.de */ public class StringUtilTest extends DefaultTest { - @Test public void testGetValueBetweenKeywords() throws ParseException, IOException { - String content = readContentFromFile("test.txt"); - - String ipAddress = StringUtil.getValueBetweenKeywords(content, "The IP", "has just"); - assertEquals ("ipAddress", "222.184.230.118", ipAddress); + String content = readContentFromFile("test.txt"); + + String ipAddress = StringUtil.getValueBetweenKeywords(content, "The IP", "has just"); + assertEquals("ipAddress", "222.184.230.118", ipAddress); } }