001: /*
002: ******************************************************************
003: Copyright (c) 200, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit;
038:
039: import junit.framework.TestCase;
040: import junit.framework.TestSuite;
041:
042: /**
043: * JUnit test for Replacement
044: */
045: public class test_Replacement extends TestCase {
046: private Replacement replacement;
047:
048: public void testCharReplacement() {
049: char[] ch = { 'h', 'o', 'a', 'g' };
050: replacement = new Replacement(new char[] { 'o', 'a' },
051: new char[] { 'u' });
052: assertEquals("hug", new String(replacement.replace(ch)));
053:
054: replacement = new Replacement(new char[] { 'g' }, new char[] {
055: 'r', 's', 'e' });
056: assertEquals("hoarse", new String(replacement.replace(ch)));
057: assertEquals("hasReplaceBy", true, replacement.hasReplaceBy());
058: }
059:
060: public void testSimpleString() {
061: replacement = new Replacement("x", "y");
062: // 1st char
063: assertEquals("yen", replacement.replace("xen"));
064: // last char
065: assertEquals("boy", replacement.replace("box"));
066: // not 1st or last
067: assertEquals("aye", replacement.replace("axe"));
068: // no replacement
069: assertEquals("bag", replacement.replace("bag"));
070: // multiple replacements
071: assertEquals("yoyo", replacement.replace("xoxo"));
072: // multiple concurrent replacements
073: assertEquals("yyjykyy", replacement.replace("xxjxkxx"));
074:
075: assertEquals("hasReplaceBy", true, replacement.hasReplaceBy());
076: }
077:
078: public void testComplexString() {
079: replacement = new Replacement(" a whole bunch of words",
080: "some other words altogether");
081: assertEquals("Here aresome other words altogether...",
082: replacement
083: .replace("Here are a whole bunch of words..."));
084:
085: replacement = new Replacement("pp", "p");
086: assertEquals("hapy", replacement.replace("happy"));
087: assertEquals("happy", replacement.replace("happppy"));
088: assertEquals("tap", replacement.replace("tapp"));
089: assertEquals("paw", replacement.replace("ppaw"));
090: }
091:
092: public void testNullReplacebyString() {
093: replacement = new Replacement(" ", null);
094: assertEquals("hasReplaceBy", false, replacement.hasReplaceBy());
095: assertEquals("wedon'twantnowhitespace", replacement
096: .replace("we don't want no whitespace"));
097: }
098:
099: public void testNullReplacebyChars() {
100: replacement = new Replacement(new char[] { 'a' }, null);
101: assertEquals("hasReplaceBy", false, replacement.hasReplaceBy());
102: assertEquals("ntidisestblishmentrinism", replacement
103: .replace("antidisestablishmentarianism"));
104: }
105:
106: public void testEmptyReplaceby() {
107: replacement = new Replacement(new char[] { 'w' }, new char[0]);
108: assertEquals("hasReplaceBy", false, replacement.hasReplaceBy());
109: assertEquals("ibbleobble", replacement.replace("wibblewobble"));
110: }
111:
112: public test_Replacement(String name) {
113: super (name);
114: }
115:
116: public static TestSuite suite() {
117: return new TestSuite(test_Replacement.class);
118: }
119:
120: }
|