Skip to content
Snippets Groups Projects
Commit de657fa3 authored by brinn's avatar brinn
Browse files

[SE-295] change: GlobalpropertiesLoader to have start and stop codon and to...

[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
parent 347768cc
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
}
/*
* 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());
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment