~ubuntu-branches/ubuntu/lucid/dnsjava/lucid

« back to all changes in this revision

Viewing changes to tests/org/xbill/DNS/MnemonicTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Thierry Carrez
  • Date: 2009-07-21 15:17:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090721151703-6v0107p1s3h7gv1c
Tags: upstream-2.0.6
ImportĀ upstreamĀ versionĀ 2.0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Java -*-
 
2
//
 
3
// Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
 
4
// Copyright (c) 2005, University of Colorado at Boulder
 
5
// All rights reserved.
 
6
// 
 
7
// Redistribution and use in source and binary forms, with or without
 
8
// modification, are permitted provided that the following conditions are
 
9
// met:
 
10
// 
 
11
// * Redistributions of source code must retain the above copyright
 
12
//   notice, this list of conditions and the following disclaimer.
 
13
// 
 
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.
 
17
// 
 
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
 
21
//   permission.
 
22
// 
 
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.
 
34
//
 
35
package org.xbill.DNS;
 
36
 
 
37
// Mnemonic has package-level access.
 
38
 
 
39
import junit.framework.TestCase;
 
40
 
 
41
public class MnemonicTest extends TestCase
 
42
{
 
43
    private Mnemonic m_mn;
 
44
 
 
45
    public MnemonicTest( String name )
 
46
    {
 
47
        super( name );
 
48
    }
 
49
    
 
50
    public void setUp()
 
51
    {
 
52
        m_mn = new Mnemonic(MnemonicTest.class.getName() + " UPPER", Mnemonic.CASE_UPPER);
 
53
    }
 
54
 
 
55
    public void test_toInteger()
 
56
    {
 
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 );
 
62
 
 
63
        i = Mnemonic.toInteger(-1);
 
64
        assertEquals( new Integer(-1), i );
 
65
        i2 = Mnemonic.toInteger(-1);
 
66
        assertEquals( i, i2 );
 
67
        assertNotSame( i, i2 );
 
68
 
 
69
        i = Mnemonic.toInteger(0);
 
70
        assertEquals( new Integer(0), i );
 
71
        i2 = Mnemonic.toInteger(0);
 
72
        assertEquals( i, i2 );
 
73
        assertSame( i, i2 );
 
74
 
 
75
        i = Mnemonic.toInteger(63);
 
76
        assertEquals( new Integer(63), i );
 
77
        i2 = Mnemonic.toInteger(63);
 
78
        assertEquals( i, i2 );
 
79
        assertSame( i, i2 );
 
80
    }
 
81
 
 
82
    public void test_no_maximum()
 
83
    {
 
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());}
 
87
 
 
88
        m_mn.setNumericAllowed(true);
 
89
 
 
90
        int val = m_mn.getValue("-2");
 
91
        assertEquals( -1, val );
 
92
        
 
93
        val = m_mn.getValue("0");
 
94
        assertEquals( 0, val );
 
95
       
 
96
        val = m_mn.getValue("" + Integer.MAX_VALUE);
 
97
        assertEquals( Integer.MAX_VALUE, val );
 
98
    }
 
99
 
 
100
    public void test_setMaximum()
 
101
    {
 
102
        m_mn.setMaximum(15);
 
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 ){}
 
107
 
 
108
        // need numericok to exercise the usage of max in parseNumeric
 
109
        m_mn.setNumericAllowed(true);
 
110
        
 
111
        int val = m_mn.getValue("-2");
 
112
        assertEquals( -1, val );
 
113
        
 
114
        val = m_mn.getValue( "0" );
 
115
        assertEquals( 0, val );
 
116
 
 
117
        val = m_mn.getValue( "15" );
 
118
        assertEquals( 15, val );
 
119
 
 
120
        val = m_mn.getValue( "16" );
 
121
        assertEquals( -1, val );
 
122
    }
 
123
 
 
124
    public void test_setPrefix()
 
125
    {
 
126
        final String prefix = "A mixed CASE Prefix".toUpperCase();
 
127
        m_mn.setPrefix(prefix);
 
128
 
 
129
        String out = m_mn.getText(10);
 
130
        assertEquals( prefix + "10", out );
 
131
 
 
132
        int i = m_mn.getValue( out );
 
133
        assertEquals( 10, i );
 
134
    }
 
135
 
 
136
    public void test_basic_operation()
 
137
    {
 
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" );
 
143
 
 
144
        String text = m_mn.getText(10);
 
145
        assertEquals( "TEN", text );
 
146
        
 
147
        text = m_mn.getText(20);
 
148
        assertEquals( "TWENTY", text );
 
149
        
 
150
        text = m_mn.getText(30);
 
151
        assertEquals( "THIRTY", text );
 
152
 
 
153
        text = m_mn.getText(40);
 
154
        assertEquals( "40", text );
 
155
 
 
156
        int value = m_mn.getValue("tEn");
 
157
        assertEquals(10, value);
 
158
 
 
159
        value = m_mn.getValue("twenty");
 
160
        assertEquals(20, value);
 
161
 
 
162
        value = m_mn.getValue("VeiNTe");
 
163
        assertEquals(20, value);
 
164
 
 
165
        value = m_mn.getValue("THIRTY");
 
166
        assertEquals(30, value);
 
167
    }
 
168
 
 
169
    public void test_basic_operation_lower()
 
170
    {
 
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" );
 
176
 
 
177
        String text = m_mn.getText(10);
 
178
        assertEquals( "ten", text );
 
179
        
 
180
        text = m_mn.getText(20);
 
181
        assertEquals( "twenty", text );
 
182
        
 
183
        text = m_mn.getText(30);
 
184
        assertEquals( "thirty", text );
 
185
 
 
186
        text = m_mn.getText(40);
 
187
        assertEquals( "40", text );
 
188
 
 
189
        int value = m_mn.getValue("tEn");
 
190
        assertEquals(10, value);
 
191
 
 
192
        value = m_mn.getValue("twenty");
 
193
        assertEquals(20, value);
 
194
 
 
195
        value = m_mn.getValue("VeiNTe");
 
196
        assertEquals(20, value);
 
197
 
 
198
        value = m_mn.getValue("THIRTY");
 
199
        assertEquals(30, value);
 
200
    }
 
201
 
 
202
    public void test_basic_operation_sensitive()
 
203
    {
 
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" );
 
209
 
 
210
        String text = m_mn.getText(10);
 
211
        assertEquals( "Ten", text );
 
212
        
 
213
        text = m_mn.getText(20);
 
214
        assertEquals( "Twenty", text );
 
215
        
 
216
        text = m_mn.getText(30);
 
217
        assertEquals( "Thirty", text );
 
218
 
 
219
        text = m_mn.getText(40);
 
220
        assertEquals( "40", text );
 
221
 
 
222
        int value = m_mn.getValue("Ten");
 
223
        assertEquals(10, value);
 
224
 
 
225
        value = m_mn.getValue("twenty");
 
226
        assertEquals(-1, value);
 
227
 
 
228
        value = m_mn.getValue("Twenty");
 
229
        assertEquals(20, value);
 
230
 
 
231
        value = m_mn.getValue("VEINTE");
 
232
        assertEquals(-1, value);
 
233
 
 
234
        value = m_mn.getValue("Veinte");
 
235
        assertEquals(20, value);
 
236
 
 
237
        value = m_mn.getValue("Thirty");
 
238
        assertEquals(30, value);
 
239
    }
 
240
 
 
241
    public void test_invalid_numeric()
 
242
    {
 
243
        m_mn.setNumericAllowed(true);
 
244
        int value = m_mn.getValue("Not-A-Number");
 
245
        assertEquals(-1, value);
 
246
    }
 
247
 
 
248
    public void test_addAll()
 
249
    {
 
250
        m_mn.add( 10, "Ten" );
 
251
        m_mn.add( 20, "Twenty" );
 
252
 
 
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" );
 
257
 
 
258
        m_mn.addAll( mn2 );
 
259
 
 
260
        String text = m_mn.getText(10);
 
261
        assertEquals( "TEN", text );
 
262
        
 
263
        text = m_mn.getText(20);
 
264
        assertEquals( "TWENTY", text );
 
265
        
 
266
        text = m_mn.getText(30);
 
267
        assertEquals( "THIRTY", text );
 
268
 
 
269
        text = m_mn.getText(40);
 
270
        assertEquals( "40", text );
 
271
 
 
272
        int value = m_mn.getValue("tEn");
 
273
        assertEquals(10, value);
 
274
 
 
275
        value = m_mn.getValue("twenty");
 
276
        assertEquals(20, value);
 
277
 
 
278
        value = m_mn.getValue("VeiNTe");
 
279
        assertEquals(20, value);
 
280
 
 
281
        value = m_mn.getValue("THIRTY");
 
282
        assertEquals(30, value);
 
283
    }
 
284
}