From bc53c27e46004a34fd49fae4e4d5e4dcb79d9bda Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Fri, 22 Jun 2012 13:42:51 +0200 Subject: [PATCH] Added OnlineGlomPropertiesTest. * src/main/java/org/glom/web/server/OnlineGlomProperties.java: Make sure we never return a null string. * src/test/java/org/glom/web/server/OnlineGlomPropertiesTest.java: Added tests of the OnlineGlomProperties API, using our sample file. --- ChangeLog | 9 ++ .../org/glom/web/server/OnlineGlomProperties.java | 68 +++++++++------- .../glom/web/server/OnlineGlomPropertiesTest.java | 95 ++++++++++++++++++++++ 3 files changed, 141 insertions(+), 31 deletions(-) create mode 100644 src/test/java/org/glom/web/server/OnlineGlomPropertiesTest.java diff --git a/ChangeLog b/ChangeLog index f178c62..f68a40b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2012-06-22 Murray Cumming + + Added OnlineGlomPropertiesTest. + + * src/main/java/org/glom/web/server/OnlineGlomProperties.java: + Make sure we never return a null string. + * src/test/java/org/glom/web/server/OnlineGlomPropertiesTest.java: + Added tests of the OnlineGlomProperties API, using our sample file. + 2012-06-20 Murray Cumming Make OnlineGlomProperties be a normal class. diff --git a/src/main/java/org/glom/web/server/OnlineGlomProperties.java b/src/main/java/org/glom/web/server/OnlineGlomProperties.java index c83fdd6..dcf3247 100644 --- a/src/main/java/org/glom/web/server/OnlineGlomProperties.java +++ b/src/main/java/org/glom/web/server/OnlineGlomProperties.java @@ -21,37 +21,14 @@ package org.glom.web.server; import java.util.Properties; +import org.apache.commons.lang3.StringUtils; + /** A convenience class for dealing with the Online Glom configuration file * TODO: test this. */ class OnlineGlomProperties extends Properties { private static final long serialVersionUID = 4290997725469072758L; - - /** Get the key for any *.*.filename = thefilename line. - * - * @param value - * @return - */ - private String getKey(final String filename) { - - for (final String key : stringPropertyNames()) { - - //Split the line at the . separators, - final String[] keyArray = key.split("\\."); - if (keyArray.length != 3) - continue; - if(!("filename".equals(keyArray[2]))) { - continue; - } - - if (getProperty(key).trim().equals(filename)) { - return key; - } - } - - return null; - } public static class Credentials { public String userName = ""; @@ -76,26 +53,55 @@ class OnlineGlomProperties extends Properties { //Get the username and password for this file: final String usernameKey = key.replaceAll(keyArray[2], "username"); final String passwordKey = key.replaceAll(keyArray[2], "password"); - result.userName = getProperty(usernameKey).trim(); - result.password = getProperty(passwordKey); + result.userName = getPropertyNonNull(usernameKey).trim(); + result.password = getPropertyNonNull(passwordKey); } return result; } public String getGlobalUsername() { - return getProperty("glom.document.username").trim(); + return getPropertyNonNull("glom.document.username").trim(); } public String getGlobalPassword() { - return getProperty("glom.document.password"); + return getPropertyNonNull("glom.document.password"); } public String getGlobalLocale() { - return getProperty("glom.document.locale"); + return getPropertyNonNull("glom.document.locale"); } public String getDocumentsDirectory() { - return getProperty("glom.document.directory"); + return getPropertyNonNull("glom.document.directory"); + } + + /** Get the key for any *.*.filename = thefilename line. + * + * @param value + * @return + */ + private String getKey(final String filename) { + + for (final String key : stringPropertyNames()) { + + //Split the line at the . separators, + final String[] keyArray = key.split("\\."); + if (keyArray.length != 3) + continue; + if(!("filename".equals(keyArray[2]))) { + continue; + } + + if (getPropertyNonNull(key).trim().equals(filename)) { + return key; + } + } + + return null; + } + + private String getPropertyNonNull(final String key) { + return StringUtils.defaultString(getProperty(key)); } } \ No newline at end of file diff --git a/src/test/java/org/glom/web/server/OnlineGlomPropertiesTest.java b/src/test/java/org/glom/web/server/OnlineGlomPropertiesTest.java new file mode 100644 index 0000000..623e000 --- /dev/null +++ b/src/test/java/org/glom/web/server/OnlineGlomPropertiesTest.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2012 Openismus GmbH + * + * This file is part of GWT-Glom. + * + * GWT-Glom is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with GWT-Glom. If not, see . + */ + +package org.glom.web.server; + + +import java.io.IOException; +import java.io.InputStream; + +import junit.framework.TestCase; + +import org.junit.Test; + +/** + * @author Murray Cumming + * + */ +public class OnlineGlomPropertiesTest extends TestCase { + + OnlineGlomProperties config = null; + + public void setUp() throws IOException { + config = new OnlineGlomProperties(); + + final InputStream is = Thread.currentThread().getContextClassLoader() + .getResourceAsStream("onlineglom.properties.sample"); + assertNotNull(is); + + config.load(is); // can throw an IOException + } + + /** + * Test method for {@link org.glom.web.server.OnlineGlomProperties#getCredentials(java.lang.String)}. + */ + @Test + public void testGetCredentials() { + OnlineGlomProperties.Credentials credentials = config.getCredentials("lesson-planner.glom"); + assertEquals("ben", credentials.userName); + assertEquals("ChangeMeToo", credentials.password); + } + + /** + * Test method for {@link org.glom.web.server.OnlineGlomProperties#getGlobalUsername()}. + */ + @Test + public void testGetGlobalUsername() { + assertEquals("someuser", config.getGlobalUsername()); + + } + + /** + * Test method for {@link org.glom.web.server.OnlineGlomProperties#getGlobalPassword()}. + */ + @Test + public void testGetGlobalPassword() { + assertEquals("ChangeMe", config.getGlobalPassword()); + } + + /** + * Test method for {@link org.glom.web.server.OnlineGlomProperties#getGlobalLocale()}. + */ + @Test + public void testGetGlobalLocale() { + assertEquals("", config.getGlobalLocale()); + } + + /** + * Test method for {@link org.glom.web.server.OnlineGlomProperties#getDocumentsDirectory()}. + */ + @Test + public void testGetDocumentsDirectory() { + assertEquals("/home/someuser/glomfiles", config.getDocumentsDirectory()); + } + + public void tearDown() { + config = null; + } + +} -- 2.1.4