From d33aacbad1a6eb6f17d8d03cb2e0ebf89a299965 Mon Sep 17 00:00:00 2001 From: Dirk Riehle Date: Mon, 2 Aug 2010 15:37:37 +0200 Subject: [PATCH] First commit of JValue revamp. --- .classpath | 8 + .project | 17 ++ CONTRIBUTIONS.txt | 3 + LICENSE.txt | 202 +++++++++++++++ src/org/jvalue/Name.java | 467 ++++++++++++++++++++++++++++++++++ src/org/jvalue/NameUtil.java | 143 +++++++++++ test/org/jvalue/NameTest.java | 329 ++++++++++++++++++++++++ 7 files changed, 1169 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 CONTRIBUTIONS.txt create mode 100644 LICENSE.txt create mode 100644 src/org/jvalue/Name.java create mode 100644 src/org/jvalue/NameUtil.java create mode 100644 test/org/jvalue/NameTest.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..b0e3db7 --- /dev/null +++ b/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..b47a905 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + JValue + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/CONTRIBUTIONS.txt b/CONTRIBUTIONS.txt new file mode 100644 index 0000000..b1d3c53 --- /dev/null +++ b/CONTRIBUTIONS.txt @@ -0,0 +1,3 @@ +== JValue 0.7 == + +* StringName adapted from JValue 0.6.12 to become Name (Dirk Riehle, Wolf Siberski) \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/src/org/jvalue/Name.java b/src/org/jvalue/Name.java new file mode 100644 index 0000000..74eb693 --- /dev/null +++ b/src/org/jvalue/Name.java @@ -0,0 +1,467 @@ +package org.jvalue; + +/** + * 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. + */ + +import java.io.*; +import java.util.*; + +/** + * A Name has a sequence of name components. A name component is a string. + * + * For a specific sub-type of Name, the number of name components may be + * limited and subject to access restrictions. Also, the name components + * may have special meaning, depending on the type of Name. + * + * Each Name can be represented using a common format, based on delimiter + * and control chars. In this format, the name components of the Name are + * separated by special delimiter chars. Control chars, including the + * delimiter char, are escaped using a special escape char. Default values + * for the delimiter and escape char are defined below, but typically + * method should accept arguments that override the default values. + * + * Also, each specific type of Name should have constructors that take + * strings formatted using this scheme as an input. + */ +public class Name implements Serializable { + + /** + * + */ + private static final long serialVersionUID = -1156016700271883281L; + + /** + * + */ + public static final char DEFAULT_DELIMITER_CHAR = '#'; + public static final char DEFAULT_ESCAPE_CHAR = '\\'; + + /** + * Represents name as a single string + */ + protected String name; + + /** + * Saves number of components for performance reasons + * A value of -1 implies that value has not yet been counted + */ + protected int noComponents =-1; + + /** + * @param name String representation of name with default component delimiter + */ + public Name(String name) { + this(name, DEFAULT_DELIMITER_CHAR); + } + + /** + * @param name String representation of name + * @param delimiter Component delimiter to use when parsing string + */ + public Name(String name, char delimiter) { + this(name, delimiter, DEFAULT_ESCAPE_CHAR); + } + + /** + * @param components List of string components to create name from + */ + public Name(List components) { + this(NameUtil.getMaskedString(components, DEFAULT_DELIMITER_CHAR, DEFAULT_ESCAPE_CHAR)); + } + + /** + * @param name String representation of name + * @param delimiter Component delimiter to use when parsing string + * @param escape Escape character to use when parsing string + */ + public Name(String newName, char delimiter, char escape) { + if ((delimiter != getDelimiterChar()) || (escape != getEscapeChar())) { + newName = switchDelEscScheme(newName, delimiter, escape, getDelimiterChar(), getEscapeChar()); + } + + name = newName; + } + + /** + * + */ + @Override + public String toString() { + return name; + } + + /** + * + */ + @Override + public boolean equals(Object otherObject) { + if ((otherObject != null) && (otherObject instanceof Name)) { + Name otherName = (Name) otherObject; + return name.equals(otherName.name); + } + + return false; + } + + /** + * + */ + @Override + public int hashCode() { + return name.hashCode(); + } + + /** + * + */ + public String asString() { + int size = name.length(); + StringBuilder result = new StringBuilder(size); + + boolean escapeOn = false; + char escape = getEscapeChar(); + for (int i = 0; i < size; i++) { + char c = name.charAt(i); + if (escapeOn) { + result.append(c); + escapeOn = false; + } else if (c == escape) { + escapeOn = true; + } else { + result.append(c); + } + } + + return result.toString(); + } + + /** + **/ + public String asString(char delimiter) { + if (delimiter == getDelimiterChar()) { + return asString(); + } + + int size = name.length(); + StringBuilder result = new StringBuilder(size); + + boolean escapeOn = false; + char escape = getEscapeChar(); + char oldDelimiter = getDelimiterChar(); + for (int i = 0; i < size; i++) { + char c = name.charAt(i); + if (escapeOn) { + result.append(c); + escapeOn = false; + } else if (c == oldDelimiter) { + result.append(delimiter); + } else if (c == escape) { + escapeOn = true; + } else { + result.append(c); + } + } + + return result.toString(); + } + + /** + * + */ + public String[] asStringArray() { + int max = getNoComponents(); + String[] sa = new String[max]; + for (int i = 0; i < max; i++) { + sa[i] = getComponent(i); + } + return sa; + } + + /** + **/ + public char getDelimiterChar() { + return DEFAULT_DELIMITER_CHAR; + } + + /** + **/ + public char getEscapeChar() { + return DEFAULT_ESCAPE_CHAR; + } + + /** + * + */ + public boolean isEmpty() { + return name.equals(""); + } + + /** + * + */ + public Name getContextName() { + assertIsValidIndex(getNoComponents() - 1); + if (getNoComponents() == 1) { + return new Name(""); + } + + int endPos = getIndexOfEndOfComponent(getNoComponents() - 2); + return new Name(name.substring(0, endPos)); + } + + /** + * + */ + public int getNoComponents() { + if (noComponents == -1) { + if (!isEmpty()) { + noComponents = NameUtil.getNoChars(name, getDelimiterChar(), getEscapeChar()) + 1; + } else { + noComponents = 0; + } + } + return noComponents; + } + + /** + * + */ + public Iterable components() { + return Arrays.asList(asStringArray()); // TODO + } + + /** + * + */ + public boolean hasComponent(int index) { + return (0 <= index) && (index < getNoComponents()); + } + + /** + * + */ + public String getComponent(int index) { + assertIsValidIndex(index); + return doGetComponent(index); + } + + /** + * + */ + public String getFirstComponent() { + return getComponent(0); + } + + /** + * + */ + public String getLastComponent() { + assertIsValidIndex(getNoComponents() - 1); + return getComponent(getNoComponents() - 1); + } + + /** + * + */ + public Name append(String component) { + return doInsert(getNoComponents(), component); + } + + /** + * + */ + public Name insert(int index, String component) { + assertIsValidIndex(index, getNoComponents()+1); + return doInsert(index, component); + } + + /** + * + */ + public Name prepend(String component) { + if (getNoComponents() != 0) { + return doInsert(0, component); + } else { + return new Name(component); + } + } + + /** + * + */ + public Name remove(int index) { + assertIsValidIndex(index); + return doRemove(index); + } + + /** + * + */ + public Name replace(int index, String component) { + assertIsValidIndex(index); + return doReplace(index, component); + } + + /** + * Returns -1 if index is not available. + */ + protected int getIndexOfStartOfComponent(int index) { + if (index == 0) { + return 0; + } + + int pos = NameUtil.findChar(name, 0, getDelimiterChar(), getEscapeChar(), index); + if (pos == -1) { + return -1; + } + + return pos+1; + } + + /** + * Returns -1 if index is not available. + */ + protected int getIndexOfEndOfComponent(int index) { + if (index == (getNoComponents() - 1)) { + return name.length(); + } + + return getIndexOfStartOfComponent(index + 1) - 1; + } + + /** + * + */ + public Name getDefaultValue() { + return new Name(""); + } + + /** + * @return A String masked with new delimiter and escape chars. + */ + protected String switchDelEscScheme(String name, char fromDel, char fromEsc, char toDel, char toEsc) { + StringBuilder result = new StringBuilder(); + int length = name.length(); + if (length != 0) { + for (int end = 0; end < length; end++) { + int startPos = end; + String component = ""; + if (startPos < name.length()) { + int endPos = NameUtil.findChar(name, startPos, fromDel, fromEsc); + if (endPos == -1) { + endPos = name.length(); + } + end = endPos; + + component = name.substring(startPos, endPos); + component = NameUtil.getUnmaskedString(component, fromEsc); + } + + result.append(NameUtil.getMaskedString(component, toDel, toEsc)); + result.append(toDel); + } + result.setLength(result.length() - 1); + } + + return result.toString(); + } + + /** + * Asserts that given index is valid + */ + protected void assertIsValidIndex(int i) throws IndexOutOfBoundsException { + assertIsValidIndex(i, getNoComponents()); + } + + /** +  * Asserts that given index is valid +  */ + protected void assertIsValidIndex(int i, int upperLimit) throws IndexOutOfBoundsException { + if ((i < 0) || (i >= upperLimit)) { + String msg = String.valueOf(i) + "(of " + String.valueOf(getNoComponents()) +")"; + throw new IndexOutOfBoundsException(msg); + } + } + + /** + * + */ + protected String doGetComponent(int i) { + return NameUtil.getUnmaskedString(doGetMaskedComponent(i), getEscapeChar()); + } + + /** + * + */ + protected Name doInsert(int index, String component) { + component = NameUtil.getMaskedString(component, getDelimiterChar(), getEscapeChar()); + + if (getNoComponents() == 0) { + return new Name(component); + } + + if (index == getNoComponents()) { + return new Name(name + getDelimiterChar() + component); + } + + component = component + getDelimiterChar(); + int pos = getIndexOfStartOfComponent(index); + StringBuilder sb = new StringBuilder(name); + sb.insert(pos, component); + + return new Name(sb.toString()); + } + + /** + **/ + protected Name doRemove(int index) { + int startPos = getIndexOfStartOfComponent(index); + int endPos = getIndexOfEndOfComponent(index); + StringBuilder sb = new StringBuilder(); + + if (index == 0) { + if (getNoComponents() != 1) { + sb.append(name.substring(endPos + 1, name.length())); + } + } else if (index == (getNoComponents() - 1)) { + sb.append(name.substring(0, startPos - 1)); + } else { + sb.append(name.substring(0, startPos)); + sb.append(name.substring(endPos + 1, name.length())); + } + + return new Name(sb.toString()); + } + + /** + **/ + protected Name doReplace(int index, String component) { + component = NameUtil.getMaskedString(component, getDelimiterChar(), getEscapeChar()); + int startPos = getIndexOfStartOfComponent(index); + int endPos = getIndexOfEndOfComponent(index); + StringBuilder sb = new StringBuilder(name.substring(0, startPos)); + sb.append(component); + sb.append(name.substring(endPos, name.length())); + return new Name(sb.toString()); + } + + /** + **/ + protected String doGetMaskedComponent(int i) { + int startPos = getIndexOfStartOfComponent(i); + int endPos = getIndexOfEndOfComponent(i); + return name.substring(startPos, endPos); + } + +} diff --git a/src/org/jvalue/NameUtil.java b/src/org/jvalue/NameUtil.java new file mode 100644 index 0000000..c6c71f7 --- /dev/null +++ b/src/org/jvalue/NameUtil.java @@ -0,0 +1,143 @@ +package org.jvalue; + +/** + * 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. + */ + +import java.util.Iterator; +import java.util.List; + +/** + * Utility class for parsing and masking Strings. + */ +public final class NameUtil { + + /** + * Returns -1 if searched-for char is not found. + */ + public static int findChar(String name, int startPos, char searched, char escape) { + boolean eFlag = false; + for (int i = startPos; i < name.length(); i++) { + char c = name.charAt(i); + if (!eFlag && c == escape) { + eFlag = true; + continue; + } + if (!eFlag && c == searched) { + return i; + } + eFlag = false; + } + return -1; + } + + /** + * Returns -1 if searched-for char is not found. + */ + public static int findChar(String name, int startPos, char searched, char escape, int noOccurrences) { + if (noOccurrences == 0) { + return startPos; + } + for (int no = 0; no < noOccurrences; no++) { + startPos = findChar(name, startPos, searched, escape); + if (startPos == -1) { + return -1; + }; + startPos++; + } + return --startPos; + } + + /** + * Returns number of occurrences of searched char in name. + */ + public static int getNoChars(String name, char searched, char escape) { + int no = 0; // = number of occurrences of searched + boolean eFlag = false; // flags escaped char + final int size = name.length(); + for (int i = 0; i < size; i++) { + if (eFlag) { + eFlag = false; + continue; + } + if (name.charAt(i) == escape) { + eFlag = true; + } + if (name.charAt(i) == searched) { + no++; + } + } + return no; + } + + /** + * Returns name argument stripped from masking control chars. + */ + public static String getUnmaskedString(String name, char escapeChar) { + boolean eFlag = false; // flags escape char + StringBuilder result = new StringBuilder(); + final int size = name.length(); + for (int i = 0; i < size; i++) { + char c = name.charAt(i); + if (!eFlag && (c == escapeChar)) { + eFlag = true; + continue; + } + result.append(c); + eFlag = false; + } + return result.toString(); + } + + /** + * Returns name argument masked using control chars. + */ + public static String getMaskedString(String name, char delimiter, char escape) { + StringBuilder result = new StringBuilder(); + final int size = name.length(); + for (int i = 0; i < size; i++) { + char c = name.charAt(i); + if (c == delimiter) { + result.append(escape); + } + if (c == escape) { + result.append(escape); + } + result.append(c); + } + + return result.toString(); + } + + /** + * @param components + * @return + */ + public static String getMaskedString (List components, char delimiter, char escape) { + String ds = String.valueOf(delimiter); + StringBuilder sb = new StringBuilder(); + Iterator i = components.iterator(); + while(i.hasNext()) { + String component = i.next(); + String maskedComponent = NameUtil.getMaskedString(component, delimiter, escape); + sb.append(maskedComponent); + if (!i.hasNext()) { + sb.append(ds); + } + } + + return sb.toString(); + } + +} + diff --git a/test/org/jvalue/NameTest.java b/test/org/jvalue/NameTest.java new file mode 100644 index 0000000..2f0d2a0 --- /dev/null +++ b/test/org/jvalue/NameTest.java @@ -0,0 +1,329 @@ +package org.jvalue; + +/** + * 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. + */ + +import org.junit.*; +import static org.junit.Assert.*; +import java.util.*; + +/** + * TestCase for Name. + */ +public class NameTest { + + /** + * + */ + protected Name fN0; // ("") + protected Name fN1; // ("org", '.') + protected Name fN2; // ("jvalue.org", '.') + protected Name fN3; // ("www.jvalue.org", '.') + + @Before + public void setUp() { + fN0 = getName(""); + + fN1 = getName("org", '.'); + fN2 = getName("jvalue.org", '.'); + fN3 = 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")); + } + + @Test + public void testToString() { + String del0s = ""; + assertEquals(del0s, fN0.toString()); + + String del1s = "org"; + assertEquals(del1s, fN1.toString()); + + char del2 = fN2.getDelimiterChar(); + String del2s = "jvalue" + del2 + "org"; + assertEquals(del2s, fN2.toString()); + + char del3 = fN3.getDelimiterChar(); + String del3s = "www" + del3 + "jvalue" + del3 + "org"; + assertEquals(del3s, fN3.toString()); + } + + @Test + public void testDefaultValue() { + Name defaultValue = getName(""); + assertEquals(defaultValue, fN0.getDefaultValue()); + assertEquals(defaultValue, fN1.getDefaultValue()); + } + + @Test + public void testAppend() { + assertEquals(fN1, getName("").append("org")); + assertEquals(fN2, getName("jvalue").append("org")); + assertEquals(fN3, 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('*')); + } + + @Test + public void testAsStringArray() { + String[] c0 = fN0.asStringArray(); + assertEquals(0, c0.length); + + String[] c1 = fN1.asStringArray(); + assertEquals(1, c1.length); + assertEquals("org", c1[0]); + + String[] c2 = fN2.asStringArray(); + assertEquals(2, c2.length); + assertEquals("jvalue", c2[0]); + assertEquals("org", c2[1]); + + String[] c3 = fN3.asStringArray(); + assertEquals(3, c3.length); + assertEquals("www", c3[0]); + assertEquals("jvalue", c3[1]); + assertEquals("org", c3[2]); + } + + @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)); + } catch (IndexOutOfBoundsException iie) { + fail("getComponent() threw exception on valid index"); + } + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetComponent0() { + fN0.getComponent(1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetComponent1() { + fN1.getComponent(-1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetComponent2() { + fN3.getComponent(17); + } + + @Test + public void testGetComponents() { + Iterator ni = fN3.components().iterator(); + for (int i = 0; i < fN3.getNoComponents(); i++) { + try { + assertEquals(ni.next(), fN3.getComponent(i)); + } catch (IndexOutOfBoundsException iie) { + fail("getComponent() throw exception on valid index"); + } + } + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetContextName0() { + fN0.getContextName(); + } + + @Test + public void testGetContextName1() { + try { + assertEquals(getName(""), fN1.getContextName()); + assertEquals(getName("jvalue"), fN2.getContextName()); + assertEquals(getName("www#jvalue"), fN3.getContextName()); + } catch (IndexOutOfBoundsException nioob) { + fail("getContextName of non-empty Name threw exception"); + } + } + + @Test + public void testDelEscScheme() { + char td = '*'; + char te = '&'; + + Name gn3 = getName("w\\\\ww#jva\\#lue#or\\#\\\\\\#g"); + + assertEquals("w\\ww jva#lue or#\\#g", gn3.asString(' ')); + assertEquals(getName("w\\ww*jva#lue*or#\\#g", td, te), gn3); + + Name gn4 = getName("w&ww#jva*lue#or*&*g"); + + assertEquals(getName("w&&ww*jva&*lue*or&*&&&*g", td, te), gn4); + } + + @Test + public void testGetFirstComponent() { + try { + assertEquals("org", fN1.getFirstComponent()); + assertEquals("jvalue", fN2.getFirstComponent()); + assertEquals("www", fN3.getFirstComponent()); + } catch (IndexOutOfBoundsException iie) { + fail("getFirstComponent with first name threw exception"); + } + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testInsert0() { + fN0.insert(2, "test"); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testInsert1() { + fN3.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")); + } catch (IndexOutOfBoundsException iie) { + fail("insert() rejected valid index"); + }; + } + + @Test + public void testIsEmpty() { + assertTrue(fN0.isEmpty()); + assertTrue(!fN1.isEmpty()); + assertTrue(!fN2.isEmpty()); + assertTrue(!fN3.isEmpty()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetLastComponent0() { + fN0.getLastComponent(); + } + + @Test + public void testGetLastComponent1() { + try { + assertEquals("org", fN1.getLastComponent()); + assertEquals("org", fN2.getLastComponent()); + assertEquals("org", fN3.getLastComponent()); + } catch (IndexOutOfBoundsException iie) { + fail("getLastComponent() rejected valid index"); + } + } + + @Test + public void testGetNoComponent() { + assertEquals(0, fN0.getNoComponents()); + assertEquals(1, fN1.getNoComponents()); + assertEquals(2, fN2.getNoComponents()); + assertEquals(3, fN3.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")); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemove0() { + fN0.remove(1); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemove1() { + fN3.remove(7); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemove2() { + fN2.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)); + } catch (IndexOutOfBoundsException iie) { + fail("remove() rejected valid index"); + } + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testReplace0() { + fN0.replace(0, "test"); + } + + + @Test(expected = IndexOutOfBoundsException.class) + public void testReplace1() { + fN1.replace(-1, "test"); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testReplace2() { + fN2.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")); + } catch (IndexOutOfBoundsException iie) { + fail("replace() rejected valid index"); + }; + } + + /** + * + */ + protected Name getName(String name) { + return getName(name, Name.DEFAULT_DELIMITER_CHAR); + } + + /** + * + */ + protected Name getName(String name, char delimiter) { + return getName(name, delimiter, Name.DEFAULT_ESCAPE_CHAR); + } + + /** + * + */ + protected Name getName(String name, char delimiter, char escape) { + return new Name(name, delimiter, escape); + } + +}