/* XBN Java: Generically useful, non-GUI Java code. http://sourceforge.net/projects/xbnjava Copyright (C) 1997-2003, Jeff Epstein All rights reserved. Modifications: No Redistribution in binary form, with or without modifications, are permitted provided that the following conditions are met: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * If modifications are made to source code then this license should indicate that fact in the "Modifications" section above. * Neither the author, nor the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [NOTE: This license contains NO advertising clause.] */ package xbn_junit.string; import xbn.util.XBNTestCase; import xbn.AssertException; import xbn.string.SOBString; import xbn.string.SOBStringBuffer; import xbn.string.UtilSOB; import junit.framework.Test; import junit.framework.TestSuite; import junit.textui.TestRunner; public class JUTUtilSOB extends XBNTestCase { private static final String sHTMF = "Hello there, my friend."; private static final String sES = ""; private UtilSOB uSOB = new UtilSOB(); private SOBString ssES = null; private SOBStringBuffer ssbES = null; private SOBString ssHTMF = null; private SOBStringBuffer ssbHTMF = null; public JUTUtilSOB(String s_name) { super(s_name); } public void test_indexOfWord() { final String sTHERE = "there"; //Bad search-for string iow_assertAX(null, sHTMF, 0, sHTMF.length()); iow_assertAX(sES, sHTMF, 0, sHTMF.length()); //Not letter digit underscore. iow_assertAX(" ", sHTMF, 0, sHTMF.length()); //Bad sob iow_assertAX("xyz", null, 0, sHTMF.length()); iow_assertAX(sES, sES, 0, sHTMF.length()); //No invalid parameters. try { uSOB.indexOfWord(ssHTMF, "abc", 0, sHTMF.length()); xassertTrue(); uSOB.indexOfWord(ssbHTMF, "abc", 0, sHTMF.length()); xassertTrue(); } catch(AssertException ax) { xfail("indexOfWord('" + ssbHTMF + "', 'abc', 0, '" + sHTMF + "'.length()) should not throw an ax: " + ax.toString()); } final String sTHER = "ther"; final String sRIEND = "riend"; //Not found at all iow_assert(-1, -1, -1, -1, "xyz", sHTMF, 0, sHTMF.length()); //Found once, not a word...START //Start of word iow_assert(-1, -1, -1, -1, sTHER, sHTMF, 0, sHTMF.length()); //Start of word at start of bounds (and start of string) iow_assert(-1, -1, -1, -1, "Hell", sHTMF, 0, sHTMF.length()); //Start of word at start of bounds iow_assert(-1, -1, -1, -1, sTHER, sHTMF, 6, sHTMF.length()); //End of word iow_assert(-1, -1, -1, -1, "here", sHTMF, 0, sHTMF.length()); //End of word at end of bounds (and end of string) iow_assert(-1, -1, -1, -1, sRIEND, "my friend", 0, 9); //End of word at end of bounds iow_assert(-1, -1, -1, -1, sRIEND, sHTMF, 0, (sHTMF.length() - 1)); //Neither start nor end of word iow_assert(-1, -1, -1, -1, "her", sHTMF, 0, sHTMF.length()); //Found once, not a word...START String sHTMFTwice = sHTMF + " " + sHTMF; //0 1 2 3 4 //012345678901234567890123456789012345678901234567 //Hello there, my friend. Hello there, my friend. //Found multiple times, not a word...START //End of word iow_assert(-1, -1, -1, -1, "ello", sHTMFTwice, 0, sHTMFTwice.length()); //End of word at end of bounds (and end of string) iow_assert(-1, -1, -1, -1, sRIEND, "friend friend", 0, 13); //End of word at end of bounds. iow_assert(-1, -1, -1, -1, sRIEND, sHTMFTwice, 0, (sHTMFTwice.length() - 1)); //Start of word iow_assert(-1, -1, -1, -1, sTHER, sHTMFTwice, 0, sHTMFTwice.length()); //Start of word at start of bounds (and start of string) iow_assert(-1, -1, -1, -1, "Hell", sHTMFTwice, 0, sHTMFTwice.length()); //Start of word at start of bounds iow_assert(-1, -1, -1, -1, sTHER, sHTMFTwice, 6, sHTMFTwice.length()); //Neither start nor end iow_assert(-1, -1, -1, -1, "ell", sHTMFTwice, 0, sHTMFTwice.length()); //Found multiple times, not a word...END //Word found once...START //Not at start of bounds. iow_assert(6, 6, 6, 6, sTHERE, sHTMF, 0, sHTMF.length()); //Start of bounds. iow_assert(6, 6, 6, 6, sTHERE, sHTMF, 6, sHTMF.length()); //End of bounds. iow_assert(6, 6, 6, 6, sTHERE, sHTMF, 0, (6 + 5)); //Start and end of bounds. iow_assert(6, 6, 6, 6, sTHERE, sHTMF, 6, (6 + 5)); //Start and end of bounds, exceeds bounds iow_assert(-1, 6, -1, 6, sTHERE, sHTMF, 6, (6 + 4)); //At start of bounds.(and start of string) iow_assert(0, 0, 0, 0, "Hello", sHTMF, 0, sHTMF.length()); final String sFRIEND = "friend"; //At end of bounds (and and of string) iow_assert(3, 3, 3, 3, sFRIEND, "my friend", 0, 9); //ENTIRE STRING: At start and end of bounds (and //string), in bounds. iow_assert(0, 0, 0, 0, sFRIEND, sFRIEND, 0, 6); //Same, exceeds bounds. iow_assert(-1, 0, -1, 0, sFRIEND, sFRIEND, 0, 5); //Word found once...END //Word found twice...START //Both instances somewhere in the middle iow_assert(6, 6, 31, 31, sTHERE, sHTMFTwice, 0, sHTMFTwice.length()); //Start of bounds (and start of string) iow_assert(0, 0, 25, 25, "Hello", sHTMFTwice, 0, sHTMFTwice.length()); //Start of bounds iow_assert(6, 6, 31, 31, sTHERE, sHTMFTwice, 6, sHTMFTwice.length()); //Second instance end of bounds (and end of string) iow_assert(0, 0, 6, 6, sTHERE, "there there", 0, 11); //Second instance end of bounds iow_assert(6, 6, 31, 31, sTHERE, sHTMFTwice, 0, 36); //Word found twice...END //This caused a bug, even though all the previous unit //tests worked. String s = "int iMills = 5000 + uDate.getXMinusYUnits(ts.getDateStarted(), (new Date()), 1);"; iow_assert(68, 68, 68, 68, "Date", s, 0, s.length()); } private void iow_assert(int i_xpctd_iob, int i_xpctd_ioub, int i_xpctd_liob, int i_xpctd_lioub, String s_toSearchFor, String s_searchIn, int i_idxLeft, int i_idxAfterRight) { SOBString ss = new SOBString(s_searchIn); SOBString ssb = new SOBString(s_searchIn); xassertEquals(i_xpctd_iob, uSOB.indexOfWord(ss, s_toSearchFor, i_idxLeft, i_idxAfterRight)); xassertEquals(i_xpctd_iob, uSOB.indexOfWord(ss, s_toSearchFor, i_idxLeft, i_idxAfterRight, true)); xassertEquals(i_xpctd_ioub, uSOB.indexOfWord(ss, s_toSearchFor, i_idxLeft, i_idxAfterRight, false)); xassertEquals(i_xpctd_liob, uSOB.lastIndexOfWord(ssb, s_toSearchFor, i_idxLeft, i_idxAfterRight)); xassertEquals(i_xpctd_liob, uSOB.lastIndexOfWord(ssb, s_toSearchFor, i_idxLeft, i_idxAfterRight, true)); xassertEquals(i_xpctd_lioub, uSOB.lastIndexOfWord(ssb, s_toSearchFor, i_idxLeft, i_idxAfterRight, false)); } private void iow_assertAX(String s_toSearchFor, String s_searchIn, int i_idxLeft, int i_idxAfterRight) { SOBStringBuffer ssb = null; if(s_searchIn != null) { ssb = new SOBStringBuffer(s_searchIn); } try { uSOB.indexOfWord(ssb, s_toSearchFor, i_idxLeft, i_idxAfterRight); xfail("indexOfWord('" + ssb + "', '" + s_toSearchFor + "', " + i_idxLeft + ", " + i_idxAfterRight + " should throw an ax"); } catch(AssertException ax) { xassertTrue(); } try { uSOB.indexOfWord(ssb, s_toSearchFor, i_idxLeft, i_idxAfterRight, true); xfail("indexOfWord('" + ssb + "', '" + s_toSearchFor + "', " + i_idxLeft + ", " + i_idxAfterRight + ", true) should throw an ax"); } catch(AssertException ax) { xassertTrue(); } SOBString sob = null; if(s_searchIn != null) { sob = new SOBString(s_searchIn); } try { uSOB.indexOfWord(sob, s_toSearchFor, i_idxLeft, i_idxAfterRight); xfail("indexOfWord('" + sob + "', '" + s_toSearchFor + "', " + i_idxLeft + ", " + i_idxAfterRight + ") should throw an ax"); } catch(AssertException ax) { xassertTrue(); } try { uSOB.indexOfWord(sob, s_toSearchFor, i_idxLeft, i_idxAfterRight, true); xfail("indexOfWord('" + sob + "', '" + s_toSearchFor + "', " + i_idxLeft + ", " + i_idxAfterRight + ", true) should throw an ax"); } catch(AssertException ax) { xassertTrue(); } } public void test_getStringAround() { final String sESC_QT = "\""; //Test documentation examples: String sJJ = "http://www.jeffyjeffy.com"; String sJJwq = "\"http://www.jeffyjeffy.com\""; SOBString ssJJ = new SOBString("click here"); xassertTrue(sJJ.equals(uSOB.getStringAround(ssJJ, 12, sESC_QT, sESC_QT, false, false))); gsa_assertInfo(8, 9, 34, 34); xassertTrue(sJJwq.equals(uSOB.getStringAround(ssJJ, 12, sESC_QT, sESC_QT, false, true))); gsa_assertInfo(8, 8, 34, 35); //With end quote only: weqo String sJJweqo = "http://www.jeffyjeffy.com\""; SOBString ssJJweqo = new SOBString("click here"); xassertTrue(("click here"); gsa_assertAX(ssJJwsqo, 12, sESC_QT, sESC_QT, false, false); } public void gsa_assertAX(SOBString sob_string, int i_charIdx, String s_delimLeft, String s_delimRight, boolean b_endsOfStrAltDelims, boolean b_includeDelimiters) { try { uSOB.getStringAround(sob_string, i_charIdx, s_delimLeft, s_delimRight, b_endsOfStrAltDelims, b_includeDelimiters); xfail("getStringAround('" + sob_string + "', " + i_charIdx + ", '" + s_delimLeft + "', '" + s_delimRight + "', " + b_endsOfStrAltDelims + ", " + b_includeDelimiters + ") should throw an ax"); } catch(AssertException ax) { xassertTrue(); } } public void gsa_assertInfo(int i_1, int i_2, int i_3, int i_4) { xassertTrue(i_1 + " should equal uSOB.getGSAInfo().i1 (" + uSOB.getGSAInfo().i1 + ")", i_1 == uSOB.getGSAInfo().i1); xassertTrue(i_2 + " should equal uSOB.getGSAInfo().i2 (" + uSOB.getGSAInfo().i2 + ")", i_2 == uSOB.getGSAInfo().i2); xassertTrue(i_3 + " should equal uSOB.getGSAInfo().i3 (" + uSOB.getGSAInfo().i3 + ")", i_3 == uSOB.getGSAInfo().i3); xassertTrue(i_4 + " should equal uSOB.getGSAInfo().i4 (" + uSOB.getGSAInfo().i4 + ")", i_4 == uSOB.getGSAInfo().i4); } public void setUp() throws Exception { ssES = new SOBString(sES); ssbES = new SOBStringBuffer(sES); ssHTMF = new SOBString(sHTMF); ssbHTMF = new SOBStringBuffer(sHTMF); super.setUp(); } public void tearDown() throws Exception { ssES = null; ssbES = null; ssHTMF = null; ssbHTMF = null; super.tearDown(); } public static Test suite() { TestSuite ts = new TestSuite(JUTUtilSOB.class); return ts; } public static void main(String[] as_cmdLineParams) { TestRunner.run(suite()); printSummary(); } }