< prev index next >

src/com/sun/javatest/finder/BinaryTestWriter.java

Print this page
rev 145 : 7902237: Fixing raw use of parameterized class
Reviewed-by: jjg


 261                 return 0;
 262             }
 263         }
 264     }
 265 
 266     /**
 267      * Creates and initializes an instance of a test finder
 268      *
 269      * @param finder The class name of the required test finder
 270      * @param args any args to pass to the TestFinder's init method.
 271      * @param ts The testsuite root file
 272      * @return The newly created TestFinder.
 273      */
 274     private TestFinder initializeTestFinder(String finder, String[] args, File ts) throws Fault {
 275         TestFinder testFinder;
 276 
 277         if (ts == null)
 278             throw new NullPointerException();
 279 
 280         try {
 281             Class c = Class.forName(finder);
 282             testFinder = (TestFinder) (c.newInstance());
 283             testFinder.init(args, ts, null);
 284         }
 285         catch (ClassNotFoundException e) {
 286             throw new Fault("Error: Can't find class for test finder specified: " + finder);
 287         }
 288         catch (InstantiationException e) {
 289             throw new Fault("Error: Can't create new instance of test finder: " + e);
 290         }
 291         catch (IllegalAccessException e) {
 292             throw new Fault("Error: Can't access test finder: " + e);
 293         }
 294         catch (TestFinder.Fault e) {
 295             throw new Fault("Error: Can't initialize test-finder: " + e.getMessage());
 296         }
 297 
 298         return testFinder;
 299     }
 300 
 301 


 369 
 370     /**
 371      * Read the tests from a file in test suite
 372      */
 373     private TestTree.Node read0(TestFinder finder, File file, TestTree testTree, Set<File> allFiles)
 374     {
 375         // keep track of which files we have read, and ignore duplicates
 376         if (allFiles.contains(file))
 377             return null;
 378         else
 379             allFiles.add(file);
 380 
 381         finder.read(file);
 382         TestDescription[] tests = finder.getTests();
 383         File[] files = finder.getFiles();
 384 
 385         if (tests.length == 0 && files.length == 0)
 386             return null;
 387 
 388         Arrays.sort(files);
 389         Arrays.sort(tests, new Comparator() {
 390             public int compare(Object o1, Object o2) {
 391                 TestDescription td1 = (TestDescription) o1;
 392                 TestDescription td2 = (TestDescription) o2;
 393                 return td1.getRootRelativeURL().compareTo(td2.getRootRelativeURL());
 394             }
 395         });
 396 
 397         Vector<TestTree.Node> v = new Vector<>();
 398         for (int i = 0; i < files.length; i++) {
 399             TestTree.Node n = read0(finder, files[i], testTree, allFiles);
 400             if (n != null)
 401                 v.addElement(n);
 402         }
 403         TestTree.Node[] nodes = new TestTree.Node[v.size()];
 404         v.copyInto(nodes);
 405 
 406         return testTree.new Node(file.getName(), tests, nodes);
 407     }
 408 
 409     //------------------------------------------------------------------------------------------
 410 
 411     /**
 412      * Write an int to a data output stream using a variable length encoding.


 446      * @see BinaryTestFinder.StringTable
 447      */
 448     static class StringTable {
 449         /**
 450          * Add a new string to the table; if it has already been added,
 451          * increase its use count.
 452          */
 453         void add(String s) {
 454             Entry e = map.get(s);
 455             if (e == null) {
 456                 e = new Entry();
 457                 map.put(s, e);
 458             }
 459             e.useCount++;
 460         }
 461 
 462         /**
 463          * Add all the strings used in a test description to the table.
 464          */
 465         void add(TestDescription test) {
 466             for (Iterator i = test.getParameterKeys(); i.hasNext(); ) {
 467                 String key = (String) (i.next());
 468                 String param = test.getParameter(key);
 469                 add(key);
 470                 add(param);
 471             }
 472         }
 473 
 474         /**
 475          * Return the number of strings in the table.
 476          */
 477         int getSize() {
 478             return map.size();
 479         }
 480 
 481         /**
 482          * Return the number of sstrings that were written to the output file.
 483          * Not all strings are written out: only frequently used ones are.
 484          */
 485         int getWrittenSize() {
 486             return writtenSize;
 487         }


 652          * Each test description is written as a count followed by that many
 653          * name-value pairs of string references.
 654          */
 655         void write(DataOutputStream o) throws IOException {
 656             writeInt(o, tests.size());
 657             for (int i = 0; i < tests.size(); i++) {
 658                 TestDescription td = tests.elementAt(i);
 659                 Entry e = testMap.get(td);
 660                 e.index = o.size();
 661                 write(td, o);
 662             }
 663         }
 664 
 665         /**
 666          * Write a single test description to a stream. It is written as a count,
 667          * followed by that many name-value pairs of string references.
 668          */
 669         private void write(TestDescription td, DataOutputStream o) throws IOException {
 670             // should consider using load/save here
 671             writeInt(o, td.getParameterCount());
 672             for (Iterator i = td.getParameterKeys(); i.hasNext(); ) {
 673                 String key = (String) (i.next());
 674                 String value = td.getParameter(key);
 675                 stringTable.writeRef(key, o);
 676                 stringTable.writeRef(value, o);
 677             }
 678         }
 679 
 680         private Map<TestDescription, Entry> testMap = new HashMap<>();
 681         private Vector<TestDescription> tests = new Vector<>();
 682         private StringTable stringTable;
 683 
 684         /**
 685          * Data for each test description in the table.
 686          */
 687         class Entry {
 688             /**
 689              * The byte offset of the test description in the stream when
 690              * last written out.
 691              */
 692             int index = -1;
 693         }




 261                 return 0;
 262             }
 263         }
 264     }
 265 
 266     /**
 267      * Creates and initializes an instance of a test finder
 268      *
 269      * @param finder The class name of the required test finder
 270      * @param args any args to pass to the TestFinder's init method.
 271      * @param ts The testsuite root file
 272      * @return The newly created TestFinder.
 273      */
 274     private TestFinder initializeTestFinder(String finder, String[] args, File ts) throws Fault {
 275         TestFinder testFinder;
 276 
 277         if (ts == null)
 278             throw new NullPointerException();
 279 
 280         try {
 281             Class<?> c = Class.forName(finder);
 282             testFinder = (TestFinder) (c.newInstance());
 283             testFinder.init(args, ts, null);
 284         }
 285         catch (ClassNotFoundException e) {
 286             throw new Fault("Error: Can't find class for test finder specified: " + finder);
 287         }
 288         catch (InstantiationException e) {
 289             throw new Fault("Error: Can't create new instance of test finder: " + e);
 290         }
 291         catch (IllegalAccessException e) {
 292             throw new Fault("Error: Can't access test finder: " + e);
 293         }
 294         catch (TestFinder.Fault e) {
 295             throw new Fault("Error: Can't initialize test-finder: " + e.getMessage());
 296         }
 297 
 298         return testFinder;
 299     }
 300 
 301 


 369 
 370     /**
 371      * Read the tests from a file in test suite
 372      */
 373     private TestTree.Node read0(TestFinder finder, File file, TestTree testTree, Set<File> allFiles)
 374     {
 375         // keep track of which files we have read, and ignore duplicates
 376         if (allFiles.contains(file))
 377             return null;
 378         else
 379             allFiles.add(file);
 380 
 381         finder.read(file);
 382         TestDescription[] tests = finder.getTests();
 383         File[] files = finder.getFiles();
 384 
 385         if (tests.length == 0 && files.length == 0)
 386             return null;
 387 
 388         Arrays.sort(files);
 389         Arrays.sort(tests, new Comparator<TestDescription>() {
 390             public int compare(TestDescription td1, TestDescription td2) {


 391                 return td1.getRootRelativeURL().compareTo(td2.getRootRelativeURL());
 392             }
 393         });
 394 
 395         Vector<TestTree.Node> v = new Vector<>();
 396         for (int i = 0; i < files.length; i++) {
 397             TestTree.Node n = read0(finder, files[i], testTree, allFiles);
 398             if (n != null)
 399                 v.addElement(n);
 400         }
 401         TestTree.Node[] nodes = new TestTree.Node[v.size()];
 402         v.copyInto(nodes);
 403 
 404         return testTree.new Node(file.getName(), tests, nodes);
 405     }
 406 
 407     //------------------------------------------------------------------------------------------
 408 
 409     /**
 410      * Write an int to a data output stream using a variable length encoding.


 444      * @see BinaryTestFinder.StringTable
 445      */
 446     static class StringTable {
 447         /**
 448          * Add a new string to the table; if it has already been added,
 449          * increase its use count.
 450          */
 451         void add(String s) {
 452             Entry e = map.get(s);
 453             if (e == null) {
 454                 e = new Entry();
 455                 map.put(s, e);
 456             }
 457             e.useCount++;
 458         }
 459 
 460         /**
 461          * Add all the strings used in a test description to the table.
 462          */
 463         void add(TestDescription test) {
 464             for (Iterator<String> i = test.getParameterKeys(); i.hasNext(); ) {
 465                 String key = (i.next());
 466                 String param = test.getParameter(key);
 467                 add(key);
 468                 add(param);
 469             }
 470         }
 471 
 472         /**
 473          * Return the number of strings in the table.
 474          */
 475         int getSize() {
 476             return map.size();
 477         }
 478 
 479         /**
 480          * Return the number of sstrings that were written to the output file.
 481          * Not all strings are written out: only frequently used ones are.
 482          */
 483         int getWrittenSize() {
 484             return writtenSize;
 485         }


 650          * Each test description is written as a count followed by that many
 651          * name-value pairs of string references.
 652          */
 653         void write(DataOutputStream o) throws IOException {
 654             writeInt(o, tests.size());
 655             for (int i = 0; i < tests.size(); i++) {
 656                 TestDescription td = tests.elementAt(i);
 657                 Entry e = testMap.get(td);
 658                 e.index = o.size();
 659                 write(td, o);
 660             }
 661         }
 662 
 663         /**
 664          * Write a single test description to a stream. It is written as a count,
 665          * followed by that many name-value pairs of string references.
 666          */
 667         private void write(TestDescription td, DataOutputStream o) throws IOException {
 668             // should consider using load/save here
 669             writeInt(o, td.getParameterCount());
 670             for (Iterator<String> i = td.getParameterKeys(); i.hasNext(); ) {
 671                 String key = (i.next());
 672                 String value = td.getParameter(key);
 673                 stringTable.writeRef(key, o);
 674                 stringTable.writeRef(value, o);
 675             }
 676         }
 677 
 678         private Map<TestDescription, Entry> testMap = new HashMap<>();
 679         private Vector<TestDescription> tests = new Vector<>();
 680         private StringTable stringTable;
 681 
 682         /**
 683          * Data for each test description in the table.
 684          */
 685         class Entry {
 686             /**
 687              * The byte offset of the test description in the stream when
 688              * last written out.
 689              */
 690             int index = -1;
 691         }


< prev index next >