summaryrefslogtreecommitdiffstats
path: root/simple/simple-common/src/test/java/org/simpleframework/common/KeyTest.java
blob: f9056fe5eafb542f1059fb99905c6d0b72e4d963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package org.simpleframework.common;

import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;

/**
 * Test for fast case insensitive mapping for headers that have been taken
 * from the request HTTP header or added to the response HTTP header.
 * 
 * @author Niall Gallagher
 */
public class KeyTest extends TestCase {
   
   public class Index implements Name {
      
      private final String value;
      
      public Index(String value) {
         this.value = value.toLowerCase();
      }
      
      public int hashCode() {
         return value.hashCode();
      }
      
      public boolean equals(Object key) {
         if(key instanceof Name) {
            return key.equals(value);
         }
         if(key instanceof String) {
            return key.equals(value);
         }
         return false;
      }
   }
   
   public interface Name {
      
      public int hashCode();
      public boolean equals(Object value);
   }
   
   public class ArrayName implements Name {
      
      private String cache;
      private byte[] array;
      private int off;
      private int size;
      private int hash;      

      public ArrayName(byte[] array) {
         this(array, 0, array.length);
      }
      
      public ArrayName(byte[] array, int off, int size) {
         this.array = array;
         this.size = size;
         this.off = off;
      }
      
      public boolean equals(Object value) {
         if(value instanceof String) {
            String text = value.toString();
            
            return equals(text);
         }
         return false;
      }
      
      public boolean equals(String value) {
         int length = value.length();
         
         if(length != size) {
            return false;
         }
         for(int i = 0; i < size; i++) {
            int left = value.charAt(i);            
            int right = array[off + i];
            
            if(right >= 'A' && right <= 'Z') {
               right = (right - 'A') + 'a';
            }            
            if(left != right) {
               return false;
            }
         }
         return true;
      }
      
      public int hashCode() {
         int code = hash;
         
         if(code == 0) {
            int pos = off;

            for(int i = 0; i < size; i++) {
               int next = array[pos++];
               
               if(next >= 'A' && next <= 'Z') {
                  next = (next - 'A') + 'a';
               }
               code = 31*code + next;
            }
            hash = code;
         }
         return code;
      }
   } 
   
   public class StringName implements Name {
      
      private final String value;
      private final String key;
      
      public StringName(String value) {
         this.key = value.toLowerCase();
         this.value = value;
      }
      
      public int hashCode() {
         return key.hashCode();
      }

      public boolean equals(Object value) {
         return value.equals(key);
      }
   }
   
   public class NameTable<T> {
      
      private final Map<Name, T> map;
      
      public NameTable() {
         this.map = new HashMap<Name, T>();
      }
   
      public void put(Name key, T value) {
         map.put(key, value);
      }
      
      public void put(String text, T value) {
         Name key = new StringName(text);

         map.put(key, value);
      }

      public T get(String key) {     
         Index index = new Index(key);
    
         return map.get(index);
      }
      
      public T remove(String key) {
         Index index = new Index(key);

         return map.remove(index);
      }
   }
   
   public void testName() {
      Name contentLength = new ArrayName("Content-Length".getBytes());
      Name contentType = new ArrayName("Content-Type".getBytes());
      Name transferEncoding = new ArrayName("Transfer-Encoding".getBytes());
      Name userAgent = new ArrayName("User-Agent".getBytes());      
      NameTable<String> map = new NameTable<String>();

      assertEquals(contentLength.hashCode(), "Content-Length".toLowerCase().hashCode());
      assertEquals(contentType.hashCode(), "Content-Type".toLowerCase().hashCode());
      assertEquals(transferEncoding.hashCode(), "Transfer-Encoding".toLowerCase().hashCode());
      assertEquals(userAgent.hashCode(), "User-Agent".toLowerCase().hashCode());
      
      map.put(contentLength, "1024");
      map.put(contentType, "text/html");
      map.put(transferEncoding, "chunked");
      map.put(userAgent, "Mozilla/4.0");
      map.put("Date", "18/11/1977");
      map.put("Accept", "text/plain, text/html, image/gif");
      
      assertEquals(map.get("Content-Length"), "1024");
      assertEquals(map.get("CONTENT-LENGTH"), "1024");
      assertEquals(map.get("content-length"), "1024");
      assertEquals(map.get("Content-length"), "1024");
      assertEquals(map.get("Content-Type"), "text/html");
      assertEquals(map.get("Transfer-Encoding"), "chunked");
      assertEquals(map.get("USER-AGENT"), "Mozilla/4.0");
      assertEquals(map.get("Accept"), "text/plain, text/html, image/gif");
      assertEquals(map.get("ACCEPT"), "text/plain, text/html, image/gif");
      assertEquals(map.get("accept"), "text/plain, text/html, image/gif");      
      assertEquals(map.get("DATE"), "18/11/1977");
      assertEquals(map.get("Date"), "18/11/1977");
      assertEquals(map.get("date"), "18/11/1977");
   }
}