1 /*
   2  * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 import java.io.*;
  25 import java.module.*;
  26 import java.util.*;
  27 import sun.module.tools.JRepo;
  28 
  29 /**
  30  * @test
  31  * @library ../tools ../tools/jrepo
  32  * @compile -XDignore.symbol.file
  33  *    RunMTest.java
  34  *    classp/MainX.java
  35  *    JRepoDependenciesTest.java
  36  * @run main/othervm
  37  *    -DTestDescriptionFactory.classname=JRepoDependenciesTest$MyFactory
  38  *    JRepoDependenciesTest
  39  */
  40 public class JRepoDependenciesTest {
  41     private static final boolean debug = Boolean.getBoolean("module.tools.debug");
  42 
  43     private static PrintStream outStream = null;
  44 
  45     private static final String[] tests = {
  46         "basic/import.mtest",
  47         "circular/circular2.mtest",
  48         "importpolicy/import1.mtest",
  49         "importpolicy/optional1.mtest",
  50         "importpolicy/optional2.mtest",
  51         "importpolicy/recurse1.mtest",
  52         "importpolicy/version1.mtest",
  53         "importpolicy/version3.mtest",
  54         "optional/basic.mtest",
  55         "optional/indirect.mtest",
  56         "optional/missing.mtest",
  57         "validation/shadow4.mtest",
  58         "version/version3.mtest"
  59     };
  60 
  61     private static PrintStream resultStream;
  62 
  63     public static void realMain(String args[]) throws Throwable {
  64         if (args.length == 0) {
  65             args = tests;
  66         }
  67         File resultFile = new File(
  68             System.getProperty("test.scratch", "."), "JRepoDepOut.txt");
  69         resultFile.delete();
  70         resultStream = new PrintStream(new FileOutputStream(resultFile));
  71         RunMTest.main(args);
  72         resultStream.close();
  73         compare(resultFile);
  74     }
  75 
  76     static void compare(File resultFile) throws Throwable {
  77         File goldFile = new File(
  78             System.getProperty("test.src", "."), "JRepoDepGold.txt");
  79         if (goldFile.exists()) {
  80             BufferedReader goldReader = new BufferedReader(
  81                 new FileReader(goldFile));
  82             BufferedReader resultReader = new BufferedReader(
  83                 new FileReader(resultFile));
  84             String goldLine = null;
  85             String resultLine = null;
  86             while ((goldLine = goldReader.readLine()) != null) {
  87                 resultLine = resultReader.readLine();
  88                 if (!goldLine.equals(resultLine)) {
  89                     System.err.println(
  90                         "MISMATCH: expected\n\t" + goldLine
  91                         + "but got\n\t" + resultLine);
  92                     fail();
  93                     break;
  94                 }
  95             }
  96             goldReader.close();
  97             resultReader.close();
  98             pass();
  99         } else {
 100             System.err.println(
 101                 "Error: JRepoDepGold.txt does not exist: "
 102                 + "not comparing actual v. expected results");
 103             fail();
 104         }
 105     }
 106 
 107     /** Return an array of Strings from the given String. */
 108     static String[] getArgs(String s) {
 109         List<String> args = new ArrayList<String>();
 110         StringTokenizer st = new StringTokenizer(s);
 111         while (st.hasMoreTokens()) {
 112             String token = st.nextToken();
 113             if (debug) System.err.println("adding arg " + token);
 114             args.add(token);
 115         }
 116         if (debug) System.err.println("args length is " + args.size());
 117         return args.toArray(new String[0]);
 118     }
 119 
 120     public static class MyFactory extends RunMTest.TestDescriptionFactory {
 121 
 122         protected RunMTest.TestDescription doCreate(String name) {
 123             return new MyTestDescription(name);
 124         }
 125     }
 126 
 127     public static class MyTestDescription extends RunMTest.TestDescription {
 128         MyTestDescription(String name) {
 129             super(name);
 130         }
 131 
 132         protected void runTest(RunMTest mTest) throws Exception {
 133             Repository parent = sun.module.repository.RepositoryConfig.getSystemRepository();
 134             Repository repository = Modules.newLocalRepository(
 135                 parent, mTest.getName(), mTest.outputDirectory);
 136 
 137             File file = mTest.file;
 138             char SEP = File.separatorChar;
 139             String mString = SEP + "mtest" + SEP;
 140             String cPath = file.getCanonicalPath();
 141             int k = cPath.lastIndexOf(mString);
 142             String subdir;
 143             if (k == -1) {
 144                 subdir = file.getName();
 145             } else {
 146                 subdir = cPath.substring(k + mString.length());
 147             }
 148             resultStream.println(">>>Test " + subdir);
 149 
 150             JRepo jr = new JRepo(resultStream, new PrintStream(System.err), null);
 151 
 152             try {
 153                 check(jr.run(getArgs(
 154                                  "dependencies -r "
 155                                  + mTest.outputDirectory.getAbsolutePath() + " "
 156                                  + name)));
 157             } catch (Exception ex) {
 158                 if (ex instanceof ModuleInitializationException) {
 159                     // Ignore: Assume this is expected or that it is a new error
 160                     // that will be diagnosed when by examining the modinit tests
 161                     // themselves.
 162                 } else {
 163                     unexpected(ex);
 164                 }
 165             }
 166         }
 167     }
 168 
 169     //--------------------- Infrastructure ---------------------------
 170     static volatile int passed = 0, failed = 0;
 171     static boolean pass() {passed++; return true;}
 172     static boolean fail() {failed++; Thread.dumpStack(); return false;}
 173     static boolean fail(String msg) {System.err.println(msg); return fail();}
 174     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 175     static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
 176     static boolean equal(Object x, Object y) {
 177         if (x == null ? y == null : x.equals(y)) return pass();
 178         else return fail(x + " not equal to " + y);}
 179     public static void main(String[] args) throws Throwable {
 180         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 181         System.out.println("\nPassed = " + passed + " failed = " + failed);
 182         if (failed > 0) throw new AssertionError("Some tests failed");}
 183 }