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

[LMS-460] change r6673 such that the library writes out TRUE and FALSE but...

[LMS-460] change r6673 such that the library writes out TRUE and FALSE but accepts all capitalizations when reading

SVN: 6774
parent 6c569282
No related branches found
No related tags found
No related merge requests found
......@@ -141,6 +141,8 @@ public class Utilities
/**
* Return the string content of a file from the given <var>directory</var> as boolean (<code>TRUE</code>
* or <code>FALSE</code>).
*
* @throws DataStructureException If the value of <var>name</var> is not a boolean.
*/
public static Boolean getBoolean(final IDirectory directory, final String name)
throws DataStructureException
......@@ -149,7 +151,7 @@ public class Utilities
final String value = getTrimmedString(directory, name);
try
{
return Boolean.valueOf(value);
return Boolean.fromString(value);
} catch (final IllegalArgumentException ex)
{
throw new DataStructureException("Value of '" + name
......@@ -239,23 +241,44 @@ public class Utilities
//
/**
* A boolean object that only accepts <code>TRUE</code> or <code>FALSE</code> as value
* (case-sensitive).
* A boolean object that uses <code>TRUE</code> and <code>FALSE</code> as text
* representation but accepts also <code>true</code> and <code>false</code> when converting
* from strings.
*/
public static enum Boolean
{
TRUE, FALSE;
/** Converts this object to corresponding <code>boolean</code>. */
public final boolean toBoolean()
public boolean toBoolean()
{
return this == TRUE ? true : false;
return (this == TRUE) ? true : false;
}
/** Converts given <code>boolean</code> to this enumeration item. */
public final static Boolean fromBoolean(final boolean bool)
public static Boolean fromBoolean(final boolean bool)
{
return bool ? TRUE : FALSE;
}
/**
* Converts a string value to a {@link Boolean}. Accepts either <code>true</code>,
* <code>TRUE</code>, <code>false</code> or <code>FALSE</code>.
*
* @throws IllegalArgumentException if <var>value</var> is not one of the values listed
* above.
*/
public static Boolean fromString(final String value)
{
if ("true".equalsIgnoreCase(value))
{
return TRUE;
}
if ("false".equalsIgnoreCase(value))
{
return FALSE;
}
throw new IllegalArgumentException("" + value);
}
}
}
\ No newline at end of file
......@@ -78,7 +78,7 @@ public final class FormatParameterFactory implements IFormatParameterFactory
} else if (nodeName.equals(HCSImageFormatV1_0.CONTAINS_ORIGINAL_DATA))
{
return new FormatParameter(HCSImageFormatV1_0.CONTAINS_ORIGINAL_DATA, Utilities.Boolean
.valueOf(value));
.fromString(value));
}
return formatParameter;
}
......@@ -104,7 +104,7 @@ public final class FormatParameterFactory implements IFormatParameterFactory
return new FormatParameter(name, Integer.parseInt(value));
} else if (name.equals(HCSImageFormatV1_0.CONTAINS_ORIGINAL_DATA))
{
return new FormatParameter(name, Utilities.Boolean.valueOf(value));
return new FormatParameter(name, Utilities.Boolean.fromString(value));
}
return IFormatParameterFactory.DEFAULT_FORMAT_PARAMETER_FACTORY.createFormatParameter(name,
value);
......
......@@ -138,7 +138,7 @@ public class UtilitiesTest extends AbstractFileSystemTestCase
assertEquals(key, listFiles[0].getName());
assertEquals(value, file.getStringContent().trim());
assertTrue(Utilities.getBoolean(directory, key).toBoolean());
directory.addKeyValuePair(key, "true");
directory.addKeyValuePair(key, "yes");
try
{
Utilities.getBoolean(directory, key);
......@@ -151,4 +151,31 @@ public class UtilitiesTest extends AbstractFileSystemTestCase
assertFalse(Utilities.getBoolean(directory, key).toBoolean());
}
@Test
public void testBooleanToString()
{
assertEquals("TRUE", Utilities.Boolean.TRUE.toString());
assertEquals("FALSE", Utilities.Boolean.FALSE.toString());
}
@Test
public void testBooleanFromString()
{
assertEquals(Utilities.Boolean.TRUE, Utilities.Boolean.fromString("TRUE"));
assertEquals(Utilities.Boolean.TRUE, Utilities.Boolean.fromString("true"));
assertEquals(Utilities.Boolean.FALSE, Utilities.Boolean.fromString("FALSE"));
assertEquals(Utilities.Boolean.FALSE, Utilities.Boolean.fromString("false"));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testBooleanFromStringError()
{
Utilities.Boolean.fromString("yes");
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testBooleanFromStringNull()
{
Utilities.Boolean.fromString(null);
}
}
\ No newline at end of file
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