test/org/openjdk/jigsaw/_PublishedRepository.java

Print this page




  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 // Compiled and invoked by pubrepo.sh
  25 
  26 import java.io.*;
  27 import java.util.*;
  28 import java.lang.module.*;
  29 import java.nio.*;
  30 import java.nio.channels.*;
  31 import java.nio.file.Files;
  32 import java.nio.file.*;
  33 import org.openjdk.jigsaw.*;
  34 
  35 import static java.lang.System.out;
  36 import static java.nio.file.StandardOpenOption.*;
  37 import org.openjdk.jigsaw.Repository.ModuleType;
  38 
  39 
  40 public class _PublishedRepository {
  41 
  42     private static ModuleSystem ms = ModuleSystem.base();
  43 
  44     private static <T> boolean eq(Collection<T> c1, Collection<T> c2) {
  45         return c1.containsAll(c2) && c2.containsAll(c1);
  46     }
  47 
  48     static final Path REPO = Paths.get("z.repo");
  49 
  50     static Set<ModuleId> mids = null;
  51 
  52     static Set<Path> mpaths = null;
  53 
  54     static void check(PublishedRepository pr) throws Exception {
  55         if (!pr.validate(null)) {
  56             throw new Exception("Repo invalid");
  57         }
  58         if (mids != null) {
  59             Collection<ModuleId> fmids = pr.listLocalModuleIds();
  60             assert eq(mids, fmids)
  61                 : String.format("expected %s; found %s", mids, fmids);
  62         }
  63     }
  64 
  65     static void create() throws Exception {
  66         PublishedRepository pr = PublishedRepository.open(REPO, true);
  67         check(pr);
  68     }
  69 
  70     static PublishedRepository open() throws IOException {
  71         return PublishedRepository.open(REPO, false);
  72     }
  73 
  74     static ModuleId toModuleId(Path p) {
  75         String fn = p.getFileName().toString();
  76         if (fn.endsWith(ModuleType.JAR.getFileNameSuffix())) {            
  77             return ms.parseModuleId(fn.replace(ModuleType.JAR.getFileNameSuffix(), ""));            
  78         } else {
  79             return ms.parseModuleId(fn.replace(ModuleType.JMOD.getFileNameSuffix(), ""));            
  80         }
  81     }
  82 
  83     static Path toModulePath(Path repo, ModuleId mid) {
  84         Path m = toModulePath(repo, mid, ModuleType.JAR);
  85         if (m == null) {
  86             m = toModulePath(repo, mid, ModuleType.JMOD);
  87         }
  88         return m;
  89     }
  90 
  91     private static Path toModulePath(Path repo, ModuleId mid, ModuleType type) {
  92         Path m = repo.resolve(mid.toString() + type.getFileNameSuffix());
  93         return m.toFile().exists() ? m : null;
  94     }
  95     
  96     static byte[] readStream(InputStream in)
  97         throws Exception
  98     {
  99         ByteArrayOutputStream out = new ByteArrayOutputStream();
 100         byte[] buf = new byte[8192];
 101         int n = 0;
 102         while ((n = in.read(buf)) > 0)
 103             out.write(buf, 0, n);
 104         return out.toByteArray();
 105     }
 106 
 107     static boolean equals(InputStream ia, InputStream ib)
 108         throws Exception
 109     {
 110         return Arrays.equals(readStream(ia), readStream(ib));
 111     }




  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 // Compiled and invoked by pubrepo.sh
  25 
  26 import java.io.*;
  27 import java.util.*;
  28 import java.lang.module.*;
  29 import java.nio.*;
  30 import java.nio.channels.*;
  31 import java.nio.file.Files;
  32 import java.nio.file.*;
  33 import org.openjdk.jigsaw.*;
  34 
  35 import static java.lang.System.out;
  36 import static java.nio.file.StandardOpenOption.*;
  37 import org.openjdk.jigsaw.Repository.ModuleFileType;
  38 
  39 
  40 public class _PublishedRepository {
  41 
  42     private static ModuleSystem ms = ModuleSystem.base();
  43 
  44     private static <T> boolean eq(Collection<T> c1, Collection<T> c2) {
  45         return c1.containsAll(c2) && c2.containsAll(c1);
  46     }
  47 
  48     static final Path REPO = Paths.get("z.repo");
  49 
  50     static Set<ModuleId> mids = null;
  51 
  52     static Set<Path> mpaths = null;
  53 
  54     static void check(PublishedRepository pr) throws Exception {
  55         if (!pr.validate(null)) {
  56             throw new Exception("Repo invalid");
  57         }
  58         if (mids != null) {
  59             Collection<ModuleId> fmids = pr.listLocalModuleIds();
  60             assert eq(mids, fmids)
  61                 : String.format("expected %s; found %s", mids, fmids);
  62         }
  63     }
  64 
  65     static void create() throws Exception {
  66         PublishedRepository pr = PublishedRepository.open(REPO, true);
  67         check(pr);
  68     }
  69 
  70     static PublishedRepository open() throws IOException {
  71         return PublishedRepository.open(REPO, false);
  72     }
  73 
  74     static ModuleId toModuleId(Path p) {
  75         String fn = p.getFileName().toString();
  76         if (fn.endsWith(ModuleFileType.JAR.getFileNameSuffix())) {
  77             return ms.parseModuleId(fn.replace(ModuleFileType.JAR.getFileNameSuffix(), ""));
  78         } else {
  79             return ms.parseModuleId(fn.replace(ModuleFileType.JMOD.getFileNameSuffix(), ""));
  80         }
  81     }
  82 
  83     static Path toModulePath(Path repo, ModuleId mid) {
  84         Path m = toModulePath(repo, mid, ModuleFileType.JAR);
  85         if (m == null) {
  86             m = toModulePath(repo, mid, ModuleFileType.JMOD);
  87         }
  88         return m;
  89     }
  90 
  91     private static Path toModulePath(Path repo, ModuleId mid, ModuleFileType type) {
  92         Path m = repo.resolve(mid.toString() + type.getFileNameSuffix());
  93         return m.toFile().exists() ? m : null;
  94     }
  95 
  96     static byte[] readStream(InputStream in)
  97         throws Exception
  98     {
  99         ByteArrayOutputStream out = new ByteArrayOutputStream();
 100         byte[] buf = new byte[8192];
 101         int n = 0;
 102         while ((n = in.read(buf)) > 0)
 103             out.write(buf, 0, n);
 104         return out.toByteArray();
 105     }
 106 
 107     static boolean equals(InputStream ia, InputStream ib)
 108         throws Exception
 109     {
 110         return Arrays.equals(readStream(ia), readStream(ib));
 111     }