From de657fa3e0779672f5849eb02d3ab3ff36bad43c Mon Sep 17 00:00:00 2001 From: brinn <brinn> Date: Thu, 2 Dec 2010 18:31:52 +0000 Subject: [PATCH] [SE-295] change: GlobalpropertiesLoader to have start and stop codon and to use "#!" instead of "#" to start a global property line add: unit test SVN: 18988 --- .../shared/parser/GlobalPropertiesLoader.java | 38 +++-- .../parser/GlobalPropertiesLoaderTest.java | 134 ++++++++++++++++++ 2 files changed, 161 insertions(+), 11 deletions(-) create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/parser/GlobalPropertiesLoaderTest.java diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/GlobalPropertiesLoader.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/GlobalPropertiesLoader.java index 5e85cb99ec7..32497f1cbf5 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/GlobalPropertiesLoader.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/parser/GlobalPropertiesLoader.java @@ -28,19 +28,21 @@ import org.apache.commons.lang.StringUtils; /** * Loads global properties. * <p> - * Global properties are defined as a comment and start with {@link #GLOBAL_PROPERTIES} followed by - * a new line character. Each property is defined in a separate line and has the following format: + * Global properties are defined as comments and start with a line + * <code>#! GLOBAL_PROPERTIES:</code> (followed by a new line character.) Each property is defined + * in a separate line and has the following format: * <p> - * # key = value + * #! key = value * </p> * First line in a different format marks the end of global properties. (Empty line can be used). * <p> * Example: * </p> * </p> --- FILE ---<br> - * # GLOBAL_PROPERTIES: <br> - * # key1 = value1 <br> - * # key2 = value2 <br> + * #! GLOBAL_PROPERTIES_START <br> + * #! key1 = value1 <br> + * #! key2 = value2 <br> + * #! GLOBAL_PROPERTIES_END <br> * <br> * code parent experiment <br> * --- EOF --- @@ -49,9 +51,11 @@ import org.apache.commons.lang.StringUtils; */ public class GlobalPropertiesLoader { - private static final String COMMENT_PREFIX = "#"; + private static final String PREFIX = "#!"; - static String GLOBAL_PROPERTIES = "#GLOBAL_PROPERTIES:"; + private static String GLOBAL_PROPERTIES_START = "GLOBAL_PROPERTIES_START"; + + private static String GLOBAL_PROPERTIES_END = "GLOBAL_PROPERTIES_END"; public static GlobalProperties load(File file) throws FileNotFoundException { @@ -78,7 +82,14 @@ public class GlobalPropertiesLoader continue; } else { - return properties; + if (isGlobalPropertiesStopper(line)) + { + return properties; + } else + { + throw new IllegalArgumentException("Illegal global property line '" + + line.trim() + "'"); + } } } } @@ -91,7 +102,7 @@ public class GlobalPropertiesLoader private static String[] tryGetPropertyDefinition(String line) { - String commentPrefix = COMMENT_PREFIX; + String commentPrefix = PREFIX; if (line.startsWith(commentPrefix)) { String[] splitted = StringUtils.split(line.substring(commentPrefix.length()), "=", 2); @@ -108,7 +119,12 @@ public class GlobalPropertiesLoader private static boolean isGlobalPropertiesStarter(String line) { - return line.equals(GLOBAL_PROPERTIES); + return line.substring(PREFIX.length()).trim().equals(GLOBAL_PROPERTIES_START); + } + + private static boolean isGlobalPropertiesStopper(String line) + { + return line.substring(PREFIX.length()).trim().equals(GLOBAL_PROPERTIES_END); } } diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/parser/GlobalPropertiesLoaderTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/parser/GlobalPropertiesLoaderTest.java new file mode 100644 index 00000000000..7a97d1ab303 --- /dev/null +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/shared/parser/GlobalPropertiesLoaderTest.java @@ -0,0 +1,134 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.shared.parser; + +import java.io.File; +import java.io.FileNotFoundException; + +import org.testng.annotations.Test; + +import ch.systemsx.cisd.common.filesystem.FileUtilities; + +import static org.testng.AssertJUnit.*; + +/** + * Some basic test cases for {@link GlobalPropertiesLoader}. + * + * @author Bernd Rinn + */ +public class GlobalPropertiesLoaderTest +{ + + @Test + public void testLoadGlobalPropertiesLoader() throws FileNotFoundException + { + final File f = new File("GlobalPropertiesLoaderTest.txt"); + f.deleteOnExit(); + FileUtilities.writeToFile(f, "#! GLOBAL_PROPERTIES_START\n#! a=A\n#!b = B\n#! c= C"); + final GlobalProperties props = GlobalPropertiesLoader.load(f); + assertEquals("A", props.get("a")); + assertEquals("B", props.get("b")); + assertEquals("C", props.get("c")); + assertNull(props.tryGet("d")); + } + + @Test + public void testLoadGlobalPropertiesLoaderWithEnd() throws FileNotFoundException + { + final File f = new File("GlobalPropertiesLoaderTest.txt"); + f.deleteOnExit(); + FileUtilities.writeToFile(f, "#! GLOBAL_PROPERTIES_START \n#! a=A\n#!b = B\n#! c= C\n" + + "#!GLOBAL_PROPERTIES_END\t\nUnrelated stuff\n#! d=D"); + final GlobalProperties props = GlobalPropertiesLoader.load(f); + assertEquals("A", props.get("a")); + assertEquals("B", props.get("b")); + assertEquals("C", props.get("c")); + assertNull(props.tryGet("d")); + } + + @Test + public void testLoadGlobalPropertiesLoaderIllegalLineFailed() throws FileNotFoundException + { + final File f = new File("GlobalPropertiesLoaderTest.txt"); + f.deleteOnExit(); + FileUtilities.writeToFile(f, "#! GLOBAL_PROPERTIES_START\n#! a=A\n#!b = B\n#!!!!"); + try + { + GlobalPropertiesLoader.load(f); + fail("Didn't detect an illegal global property line."); + } catch (IllegalArgumentException ex) + { + assertEquals("Illegal global property line '#!!!!'", ex.getMessage()); + } + } + + @Test + public void testLoadGlobalPropertiesHeaderVariation1Loader() throws FileNotFoundException + { + final File f = new File("GlobalPropertiesLoaderTest.txt"); + f.deleteOnExit(); + FileUtilities.writeToFile(f, "#!GLOBAL_PROPERTIES_START\n#! a=A\n#!b = B\n#! c= C"); + final GlobalProperties props = GlobalPropertiesLoader.load(f); + assertEquals("A", props.get("a")); + assertEquals("B", props.get("b")); + assertEquals("C", props.get("c")); + assertNull(props.tryGet("d")); + } + + @Test + public void testLoadGlobalPropertiesHeaderVariation2Loader() throws FileNotFoundException + { + final File f = new File("GlobalPropertiesLoaderTest.txt"); + f.deleteOnExit(); + FileUtilities.writeToFile(f, "#!\tGLOBAL_PROPERTIES_START\n#! a=A\n#!b = B\n#! c= C"); + final GlobalProperties props = GlobalPropertiesLoader.load(f); + assertEquals("A", props.get("a")); + assertEquals("B", props.get("b")); + assertEquals("C", props.get("c")); + assertNull(props.tryGet("d")); + } + + @Test + public void testLoadGlobalPropertiesHeaderVariation3Loader() throws FileNotFoundException + { + final File f = new File("GlobalPropertiesLoaderTest.txt"); + f.deleteOnExit(); + FileUtilities.writeToFile(f, "#! GLOBAL_PROPERTIES_START\n#! a=A\n#!b = B\n#! c= C"); + final GlobalProperties props = GlobalPropertiesLoader.load(f); + assertEquals("A", props.get("a")); + assertEquals("B", props.get("b")); + assertEquals("C", props.get("c")); + assertNull(props.tryGet("d")); + } + + @Test + public void testLoadGlobalPropertiesDefineTwiceFailed() throws FileNotFoundException + { + final File f = new File("GlobalPropertiesLoaderTest.txt"); + f.deleteOnExit(); + FileUtilities.writeToFile(f, "#! GLOBAL_PROPERTIES_START\n#! a=A\n#!b = B\n#! b= BB"); + try + { + GlobalPropertiesLoader.load(f); + fail("Didn't detect that property 'b' was defined twice."); + } catch (IllegalArgumentException ex) + { + assertEquals("Property 'b' defined twice.", ex.getMessage()); + } + } + +} -- GitLab