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  
21  package org.apache.directory.server.dns.io.encoder;
22  
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.directory.server.dns.messages.QuestionRecord;
27  import org.apache.directory.server.dns.messages.RecordClass;
28  import org.apache.directory.server.dns.messages.RecordType;
29  import org.apache.mina.common.ByteBuffer;
30  
31  
32  /**
33   * Tests for the Question record encoder.
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   * @version $Rev: 501160 $, $Date: 2007-01-29 12:41:33 -0700 (Mon, 29 Jan 2007) $
37   */
38  public class QuestionRecordEncoderTest extends TestCase
39  {
40      ByteBuffer expectedData;
41  
42      QuestionRecordEncoder encoder;
43  
44      String name = "www.apache.org";
45      String[] nameParts = name.split( "\\." );
46      RecordType type = RecordType.A;
47      RecordClass rClass = RecordClass.IN;
48  
49      QuestionRecord record = new QuestionRecord( name, type, rClass );
50  
51  
52      public void setUp()
53      {
54          encoder = new QuestionRecordEncoder();
55  
56          expectedData = ByteBuffer.allocate( 128 );
57          expectedData.put( ( byte ) nameParts[0].length() ); // 1
58          expectedData.put( nameParts[0].getBytes() ); // + 3
59          expectedData.put( ( byte ) nameParts[1].length() ); // + 1
60          expectedData.put( nameParts[1].getBytes() ); // + 6
61          expectedData.put( ( byte ) nameParts[2].length() ); // + 1
62          expectedData.put( nameParts[2].getBytes() ); // + 3
63          expectedData.put( ( byte ) 0x00 ); // + 1 = 16
64          expectedData.putShort( type.convert() );
65          expectedData.putShort( rClass.convert() );
66      }
67  
68  
69      public void testEncode()
70      {
71          ByteBuffer out = ByteBuffer.allocate( 128 );
72          encoder.put( out, record );
73          assertEquals( expectedData, out );
74      }
75  }