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.dns.io.encoder;
21
22
23 import java.io.IOException;
24 import java.net.UnknownHostException;
25 import java.util.Map;
26
27 import junit.framework.TestCase;
28
29 import org.apache.directory.server.dns.messages.RecordClass;
30 import org.apache.directory.server.dns.messages.RecordType;
31 import org.apache.directory.server.dns.messages.ResourceRecord;
32 import org.apache.directory.server.dns.messages.ResourceRecordImpl;
33 import org.apache.mina.common.ByteBuffer;
34
35
36 /**
37 * A base class for testing different types of ResourceRecordEncoders. It
38 * handles setting up the expected output buffer not having to do specifically
39 * with the resource data, and handles creating the ResourceRecord to be tested.
40 *
41 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42 * @version $Rev: 501160 $, $Date: 2007-01-29 12:41:33 -0700 (Mon, 29 Jan 2007) $
43 */
44 public abstract class AbstractResourceRecordEncoderTest extends TestCase
45 {
46 ByteBuffer expectedData;
47 String domainName = "herse.apache.org";
48 String[] domainNameParts = domainName.split( "\\." );
49 int timeToLive = 3400;
50 ResourceRecord record;
51
52
53 public void setUp() throws UnknownHostException
54 {
55 setUpResourceData();
56 record = new ResourceRecordImpl( domainName, RecordType.A, RecordClass.IN, timeToLive, getAttributes() );
57
58 expectedData = ByteBuffer.allocate( 128 );
59 expectedData.put( ( byte ) 18 );
60 expectedData.put( ( byte ) domainNameParts[0].length() ); // 1
61 expectedData.put( domainNameParts[0].getBytes() ); // + 5
62 expectedData.put( ( byte ) domainNameParts[1].length() ); // + 1
63 expectedData.put( domainNameParts[1].getBytes() ); // + 6
64 expectedData.put( ( byte ) domainNameParts[2].length() ); // + 1
65 expectedData.put( domainNameParts[2].getBytes() ); // + 3
66 expectedData.put( ( byte ) 0x00 ); // + 1 = 18
67 expectedData.putShort( RecordType.A.convert() );
68 expectedData.putShort( RecordClass.IN.convert() );
69 expectedData.putInt( timeToLive );
70 putExpectedResourceData( expectedData );
71 }
72
73
74 public void testEncode() throws IOException
75 {
76 ByteBuffer outBuffer = ByteBuffer.allocate( 128 );
77 getEncoder().put( outBuffer, record );
78 assertEquals( expectedData, outBuffer );
79 }
80
81
82 /**
83 * A method that implementers can override if they need to do some setup
84 * before the resource record and expected data buffer are created, such
85 * as initialize an ip address.
86 */
87 protected void setUpResourceData()
88 {
89 }
90
91
92 /**
93 * @return the encoder to be tested
94 */
95 protected abstract ResourceRecordEncoder getEncoder();
96
97
98 /**
99 * @return the attributes to be used as the resource data for the resource
100 * record
101 */
102 protected abstract Map getAttributes();
103
104
105 /**
106 * Put the encoded resource data into a buffer that will compared to the
107 * result of using the encoder under test.
108 *
109 * @param expectedData buffer where the expected resource data should be put
110 */
111 protected abstract void putExpectedResourceData( ByteBuffer expectedData );
112 }