24
24
25
25
import java .io .ByteArrayInputStream ;
26
26
import java .io .ByteArrayOutputStream ;
27
+ import java .io .File ;
27
28
import java .io .IOException ;
28
29
import java .io .ObjectInputStream ;
29
30
import java .io .ObjectOutputStream ;
@@ -51,14 +52,10 @@ public class DiskFileItemSerializeTest {
51
52
private static final int threshold = 16 ;
52
53
53
54
/**
54
- * Test creation of a field for which the amount of data falls below the
55
- * configured threshold.
55
+ * Helper method to test creation of a field when a repository is used.
56
56
*/
57
- @ Test
58
- public void testBelowThreshold () throws Exception {
59
- // Create the FileItem
60
- byte [] testFieldValueBytes = createContentBytes (threshold - 1 );
61
- FileItem item = createFileItem (testFieldValueBytes );
57
+ public void testInMemoryObject (byte [] testFieldValueBytes , File repository ) {
58
+ FileItem item = createFileItem (testFieldValueBytes , repository );
62
59
63
60
// Check state is as expected
64
61
assertTrue ("Initial: in memory" , item .isInMemory ());
@@ -75,6 +72,24 @@ public void testBelowThreshold() throws Exception {
75
72
// Compare FileItem's (except byte[])
76
73
compareFileItems (item , newItem );
77
74
}
75
+
76
+ /**
77
+ * Helper method to test creation of a field.
78
+ */
79
+ private void testInMemoryObject (byte [] testFieldValueBytes ) {
80
+ testInMemoryObject (testFieldValueBytes , null );
81
+ }
82
+
83
+ /**
84
+ * Test creation of a field for which the amount of data falls below the
85
+ * configured threshold.
86
+ */
87
+ @ Test
88
+ public void testBelowThreshold () throws Exception {
89
+ // Create the FileItem
90
+ byte [] testFieldValueBytes = createContentBytes (threshold - 1 );
91
+ testInMemoryObject (testFieldValueBytes );
92
+ }
78
93
79
94
/**
80
95
* Test creation of a field for which the amount of data equals the
@@ -84,23 +99,7 @@ public void testBelowThreshold() throws Exception {
84
99
public void testThreshold () throws Exception {
85
100
// Create the FileItem
86
101
byte [] testFieldValueBytes = createContentBytes (threshold );
87
- FileItem item = createFileItem (testFieldValueBytes );
88
-
89
- // Check state is as expected
90
- assertTrue ("Initial: in memory" , item .isInMemory ());
91
- assertEquals ("Initial: size" , item .getSize (), testFieldValueBytes .length );
92
- compareBytes ("Initial" , item .get (), testFieldValueBytes );
93
-
94
-
95
- // Serialize & Deserialize
96
- FileItem newItem = (FileItem )serializeDeserialize (item );
97
-
98
- // Test deserialized content is as expected
99
- assertTrue ("Check in memory" , newItem .isInMemory ());
100
- compareBytes ("Check" , testFieldValueBytes , newItem .get ());
101
-
102
- // Compare FileItem's (except byte[])
103
- compareFileItems (item , newItem );
102
+ testInMemoryObject (testFieldValueBytes );
104
103
}
105
104
106
105
/**
@@ -128,6 +127,41 @@ public void testAboveThreshold() throws Exception {
128
127
// Compare FileItem's (except byte[])
129
128
compareFileItems (item , newItem );
130
129
}
130
+
131
+ /**
132
+ * Test serialization and deserialization when repository is not null.
133
+ */
134
+ @ Test
135
+ public void testValidRepository () throws Exception {
136
+ // Create the FileItem
137
+ byte [] testFieldValueBytes = createContentBytes (threshold );
138
+ File repository = new File (System .getProperty ("java.io.tmpdir" ));
139
+ testInMemoryObject (testFieldValueBytes , repository );
140
+ }
141
+
142
+ /**
143
+ * Test deserialization fails when repository is not valid.
144
+ */
145
+ @ Test (expected =IOException .class )
146
+ public void testInvalidRepository () throws Exception {
147
+ // Create the FileItem
148
+ byte [] testFieldValueBytes = createContentBytes (threshold );
149
+ File repository = new File (System .getProperty ("java.io.tmpdir" ) + "file" );
150
+ FileItem item = createFileItem (testFieldValueBytes , repository );
151
+ deserialize (serialize (item ));
152
+ }
153
+
154
+ /**
155
+ * Test deserialization fails when repository contains a null character.
156
+ */
157
+ @ Test (expected =IOException .class )
158
+ public void testInvalidRepositoryWithNullChar () throws Exception {
159
+ // Create the FileItem
160
+ byte [] testFieldValueBytes = createContentBytes (threshold );
161
+ File repository = new File (System .getProperty ("java.io.tmpdir" ) + "\0 " );
162
+ FileItem item = createFileItem (testFieldValueBytes , repository );
163
+ deserialize (serialize (item ));
164
+ }
131
165
132
166
/**
133
167
* Compare FileItem's (except the byte[] content)
@@ -169,10 +203,10 @@ private byte[] createContentBytes(int size) {
169
203
}
170
204
171
205
/**
172
- * Create a FileItem with the specfied content bytes.
206
+ * Create a FileItem with the specfied content bytes and repository .
173
207
*/
174
- private FileItem createFileItem (byte [] contentBytes ) {
175
- FileItemFactory factory = new DiskFileItemFactory (threshold , null );
208
+ private FileItem createFileItem (byte [] contentBytes , File repository ) {
209
+ FileItemFactory factory = new DiskFileItemFactory (threshold , repository );
176
210
String textFieldName = "textField" ;
177
211
178
212
FileItem item = factory .createItem (
@@ -192,33 +226,60 @@ private FileItem createFileItem(byte[] contentBytes) {
192
226
return item ;
193
227
194
228
}
229
+
230
+ /**
231
+ * Create a FileItem with the specfied content bytes.
232
+ */
233
+ private FileItem createFileItem (byte [] contentBytes ) {
234
+ return createFileItem (contentBytes , null );
235
+ }
236
+
237
+ /**
238
+ * Do serialization
239
+ */
240
+ private ByteArrayOutputStream serialize (Object target ) throws Exception {
241
+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
242
+ ObjectOutputStream oos = new ObjectOutputStream (baos );
243
+ oos .writeObject (target );
244
+ oos .flush ();
245
+ oos .close ();
246
+ return baos ;
247
+ }
248
+
249
+ /**
250
+ * Do deserialization
251
+ */
252
+ private Object deserialize (ByteArrayOutputStream baos ) throws Exception {
253
+ Object result = null ;
254
+ ByteArrayInputStream bais =
255
+ new ByteArrayInputStream (baos .toByteArray ());
256
+ ObjectInputStream ois = new ObjectInputStream (bais );
257
+ result = ois .readObject ();
258
+ bais .close ();
195
259
260
+ return result ;
261
+ }
262
+
196
263
/**
197
264
* Do serialization and deserialization.
198
265
*/
199
266
private Object serializeDeserialize (Object target ) {
200
267
// Serialize the test object
201
- ByteArrayOutputStream baos = new ByteArrayOutputStream () ;
268
+ ByteArrayOutputStream baos = null ;
202
269
try {
203
- ObjectOutputStream oos = new ObjectOutputStream (baos );
204
- oos .writeObject (target );
205
- oos .flush ();
206
- oos .close ();
270
+ baos = serialize (target );
207
271
} catch (Exception e ) {
208
272
fail ("Exception during serialization: " + e );
209
273
}
210
-
274
+
211
275
// Deserialize the test object
212
276
Object result = null ;
213
277
try {
214
- ByteArrayInputStream bais =
215
- new ByteArrayInputStream (baos .toByteArray ());
216
- ObjectInputStream ois = new ObjectInputStream (bais );
217
- result = ois .readObject ();
218
- bais .close ();
278
+ result = deserialize (baos );
219
279
} catch (Exception e ) {
220
280
fail ("Exception during deserialization: " + e );
221
281
}
282
+
222
283
return result ;
223
284
}
224
285
0 commit comments