/* 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.UtilString; import junit.framework.Test; import junit.framework.TestSuite; import junit.textui.TestRunner; public class JUTUtilString extends XBNTestCase { private UtilString uStr = null; //ENTIRE (and obvious to understand from the name) strings used four or more times private static final String sES = ""; private static final String sNEG_ZERO = "-0"; public JUTUtilString(String s_name) { super(s_name); } public void test_isEmail() { //Null and empty string xassertFalse(uStr.isEmail(null)); xassertFalse(uStr.isEmail(sES)); //Bad "@", otherwise legal. //None xassertFalse(uStr.isEmail("blabloo.com")); //First char xassertFalse(uStr.isEmail("@blabloo.com")); //Last char xassertFalse(uStr.isEmail("blabloo.com@")); //More than one xassertFalse(uStr.isEmail("bla@b@loo.com")); //Bad ".", otherwise legal //First char xassertFalse(uStr.isEmail(".bla@bloo.com")); //Last char xassertFalse(uStr.isEmail("bla@bloo.com.")); //None xassertFalse(uStr.isEmail("bla@bloocom")); //Illegal characters: " ", ",", "\\", "/". Otherwise legal xassertFalse(uStr.isEmail("bla@bl oo.com")); xassertFalse(uStr.isEmail("bla@bl,oo.com")); xassertFalse(uStr.isEmail("bla@bl\\oo.com")); xassertFalse(uStr.isEmail("bla@bl/oo.com")); //One "@" and one or more ".", but next to each other xassertFalse(uStr.isEmail("bla@.bloo.com")); xassertFalse(uStr.isEmail("bla.@bloo.com")); xassertFalse(uStr.isEmail("bla@bloo..com")); //Last "." before "@" xassertFalse(uStr.isEmail("bla.bloo.blee.blay@nodotcom")); //Legal! xassertTrue(uStr.isEmail("bla@bloo.com")); } public void test_isBoolean() { //Null/empty string ib_assert(false, null); ib_assert(false, sES); //Legal ib_assert(true, "true"); ib_assert(true, "false"); //Everything else ib_assert(false, "tRuE"); ib_assert(false, "TRUE"); ib_assert(false, "fAlSe"); ib_assert(false, "FALSE"); ib_assert(false, " true "); ib_assert(false, "false "); ib_assert(false, "1234"); ib_assert(false, "*&#&%^$("); } private void ib_assert(boolean b_legalNotLegal, String s_potentialBoolean) { String sMsg = "'" + s_potentialBoolean + "'"; xassertTrue(sMsg, b_legalNotLegal == uStr.isBoolean(s_potentialBoolean)); try { uStr.getBoolean(s_potentialBoolean); xassertTrue(sMsg, b_legalNotLegal); } catch(AssertException ax) { xassertTrue(sMsg, !b_legalNotLegal); } } public void test_getIdxNEToLmnt0() { //If is null (yes)... try { uStr.getIdxNEToLmnt0(null); xfail("getIdxNEToLmnt0(null) should throw an ax"); } catch(AssertException ax) { xassertTrue(); } //If is 0 elements (yes)... try { uStr.getIdxNEToLmnt0(new String[] {}); xassertTrue(); } catch(AssertException ax) { xfail("getIdxNEToLmnt0(new String[] {}) should not throw an ax: " + ax.toString()); } //...else not null AND at least one element try { uStr.getIdxNEToLmnt0(new String[] {"a"}); xassertTrue(); } catch(AssertException ax) { xfail("getIdxNEToLmnt0(new String[] {'a'}) should not throw an ax: " + ax.toString()); } //Only one element... xassertTrue(-1 == uStr.getIdxNEToLmnt0(new String[] {"a"})); //...and it is null xassertTrue(-1 == uStr.getIdxNEToLmnt0(new String[] {null})); xassertTrue(-1 == uStr.getIdxNEToLmnt0(new String[] {"a", "a", "a", "a"})); //Legal xassertTrue(2 == uStr.getIdxNEToLmnt0(new String[] {"a", "a", "b", "a"})); xassertTrue(2 == uStr.getIdxNEToLmnt0(new String[] {"a", "a", null, "a"})); } public void test_isNumber() { //Is null or ES (yes)... in_testCombos(null, false, false); in_testCombos(sES, false, false); //...else not ES in_testCombos("1", true, true); //Is first char a dash AND length is one (yes)... in_testCombos("-", false, false); //...else first char is not a dash.... in_testCombos("1", true, true); //... or first char *is* a dash, but length > 1 in_testCombos("-1", true, true); //for each digit (after the "-" if any), is there a decimal (yes)? in_testCombos("-.", false, false); in_testCombos("-1.0", false, true); in_testCombos("1.0", false, true); //Two decimals bad in_testCombos("1..0", false, false); in_testCombos("1.0.", false, false); in_testCombos("1.0.0", false, false); //If any character is not a dot, dash, or number. in_testCombos("x", false, false); //Other examples in_testCombos("0", true, true); in_testCombos("0.3984732", false, true); in_testCombos(".3984732", false, true); in_testCombos("-0.3984732", false, true); in_testCombos("-.3984732", false, true); in_testCombos("7401624032892", true, true); in_testCombos("1018374018327401874.381740328143984732", false, true); in_testCombos("-1018374018327401874.381740328143984732", false, true); in_testCombos(sNEG_ZERO, true, true); } private void in_testCombos(String s_potentialNumber, boolean b_integer, boolean b_decimal) { xassertTrue(b_integer == uStr.isInteger(s_potentialNumber)); xassertTrue(b_decimal == uStr.isDecimal(s_potentialNumber)); xassertTrue(b_integer == uStr.isNumber(s_potentialNumber, false)); xassertTrue(b_decimal == uStr.isNumber(s_potentialNumber, true)); } public void test_getInt() { gi_assertItself(1); gi_assertItself(123456); gi_assertItself(2147483647); //Maximum legal int gi_assertItself(-2147483648); //Minimum legal int gi_assertItself(-1); gi_assertItself(-123456); gi_assertItself(0); xassertTrue(uStr.getInt(sNEG_ZERO, true) == 0); xassertTrue(uStr.getInt(sNEG_ZERO, false) == 0); gi_assertX("1234567890123456789"); gi_assertX("-1234567890123456789"); gi_assertX("120837410827341234567890123456789"); gi_assertX("7401624032892"); gi_assertX("2147483648"); //Maximum legal int PLUS ONE gi_assertX("-2147483649"); //Minimum legal int MINUS ONE gi_assertX("0.3984732"); gi_assertX("123.456"); gi_assertX("1018374018327401874.381740328143984732"); gi_assertX("-1018374018327401874.381740328143984732"); //Null is a special case that should always throw AX. gi_assertAX(null, true); gi_assertAX(null, false); gi_assertX("1-"); gi_assertX("-."); gi_assertX("."); gi_assertX("-"); gi_assertX("8471."); gi_assertX("0.-"); gi_assertX(" 123 "); gi_assertX("456 "); gi_assertX(" 789"); gi_assertX("123 456 789"); gi_assertX("bla"); gi_assertX("!@#$^%@^&^#"); } private void gi_assertItself(int i_nt) { String sInt = Integer.toString(i_nt); xassertTrue(uStr.getInt(sInt) == i_nt); xassertTrue(uStr.getInt(sInt, true) == i_nt); xassertTrue(uStr.getInt(sInt, false) == i_nt); } private void gi_assertX(String s_notAnInt) { try { uStr.getInt(s_notAnInt); xfail("getInt('" + s_notAnInt + "') should throw an ax"); } catch(AssertException ax) { xassertTrue(); } gi_assertAX(s_notAnInt, false); try { uStr.getInt(s_notAnInt, true); xfail("getInt('" + s_notAnInt + "', true) should throw an ax"); } catch(NumberFormatException nfx) { xassertTrue(); } } private void gi_assertAX(String s_notAnInt, boolean b_nfxInsteadOfAX) { try { uStr.getInt(s_notAnInt, b_nfxInsteadOfAX); xfail("getInt('" + s_notAnInt + "', " + b_nfxInsteadOfAX + ") should throw an ax"); } catch(AssertException ax) { xassertTrue(); } } public void test_getLong() { gl_assertItself(1); gl_assertItself(123456); gl_assertItself(2147483647); gl_assertItself(-2147483648); gl_assertItself(-1); gl_assertItself(-123456); gl_assertItself(0); //When you do either of the following, Jikes chokes with //an "int too large":error. What?! // gl_assertItself(1234567890123456789); // long l = 1234567890123456789; gl_assertItself(Long.parseLong("1234567890123456789")); gl_assertItself(Long.parseLong("-1234567890123456789")); gl_assertItself(Long.parseLong("2147483648")); gl_assertItself(Long.parseLong("-2147483649")); gl_assertItself(Long.parseLong("7401624032892")); xassertTrue(uStr.getLong(sNEG_ZERO, true) == 0); xassertTrue(uStr.getLong(sNEG_ZERO, false) == 0); gl_assertX("-120837410827341234567890123456789"); gl_assertX("1018374018327401874.381740328143984732"); gl_assertX("-1018374018327401874.381740328143984732"); gl_assertX("0.3984732"); gl_assertX("123.456"); //Null is a special case that should always throw AX. gl_assertAX(null, true); gl_assertAX(null, false); gl_assertX("1-"); gl_assertX("-."); gl_assertX("."); gl_assertX("-"); gl_assertX("8471."); gl_assertX("0.-"); gl_assertX(" 123 "); gl_assertX("456 "); gl_assertX(" 789"); gl_assertX("123 456 789"); gl_assertX("bla"); gl_assertX("!@#$^%@^&^#"); } private void gl_assertItself(long l_ong) { String sLong = Long.toString(l_ong); xassertTrue(uStr.getLong(sLong) == l_ong); xassertTrue(uStr.getLong(sLong, true) == l_ong); xassertTrue(uStr.getLong(sLong, false) == l_ong); } private void gl_assertX(String s_notALongNotByALongShot) { try { uStr.getLong(s_notALongNotByALongShot); xfail("getLong('" + s_notALongNotByALongShot + "') should throw an ax"); } catch(AssertException ax) { xassertTrue(); } gl_assertAX(s_notALongNotByALongShot, false); try { uStr.getLong(s_notALongNotByALongShot, true); xfail("getLong('" + s_notALongNotByALongShot + "', true) should throw an ax"); } catch(NumberFormatException nfx) { xassertTrue(); } } private void gl_assertAX(String s_notALongNotByALongShot, boolean b_nfxInsteadOfAX) { try { uStr.getLong(s_notALongNotByALongShot, b_nfxInsteadOfAX); xfail("getLong('" + s_notALongNotByALongShot + "', " + b_nfxInsteadOfAX + ") should throw an ax"); } catch(AssertException ax) { xassertTrue(); } } public void test_getConditional() { String sin = "STRING_IS_NULL"; String sie = "STRING_IS_EMPTY"; String sMyString = "My very own string. :' )"; //If string null (yes)... xassertTrue(sES.equals(uStr.getConditional(null))); xassertTrue(sin.equals(uStr.getConditional(null, sin))); xassertTrue(sin.equals(uStr.getConditional("[", null, "]", sin))); xassertTrue(sin.equals(uStr.getConditional("[", null, "]", sin))); xassertTrue(null == uStr.getConditional("[", null, "]", null, sie)); xassertTrue(sin.equals(uStr.getConditional("[", null, "]", sin, sie))); //...else string not null. xassertTrue(!sES.equals(uStr.getConditional(sMyString))); xassertTrue(!sin.equals(uStr.getConditional(sMyString, sin))); xassertTrue(!sin.equals(uStr.getConditional("[", sMyString, "]", sin))); xassertTrue(null != uStr.getConditional("[", sMyString, "]", null, sie)); xassertTrue(!sin.equals(uStr.getConditional("[", sMyString, "]", sie))); //If string ES (yes)... xassertTrue(sES.equals(uStr.getConditional(sES))); xassertTrue(sES.equals(uStr.getConditional(sES, sin))); xassertTrue(sES.equals(uStr.getConditional("[", sES, "]", sin))); xassertTrue(null == uStr.getConditional("[", sES, "]", sin, null)); xassertTrue(sie.equals(uStr.getConditional("[", sES, "]", sin, sie))); //...else string not ES. xassertTrue(!sES.equals(uStr.getConditional(sMyString))); xassertTrue(!sES.equals(uStr.getConditional(sMyString, sin))); xassertTrue(!sES.equals(uStr.getConditional("[", sMyString, "]", sin))); xassertTrue(null != uStr.getConditional("[", sMyString, "]", sin, null)); xassertTrue(!sie.equals(uStr.getConditional("[", sMyString, "]", sie))); //Non-null, non-empty string, both braces. String sbmsb = "[" + sMyString + "]"; xassertTrue(sMyString.equals(uStr.getConditional(sMyString))); xassertTrue(sMyString.equals(uStr.getConditional(sMyString, sin))); xassertTrue(sMyString.equals(uStr.getConditional(null, sMyString, null, sin))); xassertTrue(sMyString.equals(uStr.getConditional(sES, sMyString, sES, sin))); xassertTrue(sbmsb.equals(uStr.getConditional("[", sMyString, "]", sin))); xassertTrue(sbmsb.equals(uStr.getConditional("[", sMyString, "]", sin))); xassertTrue(sbmsb.equals(uStr.getConditional("[", sMyString, "]", sie))); //Non-null, non-empty string, one brace. xassertTrue(("[" + sMyString).equals(uStr.getConditional("[", sMyString, null, sin))); xassertTrue(("[" + sMyString).equals(uStr.getConditional("[", sMyString, sES, sin))); xassertTrue((sMyString + "]").equals(uStr.getConditional(null, sMyString, "]", sin))); xassertTrue((sMyString + "]").equals(uStr.getConditional(sES, sMyString, "]", sin))); } public void setUp() throws Exception { super.setUp(); uStr = new UtilString(); } public void tearDown() throws Exception { super.tearDown(); uStr = null; } public static Test suite() { TestSuite ts = new TestSuite(JUTUtilString.class); return ts; } public static void main(String[] as_cmdLineParams) { TestRunner.run(suite()); printSummary(); } }