001: package org.andromda.utils;
002:
003: import junit.framework.TestCase;
004: import org.andromda.utils.StringUtilsHelper;
005:
006: /**
007: * JUnit test for {@link org.andromda.utils.StringUtilsHelper}
008: *
009: * @author Wouter Zoons
010: */
011: public class StringUtilsHelperTest extends TestCase {
012: public StringUtilsHelperTest(String name) {
013: super (name);
014: }
015:
016: public void testReplaceSuffix() {
017: final String[][] fixture = new String[][] {
018: new String[] { "EntityHibernate", "Hibernate", "EJB",
019: "EntityEJB" },
020: new String[] { "EntityHibernate", "Hibernate",
021: "Hibernate", "EntityHibernate" },
022: new String[] { "EntityHibernate", "hibernate", "EJB",
023: "EntityHibernate" },
024: new String[] { "EntityHibernate", "Entity", "EJB",
025: "EntityHibernate" },
026: new String[] { "EntityHibernate", "ernate", "qwErty",
027: "EntityHibqwErty" } };
028:
029: for (int i = 0; i < fixture.length; i++) {
030: String[] strings = fixture[i];
031: assertEquals(StringUtilsHelper.replaceSuffix(strings[0],
032: strings[1], strings[2]), strings[3]);
033: }
034: }
035:
036: public void testUpperCamelCaseName() {
037: final String[][] fixture = new String[][] {
038: new String[] { "ejb", "Ejb" },
039: new String[] { "EJB", "EJB" },
040: new String[] { "an EJB class", "AnEJBClass" },
041: new String[] { "an EJB Class", "AnEJBClass" },
042: new String[] { "HibernateEntity", "HibernateEntity" },
043: new String[] { "Hibernate Entity", "HibernateEntity" },
044: new String[] {
045: "Welcome... to the jungle (Guns \'n\' Roses)",
046: "WelcomeToTheJungleGunsNRoses" } };
047:
048: for (int i = 0; i < fixture.length; i++) {
049: String[] strings = fixture[i];
050: assertEquals(StringUtilsHelper
051: .upperCamelCaseName(strings[0]), strings[1]);
052: }
053: }
054:
055: public void testLowerCamelCaseName() {
056: final String[][] fixture = new String[][] {
057: new String[] { "ejb", "ejb" },
058: new String[] { "EJB", "eJB" },
059: new String[] { "an EJB class", "anEJBClass" },
060: new String[] { "an EJB Class", "anEJBClass" },
061: new String[] { "HibernateEntity", "hibernateEntity" },
062: new String[] { "Hibernate Entity", "hibernateEntity" },
063: new String[] {
064: "Welcome... to the jungle (Guns \'n\' Roses)",
065: "welcomeToTheJungleGunsNRoses" } };
066:
067: for (int i = 0; i < fixture.length; i++) {
068: String[] strings = fixture[i];
069: assertEquals(StringUtilsHelper
070: .lowerCamelCaseName(strings[0]), strings[1]);
071: }
072: }
073:
074: public void testToResourceMessageKey() {
075: final String[][] fixture = new String[][] {
076: new String[] { "ejb", "ejb" },
077: new String[] { "EJB", "ejb" },
078: new String[] { "an EJB class", "an.ejb.class" },
079: new String[] { "an EJB Class", "an.ejb.class" },
080: new String[] { "HibernateEntity", "hibernate.entity" },
081: new String[] { "Hibernate Entity", "hibernate.entity" } };
082:
083: for (int i = 0; i < fixture.length; i++) {
084: String[] strings = fixture[i];
085: assertEquals(StringUtilsHelper
086: .toResourceMessageKey(strings[0]), strings[1]);
087: }
088: }
089:
090: public void testToPhrase() {
091: final String[][] fixture = new String[][] {
092: new String[] { "ejb", "Ejb" },
093: new String[] { "EJB", "EJB" },
094: new String[] { "an EJB class", "An EJB class" },
095: new String[] { "an EJB Class", "An EJB Class" },
096: new String[] { "HibernateEntity", "Hibernate Entity" },
097: new String[] { "Hibernate Entity", "Hibernate Entity" } };
098:
099: for (int i = 0; i < fixture.length; i++) {
100: String[] strings = fixture[i];
101: assertEquals(StringUtilsHelper.toPhrase(strings[0]),
102: strings[1]);
103: }
104: }
105:
106: public void testPrefixWithAPredicate() {
107: final String[][] fixture = new String[][] {
108: new String[] { "a", "an a" },
109: new String[] { "b", "a b" },
110: new String[] { "c", "a c" },
111: new String[] { "d", "a d" },
112: new String[] { "e", "an e" },
113: new String[] { "f", "a f" },
114: new String[] { "g", "a g" },
115: new String[] { "h", "a h" },
116: new String[] { "i", "an i" },
117: new String[] { "j", "a j" },
118: new String[] { "k", "a k" },
119: new String[] { "l", "a l" },
120: new String[] { "m", "a m" },
121: new String[] { "n", "a n" },
122: new String[] { "o", "an o" },
123: new String[] { "p", "a p" },
124: new String[] { "q", "a q" },
125: new String[] { "r", "a r" },
126: new String[] { "s", "a s" },
127: new String[] { "t", "a t" },
128: new String[] { "u", "a u" },
129: new String[] { "v", "a v" },
130: new String[] { "w", "a w" },
131: new String[] { "x", "a x" },
132: new String[] { "y", "a y" },
133: new String[] { "z", "a z" },
134: new String[] { "elephant", "an elephant" },
135: new String[] { "cat", "a cat" },
136: new String[] { "horse", "a horse" },
137: new String[] { "building", "a building" } };
138:
139: for (int i = 0; i < fixture.length; i++) {
140: String[] strings = fixture[i];
141: assertEquals(StringUtilsHelper
142: .prefixWithAPredicate(strings[0]), strings[1]);
143: }
144: }
145:
146: public void testToSingleLine() {
147: final String[][] fixture = new String[][] {
148: new String[] { "", "" },
149: new String[] { "", "" },
150: new String[] { " ", "" },
151: new String[] { "\n", "" },
152: new String[] { " \t ", "" },
153: new String[] { "null", "null" },
154: new String[] { "\r\ntest \nthis\n", "test this" },
155: new String[] { "word", "word" },
156: new String[] { " horse ", "horse" },
157: new String[] { " clean me up ", "clean me up" },
158: new String[] { "\n\n\r\n\n\n ", "" },
159: new String[] { "This is\na multiline\n\n?",
160: "This is a multiline ?" },
161: new String[] { "This is \na multiline\n\n?",
162: "This is a multiline ?" } };
163:
164: for (int i = 0; i < fixture.length; i++) {
165: String[] strings = fixture[i];
166: assertEquals(StringUtilsHelper.toSingleLine(strings[0]),
167: strings[1]);
168: }
169: }
170:
171: public void testRemoveLastOccurence() {
172: final String testString = "This is a test string ending with a comma";
173: final String someString = testString + ", ";
174: assertEquals(StringUtilsHelper.removeLastOccurrence(someString,
175: ","), testString + " ");
176: }
177:
178: public void testPluralize() {
179: final String[][] fixture = new String[][] {
180: new String[] { null, "" }, new String[] { "", "" },
181: new String[] { " ", "" },
182: new String[] { "key", "keys" },
183: new String[] { "word", "words" },
184: new String[] { "property", "properties" },
185: new String[] { "bus", "buses" },
186: new String[] { "cross", "crosses" },
187: new String[] { "lackey", "lackeys" },
188: new String[] { "noun", "nouns" },
189: new String[] { "knife", "knives" },
190: new String[] { "child", "children" },
191: new String[] { "person", "people" },
192: new String[] { "foot", "feet" },
193: new String[] { "woman", "women" },
194: new String[] { "series", "series" }, };
195:
196: for (int i = 0; i < fixture.length; i++) {
197: String[] strings = fixture[i];
198: assertEquals(StringUtilsHelper.pluralize(strings[0]),
199: strings[1]);
200: }
201: }
202:
203: public void testSeparate() {
204: final String[][] fixture = new String[][] {
205: new String[] {
206: "Transfer from a Critical Access Hospital",
207: "Transfer_from_a_Critical_Access_Hospital" },
208: new String[] { "UNDERSCORE_TEST", "UNDERSCORE_TEST" } };
209:
210: for (int i = 0; i < fixture.length; i++) {
211: String[] strings = fixture[i];
212: assertEquals(StringUtilsHelper.separate(strings[0], "_"),
213: strings[1]);
214: }
215: }
216:
217: public void testSimpleFormat() {
218: final String newline = StringUtilsHelper.getLineSeparator();
219:
220: final String[][] fixture = new String[][] {
221: new String[] { null, "" },
222: new String[] { "", "" },
223: new String[] { " ", "" },
224: new String[] { "word", "word" },
225: new String[] { "hottentottententoonstelling",
226: "hottentottententoonstelling" },
227: new String[] { "line1\nline2",
228: "line1" + newline + "line2" },
229: new String[] {
230: "testing without any indentation",
231: "testing" + newline + "without" + newline
232: + "any" + newline + "indentation" },
233: new String[] {
234: "do you know the a b c ?",
235: "do" + newline + "you" + newline + "know"
236: + newline + "the a" + newline + "b c ?" },
237: new String[] { "dodo do do doooo",
238: "dodo" + newline + "do do" + newline + "doooo" } };
239:
240: for (int i = 0; i < fixture.length; i++) {
241: String[] strings = fixture[i];
242: assertEquals(StringUtilsHelper.format(strings[0], null, 5,
243: false), strings[1]);
244: }
245: }
246:
247: public void testIndentedFormat() {
248: final String indentation = " * ";
249: final String newline = StringUtilsHelper.getLineSeparator()
250: + indentation;
251:
252: final String[][] fixture = new String[][] {
253: new String[] { "", indentation },
254: new String[] { "one two three",
255: indentation + "one two three" },
256: new String[] {
257: "Sets the <code>thisArgumentIsMissingFromTheActionForm</code> field.\n",
258: indentation
259: + "Sets the <code>thisArgumentIsMissingFromTheActionForm</code>"
260: + newline + "field." } };
261:
262: for (int i = 0; i < fixture.length; i++) {
263: String[] strings = fixture[i];
264: assertEquals(StringUtilsHelper.format(strings[0],
265: indentation, 64, false), strings[1]);
266: }
267: }
268:
269: public void testHtmlFormat() {
270: final String newline = StringUtilsHelper.getLineSeparator();
271:
272: final String[][] fixture = new String[][] { new String[] {
273: "one two three",
274: "<p>" + newline + "one" + newline + "two" + newline
275: + "three" + newline + "</p>" } };
276:
277: for (int i = 0; i < fixture.length; i++) {
278: String[] strings = fixture[i];
279: assertEquals(StringUtilsHelper.format(strings[0], "", 5,
280: true), strings[1]);
281: }
282: }
283: }
|