-
Notifications
You must be signed in to change notification settings - Fork 19
/
TRT_TreeNode.java
1966 lines (1673 loc) · 66 KB
/
TRT_TreeNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id$
*
* Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.javatest;
import com.sun.javatest.TestResultTable.TreeNode;
import com.sun.javatest.util.Debug;
import com.sun.javatest.util.DynamicArray;
import com.sun.javatest.util.I18NResourceBundle;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalLong;
/**
* This is the implementation of a tree node structure for TestResultTable.
* Only the interface implementation is meant to be exposed. Assumptions are made
* that this is the only node class (implementation of TreeNode) used in the tree.
*/
public class TRT_TreeNode implements TestResultTable.TreeNode {
// ------- See interface in TestResultTable for docs. on these methods
//static protected boolean debug = Boolean.getBoolean("debug." + TRT_TreeNode.class.getName());
static protected int debug = Debug.getInt(TRT_TreeNode.class);
// no per-instance array of observers, use a static Hashtable of arrays
private static Map<TRT_TreeNode, TestResultTable.TreeNodeObserver[]> observerTable = new Hashtable<>(16);
private static I18NResourceBundle i18n = I18NResourceBundle.getBundleForClass(TRT_TreeNode.class);
/**
* null if the node has not been scanned, zero length if it is actually empty
*/
private Object[] children; // contains combo of TreeNodes or TestResults
private TRT_TreeNode parent; // should never be null, unless root
private TestResultTable table; // what table this node is in
private int counter; // nodes below this point and including self
private int[] childStats;
private String name; // basically the directory name, null means root node
private OptionalLong lastScanDate = OptionalLong.empty();
/**
* List of files that makeup the on-disk contents of this node.
* These are probably HTML files with test descriptions in them. The string is a
* URL which is relative to the current location, so the typical entry will be
* "index.html". The only special case is a reference to the "name" field of the
* node which indicates that the current directory should be scanned by the
* TestFinder. This is to support directory walk style finders.
*/
private String[] filesToScan; // in cases where the finder must scan files
/**
* Needed in cases where we need to get the return value from insert().
* Only used during node insertion right now. It is not really legal
* to create one of these without immediately populating it with
* a name.
*/
TRT_TreeNode(TestResultTable table, TestResultTable.TreeNode parent) {
children = null;
counter = 0;
name = null; // the only node with this value null is the root
this.table = table;
this.parent = (TRT_TreeNode) parent;
}
/**
* Increment the child status counter for the given Status type.
* The parameter should correspond to one of the states defined in
* the Status object.
*
* @see Status
* @deprecated
*/
@java.lang.Deprecated
static void bubbleUpChildStat(TRT_TreeNode node, int which) {
node.childStats[which]++;
TRT_TreeNode parent = (TRT_TreeNode) node.getParent();
if (parent != null) {
bubbleUpChildStat(parent, which);
}
}
/**
* Decrement then increment the child status counter for the given Status type.
* The parameters should correspond to one of the states defined in
* the Status object. This method facilitates updating of TestResults - changing
* from one status to another. This change in similar to bubbleUpChildStat()
* in that it automatically affects node from the current location up to the
* root.
*
* @see Status
*/
static void swapChildStat(TRT_TreeNode node, int oldStatus, int newStatus) {
node.childStats[oldStatus]--;
node.childStats[newStatus]++;
TRT_TreeNode parent = (TRT_TreeNode) node.getParent();
if (parent != null) {
swapChildStat(parent, oldStatus, newStatus);
}
}
private static void addToScanList(TRT_TreeNode node, String file, File fullFile) {
if (debug > 1) {
Debug.println(" => Adding " + file + " to scan list and leaving.");
Debug.println(" => Local node is : " + node);
Debug.println(" -> local size b4: " +
(node.filesToScan == null ? 0 : node.filesToScan.length));
}
int i = 0;
// check for a name clash
if (node.filesToScan != null) {
// the first entry in the list is (always?) a special case
if (node.filesToScan.length > 0 && node.filesToScan[0].equals(node.getName())) {
i = 1;
}
for (; i < node.filesToScan.length; i++) {
if (node.filesToScan[i].equals(file)) {
break;
}
}
}
// if no conflicts were found, append it
if (node.filesToScan == null || i >= node.filesToScan.length) {
node.filesToScan = DynamicArray.append(node.filesToScan, file);
} else {
// name collision, ignore
// actually, with scan suppression, this may be normal
if (debug > 1) {
Debug.println("Warning: File " +
fullFile.getPath() +
" may be referenced more than once in the test suite. Ignoring.");
}
}
if (debug > 1) {
Debug.println(" -> local size after: " +
(node.filesToScan == null ? 0 : node.filesToScan.length));
}
}
/**
* Recalculate the counters which track the status of tests below this node.
* This can be a high cost calculation, but will do nothing if all the counters
* are up to date.
* It is assumed that if a node has null childStats, that all nodes between
* it and the root are also marked invalid.
*/
private static void refreshChildStats(TRT_TreeNode node) {
if (node.childStats != null) {
return; // nothing to do
}
node.childStats = new int[Status.NUM_STATES];
for (int i = 0; i < node.children.length; i++) {
if (node.children[i] instanceof TRT_TreeNode) {
// node is another branch
TRT_TreeNode child = (TRT_TreeNode) node.children[i];
int[] stats = child.getChildStatus();
for (int j = 0; j < stats.length; j++) {
node.childStats[j] += stats[j];
}
} else {
// node is a test result
TestResult tr = (TestResult) node.children[i];
node.childStats[tr.getStatus().getType()]++;
}
}
}
/**
* Add an observer to watch this node for changes.
*/
@Override
public synchronized void addObserver(TestResultTable.TreeNodeObserver obs) {
TestResultTable.TreeNodeObserver[] observers = observerTable.get(this);
if (observers == null) {
observers = new TestResultTable.TreeNodeObserver[0];
}
observers = DynamicArray.append(observers, obs);
observerTable.put(this, observers);
}
/**
* Remove an observer that was previously added.
*/
@Override
public synchronized void removeObserver(TestResultTable.TreeNodeObserver obs) {
TestResultTable.TreeNodeObserver[] observers = observerTable.get(this);
if (observers == null) {
return;
}
observers = DynamicArray.remove(observers, obs);
if (observers == null) {
observerTable.remove(this);
} else {
observerTable.put(this, observers);
}
}
/**
* Find out how many tests are in this node and below.
* If you invoke this on a node which is being lazily read from a
* TestFinder, this may cause a synchronous retrieval of data from the
* TestFinder. <b>Use with caution!</b>
*/
@Override
public int getSize() {
scanSubtree(this);
return counter;
}
// ------ end of interface impl ------
// ------ private methods begin ------
/**
* Get the estimated number of tests below this node. Mainly useful for
* low contention hints about the size. No locking is involved in retrieving
* the information.
*/
public int getEstimatedSize() {
return counter;
}
@Override
public TestResultTable.TreeNode getParent() {
return parent;
}
@Override
public boolean isRoot() {
return parent == null;
}
@Override
public TestResultTable getEnclosingTable() {
return table;
}
@Override
public boolean isUpToDate() {
// compare timestamp in the future
return lastScanDate.isPresent();
}
/**
* Find out how many children this node contains.
* If you invoke this on a node which is being lazily read from a
* TestFinder, this may cause a synchronous retrieval of data from the
* TestFinder.
*/
@Override
public int getChildCount() {
scanIfNeeded();
if (children == null) {
return 0;
} else {
return children.length;
}
}
@Override
public Object getChild(int index) {
return getChild(index, false);
}
/**
* Get the child at the specified location.
* May be either a TestResult or TestResultTable.TreeNode.
* If you invoke this on a node which is being lazily read from a
* TestFinder, this may cause a synchronous retrieval of data from the
* TestFinder.
*
* @return Null if there are no children here or the specified index if out of
* range.
*/
Object getChild(int index, boolean suppressScan) {
if (!suppressScan) {
scanIfNeeded();
}
if (children == null || index >= children.length) {
return null;
} else {
return children[index];
}
}
/**
* @return List of TestResult objects in this node. null if none
*/
@Override
public TestResult[] getTestResults() {
scanIfNeeded();
TestResult[] leafs = null;
if (children != null && children.length != 0) {
for (Object child : children) {
if (child instanceof TestResult) {
leafs = (TestResult[]) DynamicArray.append(leafs, child);
}
} // for
}
return leafs;
}
/**
* Finds a TestResult in this node with the given name (test URL).
* This is a match against the filename, not the test name. This is not recursive.
TestResult matchTRFile(String name) {
scanIfNeeded();
if (debug > 1) Debug.println("Matching TR: " + name + " in " + this);
File file = new File(name);
TestResult found = null;
// I don't think this is a valid check
// in this context, this would end up checking for a dir which is
// relative to the cwd
//if (file.isDirectory())
// throw new JavaTestError(i18n, "trttn.noPaths");
if (children == null || children.length == 0) return null;
for (int i = 0; i < children.length; i++) {
if (children[i] instanceof TestResult) {
File trName = new File(
((TestResult)(children[i])).getWorkRelativePath());
if (debug > 1)
Debug.println(" -> trying to match against " + trName.getName());
if ( name.equals(trName.getName()) ) {
found = (TestResult)children[i];
i = children.length; // exit loop
}
else
found = null;
}
}
return found;
}
*/
/**
* Get only the children of this node which are branches.
*
* @return List of children nodes objects in this node. null if none.
*/
@Override
public TestResultTable.TreeNode[] getTreeNodes() {
scanIfNeeded();
if (children == null) {
return null;
}
TestResultTable.TreeNode[] leafs = null;
for (Object child : children) {
if (child instanceof TreeNode) {
leafs = (TreeNode[]) DynamicArray.append(leafs, child);
}
}
return leafs;
}
@Override
public String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
@Override
public boolean isLeaf(int index) {
scanIfNeeded();
if (index < 0 || index >= children.length) {
return false;
} else if (children[index] instanceof TestResult) {
return true;
} else if (children[index] instanceof TRT_TreeNode) {
// if there are no nodes or tests below, then...
return children == null || children.length == 0;
} else // should never be the case
{
return false;
}
}
@Override
public int[] getChildStatus() {
scanSubtree(this);
if (childStats == null) {
refreshChildStats(this);
}
return childStats;
}
@Override
public int getIndex(Object target) {
return getIndex(target, false);
}
// ---- BEGIN lazy tree with finder ----
int getIndex(Object target, boolean suppressScan) {
if (!suppressScan) {
scanIfNeeded();
}
if (target == null) {
return -2;
} else if (children == null) {
return -1; // not found
} else {
for (int i = 0; i < children.length; i++) {
if (children[i] == target) {
return i;
}
}
}
// not found
return -1;
}
@Override
public TestResult matchTest(String url) {
scanIfNeeded();
if (debug > 1) {
Debug.println("Matching Test URL: " + name + " in " + this);
}
TestResult found = null;
// I don't think this is a valid check
// in this context, this would end up checking for a dir which is
// relative to the cwd
//if (file.isDirectory())
// throw new JavaTestError(i18n, "trttn.noPaths");
if (children == null || children.length == 0) {
return null;
}
for (int i = 0; i < children.length; i++) {
if (children[i] instanceof TestResult) {
TestResult tr = (TestResult) children[i];
try {
String name = tr.getDescription().getRootRelativeURL();
} catch (TestResult.Fault f) {
throw new JavaTestError(i18n, "trttn.noTd", f);
}
if (debug > 1) {
Debug.println(" -> trying to match against " + name);
}
if (name.equals(url)) {
found = (TestResult) children[i];
i = children.length; // exit loop
} else {
found = null;
}
}
}
return found;
}
/**
* Get the current size of this subtree without causing a subtree scan.
*
* @see #getSize()
*/
int getCurrentSize() {
return counter;
}
/**
* Increment the child status counter for the given Status type.
* The parameter should correspond to one of the states defined in
* the Status object.
*
* @see Status
* @deprecated Use bubbleUpChildStat()
*/
@java.lang.Deprecated
void incChildStat(int which) {
childStats[which]++;
}
/**
* Increment the child status counter for the given Status type.
* The parameter should correspond to one of the states defined in
* the Status object.
*
* @see Status
* @deprecated Use bubbleUpChildStat()
*/
@java.lang.Deprecated
void decChildStat(int which) {
childStats[which]--;
}
void invalidateChildStats() {
childStats = null;
notifyCounterChange();
TRT_TreeNode parent = (TRT_TreeNode) getParent();
if (parent != null) {
parent.invalidateChildStats();
}
}
boolean isChildStatsValid() {
return childStats == null;
}
/**
* Increment the counter that tracks how many nodes are under this node.
*
* @see #bubbleUpCounterInc()
* @deprecated Use bubbleUpCounterInc() instead
*/
@java.lang.Deprecated
void incNodeCounter() {
counter++;
}
// -- END REFRESH METHODS --
/**
* Finds a TRT_TreeNode in this node with the given name.
*
* @param url The test URL, including the test id. Basically output from
* TestDescription.getRootRelativeURL().
* @return The requested TRT_TreeNode, null if not found.
*/
TestResult getTestResult(String url) {
int index = getTestIndex(url);
if (index != -1) {
try {
return (TestResult) getChild(index);
} catch (ClassCastException e) {
// this better not happen
// if it does, then getTestIndex() is broken
throw new JavaTestError(i18n, "trttn.badCast");
}
} else {
return null;
}
}
/**
* Finds a TRT_TreeNode in this node with the given name.
*
* @param name Node name you want. For example: "java_lang", but not
* "api/java_lang"
* @return The requested TRT_TreeNode, null if not found.
*/
TRT_TreeNode getTreeNode(String name, boolean suppressScan) {
if (!suppressScan) {
scanIfNeeded();
}
int index = getNodeIndex(name, suppressScan);
if (index != -1) {
try {
return (TRT_TreeNode) getChild(index, suppressScan);
} catch (ClassCastException e) {
// this better not happen
// if it does, then getNodeIndex() is broken
throw new JavaTestError(i18n, "trttn.badCast");
}
} else {
return null;
}
}
/**
* Search for the given test result by the test URL.
* The test URL will be converted to a work relative JTR path and then found
* using getResultIndex().
*
* @param url The test URL, including the test id. Basically output from
* TestDescription.getRootRelativeURL().
* @return The index of the request test result. -1 if not found.
*/
int getTestIndex(String url) {
if (url == null) {
throw new JavaTestError(i18n, "trttn.nullSearch");
}
// we match on the JTR path, which should be equivalent to the test URL
return getResultIndex(TestResult.getWorkRelativePath(url), false);
}
/**
* Search for the given test result by the work relative path.
* This is a direct comparison of the TestResult.getWorkRelativePath() strings.
*
* @return The index of the request test result. -1 if not found.
*/
synchronized int getResultIndex(String jtrPath, boolean suppressScan) {
if (jtrPath == null) {
throw new JavaTestError(i18n, "trttn.nullSearch");
}
if (!suppressScan) {
scanIfNeeded();
}
int found = -1;
if (children != null && children.length != 0) {
for (int i = 0; i < children.length; i++) {
if (children[i] instanceof TestResult) {
TestResult tr = (TestResult) children[i];
if (tr.getWorkRelativePath().equals(jtrPath)) {
found = i;
break;
} else {
// not a previous instance of this TR, skip it
// placeholder to avoid dangling else
}
} else {
// this is a TreeNode, skip it
// placeholder to avoid dangling else
}
} // for
} // outer if
return found;
}
/**
* Finds a TestResult in this node with the same <b>Test URL</b> as the given TR.
* This is not a reference comparison, nor is this a recursive search.
*
* @return The location of the requested TestResult.
*/
int getTestIndex(TestResult target, boolean suppressScan) {
if (target == null) {
throw new JavaTestError(i18n, "trttn.nullSearch");
}
// we match on the JTR path, which should be equivalent to the test URL
return getResultIndex(target.getWorkRelativePath(),
suppressScan);
}
/**
* Finds a TRT_TreeNode in this node with the given name.
*
* @param name Node name you want. For example: "java_lang", but not
* "api/java_lang"
* @return The index of the requested TRT_TreeNode.
*/
synchronized int getNodeIndex(String name, boolean suppressScan) {
if (!suppressScan) {
scanIfNeeded();
}
int found = -1;
if (name == null) {
throw new JavaTestError(i18n, "trttn.nullSearch");
}
if (children == null || children.length == 0) {
found = -1;
} else {
for (int i = 0; i < children.length; i++) {
if (children[i] instanceof TRT_TreeNode) {
TRT_TreeNode tn = (TRT_TreeNode) children[i];
if (tn.getName().equals(name)) {
found = i;
break;
} else {
// not a match
// placeholder to avoid dangling else
}
} else {
// this is a TestResult, skip it
// placeholder to avoid dangling else
}
} // for
} // outer else
return found;
}
/**
* In the case where a test finder is being used, nodes are read lazily.
*/
synchronized void scanIfNeeded() {
if (debug > 0) {
Debug.println("starting scanIfNeeded() on node " + getName());
}
// this check is sufficient for now, but later we will want to scan
// files for changes (maybe by timestamp)
// if the finder is null, then we are operating in traditional mode
if (table.getTestFinder() == null || isUpToDate()) {
return;
}
if (children == null) {
children = new Object[0];
}
/*
File thisDir = new File(table.getTestFinder().getRootDir().getAbsolutePath() + File.separator +
TestResultTable.getRootRelativePath(this));
// I think we rely on File to correct mixed file seps.
// it might be a better idea to do a replace operation on the last item
// since we know it will contains forward slashes
File thisDir = new File(table.getTestSuiteRoot().getAbsolutePath() + File.separator +
TestResultTable.getRootRelativePath(this));
*/
// special case for root
if (isRoot() && filesToScan == null /* && root timestamp change check */) {
File thisDir = table.getTestSuiteRoot();
lastScanDate = OptionalLong.of(table.getLastModifiedTime(thisDir));
processFile(thisDir);
// to prevent infinite recursion
if (filesToScan == null) {
filesToScan = new String[0];
}
for (int i = 0; i < filesToScan.length; i++) {
processFile(new File(filesToScan[i]));
}
}
File thisDir = new File(TestResultTable.getRootRelativePath(this));
long lmd = table.getLastModifiedTime(thisDir);
if (lastScanDate.isPresent() && lmd <= lastScanDate.getAsLong()) {
return;
}
if (filesToScan != null) {
// should be seeded to skip the root, which handled above?
for (int i = 0; i < filesToScan.length; i++) {
if (Objects.equals(filesToScan[i], this.name)) {
processFile(thisDir);
} else {
String path = TestResultTable.getRootRelativePath(this);
processFile("".equals(path) // Zero length string if the node is a root
? new File(filesToScan[i])
: new File(path + File.separator + filesToScan[i]));
}
} // for
} else {
if (debug > 0) {
Debug.println("refreshing contents of " + getName() + lastScanDate);
}
refreshIfNeeded();
}
lastScanDate = OptionalLong.of(lmd);
// send observer msg?
// delete unneeded objects from TestResultTable.TreeNode?
}
// SPECIAL TEST REFRESH/REPLACE METHODS
synchronized TestResult resetTest(int index, TestResult tr) {
if (!(children[index] == tr)) {
return null;
}
// remove the JTR file
File result = tr.getFile();
// do this in in case the TR doesn't know that it has a JTR on disk
WorkDirectory workdir = table == null ? null : table.getWorkDirectory();
if (result == null && workdir != null) {
result = workdir.getFile(tr.getWorkRelativePath());
}
if (result != null) {
boolean wasDeleted = result.delete();
if (workdir != null) {
workdir.clearAnnotations(tr);
}
}
String name = tr.getTestName();
String filename = null;
int lastSlash = name.lastIndexOf('/');
int lastHash = name.lastIndexOf('#');
if (lastHash > lastSlash) {
filename = name.substring(0, lastHash);
} else {
filename = name;
}
TestResult newTr = null;
TestDescription oldTd = null;
try {
oldTd = tr.getDescription();
} catch (TestResult.Fault f) {
// oh well, recover without it
// oldTd will be null, and we'll generate a new one
}
TestDescription newTd = updateTestDescription(name, oldTd);
// this is bad actually, it means the test may not exist
// anymore
if (newTd == null) // create an "emergency" one
{
newTd = new TestDescription(table.getTestFinder().getRoot(),
new File(filename), new HashMap<String, String>());
}
newTr = TestResult.notRun(newTd);
if (newTr != null) { // success
return replaceTest(newTr, index);
}
// operation failed if we reach this point
return null;
}
/**
* Refresh this entire node if necessary.
* This means create any new nodes, and update any test descriptions.
*
* @return True if some changes in this node were needed, false otherwise.
* @see com.sun.javatest.TestResultTable#refreshIfNeeded(String)
*/
public synchronized boolean refreshIfNeeded() {
if (filesToScan != null ||
(table != null && table.isFinderScanSuppressed() && !isUpToDate())) {
//File thisDir = new File(getTestSuiteRootPathPrefix(),
// TestResultTable.getRootRelativePath(this));
File thisDir = new File(TestResultTable.getRootRelativePath(this));
TestFinder finder = table.getTestFinder();
File[] files = null;
long thisScanDate = table.getLastModifiedTime(thisDir);
// may be less than if the custom finder starts to return a
// bogus value - like zero or 1 for whatever reason
if (lastScanDate.isPresent() && thisScanDate <= lastScanDate.getAsLong()) {
return false;
}
synchronized (finder) {
finder.read(thisDir);
// we should not get back any tests
File[] tmp = finder.getFiles();
// shallow copy before releasing finder
files = new File[tmp.length];
System.arraycopy(tmp, 0, files, 0, tmp.length);
} // sync
OptionalLong cachedScanDate = lastScanDate;
// need to update lastScanDate before the loop to avoid
// excessive recursion
lastScanDate = OptionalLong.of(thisScanDate);
List<TreeNode> nodesUsed = new ArrayList<>();
List<TestResult> usedTests = new ArrayList<>();
for (File file : files) {
if (table.isBranchFile(file)) {
nodesUsed.add(updateDirectory(file));
} else {
// scan if file is newer than the last time this folder
// was scanned (cachedScanDate)
if (!cachedScanDate.isPresent() || table.getLastModifiedTime(file) > cachedScanDate.getAsLong()) {
usedTests.addAll(updateFile(file));
} else {
}
}
} // for
// look for deleted nodes
TreeNode[] origNodes = getTreeNodes();
if (origNodes != null && origNodes.length != 0) {
for (TreeNode n : nodesUsed) {
for (int j = 0; j < origNodes.length; j++) {
if (origNodes[j] == n) {
origNodes[j] = null;
}
}
} // for
for (TreeNode origNode : origNodes) {
if (origNode != null) {
rmChild((TRT_TreeNode) origNode);
}
}
}
// discard
origNodes = null;
nodesUsed = null;
// look for tests which no longer exist (from finder)
TestResult[] currTests = getTestResults();
if (currTests != null && currTests.length != 0) {
for (TestResult tr : usedTests) {
for (int j = 0; j < currTests.length; j++) {
if (currTests[j] == tr) {
currTests[j] = null;
}
}
} // for
for (int i = 0; i < currTests.length; i++) {
if (currTests[i] != null) {
//System.out.println("** Removing test " + currTests[i].getTestName());
if (rmChild(currTests[i]) != -1) {
table.notifyRemoveLeaf(TestResultTable.getObjectPath(this), currTests[i], i);
}
}
}
}
currTests = null;
usedTests = null;
} else {
if (debug > 0) {
Debug.println("nothing to refresh in " + getName());
}
}
return true;
}
/**
* @return the new test result object. may be the same if no
* refresh was needed. A null indicates a error.
*/
synchronized TestResult refreshIfNeeded(TestResult test) {