17
17
* CDDL HEADER END
18
18
*/
19
19
20
- /*
20
+ /*
21
21
* Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
22
22
*/
23
23
package org .opensolaris .opengrok .web ;
28
28
import java .io .StringWriter ;
29
29
import java .text .SimpleDateFormat ;
30
30
import java .util .Arrays ;
31
+ import java .util .List ;
31
32
import javax .xml .parsers .DocumentBuilder ;
32
33
import javax .xml .parsers .DocumentBuilderFactory ;
33
34
import org .junit .After ;
34
35
import org .junit .AfterClass ;
35
36
import org .junit .Before ;
36
37
import org .junit .BeforeClass ;
37
38
import org .junit .Test ;
39
+ import org .opensolaris .opengrok .configuration .RuntimeEnvironment ;
40
+ import org .opensolaris .opengrok .history .RepositoryFactory ;
38
41
import org .opensolaris .opengrok .util .FileUtilities ;
39
42
import org .w3c .dom .Document ;
40
43
import org .w3c .dom .Element ;
41
44
import org .w3c .dom .Node ;
42
45
import org .w3c .dom .NodeList ;
46
+
43
47
import static org .junit .Assert .*;
44
- import org .opensolaris .opengrok .configuration .RuntimeEnvironment ;
45
- import org .opensolaris .opengrok .history .RepositoryFactory ;
46
48
47
49
/**
48
50
* JUnit test to test that the DirectoryListing produce the expected result
49
51
*/
50
52
public class DirectoryListingTest {
51
53
54
+ /**
55
+ * Indication of that the file was a directory and so that the size given by
56
+ * the FS is platform dependent.
57
+ */
58
+ private static final int DIRECTORY_INTERNAL_SIZE = -2 ;
59
+ /**
60
+ * Indication of unparseable file size.
61
+ */
62
+ private static final int INVALID_SIZE = -1 ;
63
+
52
64
private File directory ;
53
65
private FileEntry [] entries ;
54
66
private SimpleDateFormat dateFormatter ;
55
67
56
68
class FileEntry implements Comparable {
69
+
57
70
String name ;
58
71
String href ;
59
72
long lastModified ;
60
- int size ; // If negative, don't check it.
73
+ /**
74
+ * May be:
75
+ * <pre>
76
+ * positive integer - for a file
77
+ * -2 - for a directory
78
+ * -1 - for an unparseable size
79
+ * </pre>
80
+ */
81
+ int size ;
82
+ List <FileEntry > subdirs ;
61
83
62
84
FileEntry () {
63
85
dateFormatter = new SimpleDateFormat ("dd-MMM-yyyy" );
64
86
}
65
87
66
- FileEntry (String name , String href , long lastModified , int size ) {
88
+ private FileEntry (String name , String href , long lastModified , int size , List <FileEntry > subdirs ) {
89
+ this ();
67
90
this .name = name ;
68
91
this .href = href ;
69
92
this .lastModified = lastModified ;
70
93
this .size = size ;
94
+ this .subdirs = subdirs ;
95
+ }
96
+
97
+ /**
98
+ * Creating the directory entry.
99
+ *
100
+ * @param name name of the file
101
+ * @param href href to the file
102
+ * @param lastModified date of last modification
103
+ * @param subdirs list of sub entries (may be empty)
104
+ */
105
+ FileEntry (String name , String href , long lastModified , List <FileEntry > subdirs ) {
106
+ this (name , href , lastModified , DIRECTORY_INTERNAL_SIZE , subdirs );
107
+ assertNotNull (subdirs );
108
+ }
109
+
110
+ /**
111
+ * Creating a regular file entry.
112
+ *
113
+ * @param name name of the file
114
+ * @param href href to the file
115
+ * @param lastModified date of last modification
116
+ * @param size the desired size of the file on the disc
117
+ */
118
+ FileEntry (String name , String href , long lastModified , int size ) {
119
+ this (name , href , lastModified , size , null );
71
120
}
72
121
73
122
private void create () throws Exception {
74
123
File file = new File (directory , name );
75
- if (!file .exists ()) {
124
+
125
+ if (subdirs != null && subdirs .size () > 0 ) {
126
+ // this is a directory
127
+ assertTrue ("Failed to create a directory" , file .mkdirs ());
128
+ for (FileEntry entry : subdirs ) {
129
+ entry .name = name + File .separator + entry .name ;
130
+ entry .create ();
131
+ }
132
+ } else {
76
133
assertTrue ("Failed to create file" , file .createNewFile ());
77
134
}
78
135
@@ -82,35 +139,37 @@ private void create() throws Exception {
82
139
}
83
140
84
141
assertTrue ("Failed to set modification time" ,
85
- file .setLastModified (val ));
142
+ file .setLastModified (val ));
86
143
87
- if (size > 0 ) {
88
- FileOutputStream out = new FileOutputStream (file );
89
- byte [] buffer = new byte [size ];
90
- out .write (buffer );
91
- out . close ();
144
+ if (subdirs == null && size > 0 ) {
145
+ try ( FileOutputStream out = new FileOutputStream (file )) {
146
+ byte [] buffer = new byte [size ];
147
+ out .write (buffer );
148
+ }
92
149
}
93
150
}
94
151
152
+ @ Override
95
153
public int compareTo (Object o ) {
96
154
int ret = -1 ;
97
155
98
156
if (o instanceof FileEntry ) {
99
157
FileEntry fe = (FileEntry ) o ;
100
158
101
159
// @todo verify all attributes!
102
- if (name .compareTo (fe .name ) == 0 &&
103
- href .compareTo (fe .href ) == 0 ) {
104
- ret = 0 ;
105
- }
106
- // Negative size is not verified.
107
- if (size >= 0 && size == fe .size ) {
108
- ret = 0 ;
160
+ if (name .compareTo (fe .name ) == 0
161
+ && href .compareTo (fe .href ) == 0 ) {
162
+ if ( // this is a file so the size must be exact
163
+ (subdirs == null && size == fe .size )
164
+ // this is a directory so the size must have been "-" char
165
+ || (subdirs != null && size == DIRECTORY_INTERNAL_SIZE )) {
166
+ ret = 0 ;
167
+ }
109
168
}
110
169
}
111
-
112
170
return ret ;
113
171
}
172
+
114
173
}
115
174
116
175
public DirectoryListingTest () {
@@ -129,28 +188,28 @@ public void setUp() throws Exception {
129
188
directory = FileUtilities .createTemporaryDirectory ("directory" );
130
189
131
190
entries = new FileEntry [3 ];
132
- entries [0 ] = new FileEntry ("foo.c" , "foo.c" , 0 , 1 );
191
+ entries [0 ] = new FileEntry ("foo.c" , "foo.c" , 0 , 112 );
133
192
entries [1 ] = new FileEntry ("bar.h" , "bar.h" , Long .MAX_VALUE , 0 );
134
- entries [2 ] = null ;
193
+ // Will test getSimplifiedPath() behavior for ignored directories.
194
+ // Use DIRECTORY_INTERNAL_SIZE value for length so it is checked as the directory
195
+ // should contain "-" (DIRECTORY_SIZE_PLACEHOLDER) string.
196
+ entries [2 ] = new FileEntry ("subdir" , "subdir/" , 0 , Arrays .asList (
197
+ new FileEntry []{
198
+ new FileEntry ("SCCS" , "SCCS/" , 0 , Arrays .asList (
199
+ new FileEntry []{
200
+ new FileEntry ("version" , "version" , 0 , 312 )
201
+ })
202
+ )}
203
+ ));
135
204
136
205
for (FileEntry entry : entries ) {
137
- if (entry != null ) {
138
- entry .create ();
139
- }
206
+ entry .create ();
140
207
}
208
+
141
209
// Create the entry that will be ignored separately.
142
210
FileEntry hgtags = new FileEntry (".hgtags" , ".hgtags" , 0 , 1 );
143
211
hgtags .create ();
144
212
145
- // Will test getSimplifiedPath() behavior for ignored directories.
146
- // Use negative value for length so it is not checked as the return value
147
- // of length() is unspecified for directories.
148
- entries [2 ] = new FileEntry ("subdir" , "subdir/" , 0 , -1 );
149
- File subdir = new File (directory , "subdir" );
150
- subdir .mkdir ();
151
- File SCCSdir = new File (subdir , "SCCS" );
152
- SCCSdir .mkdir ();
153
-
154
213
// Need to populate list of ignored entries for all repository types.
155
214
RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
156
215
RepositoryFactory .setIgnored (env );
@@ -160,14 +219,14 @@ public void setUp() throws Exception {
160
219
public void tearDown () throws Exception {
161
220
if (directory != null && directory .exists ()) {
162
221
removeDirectory (directory );
222
+ directory .delete ();
163
223
}
164
224
}
165
225
166
226
private void removeDirectory (File dir ) {
167
227
File [] childs = dir .listFiles ();
168
228
if (childs != null ) {
169
229
for (File f : childs ) {
170
-
171
230
if (f .isDirectory ()) {
172
231
removeDirectory (f );
173
232
}
@@ -177,8 +236,8 @@ private void removeDirectory(File dir) {
177
236
}
178
237
179
238
/**
180
- * Get the href attribute from: <td align="left"><tt><a href="foo"
181
- * class="p">foo</a></tt></td>
239
+ * Get the href attribute from: <td align="left"><tt><a
240
+ * href="foo" class="p">foo</a></tt></td>
182
241
*
183
242
* @param item
184
243
* @return
@@ -226,6 +285,7 @@ private String getFilename(Node item) throws Exception {
226
285
227
286
/**
228
287
* Get the LastModified date from the <td>date</td>
288
+ *
229
289
* @todo fix the item
230
290
* @param item the node representing <td>
231
291
* @return last modified date of the file
@@ -238,25 +298,36 @@ private long getLastModified(Node item) throws Exception {
238
298
239
299
String value = val .getNodeValue ();
240
300
return value .equalsIgnoreCase ("Today" )
241
- ? Long .MAX_VALUE
242
- : dateFormatter .parse (value ).getTime ();
301
+ ? Long .MAX_VALUE
302
+ : dateFormatter .parse (value ).getTime ();
243
303
}
244
304
245
305
/**
246
306
* Get the size from the: <td><tt>size</tt></td>
307
+ *
247
308
* @param item the node representing <td>
248
- * @return The size
249
- * @throws java.lang.Exception if an error occurs
309
+ * @return positive integer if the record was a file<br>
310
+ * -1 if the size could not be parsed<br>
311
+ * -2 if the record was a directory<br>
250
312
*/
251
- private int getSize (Node item ) throws Exception {
313
+ private int getSize (Node item ) throws NumberFormatException {
252
314
Node val = item .getFirstChild ();
253
315
assertNotNull (val );
254
316
assertEquals (Node .TEXT_NODE , val .getNodeType ());
255
- return Integer .parseInt (val .getNodeValue ().trim ());
317
+ if (DirectoryListing .DIRECTORY_SIZE_PLACEHOLDER .equals (val .getNodeValue ().trim ())) {
318
+ // track that it had the DIRECTORY_SIZE_PLACEHOLDER character
319
+ return DIRECTORY_INTERNAL_SIZE ;
320
+ }
321
+ try {
322
+ return Integer .parseInt (val .getNodeValue ().trim ());
323
+ } catch (NumberFormatException ex ) {
324
+ return INVALID_SIZE ;
325
+ }
256
326
}
257
327
258
328
/**
259
329
* Validate this file-entry in the table
330
+ *
260
331
* @param element The <tr> element
261
332
* @throws java.lang.Exception
262
333
*/
@@ -274,11 +345,7 @@ private void validateEntry(Element element) throws Exception {
274
345
entry .name = getFilename (nl .item (1 ));
275
346
entry .href = getHref (nl .item (1 ));
276
347
entry .lastModified = getLastModified (nl .item (3 ));
277
- try {
278
- entry .size = getSize (nl .item (4 ));
279
- } catch (Exception e ) {
280
- entry .size = -1 ;
281
- }
348
+ entry .size = getSize (nl .item (4 ));
282
349
283
350
// Try to look it up in the list of files.
284
351
for (int ii = 0 ; ii < entries .length ; ++ii ) {
@@ -293,8 +360,8 @@ private void validateEntry(Element element) throws Exception {
293
360
294
361
/**
295
362
* Test directory listing
296
- * @throws java.lang.Exception if an error occurs while generating the
297
- * list.
363
+ *
364
+ * @throws java.lang.Exception if an error occurs while generating the list.
298
365
*/
299
366
@ Test
300
367
public void directoryListing () throws Exception {
@@ -303,7 +370,7 @@ public void directoryListing() throws Exception {
303
370
304
371
DirectoryListing instance = new DirectoryListing ();
305
372
instance .listTo ("ctx" , directory , out , directory .getPath (),
306
- Arrays .asList (directory .list ()));
373
+ Arrays .asList (directory .list ()));
307
374
308
375
DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
309
376
assertNotNull ("DocumentBuilderFactory is null" , factory );
@@ -313,7 +380,6 @@ public void directoryListing() throws Exception {
313
380
314
381
out .append ("</start>\n " );
315
382
String str = out .toString ();
316
- System .out .println (str );
317
383
Document document = builder .parse (new ByteArrayInputStream (str .getBytes ()));
318
384
319
385
NodeList nl = document .getElementsByTagName ("tr" );
0 commit comments