1 package wjhk.jupload2.upload.helper;
2
3 import java.util.Arrays;
4
5 import org.junit.Assert;
6 import org.junit.Before;
7 import org.junit.Test;
8
9 import wjhk.jupload2.exception.JUploadIOException;
10 import wjhk.jupload2.upload.AbstractJUploadTestHelper;
11
12
13
14
15
16
17
18 public class ByteArrayEncoderHTTPTest extends AbstractJUploadTestHelper {
19
20 ByteArrayEncoderHTTP byteArrayEncoderHTTP = null;
21
22 final static String testCase = "A string, with accents: éèùà\u00f8\u00e5\u00d8\u00e6";
23
24 String boundary = "A boundary";
25
26 String encoding = null;
27
28 byte[] target = null;
29
30
31
32
33 @Before
34 public void setUp() throws Exception {
35 this.byteArrayEncoderHTTP = new ByteArrayEncoderHTTP(
36 this.juploadContext.getUploadPolicy(), this.boundary,
37 ByteArrayEncoderHTTP.DEFAULT_ENCODING);
38 this.encoding = this.byteArrayEncoderHTTP.getEncoding();
39 this.target = testCase.getBytes(this.encoding);
40 }
41
42
43
44
45
46 @Test
47 public void testByteArrayEncoderHTTPUploadPolicy() throws Exception {
48 this.byteArrayEncoderHTTP.close();
49 this.byteArrayEncoderHTTP = new ByteArrayEncoderHTTP(
50 this.juploadContext.getUploadPolicy());
51
52 }
53
54
55
56
57
58 @Test
59 public void testByteArrayEncoderHTTPUploadPolicyString() throws Exception {
60 this.byteArrayEncoderHTTP.close();
61 this.byteArrayEncoderHTTP = new ByteArrayEncoderHTTP(
62 this.juploadContext.getUploadPolicy(), null);
63 this.byteArrayEncoderHTTP.close();
64 this.byteArrayEncoderHTTP = new ByteArrayEncoderHTTP(
65 this.juploadContext.getUploadPolicy(), "A boundary");
66
67 }
68
69
70
71
72
73 @Test
74 public void testByteArrayEncoderHTTPUploadPolicyStringString()
75 throws Exception {
76 this.byteArrayEncoderHTTP.close();
77 this.byteArrayEncoderHTTP = new ByteArrayEncoderHTTP(
78 this.juploadContext.getUploadPolicy(), "A boundary",
79 ByteArrayEncoderHTTP.DEFAULT_ENCODING);
80
81 }
82
83 private void finishTestAppend() throws Exception {
84 this.byteArrayEncoderHTTP.close();
85 byte[] result = this.byteArrayEncoderHTTP.getEncodedByteArray();
86 Assert.assertTrue(Arrays.equals(result, this.target));
87 }
88
89
90
91
92
93 @Test
94 public void testAppendString() throws Exception {
95 this.byteArrayEncoderHTTP.append(testCase);
96 finishTestAppend();
97 }
98
99
100
101
102
103 @Test
104 public void testAppendInt() throws Exception {
105 this.byteArrayEncoderHTTP.append(65);
106 this.byteArrayEncoderHTTP.close();
107 byte[] result = this.byteArrayEncoderHTTP.getEncodedByteArray();
108 byte[] targetInt = { 65 };
109 Assert.assertTrue(Arrays.equals(result, targetInt));
110 }
111
112
113
114
115
116 @Test
117 public void testAppendByteArray() throws Exception {
118 this.byteArrayEncoderHTTP.append(this.target);
119 finishTestAppend();
120 }
121
122
123
124
125
126 @Test
127 public void testAppendByteArrayEncoder() throws Exception {
128 ByteArrayEncoderHTTP bae = new ByteArrayEncoderHTTP(this.juploadContext
129 .getUploadPolicy());
130 bae.append(ByteArrayEncoderHTTPTest.testCase);
131 bae.close();
132
133 this.byteArrayEncoderHTTP.append(bae);
134 finishTestAppend();
135 }
136
137
138
139
140
141 @Test
142 public void testAppendTextProperty() throws Exception {
143 String name = "A name";
144 String value = "A value";
145
146
147 StringBuffer sb = new StringBuffer();
148 sb.append(this.byteArrayEncoderHTTP.getBoundary()).append("\r\n");
149 sb.append("Content-Disposition: form-data; name=\"").append(name)
150 .append("\"\r\n");
151 sb.append("Content-Transfer-Encoding: 8bit\r\n");
152 sb.append("Content-Type: text/plain; charset=").append(this.encoding).append(
153 "\r\n");
154 sb.append("\r\n");
155 sb.append(value).append("\r\n");
156 this.target = sb.toString().getBytes(this.encoding);
157
158
159 this.byteArrayEncoderHTTP.appendTextProperty(name, value, -1);
160 finishTestAppend();
161 }
162
163
164
165
166
167 @Test
168 public void testAppendEndPropertyList() throws Exception {
169
170 StringBuffer sb = new StringBuffer();
171 sb.append(this.byteArrayEncoderHTTP.getBoundary()).append("--\r\n");
172 this.target = sb.toString().getBytes(this.encoding);
173
174
175 this.byteArrayEncoderHTTP.appendEndPropertyList();
176 finishTestAppend();
177 }
178
179
180
181
182
183 @Test
184 public void testGetBoundary() throws Exception {
185 Assert.assertTrue("Boundary should be the one given on creation",
186 this.boundary.equals(this.byteArrayEncoderHTTP.getBoundary()));
187 }
188
189
190
191
192
193 @Test
194 public void testGetDefaultEncoding() throws Exception {
195 Assert.assertTrue("UTF-8".equals(ByteArrayEncoderHTTP
196 .getDefaultEncoding()));
197 }
198
199
200
201
202
203 @Test
204 public void testIsClosed() throws Exception {
205 Assert
206 .assertTrue(this.byteArrayEncoderHTTP.isClosed() == this.byteArrayEncoderHTTP.closed);
207 }
208
209
210
211
212
213 @Test
214 public void testGetEncoding() throws Exception {
215 Assert.assertTrue(this.byteArrayEncoderHTTP.getEncoding().equals(
216 this.encoding));
217 }
218
219
220
221
222
223 @Test
224 public void testGetEncodedLength() throws Exception {
225 testAppendString();
226 Assert
227 .assertTrue(this.byteArrayEncoderHTTP.getEncodedLength() == this.target.length);
228 }
229
230
231
232
233
234 @Test
235 public void testGetEncodedByteArray() throws Exception {
236 this.byteArrayEncoderHTTP.append("Any string");
237
238 boolean aJUploadIOExceptionWasFired = false;
239 try {
240 this.byteArrayEncoderHTTP.getEncodedByteArray();
241 } catch (JUploadIOException e) {
242 aJUploadIOExceptionWasFired = true;
243 }
244 Assert
245 .assertTrue(
246 "An exception should be fired when getEncodedByteArray is called on a non closed ByteArray",
247 aJUploadIOExceptionWasFired);
248 }
249
250 }