1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.server.core.partition.impl.btree.jdbm;
21
22 import java.io.IOException;
23
24 import org.apache.commons.lang.RandomStringUtils;
25
26 import junit.framework.TestCase;
27
28 /**
29 *
30 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31 * @version $Rev$, $Date$
32 */
33 public class StringSerializerTest extends TestCase
34 {
35 public void testRandom() throws IOException
36 {
37 StringSerializer serializer = new StringSerializer();
38 for ( int ii = 0; ii < 100; ii++ )
39 {
40 String str = RandomStringUtils.random( ii );
41 byte [] serialized = serializer.serialize( str );
42 String deserialized = ( String ) serializer.deserialize( serialized );
43 assertEquals( str, deserialized );
44 }
45 }
46
47
48 char getChar( byte[] bites )
49 {
50 int ch = bites[0] << 8 & 0x0000FF00;
51 ch |= bites[1] & 0x000000FF;
52 return ( char ) ch;
53 }
54
55
56 byte[] getBytes( char ch )
57 {
58 byte[] bites = new byte[2];
59 bites[0] = ( byte ) ( ch >> 8 & 0x00FF );
60 bites[1] = ( byte ) ( ch & 0x00FF );
61 return bites;
62 }
63
64
65 public void testConversion()
66 {
67 for ( char ch = 0; ch < 16383; ch++ )
68 {
69 byte[] bites = getBytes( ch );
70 char deserialized = getChar( bites );
71 assertEquals( ch, deserialized );
72 }
73 }
74 }