--- /dev/null	Wed Aug  6 09:30:38 2008
+++ new/test/java/module/modinit/JRepoInstallTest.java	Wed Aug  6 09:30:43 2008
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+import java.io.*;
+import java.module.*;
+import java.util.*;
+import sun.module.JamUtils;
+import sun.module.tools.JRepo;
+
+/**
+ * @test
+ * @compile -XDignore.symbol.file
+ *    RunMTest.java
+ *    classp/MainX.java
+ *    JRepoInstallTest.java
+ * @run main/othervm
+ *    -DTestDescriptionFactory.classname=JRepoInstallTest$MyFactory
+ *    JRepoInstallTest
+ */
+public class JRepoInstallTest {
+    private static final boolean debug = Boolean.getBoolean("module.tools.debug");
+
+    private static final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    private static final PrintStream resultStream = new PrintStream(baos);
+
+    private static final String[] tests = {
+        "basic/import.mtest",
+    };
+
+
+    public static void realMain(String args[]) throws Throwable {
+        RunMTest.main(tests);
+    }
+
+    /** Return an array of Strings from the given String. */
+    static String[] getArgs(String s) {
+        List<String> args = new ArrayList<String>();
+        StringTokenizer st = new StringTokenizer(s);
+        while (st.hasMoreTokens()) {
+            String token = st.nextToken();
+            if (debug) System.err.println("adding arg " + token);
+            args.add(token);
+        }
+        if (debug) System.err.println("args length is " + args.size());
+        return args.toArray(new String[0]);
+    }
+
+    public static class MyFactory extends RunMTest.TestDescriptionFactory {
+
+        protected RunMTest.TestDescription doCreate(String name) {
+            return new MyTestDescription(name);
+        }
+    }
+
+    public static class MyTestDescription extends RunMTest.TestDescription {
+        MyTestDescription(String name) {
+            super(name);
+        }
+
+        protected void runTest(RunMTest mTest) throws Exception {
+            if (name.equals("m2")) {
+                // Only run this for m1.
+                return;
+            }
+
+            // Enables shadow file copies in the repository if we're running
+            // on Windows. This is to prevent file locking in the source
+            // location.
+            if (System.getProperty("os.platform").equalsIgnoreCase("windows")) {
+                System.setProperty("java.module.repository.shadowcopyfiles", "true");
+            }
+
+            Repository parent = sun.module.repository.RepositoryConfig.getApplicationRepository();
+            Repository repository = Modules.newLocalRepository(
+                mTest.getName(), mTest.outputDirectory, null, parent);
+
+            ModuleArchiveInfo[] maiList = new ModuleArchiveInfo[2];
+            for (ModuleArchiveInfo mai : repository.list()) {
+                if (mai.getName().equals("m1")) {
+                    maiList[0] = mai;
+                } else if (mai.getName().equals("m2")) {
+                    maiList[1] = mai;
+                }
+            }
+
+            JRepo jr = new JRepo(new PrintStream(System.out), resultStream, null);
+
+            // An mtest's initial repository has modules already installed.
+            // Since here we test installation itself, do so in a new
+            // repository.
+            File repoDir = new File(mTest.outputDirectory, "installA");
+            check(JamUtils.recursiveDelete(repoDir));
+            check(repoDir.mkdirs());
+            repository = Modules.newLocalRepository(
+                mTest.getName() + "installA", repoDir, null, parent);
+
+            // Install m1 with the -q flag, which should succeed even though
+            // m2 is not installed.
+            try {
+                check(jr.run(getArgs(
+                                 "install -q -r "
+                                 + repoDir.getAbsolutePath() + " "
+                                 + maiList[0].getFileName())));
+                pass();
+            } catch (Throwable t) {
+                unexpected(t);
+            }
+
+            // In a new repository, install m2 then m1: this should work:
+            repoDir = new File(mTest.outputDirectory, "installB");
+            check(JamUtils.recursiveDelete(repoDir));
+            check(repoDir.mkdirs());
+            repository = Modules.newLocalRepository(
+                mTest.getName() + "installB", repoDir, null, parent);
+            ModuleArchiveInfo maiM2 = repository.install(
+                new File(maiList[1].getFileName()).toURI());
+            check(maiM2 != null);
+            try {
+                check(jr.run(getArgs(
+                                 "install -r "
+                                 + repoDir.getAbsolutePath() + " "
+                                 + maiList[0].getFileName())));
+                pass();
+            } catch (Throwable t) {
+                unexpected(t);
+            }
+
+            // In a new repository, install m1 without the -q flag: this
+            // should fail
+            repoDir = new File(mTest.outputDirectory, "installC");
+            check(JamUtils.recursiveDelete(repoDir));
+            check(repoDir.mkdirs());
+            repository = Modules.newLocalRepository(
+                mTest.getName() + "installC", repoDir, null, parent);
+            try {
+                check(false == jr.run(getArgs(
+                                          "install -r "
+                                          + repoDir.getAbsolutePath() + " "
+                                          + maiList[0].getFileName())));
+                checkOutput("no module definition in the repository can satisfy the import dependency ModuleDependency");
+            } catch (Throwable t) {
+                pass();
+            }
+        }
+
+        static void checkOutput(String expected) {
+            String actual = baos.toString();
+            check(actual.contains(expected));
+            baos.reset();
+        }
+    }
+
+    //--------------------- Infrastructure ---------------------------
+    static volatile int passed = 0, failed = 0;
+    static boolean pass() {passed++; return true;}
+    static boolean fail() {failed++; Thread.dumpStack(); return false;}
+    static boolean fail(String msg) {System.err.println(msg); return fail();}
+    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
+    static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
+    static boolean equal(Object x, Object y) {
+        if (x == null ? y == null : x.equals(y)) return pass();
+        else return fail(x + " not equal to " + y);}
+    public static void main(String[] args) throws Throwable {
+        try {realMain(args);} catch (Throwable t) {unexpected(t);}
+        System.out.println("\nPassed = " + passed + " failed = " + failed);
+        if (failed > 0) throw new AssertionError("Some tests failed");}
+}
