001: // -*- Java -*-
002: //
003: // Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
004: // Copyright (c) 2005, University of Colorado at Boulder
005: // All rights reserved.
006: //
007: // Redistribution and use in source and binary forms, with or without
008: // modification, are permitted provided that the following conditions are
009: // met:
010: //
011: // * Redistributions of source code must retain the above copyright
012: // notice, this list of conditions and the following disclaimer.
013: //
014: // * Redistributions in binary form must reproduce the above copyright
015: // notice, this list of conditions and the following disclaimer in the
016: // documentation and/or other materials provided with the distribution.
017: //
018: // * Neither the name of the University of Colorado at Boulder nor the
019: // names of its contributors may be used to endorse or promote
020: // products derived from this software without specific prior written
021: // permission.
022: //
023: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
024: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
025: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
026: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
027: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
028: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
029: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
030: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
031: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
033: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034: //
035: package org.xbill.DNS;
036:
037: // Mnemonic has package-level access.
038:
039: import junit.framework.TestCase;
040:
041: public class MnemonicTest extends TestCase {
042: private Mnemonic m_mn;
043:
044: public MnemonicTest(String name) {
045: super (name);
046: }
047:
048: public void setUp() {
049: m_mn = new Mnemonic(MnemonicTest.class.getName() + " UPPER",
050: Mnemonic.CASE_UPPER);
051: }
052:
053: public void test_toInteger() {
054: Integer i = Mnemonic.toInteger(64);
055: assertEquals(new Integer(64), i);
056: Integer i2 = Mnemonic.toInteger(64);
057: assertEquals(i, i2);
058: assertNotSame(i, i2);
059:
060: i = Mnemonic.toInteger(-1);
061: assertEquals(new Integer(-1), i);
062: i2 = Mnemonic.toInteger(-1);
063: assertEquals(i, i2);
064: assertNotSame(i, i2);
065:
066: i = Mnemonic.toInteger(0);
067: assertEquals(new Integer(0), i);
068: i2 = Mnemonic.toInteger(0);
069: assertEquals(i, i2);
070: assertSame(i, i2);
071:
072: i = Mnemonic.toInteger(63);
073: assertEquals(new Integer(63), i);
074: i2 = Mnemonic.toInteger(63);
075: assertEquals(i, i2);
076: assertSame(i, i2);
077: }
078:
079: public void test_no_maximum() {
080: try {
081: m_mn.check(-1);
082: fail("IllegalArgumentException not thrown");
083: } catch (IllegalArgumentException e) {
084: }
085: try {
086: m_mn.check(0);
087: } catch (IllegalArgumentException e) {
088: fail(e.getMessage());
089: }
090: try {
091: m_mn.check(Integer.MAX_VALUE);
092: } catch (IllegalArgumentException e) {
093: fail(e.getMessage());
094: }
095:
096: m_mn.setNumericAllowed(true);
097:
098: int val = m_mn.getValue("-2");
099: assertEquals(-1, val);
100:
101: val = m_mn.getValue("0");
102: assertEquals(0, val);
103:
104: val = m_mn.getValue("" + Integer.MAX_VALUE);
105: assertEquals(Integer.MAX_VALUE, val);
106: }
107:
108: public void test_setMaximum() {
109: m_mn.setMaximum(15);
110: try {
111: m_mn.check(-1);
112: fail("IllegalArgumentException not thrown");
113: } catch (IllegalArgumentException e) {
114: }
115: try {
116: m_mn.check(0);
117: } catch (IllegalArgumentException e) {
118: fail(e.getMessage());
119: }
120: try {
121: m_mn.check(15);
122: } catch (IllegalArgumentException e) {
123: fail(e.getMessage());
124: }
125: try {
126: m_mn.check(16);
127: fail("IllegalArgumentException not thrown");
128: } catch (IllegalArgumentException e) {
129: }
130:
131: // need numericok to exercise the usage of max in parseNumeric
132: m_mn.setNumericAllowed(true);
133:
134: int val = m_mn.getValue("-2");
135: assertEquals(-1, val);
136:
137: val = m_mn.getValue("0");
138: assertEquals(0, val);
139:
140: val = m_mn.getValue("15");
141: assertEquals(15, val);
142:
143: val = m_mn.getValue("16");
144: assertEquals(-1, val);
145: }
146:
147: public void test_setPrefix() {
148: final String prefix = "A mixed CASE Prefix".toUpperCase();
149: m_mn.setPrefix(prefix);
150:
151: String out = m_mn.getText(10);
152: assertEquals(prefix + "10", out);
153:
154: int i = m_mn.getValue(out);
155: assertEquals(10, i);
156: }
157:
158: public void test_basic_operation() {
159: // setUp creates Mnemonic with CASE_UPPER
160: m_mn.add(10, "Ten");
161: m_mn.add(20, "Twenty");
162: m_mn.addAlias(20, "Veinte");
163: m_mn.add(30, "Thirty");
164:
165: String text = m_mn.getText(10);
166: assertEquals("TEN", text);
167:
168: text = m_mn.getText(20);
169: assertEquals("TWENTY", text);
170:
171: text = m_mn.getText(30);
172: assertEquals("THIRTY", text);
173:
174: text = m_mn.getText(40);
175: assertEquals("40", text);
176:
177: int value = m_mn.getValue("tEn");
178: assertEquals(10, value);
179:
180: value = m_mn.getValue("twenty");
181: assertEquals(20, value);
182:
183: value = m_mn.getValue("VeiNTe");
184: assertEquals(20, value);
185:
186: value = m_mn.getValue("THIRTY");
187: assertEquals(30, value);
188: }
189:
190: public void test_basic_operation_lower() {
191: m_mn = new Mnemonic(MnemonicTest.class.getName() + " LOWER",
192: Mnemonic.CASE_LOWER);
193: m_mn.add(10, "Ten");
194: m_mn.add(20, "Twenty");
195: m_mn.addAlias(20, "Veinte");
196: m_mn.add(30, "Thirty");
197:
198: String text = m_mn.getText(10);
199: assertEquals("ten", text);
200:
201: text = m_mn.getText(20);
202: assertEquals("twenty", text);
203:
204: text = m_mn.getText(30);
205: assertEquals("thirty", text);
206:
207: text = m_mn.getText(40);
208: assertEquals("40", text);
209:
210: int value = m_mn.getValue("tEn");
211: assertEquals(10, value);
212:
213: value = m_mn.getValue("twenty");
214: assertEquals(20, value);
215:
216: value = m_mn.getValue("VeiNTe");
217: assertEquals(20, value);
218:
219: value = m_mn.getValue("THIRTY");
220: assertEquals(30, value);
221: }
222:
223: public void test_basic_operation_sensitive() {
224: m_mn = new Mnemonic(
225: MnemonicTest.class.getName() + " SENSITIVE",
226: Mnemonic.CASE_SENSITIVE);
227: m_mn.add(10, "Ten");
228: m_mn.add(20, "Twenty");
229: m_mn.addAlias(20, "Veinte");
230: m_mn.add(30, "Thirty");
231:
232: String text = m_mn.getText(10);
233: assertEquals("Ten", text);
234:
235: text = m_mn.getText(20);
236: assertEquals("Twenty", text);
237:
238: text = m_mn.getText(30);
239: assertEquals("Thirty", text);
240:
241: text = m_mn.getText(40);
242: assertEquals("40", text);
243:
244: int value = m_mn.getValue("Ten");
245: assertEquals(10, value);
246:
247: value = m_mn.getValue("twenty");
248: assertEquals(-1, value);
249:
250: value = m_mn.getValue("Twenty");
251: assertEquals(20, value);
252:
253: value = m_mn.getValue("VEINTE");
254: assertEquals(-1, value);
255:
256: value = m_mn.getValue("Veinte");
257: assertEquals(20, value);
258:
259: value = m_mn.getValue("Thirty");
260: assertEquals(30, value);
261: }
262:
263: public void test_invalid_numeric() {
264: m_mn.setNumericAllowed(true);
265: int value = m_mn.getValue("Not-A-Number");
266: assertEquals(-1, value);
267: }
268:
269: public void test_addAll() {
270: m_mn.add(10, "Ten");
271: m_mn.add(20, "Twenty");
272:
273: Mnemonic mn2 = new Mnemonic("second test Mnemonic",
274: Mnemonic.CASE_UPPER);
275: mn2.add(20, "Twenty");
276: mn2.addAlias(20, "Veinte");
277: mn2.add(30, "Thirty");
278:
279: m_mn.addAll(mn2);
280:
281: String text = m_mn.getText(10);
282: assertEquals("TEN", text);
283:
284: text = m_mn.getText(20);
285: assertEquals("TWENTY", text);
286:
287: text = m_mn.getText(30);
288: assertEquals("THIRTY", text);
289:
290: text = m_mn.getText(40);
291: assertEquals("40", text);
292:
293: int value = m_mn.getValue("tEn");
294: assertEquals(10, value);
295:
296: value = m_mn.getValue("twenty");
297: assertEquals(20, value);
298:
299: value = m_mn.getValue("VeiNTe");
300: assertEquals(20, value);
301:
302: value = m_mn.getValue("THIRTY");
303: assertEquals(30, value);
304: }
305: }
|