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 * JRepoInstallTest.java
36 * @run main/othervm
37 * -DTestDescriptionFactory.classname=JRepoInstallTest$MyFactory
38 * JRepoInstallTest
39 */
40 public class JRepoInstallTest {
41 private static final boolean debug = Boolean.getBoolean("module.tools.debug");
42
43 private static final ByteArrayOutputStream baos = new ByteArrayOutputStream();
44 private static final PrintStream resultStream = new PrintStream(baos);
45
46 private static final String[] tests = {
47 "basic/import.mtest",
48 };
49
50
51 public static void realMain(String args[]) throws Throwable {
52 RunMTest.main(tests);
53 }
54
55 /** Return an array of Strings from the given String. */
56 static String[] getArgs(String s) {
57 List<String> args = new ArrayList<String>();
58 StringTokenizer st = new StringTokenizer(s);
59 while (st.hasMoreTokens()) {
60 String token = st.nextToken();
61 if (debug) System.err.println("adding arg " + token);
62 args.add(token);
63 }
64 if (debug) System.err.println("args length is " + args.size());
65 return args.toArray(new String[0]);
66 }
67
68 public static class MyFactory extends RunMTest.TestDescriptionFactory {
69
70 protected RunMTest.TestDescription doCreate(String name) {
71 return new MyTestDescription(name);
72 }
73 }
74
75 public static class MyTestDescription extends RunMTest.TestDescription {
76 MyTestDescription(String name) {
77 super(name);
78 }
79
80 protected void runTest(RunMTest mTest) throws Exception {
81 if (name.equals("m2")) {
82 // Only run this for m1.
83 return;
84 }
85
86 // Enables shadow file copies in the repository if we're running
87 // on Windows. This is to prevent file locking in the source
88 // location.
89 if (System.getProperty("os.platform").equalsIgnoreCase("windows")) {
90 System.setProperty("java.module.repository.shadowcopyfiles", "true");
91 }
92
93 Repository parent = sun.module.repository.RepositoryConfig.getApplicationRepository();
94 Repository repository = Modules.newLocalRepository(
95 mTest.getName(), mTest.outputDirectory, null, parent);
96
97 ModuleArchiveInfo[] maiList = new ModuleArchiveInfo[2];
98 for (ModuleArchiveInfo mai : repository.list()) {
99 if (mai.getName().equals("m1")) {
100 maiList[0] = mai;
101 } else if (mai.getName().equals("m2")) {
102 maiList[1] = mai;
103 }
104 }
105
106 JRepo jr = new JRepo(new PrintStream(System.out), resultStream, null);
107
108 // An mtest's initial repository has modules already installed.
109 // Since here we test installation itself, do so in a new
110 // repository.
111 File repoDir = new File(mTest.outputDirectory, "installA");
112 check(JamUtils.recursiveDelete(repoDir));
113 check(repoDir.mkdirs());
114 repository = Modules.newLocalRepository(
115 mTest.getName() + "installA", repoDir, null, parent);
116
117 // Install m1 with the -q flag, which should succeed even though
118 // m2 is not installed.
119 try {
120 check(jr.run(getArgs(
121 "install -q -r "
122 + repoDir.getAbsolutePath() + " "
123 + maiList[0].getFileName())));
124 pass();
125 } catch (Throwable t) {
126 unexpected(t);
127 }
128
129 // In a new repository, install m2 then m1: this should work:
130 repoDir = new File(mTest.outputDirectory, "installB");
131 check(JamUtils.recursiveDelete(repoDir));
132 check(repoDir.mkdirs());
133 repository = Modules.newLocalRepository(
134 mTest.getName() + "installB", repoDir, null, parent);
135 ModuleArchiveInfo maiM2 = repository.install(
136 new File(maiList[1].getFileName()).toURI());
137 check(maiM2 != null);
138 try {
139 check(jr.run(getArgs(
140 "install -r "
141 + repoDir.getAbsolutePath() + " "
142 + maiList[0].getFileName())));
143 pass();
144 } catch (Throwable t) {
145 unexpected(t);
146 }
147
148 // In a new repository, install m1 without the -q flag: this
149 // should fail
150 repoDir = new File(mTest.outputDirectory, "installC");
151 check(JamUtils.recursiveDelete(repoDir));
152 check(repoDir.mkdirs());
153 repository = Modules.newLocalRepository(
154 mTest.getName() + "installC", repoDir, null, parent);
155 try {
156 check(false == jr.run(getArgs(
157 "install -r "
158 + repoDir.getAbsolutePath() + " "
159 + maiList[0].getFileName())));
160 checkOutput("no module definition in the repository can satisfy the import dependency ModuleDependency");
161 } catch (Throwable t) {
162 pass();
163 }
164 }
165
166 static void checkOutput(String expected) {
167 String actual = baos.toString();
168 check(actual.contains(expected));
169 baos.reset();
170 }
171 }
172
173 //--------------------- Infrastructure ---------------------------
174 static volatile int passed = 0, failed = 0;
175 static boolean pass() {passed++; return true;}
176 static boolean fail() {failed++; Thread.dumpStack(); return false;}
177 static boolean fail(String msg) {System.err.println(msg); return fail();}
178 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
179 static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
180 static boolean equal(Object x, Object y) {
181 if (x == null ? y == null : x.equals(y)) return pass();
182 else return fail(x + " not equal to " + y);}
183 public static void main(String[] args) throws Throwable {
184 try {realMain(args);} catch (Throwable t) {unexpected(t);}
185 System.out.println("\nPassed = " + passed + " failed = " + failed);
186 if (failed > 0) throw new AssertionError("Some tests failed");}
187 }