You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/07/07 18:46:39 UTC
svn commit: r1500475 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/DisabledListDelimiterHandler.java
test/java/org/apache/commons/configuration/TestDisabledListDelimiterHandler.java
Author: oheger
Date: Sun Jul 7 16:46:39 2013
New Revision: 1500475
URL: http://svn.apache.org/r1500475
Log:
Added DisabledListDelimiterHandler class.
This is a special implementation of ListDelimiterHandler which does not
support any list splitting. Thus it simulates the mode which was achieved in
the passed by setting the delimiterParsingDisabled property to true.
Added:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DisabledListDelimiterHandler.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDisabledListDelimiterHandler.java
Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DisabledListDelimiterHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DisabledListDelimiterHandler.java?rev=1500475&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DisabledListDelimiterHandler.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/DisabledListDelimiterHandler.java Sun Jul 7 16:46:39 2013
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.configuration;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * <p>
+ * A specialized implementation of the {@code ListDelimiterHandler} interface
+ * which disables list splitting.
+ * </p>
+ * <p>
+ * This class does not recognize any list delimiters; passed in strings are
+ * returned unchanged. Also the {@code escape()} method is a dummy - there is no
+ * need for escaping delimiter characters as none are supported. Note that the
+ * method for escaping a list throws an {@code UnsupportedOperationException}.
+ * If list delimiters are not supported, there is no point in squashing multiple
+ * values into a single one.
+ * </p>
+ * <p>
+ * Implementation note: An instance of this class can be shared between multiple
+ * configuration objects. It is state-less and thread-safe.
+ * </p>
+ *
+ * @version $Id: $
+ * @since 2.0
+ */
+public class DisabledListDelimiterHandler extends AbstractListDelimiterHandler
+{
+ /**
+ * {@inheritDoc} This implementation always throws an
+ * {@code UnsupportedOperationException} exception.
+ */
+ public Object escapeList(List<?> values)
+ {
+ throw new UnsupportedOperationException(
+ "Escaping lists is not supported!");
+ }
+
+ /**
+ * {@inheritDoc} This implementation always returns a collection containing
+ * the passed in string as its single element. The string is not changed,
+ * the {@code trim} flag is ignored. (The {@code trim} flag refers to the
+ * components extracted from the string. Because no components are extracted
+ * nothing is trimmed.)
+ */
+ @Override
+ protected Collection<String> splitString(String s, boolean trim)
+ {
+ Collection<String> result = new ArrayList<String>(1);
+ result.add(s);
+ return result;
+ }
+
+ /**
+ * {@inheritDoc} This implementation returns the passed in string without
+ * any changes.
+ */
+ @Override
+ protected String escapeString(String s)
+ {
+ return s;
+ }
+}
Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDisabledListDelimiterHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDisabledListDelimiterHandler.java?rev=1500475&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDisabledListDelimiterHandler.java (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestDisabledListDelimiterHandler.java Sun Jul 7 16:46:39 2013
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.configuration;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import java.util.Arrays;
+import java.util.Iterator;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test class for {@code DisabledListDelimiterHandler}. Note that some
+ * functionality of the base class is tested, too.
+ *
+ * @version $Id: $
+ */
+public class TestDisabledListDelimiterHandler
+{
+ /** An array with some test values. */
+ private static final Object[] VALUES = {
+ 20130630213801L, "A test value", 5
+ };
+
+ /** Constant for a test string value. */
+ private static final String STR_VALUE = " A test, string; value! ";
+
+ /** The instance to be tested. */
+ private DisabledListDelimiterHandler handler;
+
+ @Before
+ public void setUp() throws Exception
+ {
+ handler = new DisabledListDelimiterHandler();
+ }
+
+ /**
+ * Checks whether the passed in iterator contains the expected values.
+ *
+ * @param it the iterator to test
+ */
+ private static void checkIterator(Iterator<?> it)
+ {
+ for (Object o : VALUES)
+ {
+ assertEquals("Wrong value", o, it.next());
+ }
+ assertFalse("Iterator has too many objects", it.hasNext());
+ }
+
+ /**
+ * Tests whether the values of an array can be extracted.
+ */
+ @Test
+ public void testParseArray()
+ {
+ checkIterator(handler.parse(VALUES));
+ }
+
+ /**
+ * Tests whether the values of an Iterable object can be extracted.
+ */
+ @Test
+ public void testParseIterable()
+ {
+ checkIterator(handler.parse(Arrays.asList(VALUES)));
+ }
+
+ /**
+ * Tests whether the values of an Iterator object can be extracted.
+ */
+ @Test
+ public void testParseIterator()
+ {
+ checkIterator(handler.parse(Arrays.asList(VALUES).iterator()));
+ }
+
+ /**
+ * Tests whether a simple string value can be parsed.
+ */
+ @Test
+ public void testParseSimpleValue()
+ {
+ Iterator<?> it = handler.parse(STR_VALUE);
+ assertEquals("Wrong value", STR_VALUE, it.next());
+ assertFalse("Too many values", it.hasNext());
+ }
+
+ /**
+ * Tests whether a null value can be parsed.
+ */
+ @Test
+ public void testParseNull()
+ {
+ assertFalse("Got a value", handler.parse(null).hasNext());
+ }
+
+ /**
+ * Tests whether a string value is correctly escaped. The string should not
+ * be modified.
+ */
+ @Test
+ public void testEscapeStringValue()
+ {
+ assertEquals("Wrong escaped string", STR_VALUE,
+ handler.escape(STR_VALUE));
+ }
+
+ /**
+ * Tests whether a non-string value is correctly escaped. The object should
+ * not be modified.
+ */
+ @Test
+ public void testEscapeNonStringValue()
+ {
+ Object value = 42;
+ assertEquals("Wrong escaped object", value, handler.escape(value));
+ }
+
+ /**
+ * Tests escapeList(). This operation is not supported.
+ */
+ @Test(expected = UnsupportedOperationException.class)
+ public void testEscapeList()
+ {
+ handler.escapeList(Arrays.asList(VALUES));
+ }
+}