18
18
*/
19
19
20
20
/*
21
- * Copyright (c) 2008, 2018 , Oracle and/or its affiliates. All rights reserved.
21
+ * Copyright (c) 2008, 2019 , Oracle and/or its affiliates. All rights reserved.
22
22
* Portions Copyright (c) 2017, Chris Fraire <[email protected] >.
23
+ * Portions Copyright (c) 2019, Krystof Tulinger <[email protected] >.
23
24
*/
24
25
package org .opengrok .indexer .history ;
25
26
32
33
import java .io .IOException ;
33
34
import java .io .InputStream ;
34
35
import java .nio .file .Paths ;
35
- import java .text .DateFormat ;
36
36
import java .text .ParseException ;
37
+ import java .util .Arrays ;
37
38
import java .util .Collections ;
38
39
import java .util .HashSet ;
40
+ import java .util .List ;
39
41
import java .util .Set ;
40
42
import org .junit .After ;
41
43
import org .junit .AfterClass ;
@@ -174,6 +176,16 @@ public void testDateFormats() {
174
176
}
175
177
}
176
178
179
+ /**
180
+ * For the following renamed tests the structure in the git repo is as following:
181
+ * <pre>
182
+ * ce4c98ec - new file renamed.c (with some content)
183
+ * b6413947 - renamed.c renamed to moved/renamed.c
184
+ * 1086eaf5 - modification of file moved/renamed.c (different content)
185
+ * 67dfbe26 - moved/renamed.c renamed to moved/renamed2.c
186
+ * 84599b3c - moved/renamed2.c renamed to moved2/renamed2.c
187
+ * </pre>
188
+ */
177
189
@ Test
178
190
public void testRenamedFiles () throws Exception {
179
191
String [][] tests = new String [][]{
@@ -281,18 +293,14 @@ public void testInvalidRenamedFiles() throws Exception {
281
293
* file.
282
294
*
283
295
* @throws java.lang.Exception
296
+ * @see #testRenamedFiles for git repo structure info
284
297
*/
285
298
@ Test
286
- public void testGetHistoryForOnceRenamed () throws Exception {
287
- String exp_str
299
+ public void testGetRenamedFileContent () throws Exception {
300
+ String old_content
288
301
= "#include <stdio.h>\n "
289
302
+ "#include <stdlib.h>\n "
290
303
+ "\n "
291
- + "int foo ( const char * path )\n "
292
- + "{\n "
293
- + "\t return path && *path == 'A';\n "
294
- + "}\n "
295
- + "\n "
296
304
+ "int main ( int argc, const char * argv[] )\n "
297
305
+ "{\n "
298
306
+ "\t int i;\n "
@@ -301,29 +309,10 @@ public void testGetHistoryForOnceRenamed() throws Exception {
301
309
+ "\t \t printf ( \" %s called with %d\\ n\" , argv [ 0 ], argv [ i ] );\n "
302
310
+ "\t }\n "
303
311
+ "\n "
304
- + "\t printf ( \" Hello, world!\\ n\" );\n "
305
- + "\n "
306
- + "\t if ( foo ( argv [ 0 ] ) )\n "
307
- + "\t {\n "
308
- + "\t \t printf ( \" Correct call\\ n\" );\n "
309
- + "\t }\n "
310
- + "\n "
311
312
+ "\t return 0;\n "
312
313
+ "}\n " ;
313
314
314
- runRenamedTest ("moved2/renamed2.c" , "84599b3" , exp_str );
315
- runRenamedTest ("moved/renamed2.c" , "67dfbe2" , exp_str );
316
- }
317
-
318
- /**
319
- * Test that {@code getHistoryGet()} returns historical contents of renamed
320
- * file.
321
- *
322
- * @throws java.lang.Exception
323
- */
324
- @ Test
325
- public void testGetHistoryForTwiceRenamed () throws Exception {
326
- String exp_str
315
+ String new_content
327
316
= "#include <stdio.h>\n "
328
317
+ "#include <stdlib.h>\n "
329
318
+ "\n "
@@ -350,47 +339,56 @@ public void testGetHistoryForTwiceRenamed() throws Exception {
350
339
+ "\t return 0;\n "
351
340
+ "}\n " ;
352
341
353
- runRenamedTest (Paths .get ("moved" , "renamed.c" ).toString (), "1086eaf" , exp_str );
354
- runRenamedTest (Paths .get ("moved" , "renamed2.c" ).toString (), "67dfbe2" , exp_str );
342
+ final List <String []> tests = Arrays .asList (
343
+ // old content (after revision 1086eaf5 inclusively)
344
+ new String []{Paths .get ("moved2" , "renamed2.c" ).toString (), "84599b3c" , new_content },
345
+ new String []{Paths .get ("moved2" , "renamed2.c" ).toString (), "67dfbe26" , new_content },
346
+ new String []{Paths .get ("moved2" , "renamed2.c" ).toString (), "1086eaf5" , new_content },
347
+
348
+ new String []{Paths .get ("moved" , "renamed2.c" ).toString (), "67dfbe26" , new_content },
349
+ new String []{Paths .get ("moved" , "renamed2.c" ).toString (), "1086eaf5" , new_content },
350
+
351
+ new String []{Paths .get ("moved" , "renamed.c" ).toString (), "1086eaf5" , new_content },
352
+
353
+ // old content (before revision b6413947 inclusively)
354
+ new String []{Paths .get ("moved2" , "renamed2.c" ).toString (), "b6413947" , old_content },
355
+ new String []{Paths .get ("moved2" , "renamed2.c" ).toString (), "ce4c98ec" , old_content },
356
+ new String []{Paths .get ("moved" , "renamed2.c" ).toString (), "b6413947" , old_content },
357
+ new String []{Paths .get ("moved" , "renamed2.c" ).toString (), "ce4c98ec" , old_content },
358
+ new String []{Paths .get ("moved" , "renamed.c" ).toString (), "b6413947" , old_content },
359
+ new String []{Paths .get ("moved" , "renamed.c" ).toString (), "ce4c98ec" , old_content },
360
+ new String []{Paths .get ("renamed.c" ).toString (), "ce4c98ec" , old_content }
361
+ );
362
+
363
+ for (String [] params : tests ) {
364
+ runRenamedTest (params [0 ], params [1 ], params [2 ]);
365
+ }
355
366
}
356
367
357
368
/**
358
369
* Test that {@code getHistoryGet()} returns historical contents of renamed
359
370
* file.
360
371
*
361
372
* @throws java.lang.Exception
373
+ * @see #testRenamedFiles for git repo structure info
362
374
*/
363
375
@ Test
364
- public void testGetHistoryForThreeTimesRenamed () throws Exception {
365
- String exp_str
366
- = "#include <stdio.h>\n "
367
- + "#include <stdlib.h>\n "
368
- + "\n "
369
- + "int main ( int argc, const char * argv[] )\n "
370
- + "{\n "
371
- + "\t int i;\n "
372
- + "\t for ( i = 1; i < argc; i ++ )\n "
373
- + "\t {\n "
374
- + "\t \t printf ( \" %s called with %d\\ n\" , argv [ 0 ], argv [ i ] );\n "
375
- + "\t }\n "
376
- + "\n "
377
- + "\t return 0;\n "
378
- + "}\n " ;
376
+ public void testGetHistoryForNonExistentRenamed () throws Exception {
377
+ final List <String []> tests = Arrays .asList (
378
+ new String []{Paths .get ("moved" , "renamed2.c" ).toString (), "84599b3c" },
379
379
380
- runRenamedTest (Paths .get ("moved" , "renamed.c" ).toString (), "b641394" , exp_str );
381
- runRenamedTest (Paths .get ("renamed.c" ).toString (), "ce4c98e" , exp_str );
382
- }
380
+ new String []{Paths .get ("moved" , "renamed.c" ).toString (), "84599b3c" },
381
+ new String []{Paths .get ("moved" , "renamed.c" ).toString (), "67dfbe26" },
383
382
384
- /**
385
- * Test that {@code getHistoryGet()} returns historical contents of renamed
386
- * file.
387
- *
388
- * @throws java.lang.Exception
389
- */
390
- @ Test
391
- public void testGetHistoryForNonExistentRenamed () throws Exception {
392
- runRenamedTest (Paths .get ("moved" , "renamed.c" ).toString (), "67dfbe2" , null );
393
- runRenamedTest (Paths .get ("renamed.c" ).toString (), "67dfbe2" , null );
383
+ new String []{Paths .get ("renamed.c" ).toString (), "84599b3c" },
384
+ new String []{Paths .get ("renamed.c" ).toString (), "67dfbe26" },
385
+ new String []{Paths .get ("renamed.c" ).toString (), "1086eaf5" },
386
+ new String []{Paths .get ("renamed.c" ).toString (), "b6413947" }
387
+ );
388
+
389
+ for (String [] params : tests ) {
390
+ runRenamedTest (params [0 ], params [1 ], null );
391
+ }
394
392
}
395
393
396
394
private void runRenamedTest (String fname , String cset , String content ) throws Exception {
@@ -402,13 +400,39 @@ private void runRenamedTest(String fname, String cset, String content) throws Ex
402
400
InputStream input = gitrepo .getHistoryGet (root .getCanonicalPath (),
403
401
fname , cset );
404
402
if (content == null ) {
405
- Assert .assertNull (input );
403
+ Assert .assertNull (
404
+ String .format ("Expecting the revision %s for file %s does not exist" ,
405
+ cset ,
406
+ fname
407
+ ),
408
+ input
409
+ );
406
410
} else {
407
- Assert .assertNotNull ("expecting not null" , input );
411
+ Assert .assertNotNull (
412
+ String .format ("Expecting the revision %s for file %s does exist" ,
413
+ cset ,
414
+ fname
415
+ ),
416
+ input
417
+ );
408
418
int len = input .read (buffer );
409
- Assert .assertNotEquals (-1 , len );
419
+ Assert .assertNotEquals (
420
+ String .format ("Expecting the revision %s for file %s does have some content" ,
421
+ cset ,
422
+ fname
423
+ ),
424
+ -1 ,
425
+ len
426
+ );
410
427
String str = new String (buffer , 0 , len );
411
- Assert .assertEquals (content , str );
428
+ Assert .assertEquals (
429
+ String .format ("Expecting the revision %s for file %s does match the expected content" ,
430
+ cset ,
431
+ fname
432
+ ),
433
+ content ,
434
+ str
435
+ );
412
436
}
413
437
}
414
438
0 commit comments