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 package org.melati.util.test;
50
51 import java.util.Arrays;
52
53 import junit.framework.TestCase;
54
55 import org.melati.util.AcceptCharset;
56
57
58
59
60
61
62
63 public class AcceptCharsetTest extends TestCase {
64
65
66
67
68 public AcceptCharsetTest(String testCaseName) {
69 super(testCaseName);
70 }
71
72
73 public void testXmacRoman() throws Exception {
74
75 String headerValue = "x-mac-roman,utf-8;q=0.87,*;q=0.7";
76 String supportedPreference[] = new String[] { "UTF-16", "UTF-8",
77 "ISO-8859-1", };
78 AcceptCharset ac = new AcceptCharset(headerValue, Arrays
79 .asList(supportedPreference));
80
81
82 assertEquals("UTF-16", ac.serverChoice());
83 }
84
85
86
87
88 public void testChoices() throws Exception {
89
90 String headerValue = "ISO-8859-2, utf-8;q=0.66, *;q=0.66";
91 String supportedPreference[] = new String[] { "UTF-16", "UTF-8",
92 "ISO-8859-1", };
93 AcceptCharset ac = new AcceptCharset(headerValue, Arrays
94 .asList(supportedPreference));
95 assertEquals("ISO-8859-2", ac.clientChoice());
96 assertEquals("UTF-16", ac.serverChoice());
97
98 headerValue = "utf-8;q=0.66,ISO-8859-3,ISO-8859-2";
99 supportedPreference = new String[] { "ISO-8859-1", "UTF-16", "UTF-8",
100 "BOLLOX", };
101
102 ac = new AcceptCharset(headerValue, Arrays.asList(supportedPreference));
103 assertEquals("ISO-8859-3", ac.clientChoice());
104 assertEquals("ISO-8859-1", ac.serverChoice());
105
106 headerValue = "*;q=0.0";
107 supportedPreference = new String[] { "UTF-16", "UTF-8", "BOLLOX",
108 "ISO-8859-1", };
109 ac = new AcceptCharset(headerValue, Arrays.asList(supportedPreference));
110 assertEquals(null, ac.clientChoice());
111 assertEquals(null, ac.serverChoice());
112
113 ac = new AcceptCharset(null, Arrays.asList(supportedPreference));
114 assertEquals("ISO-8859-1", ac.clientChoice());
115 assertEquals("ISO-8859-1", ac.serverChoice());
116
117 }
118
119 public void testSensibleDefault() throws Exception {
120 String supportedPreference[] = new String[] { "UTF-16", "UTF-8",
121 "ISO-8859-1", };
122 AcceptCharset ac = new AcceptCharset("", Arrays
123 .asList(supportedPreference));
124 assertEquals("ISO-8859-1", ac.clientChoice());
125 assertEquals("ISO-8859-1", ac.serverChoice());
126
127 }
128
129 public void testSensibleDefaultForRubbish() throws Exception {
130 String supportedPreference[] = new String[] { "UTF-16", "UTF-8",
131 "ISO-8859-1", };
132 AcceptCharset ac = new AcceptCharset("BOLLOX", Arrays
133 .asList(supportedPreference));
134 assertEquals("ISO-8859-1", ac.clientChoice());
135 assertEquals("ISO-8859-1", ac.serverChoice());
136
137 }
138 }