1 /*
   2  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 import java.io.*;
  27 import java.net.URI;
  28 import java.util.*;
  29 import java.lang.module.*;
  30 import java.security.CodeSigner;
  31 import org.openjdk.jigsaw.*;
  32 
  33 class MockLibrary
  34     extends Library
  35 {
  36 
  37     private static JigsawModuleSystem jms = JigsawModuleSystem.instance();
  38 
  39     MockLibrary() { }
  40 
  41     private Map<String,List<ModuleId>> idsForName
  42         = new HashMap<String,List<ModuleId>>();
  43 
  44     private Map<ModuleId,ModuleInfo> infoForId
  45         = new HashMap<ModuleId,ModuleInfo>();
  46 
  47     MockLibrary add(ModuleInfo mi) {
  48         for (ModuleView mv : mi.views()) {
  49             String name = mv.id().name();
  50             infoForId.put(mv.id(), mi);
  51             List<ModuleId> ls = idsForName.get(name);
  52             if (ls == null) {
  53                 ls = new ArrayList<ModuleId>();
  54                 idsForName.put(name, ls);
  55             }
  56             ls.add(mv.id());
  57         }
  58         return this;
  59     }
  60 
  61     MockLibrary add(ModuleInfoBuilder mib) {
  62         return add(mib.build());
  63     }
  64 
  65     MockLibrary add(ModuleInfoBuilder.ModuleViewBuilder mvb) {
  66         return add(mvb.mib);
  67     }
  68 
  69     private Map<ModuleId,List<String>> publicClassesForId
  70         = new HashMap<ModuleId,List<String>>();
  71 
  72     private Map<ModuleId,List<String>> otherClassesForId
  73         = new HashMap<ModuleId,List<String>>();
  74 
  75     MockLibrary add(ModuleId id, String cn,
  76                     Map<ModuleId,List<String>> map)
  77     {
  78         List<String> ls = map.get(id);
  79         if (ls == null) {
  80             ls = new ArrayList<String>();
  81             map.put(id, ls);
  82         }
  83         ls.add(cn);
  84         return this;
  85     }
  86 
  87     MockLibrary addPublic(String mids, String cn) {
  88         return add(jms.parseModuleId(mids), cn, publicClassesForId);
  89     }
  90 
  91     MockLibrary addOther(String mids, String cn) {
  92         return add(jms.parseModuleId(mids), cn, otherClassesForId);
  93     }
  94 
  95     public String name() { return "mock-library"; }
  96     public int majorVersion() { return 0; }
  97     public int minorVersion() { return 1; }
  98 
  99     public URI location() {
 100         throw new UnsupportedOperationException();
 101     }
 102 
 103     public void installFromManifests(Collection<Manifest> mf) {
 104         throw new UnsupportedOperationException();
 105     }
 106 
 107     public void install(Collection<File> mf, boolean verifySignature) {
 108         throw new UnsupportedOperationException();
 109     }
 110 
 111     public Resolution resolve(Collection<ModuleIdQuery> midqs) {
 112         throw new UnsupportedOperationException();
 113     }
 114 
 115     public void install(Resolution res, boolean verifySignature) {
 116         throw new UnsupportedOperationException();
 117     }
 118 
 119     public Library parent() {
 120         return null;
 121     }
 122 
 123     protected void gatherLocalModuleIds(String mn, Set<ModuleId> mids) {
 124         throw new UnsupportedOperationException();
 125     }
 126 
 127     protected void gatherLocalDeclaringModuleIds(Set<ModuleId> set) {
 128         throw new UnsupportedOperationException();
 129     }
 130 
 131     public List<ModuleId> findModuleIds(String moduleName) {
 132         List<ModuleId> ls = idsForName.get(moduleName);
 133         if (ls == null)
 134             ls = Collections.emptyList();
 135         return ls;
 136     }
 137 
 138     public List<ModuleId> findModuleIds(ModuleIdQuery midq) {
 139         throw new UnsupportedOperationException();
 140     }
 141 
 142     public ModuleId findLatestModuleId(ModuleIdQuery midq) {
 143         throw new UnsupportedOperationException();
 144     }
 145 
 146     public ModuleInfo readLocalModuleInfo(ModuleId mid) {
 147         return infoForId.get(mid);
 148     }
 149 
 150     public byte[] readLocalModuleInfoBytes(ModuleId mid) {
 151         throw new UnsupportedOperationException();
 152     }
 153 
 154     public CodeSigner[] readLocalCodeSigners(ModuleId mid) {
 155         throw new UnsupportedOperationException();
 156     }
 157 
 158     public byte[] readLocalClass(ModuleId mid, String className) {
 159         throw new UnsupportedOperationException();
 160     }
 161 
 162     public void remove(List<ModuleId> mids, boolean dry) {
 163         throw new UnsupportedOperationException();
 164     }
 165 
 166     public void removeForcibly(List<ModuleId> mids) {
 167         throw new UnsupportedOperationException();
 168     }
 169 
 170     public ModuleId findModuleForClass(String className,
 171                                        ModuleId requestor)
 172         throws ClassNotFoundException
 173     {
 174         throw new UnsupportedOperationException();
 175     }
 176 
 177     public List<String> listLocalClasses(ModuleId mid, boolean all) {
 178         List<String> rv = new ArrayList<String>();
 179         List<String> pcns = publicClassesForId.get(mid);
 180         if (pcns != null)
 181             rv.addAll(pcns);
 182         if (all) {
 183             List<String> ocns = otherClassesForId.get(mid);
 184             if (ocns != null)
 185                 rv.addAll(ocns);
 186         }
 187         return rv;
 188     }
 189 
 190     public Configuration<Context> readConfiguration(ModuleId mid) {
 191         throw new UnsupportedOperationException();
 192     }
 193 
 194     public URI findLocalResource(ModuleId mid, String name) {
 195         throw new UnsupportedOperationException();
 196     }
 197 
 198     public File findLocalNativeLibrary(ModuleId mid, String name) {
 199         throw new UnsupportedOperationException();
 200     }
 201 
 202     public File classPath(ModuleId mid) {
 203         throw new UnsupportedOperationException();
 204     }
 205 
 206     public RemoteRepositoryList repositoryList() throws IOException {
 207         return new RemoteRepositoryList() {
 208             public List<RemoteRepository> repositories() {
 209                 return Collections.emptyList();
 210             }
 211 
 212             public RemoteRepository firstRepository() {
 213                 return null;
 214             }
 215 
 216             public RemoteRepository add(URI uri, int position) {
 217                 throw new UnsupportedOperationException();
 218             }
 219 
 220             public boolean remove(RemoteRepository rr) {
 221                 throw new UnsupportedOperationException();
 222             }
 223 
 224             public boolean areCatalogsStale() {
 225                 throw new UnsupportedOperationException();
 226             }
 227 
 228             public boolean updateCatalogs(boolean force) {
 229                 throw new UnsupportedOperationException();
 230             }
 231 
 232         };
 233     }
 234 }