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                 goldLine = goldLine.replace('/', File.separatorChar);
  88                 resultLine = resultReader.readLine();
  89                 if (!goldLine.equals(resultLine)) {
  90                     System.err.println(
  91                         "MISMATCH: expected\n\t" + goldLine
  92                         + "but got\n\t" + resultLine);
  93                     fail();
  94                     break;
  95                 }
  96             }
  97             goldReader.close();
  98             resultReader.close();
  99             pass();
 100         } else {
 101             System.err.println(
 102                 "Error: JRepoDepGold.txt does not exist: "
 103                 + "not comparing actual v. expected results");
 104             fail();
 105         }
 106     }
 107 
 108     /** Return an array of Strings from the given String. */
 109     static String[] getArgs(String s) {
 110         List<String> args = new ArrayList<String>();
 111         StringTokenizer st = new StringTokenizer(s);
 112         while (st.hasMoreTokens()) {
 113             String token = st.nextToken();
 114             if (debug) System.err.println("adding arg " + token);
 115             args.add(token);
 116         }
 117         if (debug) System.err.println("args length is " + args.size());
 118         return args.toArray(new String[0]);
 119     }
 120 
 121     public static class MyFactory extends RunMTest.TestDescriptionFactory {
 122 
 123         protected RunMTest.TestDescription doCreate(String name) {
 124             return new MyTestDescription(name);
 125         }
 126     }
 127 
 128     public static class MyTestDescription extends RunMTest.TestDescription {
 129         MyTestDescription(String name) {
 130             super(name);
 131         }
 132 
 133         protected void runTest(RunMTest mTest) throws Exception {
 134             Repository parent = sun.module.repository.RepositoryConfig.getSystemRepository();
 135             Repository repository = Modules.newLocalRepository(
 136                 mTest.getName(), mTest.outputDirectory, null, parent);
 137 
 138             File file = mTest.file;
 139             char SEP = File.separatorChar;
 140             String mString = SEP + "mtest" + SEP;
 141             String cPath = file.getCanonicalPath();
 142             int k = cPath.lastIndexOf(mString);
 143             String subdir;
 144             if (k == -1) {
 145                 subdir = file.getName();
 146             } else {
 147                 subdir = cPath.substring(k + mString.length());
 148             }
 149             resultStream.println(">>>Test " + subdir);
 150 
 151             JRepo jr = new JRepo(resultStream, new PrintStream(System.err), null);
 152 
 153             try {
 154                 check(jr.run(getArgs(
 155                                  "dependencies -r "
 156                                  + mTest.outputDirectory.getAbsolutePath() + " "
 157                                  + name)));
 158             } catch (Exception ex) {
 159                 if (ex instanceof ModuleInitializationException) {
 160                     // Ignore: Assume this is expected or that it is a new error
 161                     // that will be diagnosed when by examining the modinit tests
 162                     // themselves.
 163                 } else {
 164                     unexpected(ex);
 165                 }
 166             }
 167         }
 168     }
 169 
 170     //--------------------- Infrastructure ---------------------------
 171     static volatile int passed = 0, failed = 0;
 172     static boolean pass() {passed++; return true;}
 173     static boolean fail() {failed++; Thread.dumpStack(); return false;}
 174     static boolean fail(String msg) {System.err.println(msg); return fail();}
 175     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 176     static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
 177     static boolean equal(Object x, Object y) {
 178         if (x == null ? y == null : x.equals(y)) return pass();
 179         else return fail(x + " not equal to " + y);}
 180     public static void main(String[] args) throws Throwable {
 181         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 182         System.out.println("\nPassed = " + passed + " failed = " + failed);
 183         if (failed > 0) throw new AssertionError("Some tests failed");}
 184 }