r1740 - in trunk/src: main/java/org/nuiton/util test/java/org/nuiton/util
Author: echatellier Date: 2010-01-14 18:43:52 +0100 (Thu, 14 Jan 2010) New Revision: 1740 Modified: trunk/src/main/java/org/nuiton/util/ApplicationConfig.java trunk/src/test/java/org/nuiton/util/ApplicationConfigTest.java Log: Fix #157 : translate imbricated ${xxx} in properties value. Modified: trunk/src/main/java/org/nuiton/util/ApplicationConfig.java =================================================================== --- trunk/src/main/java/org/nuiton/util/ApplicationConfig.java 2010-01-14 16:16:12 UTC (rev 1739) +++ trunk/src/main/java/org/nuiton/util/ApplicationConfig.java 2010-01-14 17:43:52 UTC (rev 1740) @@ -564,7 +564,7 @@ } public String getConfigFileName() { - String result = options.getProperty(CONFIG_FILE_NAME); + String result = getOption(CONFIG_FILE_NAME); return result; } @@ -590,9 +590,55 @@ */ public String getOption(String key) { String value = options.getProperty(key); + // replace ${xxx} + value = replaceRecursiveOptions(value); return value; } + + /** + * Replace inlclude ${xxx} suboption by their values. + * + * @param option option to replace into + * @return replaced option + */ + protected String replaceRecursiveOptions(String option) { + // TODO do a common code with RecursiveProperties code + // TODO but can't overwrite getProperty() method + + String result = option; + + if (result == null) { + return null; + } + + //Ex : result="My name is ${myName}." + int pos = result.indexOf("${", 0); + //Ex : pos=11 + while (pos != -1) { + int posEnd = result.indexOf("}", pos + 1); + //Ex : posEnd=19 + if (posEnd != -1) { + String value = getOption(result.substring(pos + 2, posEnd)); + // Ex : getProperty("myName"); + if (value != null) { + // Ex : value="Thimel" + result = result.substring(0, pos) + value + result.substring(posEnd + 1); + // Ex : result="My name is " + "Thimel" + "." + pos = result.indexOf("${", pos + value.length()); + // Ex : pos=-1 + } else { + // Ex : value=null + pos = result.indexOf("${", posEnd + 1); + // Ex : pos=-1 + } + // Ex : pos=-1 + } + } + + return result; + } + /** * Permet de recuperer l'ensemble des options commencant par une certaine * chaine @@ -636,7 +682,7 @@ T result = null; String cacheKey = key + "-" + clazz.getName(); - String value = options.getProperty(key); + String value = getOption(key); int hash = 0; if (value != null) { hash = value.hashCode(); Modified: trunk/src/test/java/org/nuiton/util/ApplicationConfigTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/ApplicationConfigTest.java 2010-01-14 16:16:12 UTC (rev 1739) +++ trunk/src/test/java/org/nuiton/util/ApplicationConfigTest.java 2010-01-14 17:43:52 UTC (rev 1740) @@ -1,4 +1,4 @@ -/** +/* * *##% Nuiton utilities library * Copyright (C) 2004 - 2009 CodeLutin * @@ -16,34 +16,19 @@ * License along with this program. If not, see * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* */ -/* - * Copyright (C) 2002-2008 Code Lutin, Benjamin Poussin - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, - * USA. - */ package org.nuiton.util; +import java.io.File; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.ListIterator; import java.util.Map; + import junit.framework.TestCase; + import org.nuiton.util.ApplicationConfig.Action; /** @@ -244,4 +229,28 @@ assertEquals(1, dummyActionCall); } + /** + * Test that system properties such as ${user.home}, ${user.name} are + * replaced. + * @throws ArgumentsParserException + */ + public void testSystemProperties() throws ArgumentsParserException { + ApplicationConfig instance = new ApplicationConfig(); + instance.parse(new String[]{}); + instance.printConfig(); + + instance.setOption("hellomessage", "Hello ${user.name} !"); + + assertEquals("Hello " + System.getProperty("user.name") + " !", instance.getOption("hellomessage")); + assertEquals("Hello ${user.name} !", instance.options.getProperty("hellomessage")); + + instance.setOption("tempdir", "${java.io.tmpdir}" + File.separator + "blah"); + File tempDir = instance.getOptionAsFile("tempdir"); + assertEquals(System.getProperty("java.io.tmpdir") + File.separator + "blah", tempDir.getAbsolutePath()); + + instance.setOption("system", "${os.name}"); + instance.setOption("os", "${system}"); + instance.setOption("sysinfo", "I'm running ${os} :)"); + assertEquals("I'm running " + System.getProperty("os.name") + " :)", instance.getOption("sysinfo")); + } }
participants (1)
-
echatellier@users.nuiton.org