001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.xml;
023:
024: import junit.framework.TestCase;
025:
026: import org.jboss.xb.binding.Util;
027:
028: /**
029: * Tests XML names to various Java identifiers conversion.
030: *
031: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
032: * @version <tt>$Revision: 57211 $</tt>
033: */
034: public class XMLNameToJavaIdentifierUnitTestCase extends TestCase {
035: private static final String[] xmlNames = new String[] {
036: "mixedCaseName", "Answer42", "name-with-dashes",
037: "other_punct-chars", "one2three",
038: "__-invalids-at-the-end---", "ID", "iD" };
039:
040: private static final String[] clsNames = new String[] {
041: "MixedCaseName", "Answer42", "NameWithDashes",
042: "OtherPunctChars", "One2Three", "InvalidsAtTheEnd", "ID",
043: "ID" };
044:
045: private static final String[] clsNamesWithLowLines = new String[] {
046: "MixedCaseName", "Answer42", "NameWithDashes",
047: "Other_PunctChars", "One2Three", "__InvalidsAtTheEnd",
048: "ID", "ID" };
049:
050: private static final String[] fieldNames = new String[] {
051: "mixedCaseName", "answer42", "nameWithDashes",
052: "otherPunctChars", "one2Three", "invalidsAtTheEnd", "ID",
053: "iD" };
054:
055: private static final String[] getNames = new String[] {
056: "getMixedCaseName", "getAnswer42", "getNameWithDashes",
057: "getOtherPunctChars", "getOne2Three",
058: "getInvalidsAtTheEnd", "getID", "getID" };
059:
060: private static final String[] setNames = new String[] {
061: "setMixedCaseName", "setAnswer42", "setNameWithDashes",
062: "setOtherPunctChars", "setOne2Three",
063: "setInvalidsAtTheEnd", "setID", "setID" };
064:
065: private static final String[] constNames = new String[] {
066: "MIXED_CASE_NAME", "ANSWER_42", "NAME_WITH_DASHES",
067: "OTHER_PUNCT_CHARS", "ONE_2_THREE", "INVALIDS_AT_THE_END",
068: "ID", "I_D" };
069:
070: public XMLNameToJavaIdentifierUnitTestCase(String localName) {
071: super (localName);
072: }
073:
074: public void testXmlNameToClassName() throws Exception {
075: for (int i = 0; i < xmlNames.length; ++i) {
076: String clsName = Util.xmlNameToClassName(xmlNames[i], true);
077: assertEquals(clsNames[i], clsName);
078: }
079: }
080:
081: public void testXmlNameToClassNameWithLowLines() throws Exception {
082: for (int i = 0; i < xmlNames.length; ++i) {
083: String clsName = Util
084: .xmlNameToClassName(xmlNames[i], false);
085: assertEquals(clsNamesWithLowLines[i], clsName);
086: }
087: }
088:
089: public void testXmlNameToGetMethodName() throws Exception {
090: for (int i = 0; i < xmlNames.length; ++i) {
091: String clsName = Util.xmlNameToGetMethodName(xmlNames[i],
092: true);
093: assertEquals(getNames[i], clsName);
094: }
095: }
096:
097: public void testXmlNameToSetMethodName() throws Exception {
098: for (int i = 0; i < xmlNames.length; ++i) {
099: String clsName = Util.xmlNameToSetMethodName(xmlNames[i],
100: true);
101: assertEquals(setNames[i], clsName);
102: }
103: }
104:
105: public void testXmlNameToConstantName() throws Exception {
106: for (int i = 0; i < xmlNames.length; ++i) {
107: String constName = Util.xmlNameToConstantName(xmlNames[i]);
108: assertEquals(constNames[i], constName);
109: }
110: }
111:
112: public void testXmlNameToFieldName() throws Exception {
113: for (int i = 0; i < xmlNames.length; ++i) {
114: String fieldName = Util.xmlNameToFieldName(xmlNames[i],
115: true);
116: assertEquals(fieldNames[i], fieldName);
117: }
118: }
119:
120: public void testXmlNamespaceToJavaPackage() throws Exception {
121: String[] ns = new String[] {
122: "http://www.acme.com/go/espeak.xsd",
123: "http://example.org/ns/books/",
124: "http://www.w3.org/2001/XMLSchema",
125: "http://example.org/", "http://example.org",
126: "http://www.kloop.com.ua/xsd-schemas/schema1.xsd" };
127:
128: String[] pkg = new String[] { "com.acme.go.espeak",
129: "org.example.ns.books", "org.w3._2001.xmlschema",
130: "org.example", "org.example",
131: "ua.com.kloop.xsd_schemas.schema1" };
132:
133: for (int i = 0; i < ns.length; ++i) {
134: assertEquals(pkg[i], Util.xmlNamespaceToJavaPackage(ns[i]));
135: }
136: }
137: }
|