| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.util; |
| 34 | | | |
| 35 | | | import java.math.BigInteger; |
| 36 | | | import junit.framework.TestCase; |
| 37 | | | |
| 38 | | | |
| 39 | | | |
| 40 | | | @author |
| 41 | | | |
| 42 | 100 | | public class XsdUtilTest |
| 43 | | | extends TestCase |
| 44 | | | { |
| 45 | | | |
| 46 | | | public final void testIntegerFromStringGood () |
| 47 | | | { |
| 48 | 100 | | testIntegerFromStringGood("0", BigInteger.ZERO); |
| 49 | 100 | | testIntegerFromStringGood("1", BigInteger.ONE); |
| 50 | 100 | | testIntegerFromStringGood("+0", BigInteger.ZERO); |
| 51 | 100 | | testIntegerFromStringGood("+1", BigInteger.ONE); |
| 52 | 100 | | testIntegerFromStringGood("-0", BigInteger.ZERO); |
| 53 | 100 | | testIntegerFromStringGood("-1", BigInteger.valueOf(-1)); |
| 54 | 100 | | testIntegerFromStringGood("-01", BigInteger.valueOf(-1)); |
| 55 | 100 | | testIntegerFromStringGood("00", BigInteger.ZERO); |
| 56 | 100 | | testIntegerFromStringGood("01", BigInteger.ONE); |
| 57 | 100 | | testIntegerFromStringGood("2147483647", |
| 58 | | | BigInteger.valueOf(Integer.MAX_VALUE)); |
| 59 | 100 | | testIntegerFromStringGood("-2147483648", |
| 60 | | | BigInteger.valueOf(Integer.MIN_VALUE)); |
| 61 | 100 | | testIntegerFromStringGood("9223372036854775807", |
| 62 | | | BigInteger.valueOf(Long.MAX_VALUE)); |
| 63 | 100 | | testIntegerFromStringGood("-9223372036854775808", |
| 64 | | | BigInteger.valueOf(Long.MIN_VALUE)); |
| 65 | 100 | | } |
| 66 | | | |
| 67 | | | |
| 68 | | | public final void testIntegerFromStringBad () |
| 69 | | | { |
| 70 | 100 | | testIntegerFromStringBad("++0"); |
| 71 | 100 | | testIntegerFromStringBad(" +0"); |
| 72 | 100 | | testIntegerFromStringBad("--0"); |
| 73 | 100 | | testIntegerFromStringBad("-+0"); |
| 74 | 100 | | testIntegerFromStringBad("+-0"); |
| 75 | 100 | | testIntegerFromStringBad("FOO"); |
| 76 | 100 | | testIntegerFromStringBad(null); |
| 77 | 100 | | testIntegerFromStringBad("0.1"); |
| 78 | 100 | | testIntegerFromStringBad(""); |
| 79 | 100 | | testIntegerFromStringBad("123E123"); |
| 80 | 100 | | } |
| 81 | | | |
| 82 | | | |
| 83 | | | public final void testIntFromStringGood () |
| 84 | | | { |
| 85 | 100 | | testIntFromStringGood("0", 0); |
| 86 | 100 | | testIntFromStringGood("1", 1); |
| 87 | 100 | | testIntFromStringGood("+0", 0); |
| 88 | 100 | | testIntFromStringGood("+1", 1); |
| 89 | 100 | | testIntFromStringGood("-0", 0); |
| 90 | 100 | | testIntFromStringGood("-1", -1); |
| 91 | 100 | | testIntFromStringGood("-01", -1); |
| 92 | 100 | | testIntFromStringGood("00", 0); |
| 93 | 100 | | testIntFromStringGood("01", 1); |
| 94 | 100 | | testIntFromStringGood("2147483647", Integer.MAX_VALUE); |
| 95 | 100 | | testIntFromStringGood("-2147483648", Integer.MIN_VALUE); |
| 96 | 100 | | } |
| 97 | | | |
| 98 | | | |
| 99 | | | public final void testIntFromStringBad () |
| 100 | | | { |
| 101 | 100 | | testIntFromStringBad("++0"); |
| 102 | 100 | | testIntFromStringBad(" +0"); |
| 103 | 100 | | testIntFromStringBad("--0"); |
| 104 | 100 | | testIntFromStringBad("-+0"); |
| 105 | 100 | | testIntFromStringBad("+-0"); |
| 106 | 100 | | testIntFromStringBad("FOO"); |
| 107 | 100 | | testIntFromStringBad(null); |
| 108 | 100 | | testIntFromStringBad("0.1"); |
| 109 | 100 | | testIntFromStringBad(""); |
| 110 | 100 | | testIntFromStringBad("123E123"); |
| 111 | 100 | | testIntFromStringBad("9223372036854775807"); |
| 112 | 100 | | testIntFromStringBad("-9223372036854775808"); |
| 113 | 100 | | } |
| 114 | | | |
| 115 | | | |
| 116 | | | public final void testLongFromString () |
| 117 | | | { |
| 118 | 100 | | testLongFromStringGood("0", 0); |
| 119 | 100 | | testLongFromStringGood("1", 1); |
| 120 | 100 | | testLongFromStringGood("+0", 0); |
| 121 | 100 | | testLongFromStringGood("+1", 1); |
| 122 | 100 | | testLongFromStringGood("-0", 0); |
| 123 | 100 | | testLongFromStringGood("-1", -1); |
| 124 | 100 | | testLongFromStringGood("-01", -1); |
| 125 | 100 | | testLongFromStringGood("00", 0); |
| 126 | 100 | | testLongFromStringGood("01", 1); |
| 127 | 100 | | testLongFromStringGood("2147483647", Integer.MAX_VALUE); |
| 128 | 100 | | testLongFromStringGood("-2147483648", Integer.MIN_VALUE); |
| 129 | 100 | | testLongFromStringGood("9223372036854775807", Long.MAX_VALUE); |
| 130 | 100 | | testLongFromStringGood("-9223372036854775808", Long.MIN_VALUE); |
| 131 | 100 | | } |
| 132 | | | |
| 133 | | | |
| 134 | | | public final void testLongFromStringBad () |
| 135 | | | { |
| 136 | 100 | | testLongFromStringBad("++0"); |
| 137 | 100 | | testLongFromStringBad(" +0"); |
| 138 | 100 | | testLongFromStringBad("--0"); |
| 139 | 100 | | testLongFromStringBad("-+0"); |
| 140 | 100 | | testLongFromStringBad("+-0"); |
| 141 | 100 | | testLongFromStringBad("FOO"); |
| 142 | 100 | | testLongFromStringBad(null); |
| 143 | 100 | | testLongFromStringBad("0.1"); |
| 144 | 100 | | testLongFromStringBad(""); |
| 145 | 100 | | testLongFromStringBad("123E123"); |
| 146 | 100 | | } |
| 147 | | | |
| 148 | | | |
| 149 | | | |
| 150 | | | |
| 151 | | | public void testIsValidSchemaToken () |
| 152 | | | { |
| 153 | 100 | | checkValidSchemaToken("XXXX"); |
| 154 | 100 | | checkValidSchemaToken("XXX XXXX"); |
| 155 | 100 | | checkValidSchemaToken("X X"); |
| 156 | 100 | | checkValidSchemaToken(""); |
| 157 | 100 | | } |
| 158 | | | |
| 159 | | | |
| 160 | | | |
| 161 | | | |
| 162 | | | public void testIsValidSchemaTokenFalse () |
| 163 | | | { |
| 164 | 100 | | checkBrokenXmlSchemaToken(" "); |
| 165 | 100 | | checkBrokenXmlSchemaToken(" X"); |
| 166 | 100 | | checkBrokenXmlSchemaToken("X "); |
| 167 | 100 | | checkBrokenXmlSchemaToken("X "); |
| 168 | 100 | | checkBrokenXmlSchemaToken("X X"); |
| 169 | 100 | | checkBrokenXmlSchemaToken("X\tX"); |
| 170 | 100 | | checkBrokenXmlSchemaToken("X\nX"); |
| 171 | 100 | | checkBrokenXmlSchemaToken("X\rX"); |
| 172 | 100 | | checkBrokenXmlSchemaToken("X\r X"); |
| 173 | 100 | | checkBrokenXmlSchemaToken(" X\r"); |
| 174 | 100 | | checkBrokenXmlSchemaToken(null); |
| 175 | 100 | | } |
| 176 | | | |
| 177 | | | private void checkBrokenXmlSchemaToken (String pattern) |
| 178 | | | { |
| 179 | 100 | | if (XsdUtil.isValidToken(pattern)) |
| 180 | | | { |
| 181 | 0 | | fail("Pattern is invalid and should return false '" |
| 182 | | | + pattern + "'."); |
| 183 | | | } |
| 184 | 100 | | } |
| 185 | | | |
| 186 | | | private void checkValidSchemaToken (String pattern) |
| 187 | | | { |
| 188 | 100 | | if (!XsdUtil.isValidToken(pattern)) |
| 189 | | | { |
| 190 | 0 | | fail("Pattern is valid and should return true '" |
| 191 | | | + pattern + "'."); |
| 192 | | | } |
| 193 | 100 | | } |
| 194 | | | |
| 195 | | | private void testIntegerFromStringGood (String str, BigInteger i) |
| 196 | | | { |
| 197 | 100 | | assertEquals("From String value unexpected", i, |
| 198 | | | XsdUtil.integerFromString(str)); |
| 199 | 100 | | } |
| 200 | | | |
| 201 | | | private void testIntegerFromStringBad (String str) |
| 202 | | | { |
| 203 | | | try |
| 204 | | | { |
| 205 | 0 | | XsdUtil.integerFromString(str); |
| 206 | 0 | | fail("String representation should fail for: '" + str + "'"); |
| 207 | | | } |
| 208 | 100 | | catch (NumberFormatException ex) |
| 209 | | | { |
| 210 | | | |
| 211 | | | } |
| 212 | 100 | | catch (NullPointerException ex) |
| 213 | | | { |
| 214 | | | |
| 215 | 50 | | } |
| 216 | 100 | | } |
| 217 | | | |
| 218 | | | private void testIntFromStringGood (String str, int i) |
| 219 | | | { |
| 220 | 100 | | assertEquals("From String value unexpected", i, |
| 221 | | | XsdUtil.intFromString(str)); |
| 222 | 100 | | } |
| 223 | | | |
| 224 | | | private void testIntFromStringBad (String str) |
| 225 | | | { |
| 226 | | | try |
| 227 | | | { |
| 228 | 0 | | XsdUtil.intFromString(str); |
| 229 | 0 | | fail("String representation should fail for: '" + str + "'"); |
| 230 | | | } |
| 231 | 100 | | catch (NumberFormatException ex) |
| 232 | | | { |
| 233 | | | |
| 234 | | | } |
| 235 | 100 | | catch (NullPointerException ex) |
| 236 | | | { |
| 237 | | | |
| 238 | 50 | | } |
| 239 | 100 | | } |
| 240 | | | |
| 241 | | | private void testLongFromStringGood (String str, long i) |
| 242 | | | { |
| 243 | 100 | | assertEquals("From String value unexpected", i, |
| 244 | | | XsdUtil.longFromString(str)); |
| 245 | 100 | | } |
| 246 | | | |
| 247 | | | private void testLongFromStringBad (String str) |
| 248 | | | { |
| 249 | | | try |
| 250 | | | { |
| 251 | 0 | | XsdUtil.longFromString(str); |
| 252 | 0 | | fail("String representation of long should fail for: '" + str + "'"); |
| 253 | | | } |
| 254 | 100 | | catch (NumberFormatException ex) |
| 255 | | | { |
| 256 | | | |
| 257 | | | } |
| 258 | 100 | | catch (NullPointerException ex) |
| 259 | | | { |
| 260 | | | |
| 261 | 50 | | } |
| 262 | 100 | | } |
| 263 | | | |
| 264 | | | } |