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.JamUtils;
28 import sun.module.tools.JRepo;
29
30 /**
31 * @test
32 * @compile -XDignore.symbol.file
33 * RunMTest.java
34 * classp/MainX.java
35 * JRepoValidateTest.java
36 * @run main/othervm
37 * -DTestDescriptionFactory.classname=JRepoValidateTest$MyFactory
38 * JRepoValidateTest
39 */
40 public class JRepoValidateTest {
41 private static final boolean debug = Boolean.getBoolean("module.tools.debug");
42
43 private static final ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
44 private static final PrintStream outStream = new PrintStream(outBytes);
45 private static final ByteArrayOutputStream errBytes = new ByteArrayOutputStream();
46 private static final PrintStream errStream = new PrintStream(errBytes);
47
48 private static final String[] tests = {
49 "basic/import.mtest",
50 };
51
52
53 public static void realMain(String args[]) throws Throwable {
54 RunMTest.main(tests);
55 }
56
57 /** Return an array of Strings from the given String. */
58 static String[] getArgs(String s) {
59 List<String> args = new ArrayList<String>();
60 StringTokenizer st = new StringTokenizer(s);
61 while (st.hasMoreTokens()) {
62 String token = st.nextToken();
63 if (debug) System.err.println("adding arg " + token);
64 args.add(token);
65 }
66 if (debug) System.err.println("args length is " + args.size());
67 return args.toArray(new String[0]);
68 }
69
70 public static class MyFactory extends RunMTest.TestDescriptionFactory {
71
72 protected RunMTest.TestDescription doCreate(String name) {
73 return new MyTestDescription(name);
74 }
75 }
76
77 public static class MyTestDescription extends RunMTest.TestDescription {
78 MyTestDescription(String name) {
79 super(name);
80 }
81
82 protected void runTest(RunMTest mTest) throws Exception {
83 if (name.equals("m2")) {
84 // Only run this for m1.
85 return;
86 }
87
88 Repository parent = sun.module.repository.RepositoryConfig.getApplicationRepository();
89 Repository repository = Modules.newLocalRepository(
90 mTest.getName(), mTest.outputDirectory, null, parent);
91
92 ModuleArchiveInfo[] maiList = new ModuleArchiveInfo[2];
93
94 for (ModuleArchiveInfo mai : repository.list()) {
95 if (mai.getName().equals("m1")) {
96 maiList[0] = mai;
97 } else if (mai.getName().equals("m2")) {
98 maiList[1] = mai;
99 }
100 }
101
102 JRepo jr = new JRepo(outStream, errStream, null);
103
104 // Check deep validation on module m1 which imports m2
105 try {
106 check(jr.run(getArgs(
107 "validate -d -v -r "
108 + mTest.outputDirectory.getAbsolutePath() + " "
109 + maiList[0].getName())));
110 checkOutput("passed deep validation", outBytes);
111 } catch (Throwable t) {
112 unexpected(t);
113 }
114
115 // Check shallow validation on module m1 which imports m2
116 try {
117 check(jr.run(getArgs(
118 "validate -v -r "
119 + mTest.outputDirectory.getAbsolutePath() + " "
120 + maiList[0].getName())));
121 checkOutput("passed shallow validation", outBytes);
122 } catch (Throwable t) {
123 unexpected(t);
124 }
125
126 // Install m1 into a new repository, and validate, expecting failure
127 File repoDir = new File(mTest.outputDirectory, "validateA");
128 check(JamUtils.recursiveDelete(repoDir));
129 check(repoDir.mkdirs());
130 repository = Modules.newLocalRepository(
131 mTest.getName() + "validateA", repoDir, null, parent);
132 check(repository.install(
133 new File(maiList[0].getFileName()).toURI()) != null);
134 try {
135 check(false == jr.run(getArgs(
136 "validate -d -r "
137 + repoDir.getAbsolutePath() + " "
138 + maiList[0].getName())));
139 checkOutput("no module definition in the repository can satisfy the import dependency ModuleDependency", errBytes);
140 } catch (Throwable t) {
141 unexpected(t);
142 }
143 }
144
145 static void checkOutput(String expected, ByteArrayOutputStream baos) {
146 String actual = baos.toString();
147 if (!check(actual.contains(expected))) {
148 System.err.println(
149 "Got\n'" + actual + "'\nbut expected\n'" + expected + "'");
150 }
151 // Reset both streams after checking either one.
152 outBytes.reset();
153 errBytes.reset();
154 }
155 }
156
157 //--------------------- Infrastructure ---------------------------
158 static volatile int passed = 0, failed = 0;
159 static boolean pass() {passed++; return true;}
160 static boolean fail() {failed++; Thread.dumpStack(); return false;}
161 static boolean fail(String msg) {System.err.println(msg); return fail();}
162 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
163 static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
164 static boolean equal(Object x, Object y) {
165 if (x == null ? y == null : x.equals(y)) return pass();
166 else return fail(x + " not equal to " + y);}
167 public static void main(String[] args) throws Throwable {
168 try {realMain(args);} catch (Throwable t) {unexpected(t);}
169 System.out.println("\nPassed = " + passed + " failed = " + failed);
170 if (failed > 0) throw new AssertionError("Some tests failed");}
171 }