diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..11af03f --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +#Mon Aug 02 10:35:22 CEST 2010 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/src/org/jvalue/names/Name.java b/src/org/jvalue/names/Name.java index ff59b85..da2ccf0 100644 --- a/src/org/jvalue/names/Name.java +++ b/src/org/jvalue/names/Name.java @@ -47,10 +47,15 @@ public class Name implements Serializable { public static final char DEFAULT_DELIMITER_CHAR = '#'; public static final char DEFAULT_ESCAPE_CHAR = '\\'; + /** + * The empty name is a name with no components and default delimiter. + */ + public static final Name EMPTY_NAME = new Name(); + /** * Represents name as a single string */ - protected String name; + protected String name = null; /** * Saves number of components for performance reasons @@ -86,6 +91,8 @@ public Name(List components) { * @param escape Escape character to use when parsing string */ public Name(String name, char delimiter, char escape) { + assert name != null; + if ((delimiter != getDelimiterChar()) || (escape != getEscapeChar())) { name = switchDelEscScheme(name, delimiter, escape, getDelimiterChar(), getEscapeChar()); } @@ -93,6 +100,13 @@ public Name(String name, char delimiter, char escape) { this.name = name; } + /** + * Used for empty no components name. + */ + protected Name() { + this.name = null; + } + /** * */ @@ -205,18 +219,14 @@ public char getEscapeChar() { * */ public boolean isEmpty() { - return name.equals(""); + return name == null; // return name.equals(""); } /** * */ public Name getContextName() { - assertIsValidIndex(getNoComponents() - 1); - if (getNoComponents() == 1) { - return new Name(""); - } - + assertIsValidIndex(getNoComponents() - 2); int endPos = getIndexOfEndOfComponent(getNoComponents() - 2); return new Name(name.substring(0, endPos)); } @@ -355,7 +365,7 @@ protected String switchDelEscScheme(String name, char fromDel, char fromEsc, cha StringBuilder result = new StringBuilder(); int length = name.length(); if (length != 0) { - for (int end = 0; end < length; end++) { + for (int end = 0; end <= length; end++) { int startPos = end; String component = ""; if (startPos < name.length()) { diff --git a/test/org/jvalue/names/NameTest.java b/test/org/jvalue/names/NameTest.java index 161cf4f..e480b7a 100644 --- a/test/org/jvalue/names/NameTest.java +++ b/test/org/jvalue/names/NameTest.java @@ -29,82 +29,83 @@ public class NameTest { /** * */ - protected Name fN0; // ("") - protected Name fN1; // ("org", '.') - protected Name fN2; // ("jvalue.org", '.') - protected Name fN3; // ("www.jvalue.org", '.') + protected Name ne; // empty name + protected Name n0; // ("") + protected Name n1; // ("org", '.') + protected Name n2; // ("jvalue.org", '.') + protected Name n3; // ("www.jvalue.org", '.') @Before public void setUp() { - fN0 = getName(""); - - fN1 = getName("org", '.'); - fN2 = getName("jvalue.org", '.'); - fN3 = getName("www.jvalue.org", '.'); + ne = Name.EMPTY_NAME; + n0 = getName(""); + n1 = getName("org", '.'); + n2 = getName("jvalue.org", '.'); + n3 = getName("www.jvalue.org", '.'); } @Test public void testEquals() { - assertEquals(fN0, getName("")); - assertEquals(fN1, getName("org")); - assertEquals(fN2, getName("jvalue#org")); - assertEquals(fN3, getName("www#jvalue#org")); + assertEquals(n0, getName("")); + assertEquals(n1, getName("org")); + assertEquals(n2, getName("jvalue#org")); + assertEquals(n3, getName("www#jvalue#org")); } @Test public void testToString() { String del0s = ""; - assertEquals(del0s, fN0.toString()); + assertEquals(del0s, n0.toString()); String del1s = "org"; - assertEquals(del1s, fN1.toString()); + assertEquals(del1s, n1.toString()); - char del2 = fN2.getDelimiterChar(); + char del2 = n2.getDelimiterChar(); String del2s = "jvalue" + del2 + "org"; - assertEquals(del2s, fN2.toString()); + assertEquals(del2s, n2.toString()); - char del3 = fN3.getDelimiterChar(); + char del3 = n3.getDelimiterChar(); String del3s = "www" + del3 + "jvalue" + del3 + "org"; - assertEquals(del3s, fN3.toString()); + assertEquals(del3s, n3.toString()); } @Test public void testDefaultValue() { Name defaultValue = getName(""); - assertEquals(defaultValue, fN0.getDefaultValue()); - assertEquals(defaultValue, fN1.getDefaultValue()); + assertEquals(defaultValue, n0.getDefaultValue()); + assertEquals(defaultValue, n1.getDefaultValue()); } @Test public void testAppend() { - assertEquals(fN1, getName("").append("org")); - assertEquals(fN2, getName("jvalue").append("org")); - assertEquals(fN3, getName("www#jvalue").append("org")); + assertEquals(n1, ne.append("org")); + assertEquals(n2, getName("jvalue").append("org")); + assertEquals(n3, getName("www#jvalue").append("org")); } @Test public void testAsString2() { - assertEquals("", fN0.asString('*')); - assertEquals("org", fN1.asString('*')); - assertEquals("jvalue*org", fN2.asString('*')); - assertEquals("www*jvalue*org", fN3.asString('*')); + assertEquals("", n0.asString('*')); + assertEquals("org", n1.asString('*')); + assertEquals("jvalue*org", n2.asString('*')); + assertEquals("www*jvalue*org", n3.asString('*')); } @Test public void testAsStringArray() { - String[] c0 = fN0.asStringArray(); - assertEquals(0, c0.length); + String[] c0 = n0.asStringArray(); + assertEquals(1, c0.length); - String[] c1 = fN1.asStringArray(); + String[] c1 = n1.asStringArray(); assertEquals(1, c1.length); assertEquals("org", c1[0]); - String[] c2 = fN2.asStringArray(); + String[] c2 = n2.asStringArray(); assertEquals(2, c2.length); assertEquals("jvalue", c2[0]); assertEquals("org", c2[1]); - String[] c3 = fN3.asStringArray(); + String[] c3 = n3.asStringArray(); assertEquals(3, c3.length); assertEquals("www", c3[0]); assertEquals("jvalue", c3[1]); @@ -114,12 +115,12 @@ public void testAsStringArray() { @Test public void testGetComponent() { try { - assertEquals("org", fN1.getComponent(0)); - assertEquals("jvalue", fN2.getComponent(0)); - assertEquals("www", fN3.getComponent(0)); - assertEquals("org", fN2.getComponent(1)); - assertEquals("jvalue", fN3.getComponent(1)); - assertEquals("org", fN3.getComponent(2)); + assertEquals("org", n1.getComponent(0)); + assertEquals("jvalue", n2.getComponent(0)); + assertEquals("www", n3.getComponent(0)); + assertEquals("org", n2.getComponent(1)); + assertEquals("jvalue", n3.getComponent(1)); + assertEquals("org", n3.getComponent(2)); } catch (IndexOutOfBoundsException iie) { fail("getComponent() threw exception on valid index"); } @@ -127,25 +128,25 @@ public void testGetComponent() { @Test(expected = IndexOutOfBoundsException.class) public void testGetComponent0() { - fN0.getComponent(1); + n0.getComponent(1); } @Test(expected = IndexOutOfBoundsException.class) public void testGetComponent1() { - fN1.getComponent(-1); + n1.getComponent(-1); } @Test(expected = IndexOutOfBoundsException.class) public void testGetComponent2() { - fN3.getComponent(17); + n3.getComponent(17); } @Test public void testGetComponents() { - Iterator ni = fN3.components().iterator(); - for (int i = 0; i < fN3.getNoComponents(); i++) { + Iterator ni = n3.components().iterator(); + for (int i = 0; i < n3.getNoComponents(); i++) { try { - assertEquals(ni.next(), fN3.getComponent(i)); + assertEquals(ni.next(), n3.getComponent(i)); } catch (IndexOutOfBoundsException iie) { fail("getComponent() throw exception on valid index"); } @@ -154,15 +155,19 @@ public void testGetComponents() { @Test(expected = IndexOutOfBoundsException.class) public void testGetContextName0() { - fN0.getContextName(); + n0.getContextName(); } - @Test + @Test(expected = IndexOutOfBoundsException.class) public void testGetContextName1() { + n1.getContextName(); + } + + @Test + public void testGetContextName2() { try { - assertEquals(getName(""), fN1.getContextName()); - assertEquals(getName("jvalue"), fN2.getContextName()); - assertEquals(getName("www#jvalue"), fN3.getContextName()); + assertEquals(getName("jvalue"), n2.getContextName()); + assertEquals(getName("www#jvalue"), n3.getContextName()); } catch (IndexOutOfBoundsException nioob) { fail("getContextName of non-empty Name threw exception"); } @@ -186,9 +191,9 @@ public void testDelEscScheme() { @Test public void testGetFirstComponent() { try { - assertEquals("org", fN1.getFirstComponent()); - assertEquals("jvalue", fN2.getFirstComponent()); - assertEquals("www", fN3.getFirstComponent()); + assertEquals("org", n1.getFirstComponent()); + assertEquals("jvalue", n2.getFirstComponent()); + assertEquals("www", n3.getFirstComponent()); } catch (IndexOutOfBoundsException iie) { fail("getFirstComponent with first name threw exception"); } @@ -196,21 +201,22 @@ public void testGetFirstComponent() { @Test(expected = IndexOutOfBoundsException.class) public void testInsert0() { - fN0.insert(2, "test"); + n0.insert(2, "test"); } @Test(expected = IndexOutOfBoundsException.class) public void testInsert1() { - fN3.insert(-1, "test"); + n3.insert(-1, "test"); } @Test public void testInsert2() { try { - assertEquals(getName("test"), fN0.insert(0, "test")); - assertEquals(getName("test#org"), fN1.insert(0, "test")); - assertEquals(getName("jvalue#test#org"), fN2.insert(1, "test")); - assertEquals(getName("www#test#jvalue#org"), fN3.insert(1, "test")); + assertEquals(getName("test"), ne.insert(0, "test")); + assertEquals(getName("test#"), n0.insert(0, "test")); + assertEquals(getName("test#org"), n1.insert(0, "test")); + assertEquals(getName("jvalue#test#org"), n2.insert(1, "test")); + assertEquals(getName("www#test#jvalue#org"), n3.insert(1, "test")); } catch (IndexOutOfBoundsException iie) { fail("insert() rejected valid index"); }; @@ -218,23 +224,24 @@ public void testInsert2() { @Test public void testIsEmpty() { - assertTrue(fN0.isEmpty()); - assertTrue(!fN1.isEmpty()); - assertTrue(!fN2.isEmpty()); - assertTrue(!fN3.isEmpty()); + assertTrue(!n0.isEmpty()); + assertTrue(!n1.isEmpty()); + assertTrue(!n2.isEmpty()); + assertTrue(!n3.isEmpty()); } @Test(expected = IndexOutOfBoundsException.class) public void testGetLastComponent0() { - fN0.getLastComponent(); + ne.getLastComponent(); } @Test public void testGetLastComponent1() { try { - assertEquals("org", fN1.getLastComponent()); - assertEquals("org", fN2.getLastComponent()); - assertEquals("org", fN3.getLastComponent()); + assertEquals("", n0.getLastComponent()); + assertEquals("org", n1.getLastComponent()); + assertEquals("org", n2.getLastComponent()); + assertEquals("org", n3.getLastComponent()); } catch (IndexOutOfBoundsException iie) { fail("getLastComponent() rejected valid index"); } @@ -242,40 +249,42 @@ public void testGetLastComponent1() { @Test public void testGetNoComponent() { - assertEquals(0, fN0.getNoComponents()); - assertEquals(1, fN1.getNoComponents()); - assertEquals(2, fN2.getNoComponents()); - assertEquals(3, fN3.getNoComponents()); + assertEquals(0, ne.getNoComponents()); + assertEquals(1, n0.getNoComponents()); + assertEquals(1, n1.getNoComponents()); + assertEquals(2, n2.getNoComponents()); + assertEquals(3, n3.getNoComponents()); } @Test public void testPrepend() { - assertEquals(getName("org"), fN0.prepend("org")); - assertEquals(getName("jvalue#org"), fN1.prepend("jvalue")); - assertEquals(getName("www#jvalue#org"), fN2.prepend("www")); + assertEquals(getName("org"), ne.prepend("org")); + assertEquals(getName("org#"), n0.prepend("org")); + assertEquals(getName("jvalue#org"), n1.prepend("jvalue")); + assertEquals(getName("www#jvalue#org"), n2.prepend("www")); } @Test(expected = IndexOutOfBoundsException.class) public void testRemove0() { - fN0.remove(1); + n0.remove(1); } @Test(expected = IndexOutOfBoundsException.class) public void testRemove1() { - fN3.remove(7); + n3.remove(7); } @Test(expected = IndexOutOfBoundsException.class) public void testRemove2() { - fN2.remove(-1); + n2.remove(-1); } @Test public void testRemove3() { try { - assertEquals(getName(""), fN1.remove(0)); - assertEquals(getName("jvalue"), fN2.remove(1)); - assertEquals(getName("www#jvalue"), fN3.remove(2)); + assertEquals(getName(""), n1.remove(0)); + assertEquals(getName("jvalue"), n2.remove(1)); + assertEquals(getName("www#jvalue"), n3.remove(2)); } catch (IndexOutOfBoundsException iie) { fail("remove() rejected valid index"); } @@ -283,26 +292,26 @@ public void testRemove3() { @Test(expected = IndexOutOfBoundsException.class) public void testReplace0() { - fN0.replace(0, "test"); + ne.replace(0, "test"); } @Test(expected = IndexOutOfBoundsException.class) public void testReplace1() { - fN1.replace(-1, "test"); + n1.replace(-1, "test"); } @Test(expected = IndexOutOfBoundsException.class) public void testReplace2() { - fN2.replace(3, "test"); + n2.replace(3, "test"); } @Test public void testReplace3() { try { - assertEquals(getName("com"), fN1.replace(0, "com")); - assertEquals(getName("jvalue#com"), fN2.replace(1, "com")); - assertEquals(getName("test#jvalue#org"), fN3.replace(0, "test")); + assertEquals(getName("com"), n1.replace(0, "com")); + assertEquals(getName("jvalue#com"), n2.replace(1, "com")); + assertEquals(getName("test#jvalue#org"), n3.replace(0, "test")); } catch (IndexOutOfBoundsException iie) { fail("replace() rejected valid index"); }; @@ -329,4 +338,11 @@ protected Name getName(String name, char delimiter, char escape) { return new Name(name, delimiter, escape); } + /** + * + */ + protected Name getEmptyName() { + return Name.EMPTY_NAME; + } + }