16
16
17
17
import static org .eclipse .core .tests .resources .ResourceTestPluginConstants .PI_RESOURCES_TESTS ;
18
18
import static org .junit .Assert .assertNotEquals ;
19
+ import static org .junit .Assert .assertThrows ;
19
20
20
21
import java .nio .charset .StandardCharsets ;
21
22
import java .util .ArrayList ;
@@ -57,18 +58,14 @@ public String getStringValue() {
57
58
}
58
59
}
59
60
60
- private void createProperties (IFile target , QualifiedName [] names , String [] values ) {
61
+ private void createProperties (IFile target , QualifiedName [] names , String [] values ) throws CoreException {
61
62
for (int i = 0 ; i < names .length ; i ++) {
62
63
names [i ] = new QualifiedName ("org.eclipse.core.tests" , "prop" + i );
63
64
values [i ] = "property value" + i ;
64
65
}
65
66
// create properties
66
67
for (int i = 0 ; i < names .length ; i ++) {
67
- try {
68
- target .setPersistentProperty (names [i ], values [i ]);
69
- } catch (CoreException e ) {
70
- fail ("1." + i , e );
71
- }
68
+ target .setPersistentProperty (names [i ], values [i ]);
72
69
}
73
70
}
74
71
@@ -112,29 +109,14 @@ protected void doGetSetProperties(IFile target, String threadID, QualifiedName[]
112
109
}
113
110
}
114
111
115
- private void join (Thread [] threads ) {
116
- //wait for all threads to finish
117
- for (Thread thread : threads ) {
118
- try {
119
- thread .join ();
120
- } catch (InterruptedException e ) {
121
- fail ("#join" , e );
122
- }
123
- }
124
- }
125
-
126
112
/**
127
113
* Tests concurrent acces to the property store.
128
114
*/
129
- public void testConcurrentAccess () {
115
+ public void testConcurrentAccess () throws Exception {
130
116
131
117
// create common objects
132
118
final IFile target = projects [0 ].getFile ("target" );
133
- try {
134
- target .create (getRandomContents (), true , getMonitor ());
135
- } catch (CoreException e ) {
136
- fail ("0.0" , e );
137
- }
119
+ target .create (getRandomContents (), true , getMonitor ());
138
120
139
121
// prepare keys and values
140
122
final int N = 50 ;
@@ -144,24 +126,19 @@ public void testConcurrentAccess() {
144
126
145
127
final CoreException [] errorPointer = new CoreException [1 ];
146
128
Thread [] threads = createThreads (target , names , values , errorPointer );
147
- join (threads );
148
- if (errorPointer [0 ] != null ) {
149
- fail ("2.0" , errorPointer [0 ]);
129
+ for (Thread thread : threads ) {
130
+ thread .join ();
150
131
}
151
-
152
- // remove trash
153
- try {
154
- target .delete (true , getMonitor ());
155
- } catch (CoreException e ) {
156
- fail ("20.0" , e );
132
+ if (errorPointer [0 ] != null ) {
133
+ throw errorPointer [0 ];
157
134
}
158
135
}
159
136
160
137
/**
161
138
* Tests concurrent access to the property store while the project is being
162
139
* deleted.
163
140
*/
164
- public void testConcurrentDelete () throws CoreException {
141
+ public void testConcurrentDelete () throws Exception {
165
142
Thread [] threads ;
166
143
final IFile target = projects [0 ].getFile ("target" );
167
144
final int REPEAT = 8 ;
@@ -178,29 +155,17 @@ public void testConcurrentDelete() throws CoreException {
178
155
179
156
final CoreException [] errorPointer = new CoreException [1 ];
180
157
threads = createThreads (target , names , values , errorPointer );
181
- try {
182
- //give the threads a chance to start
183
- Thread .sleep (10 );
184
- } catch (InterruptedException e ) {
185
- fail ("1.98" , e );
186
- }
187
- try {
188
- //delete the project while the threads are still running
189
- target .getProject ().delete (IResource .NONE , getMonitor ());
190
- } catch (CoreException e ) {
191
- fail ("1.99." + i , e );
158
+ // give the threads a chance to start
159
+ Thread .sleep (10 );
160
+ // delete the project while the threads are still running
161
+ target .getProject ().delete (IResource .NONE , getMonitor ());
162
+ for (Thread thread : threads ) {
163
+ thread .join ();
192
164
}
193
- join (threads );
194
165
if (errorPointer [0 ] != null ) {
195
- fail ( "2.0." + i , errorPointer [0 ]) ;
166
+ throw errorPointer [0 ];
196
167
}
197
168
}
198
- // remove trash
199
- try {
200
- target .delete (true , getMonitor ());
201
- } catch (CoreException e ) {
202
- fail ("20.0" , e );
203
- }
204
169
}
205
170
206
171
public void testCache () throws Throwable {
@@ -398,7 +363,6 @@ public void testDeleteProperties() throws Throwable {
398
363
assertNull ("3.1" , manager .getProperty (source , propName ));
399
364
assertNull ("3.2" , manager .getProperty (sourceFolder , propName ));
400
365
assertNull ("3.3" , manager .getProperty (sourceFile , propName ));
401
-
402
366
}
403
367
404
368
/**
@@ -411,33 +375,16 @@ public void testFileRename() throws CoreException {
411
375
IFile file1a = folder .getFile ("file1" );
412
376
ensureExistsInWorkspace (file1a , true );
413
377
QualifiedName key = new QualifiedName (PI_RESOURCES_TESTS , "key" );
414
- try {
415
- file1a .setPersistentProperty (key , "value" );
416
- } catch (CoreException e ) {
417
- fail ("0.5" , e );
418
- }
419
- try {
420
- file1a .move (IPath .fromOSString ("file2" ), true , getMonitor ());
421
- } catch (CoreException e ) {
422
- fail ("0.6" , e );
423
- }
378
+ file1a .setPersistentProperty (key , "value" );
379
+ file1a .move (IPath .fromOSString ("file2" ), true , getMonitor ());
424
380
IFile file1b = folder .getFile ("file1" );
425
381
ensureExistsInWorkspace (file1b , true );
426
382
String value = null ;
427
- try {
428
- value = file1b .getPersistentProperty (key );
429
- } catch (CoreException e ) {
430
- fail ("0.8" , e );
431
- }
383
+ value = file1b .getPersistentProperty (key );
432
384
assertNull ("1.0" , value );
433
385
file1a = folder .getFile ("file2" );
434
- try {
435
- value = file1a .getPersistentProperty (key );
436
- } catch (CoreException e ) {
437
- fail ("1.9" , e );
438
- }
386
+ value = file1a .getPersistentProperty (key );
439
387
assertEquals ("2.0" , "value" , value );
440
-
441
388
}
442
389
443
390
/**
@@ -449,46 +396,25 @@ public void testFolderRename() throws CoreException {
449
396
IFolder folder1a = project .getFolder ("folder1" );
450
397
ensureExistsInWorkspace (folder1a , true );
451
398
QualifiedName key = new QualifiedName (PI_RESOURCES_TESTS , "key" );
452
- try {
453
- folder1a .setPersistentProperty (key , "value" );
454
- } catch (CoreException e ) {
455
- fail ("0.5" , e );
456
- }
457
- try {
458
- folder1a .move (IPath .fromOSString ("folder2" ), true , getMonitor ());
459
- } catch (CoreException e ) {
460
- fail ("0.6" , e );
461
- }
399
+ folder1a .setPersistentProperty (key , "value" );
400
+ folder1a .move (IPath .fromOSString ("folder2" ), true , getMonitor ());
462
401
IFolder folder1b = project .getFolder ("folder1" );
463
402
ensureExistsInWorkspace (folder1b , true );
464
403
String value = null ;
465
- try {
466
- value = folder1b .getPersistentProperty (key );
467
- } catch (CoreException e ) {
468
- fail ("0.8" , e );
469
- }
404
+ value = folder1b .getPersistentProperty (key );
470
405
assertNull ("1.0" , value );
471
406
folder1a = project .getFolder ("folder2" );
472
- try {
473
- value = folder1a .getPersistentProperty (key );
474
- } catch (CoreException e ) {
475
- fail ("1.9" , e );
476
- }
407
+ value = folder1a .getPersistentProperty (key );
477
408
assertEquals ("2.0" , "value" , value );
478
-
479
409
}
480
410
481
411
/**
482
412
* Do a stress test by adding a very large property to the store.
483
413
*/
484
- public void testLargeProperty () {
414
+ public void testLargeProperty () throws CoreException {
485
415
// create common objects
486
416
IFile target = projects [0 ].getFile ("target" );
487
- try {
488
- target .create (getRandomContents (), true , getMonitor ());
489
- } catch (CoreException e ) {
490
- fail ("0.0" , e );
491
- }
417
+ target .create (getRandomContents (), true , getMonitor ());
492
418
493
419
QualifiedName name = new QualifiedName ("stressTest" , "prop" );
494
420
final int SIZE = 10000 ;
@@ -497,20 +423,7 @@ public void testLargeProperty() {
497
423
valueBuf .append ("a" );
498
424
}
499
425
String value = valueBuf .toString ();
500
- try {
501
- target .setPersistentProperty (name , value );
502
- //should fail
503
- fail ("1.0" );
504
- } catch (CoreException e ) {
505
- // expected
506
- }
507
-
508
- // remove trash
509
- try {
510
- target .delete (true , getMonitor ());
511
- } catch (CoreException e ) {
512
- fail ("20.0" , e );
513
- }
426
+ assertThrows (CoreException .class , () -> target .setPersistentProperty (name , value ));
514
427
}
515
428
516
429
/**
@@ -521,32 +434,15 @@ public void testProjectRename() throws CoreException {
521
434
IProject project1a = root .getProject ("proj1" );
522
435
ensureExistsInWorkspace (project1a , true );
523
436
QualifiedName key = new QualifiedName (PI_RESOURCES_TESTS , "key" );
524
- try {
525
- project1a .setPersistentProperty (key , "value" );
526
- } catch (CoreException e ) {
527
- fail ("0.5" , e );
528
- }
529
- try {
530
- project1a .move (IPath .fromOSString ("proj2" ), true , getMonitor ());
531
- } catch (CoreException e ) {
532
- fail ("0.6" , e );
533
- }
437
+ project1a .setPersistentProperty (key , "value" );
438
+ project1a .move (IPath .fromOSString ("proj2" ), true , getMonitor ());
534
439
IProject project1b = root .getProject ("proj1" );
535
440
ensureExistsInWorkspace (project1b , true );
536
- String value = null ;
537
- try {
538
- value = project1b .getPersistentProperty (key );
539
- } catch (CoreException e ) {
540
- fail ("0.8" , e );
541
- }
441
+ String value = project1b .getPersistentProperty (key );
542
442
assertNull ("1.0" , value );
543
443
544
444
project1a = root .getProject ("proj2" );
545
- try {
546
- value = project1a .getPersistentProperty (key );
547
- } catch (CoreException e ) {
548
- fail ("1.9" , e );
549
- }
445
+ value = project1a .getPersistentProperty (key );
550
446
assertEquals ("2.0" , "value" , value );
551
447
}
552
448
@@ -582,20 +478,13 @@ public void testProperties() throws Throwable {
582
478
}
583
479
assertEquals ("3.0" , 0 , manager .getProperties (target ).size ());
584
480
manager .deleteProperties (target , IResource .DEPTH_INFINITE );
585
-
586
- // remove trash
587
- target .delete (false , monitor );
588
481
}
589
482
590
- public void testSimpleUpdate () {
483
+ public void testSimpleUpdate () throws CoreException {
591
484
592
485
// create common objects
593
486
IFile target = projects [0 ].getFile ("target" );
594
- try {
595
- target .create (getRandomContents (), true , getMonitor ());
596
- } catch (CoreException e ) {
597
- fail ("0.0" , e );
598
- }
487
+ target .create (getRandomContents (), true , getMonitor ());
599
488
600
489
// prepare keys and values
601
490
int N = 3 ;
@@ -608,51 +497,27 @@ public void testSimpleUpdate() {
608
497
609
498
// create properties
610
499
for (int i = 0 ; i < N ; i ++) {
611
- try {
612
- target .setPersistentProperty (names [i ], values [i ]);
613
- } catch (CoreException e ) {
614
- fail ("1." + i , e );
615
- }
500
+ target .setPersistentProperty (names [i ], values [i ]);
616
501
}
617
502
618
503
// verify
619
504
for (int i = 0 ; i < N ; i ++) {
620
- try {
621
- assertTrue ("2.0" , target .getPersistentProperty (names [i ]).equals (values [i ]));
622
- } catch (CoreException e ) {
623
- fail ("3." + i , e );
624
- }
505
+ assertTrue ("2.0" , target .getPersistentProperty (names [i ]).equals (values [i ]));
625
506
}
626
507
627
508
for (int j = 0 ; j < 20 ; j ++) {
628
-
629
509
// change properties
630
510
for (int i = 0 ; i < N ; i ++) {
631
- try {
632
- values [i ] = values [i ] + " - changed" ;
633
- target .setPersistentProperty (names [i ], values [i ]);
634
- } catch (CoreException e ) {
635
- fail ("4." + i , e );
636
- }
511
+ values [i ] = values [i ] + " - changed" ;
512
+ target .setPersistentProperty (names [i ], values [i ]);
637
513
}
638
514
639
515
// verify
640
516
for (int i = 0 ; i < N ; i ++) {
641
- try {
642
- assertTrue ("5.0" , target .getPersistentProperty (names [i ]).equals (values [i ]));
643
- } catch (CoreException e ) {
644
- fail ("6." + i , e );
645
- }
517
+ assertTrue ("5.0" , target .getPersistentProperty (names [i ]).equals (values [i ]));
646
518
}
647
519
648
520
}
649
-
650
- // remove trash
651
- try {
652
- target .delete (true , getMonitor ());
653
- } catch (CoreException e ) {
654
- fail ("20.0" , e );
655
- }
656
521
}
657
522
658
523
}
0 commit comments