3
// Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
4
// Copyright (c) 2005, University of Colorado at Boulder
5
// All rights reserved.
7
// Redistribution and use in source and binary forms, with or without
8
// modification, are permitted provided that the following conditions are
11
// * Redistributions of source code must retain the above copyright
12
// notice, this list of conditions and the following disclaimer.
14
// * Redistributions in binary form must reproduce the above copyright
15
// notice, this list of conditions and the following disclaimer in the
16
// documentation and/or other materials provided with the distribution.
18
// * Neither the name of the University of Colorado at Boulder nor the
19
// names of its contributors may be used to endorse or promote
20
// products derived from this software without specific prior written
23
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
package org.xbill.DNS;
37
// Mnemonic has package-level access.
39
import junit.framework.TestCase;
41
public class MnemonicTest extends TestCase
43
private Mnemonic m_mn;
45
public MnemonicTest( String name )
52
m_mn = new Mnemonic(MnemonicTest.class.getName() + " UPPER", Mnemonic.CASE_UPPER);
55
public void test_toInteger()
57
Integer i = Mnemonic.toInteger(64);
58
assertEquals( new Integer(64), i );
59
Integer i2 = Mnemonic.toInteger(64);
60
assertEquals( i, i2 );
61
assertNotSame( i, i2 );
63
i = Mnemonic.toInteger(-1);
64
assertEquals( new Integer(-1), i );
65
i2 = Mnemonic.toInteger(-1);
66
assertEquals( i, i2 );
67
assertNotSame( i, i2 );
69
i = Mnemonic.toInteger(0);
70
assertEquals( new Integer(0), i );
71
i2 = Mnemonic.toInteger(0);
72
assertEquals( i, i2 );
75
i = Mnemonic.toInteger(63);
76
assertEquals( new Integer(63), i );
77
i2 = Mnemonic.toInteger(63);
78
assertEquals( i, i2 );
82
public void test_no_maximum()
84
try {m_mn.check(-1); fail( "IllegalArgumentException not thrown" );} catch( IllegalArgumentException e ){}
85
try {m_mn.check(0);} catch( IllegalArgumentException e ){fail(e.getMessage());}
86
try {m_mn.check(Integer.MAX_VALUE);} catch( IllegalArgumentException e ){fail(e.getMessage());}
88
m_mn.setNumericAllowed(true);
90
int val = m_mn.getValue("-2");
91
assertEquals( -1, val );
93
val = m_mn.getValue("0");
94
assertEquals( 0, val );
96
val = m_mn.getValue("" + Integer.MAX_VALUE);
97
assertEquals( Integer.MAX_VALUE, val );
100
public void test_setMaximum()
103
try {m_mn.check(-1); fail("IllegalArgumentException not thrown");} catch( IllegalArgumentException e ){}
104
try {m_mn.check(0);} catch( IllegalArgumentException e ){fail( e.getMessage() );}
105
try {m_mn.check(15);} catch( IllegalArgumentException e ){fail( e.getMessage() );}
106
try {m_mn.check(16); fail("IllegalArgumentException not thrown");} catch( IllegalArgumentException e ){}
108
// need numericok to exercise the usage of max in parseNumeric
109
m_mn.setNumericAllowed(true);
111
int val = m_mn.getValue("-2");
112
assertEquals( -1, val );
114
val = m_mn.getValue( "0" );
115
assertEquals( 0, val );
117
val = m_mn.getValue( "15" );
118
assertEquals( 15, val );
120
val = m_mn.getValue( "16" );
121
assertEquals( -1, val );
124
public void test_setPrefix()
126
final String prefix = "A mixed CASE Prefix".toUpperCase();
127
m_mn.setPrefix(prefix);
129
String out = m_mn.getText(10);
130
assertEquals( prefix + "10", out );
132
int i = m_mn.getValue( out );
133
assertEquals( 10, i );
136
public void test_basic_operation()
138
// setUp creates Mnemonic with CASE_UPPER
139
m_mn.add( 10, "Ten" );
140
m_mn.add( 20, "Twenty" );
141
m_mn.addAlias( 20, "Veinte" );
142
m_mn.add( 30, "Thirty" );
144
String text = m_mn.getText(10);
145
assertEquals( "TEN", text );
147
text = m_mn.getText(20);
148
assertEquals( "TWENTY", text );
150
text = m_mn.getText(30);
151
assertEquals( "THIRTY", text );
153
text = m_mn.getText(40);
154
assertEquals( "40", text );
156
int value = m_mn.getValue("tEn");
157
assertEquals(10, value);
159
value = m_mn.getValue("twenty");
160
assertEquals(20, value);
162
value = m_mn.getValue("VeiNTe");
163
assertEquals(20, value);
165
value = m_mn.getValue("THIRTY");
166
assertEquals(30, value);
169
public void test_basic_operation_lower()
171
m_mn = new Mnemonic(MnemonicTest.class.getName() + " LOWER", Mnemonic.CASE_LOWER);
172
m_mn.add( 10, "Ten" );
173
m_mn.add( 20, "Twenty" );
174
m_mn.addAlias( 20, "Veinte" );
175
m_mn.add( 30, "Thirty" );
177
String text = m_mn.getText(10);
178
assertEquals( "ten", text );
180
text = m_mn.getText(20);
181
assertEquals( "twenty", text );
183
text = m_mn.getText(30);
184
assertEquals( "thirty", text );
186
text = m_mn.getText(40);
187
assertEquals( "40", text );
189
int value = m_mn.getValue("tEn");
190
assertEquals(10, value);
192
value = m_mn.getValue("twenty");
193
assertEquals(20, value);
195
value = m_mn.getValue("VeiNTe");
196
assertEquals(20, value);
198
value = m_mn.getValue("THIRTY");
199
assertEquals(30, value);
202
public void test_basic_operation_sensitive()
204
m_mn = new Mnemonic(MnemonicTest.class.getName() + " SENSITIVE", Mnemonic.CASE_SENSITIVE);
205
m_mn.add( 10, "Ten" );
206
m_mn.add( 20, "Twenty" );
207
m_mn.addAlias( 20, "Veinte" );
208
m_mn.add( 30, "Thirty" );
210
String text = m_mn.getText(10);
211
assertEquals( "Ten", text );
213
text = m_mn.getText(20);
214
assertEquals( "Twenty", text );
216
text = m_mn.getText(30);
217
assertEquals( "Thirty", text );
219
text = m_mn.getText(40);
220
assertEquals( "40", text );
222
int value = m_mn.getValue("Ten");
223
assertEquals(10, value);
225
value = m_mn.getValue("twenty");
226
assertEquals(-1, value);
228
value = m_mn.getValue("Twenty");
229
assertEquals(20, value);
231
value = m_mn.getValue("VEINTE");
232
assertEquals(-1, value);
234
value = m_mn.getValue("Veinte");
235
assertEquals(20, value);
237
value = m_mn.getValue("Thirty");
238
assertEquals(30, value);
241
public void test_invalid_numeric()
243
m_mn.setNumericAllowed(true);
244
int value = m_mn.getValue("Not-A-Number");
245
assertEquals(-1, value);
248
public void test_addAll()
250
m_mn.add( 10, "Ten" );
251
m_mn.add( 20, "Twenty" );
253
Mnemonic mn2 = new Mnemonic("second test Mnemonic", Mnemonic.CASE_UPPER);
254
mn2.add( 20, "Twenty" );
255
mn2.addAlias( 20, "Veinte" );
256
mn2.add( 30, "Thirty" );
260
String text = m_mn.getText(10);
261
assertEquals( "TEN", text );
263
text = m_mn.getText(20);
264
assertEquals( "TWENTY", text );
266
text = m_mn.getText(30);
267
assertEquals( "THIRTY", text );
269
text = m_mn.getText(40);
270
assertEquals( "40", text );
272
int value = m_mn.getValue("tEn");
273
assertEquals(10, value);
275
value = m_mn.getValue("twenty");
276
assertEquals(20, value);
278
value = m_mn.getValue("VeiNTe");
279
assertEquals(20, value);
281
value = m_mn.getValue("THIRTY");
282
assertEquals(30, value);