1 /*
   2  * Copyright (c) 2009, 2012, 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.
   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 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 import java.io.*;
  25 import java.net.URI;
  26 import java.util.*;
  27 import java.lang.module.*;
  28 import java.security.CodeSigner;
  29 import org.openjdk.jigsaw.*;
  30 
  31 class MockLibrary
  32     extends Library
  33 {
  34 
  35     private static JigsawModuleSystem jms = JigsawModuleSystem.instance();
  36 
  37     MockLibrary() { }
  38 
  39     private Map<String,List<ModuleId>> idsForName
  40         = new HashMap<String,List<ModuleId>>();
  41 
  42     private Map<ModuleId,ModuleInfo> infoForId
  43         = new HashMap<ModuleId,ModuleInfo>();
  44 
  45     MockLibrary add(ModuleInfo mi) {
  46         for (ModuleView mv : mi.views()) {
  47             String name = mv.id().name();
  48             infoForId.put(mv.id(), mi);
  49             List<ModuleId> ls = idsForName.get(name);
  50             if (ls == null) {
  51                 ls = new ArrayList<ModuleId>();
  52                 idsForName.put(name, ls);
  53             }
  54             ls.add(mv.id());
  55         }
  56         return this;
  57     }
  58 
  59     MockLibrary add(ModuleInfoBuilder mib) {
  60         return add(mib.build());
  61     }
  62 
  63     MockLibrary add(ModuleInfoBuilder.ModuleViewBuilder mvb) {
  64         return add(mvb.mib);
  65     }
  66 
  67     private Map<ModuleId,List<String>> publicClassesForId
  68         = new HashMap<ModuleId,List<String>>();
  69 
  70     private Map<ModuleId,List<String>> otherClassesForId
  71         = new HashMap<ModuleId,List<String>>();
  72 
  73     MockLibrary add(ModuleId id, String cn,
  74                     Map<ModuleId,List<String>> map)
  75     {
  76         List<String> ls = map.get(id);
  77         if (ls == null) {
  78             ls = new ArrayList<String>();
  79             map.put(id, ls);
  80         }
  81         ls.add(cn);
  82         return this;
  83     }
  84 
  85     MockLibrary addPublic(String mids, String cn) {
  86         return add(jms.parseModuleId(mids), cn, publicClassesForId);
  87     }
  88 
  89     MockLibrary addOther(String mids, String cn) {
  90         return add(jms.parseModuleId(mids), cn, otherClassesForId);
  91     }
  92 
  93     public String name() { return "mock-library"; }
  94     public int majorVersion() { return 0; }
  95     public int minorVersion() { return 1; }
  96 
  97     public URI location() {
  98         throw new UnsupportedOperationException();
  99     }
 100 
 101     public ModuleArchitecture architecture() {
 102         return ModuleArchitecture.ANY;
 103     }
 104 
 105     public void installFromManifests(Collection<Manifest> mf) {
 106         throw new UnsupportedOperationException();
 107     }
 108 
 109     public void install(Collection<File> mf, boolean verifySignature) {
 110         throw new UnsupportedOperationException();
 111     }
 112 
 113     public Resolution resolve(Collection<ModuleIdQuery> midqs) {
 114         throw new UnsupportedOperationException();
 115     }
 116 
 117     public void install(Resolution res, boolean verifySignature) {
 118         throw new UnsupportedOperationException();
 119     }
 120 
 121     public Library parent() {
 122         return null;
 123     }
 124 
 125     protected void gatherLocalModuleIds(String mn, Set<ModuleId> mids) {
 126         if (idsForName.containsKey(mn)) {
 127             mids.addAll(idsForName.get(mn));
 128         }
 129     }
 130 
 131     protected void gatherLocalDeclaringModuleIds(Set<ModuleId> set) {
 132         set.addAll(infoForId.keySet());
 133     }
 134 
 135     public List<ModuleId> findModuleIds(String moduleName) {
 136         List<ModuleId> ls = idsForName.get(moduleName);
 137         if (ls == null)
 138             ls = Collections.emptyList();
 139         return ls;
 140     }
 141 
 142     public List<ModuleId> findModuleIds(ModuleIdQuery midq) {
 143         throw new UnsupportedOperationException();
 144     }
 145 
 146     public ModuleId findLatestModuleId(ModuleIdQuery midq) {
 147         throw new UnsupportedOperationException();
 148     }
 149 
 150     public ModuleInfo readLocalModuleInfo(ModuleId mid) {
 151         return infoForId.get(mid);
 152     }
 153 
 154     public byte[] readLocalModuleInfoBytes(ModuleId mid) {
 155         throw new UnsupportedOperationException();
 156     }
 157 
 158     public CodeSigner[] readLocalCodeSigners(ModuleId mid) {
 159         throw new UnsupportedOperationException();
 160     }
 161 
 162     public byte[] readLocalClass(ModuleId mid, String className) {
 163         throw new UnsupportedOperationException();
 164     }
 165 
 166     public void remove(List<ModuleId> mids, boolean dry) {
 167         throw new UnsupportedOperationException();
 168     }
 169 
 170     public void removeForcibly(List<ModuleId> mids) {
 171         throw new UnsupportedOperationException();
 172     }
 173 
 174     public ModuleId findModuleForClass(String className,
 175                                        ModuleId requestor)
 176         throws ClassNotFoundException
 177     {
 178         throw new UnsupportedOperationException();
 179     }
 180 
 181     public List<String> listLocalClasses(ModuleId mid, boolean all) {
 182         List<String> rv = new ArrayList<String>();
 183         List<String> pcns = publicClassesForId.get(mid);
 184         if (pcns != null)
 185             rv.addAll(pcns);
 186         if (all) {
 187             List<String> ocns = otherClassesForId.get(mid);
 188             if (ocns != null)
 189                 rv.addAll(ocns);
 190         }
 191         return rv;
 192     }
 193 
 194     public Configuration<Context> readConfiguration(ModuleId mid) {
 195         throw new UnsupportedOperationException();
 196     }
 197 
 198     public URI findLocalResource(ModuleId mid, String name) {
 199         throw new UnsupportedOperationException();
 200     }
 201 
 202     public File findLocalNativeLibrary(ModuleId mid, String name) {
 203         throw new UnsupportedOperationException();
 204     }
 205 
 206     public File classPath(ModuleId mid) {
 207         throw new UnsupportedOperationException();
 208     }
 209 
 210     public RemoteRepositoryList repositoryList() throws IOException {
 211         return new RemoteRepositoryList() {
 212             public List<RemoteRepository> repositories() {
 213                 return Collections.emptyList();
 214             }
 215 
 216             public RemoteRepository firstRepository() {
 217                 return null;
 218             }
 219 
 220             public RemoteRepository add(URI uri, int position) {
 221                 throw new UnsupportedOperationException();
 222             }
 223 
 224             public boolean remove(RemoteRepository rr) {
 225                 throw new UnsupportedOperationException();
 226             }
 227 
 228             public boolean areCatalogsStale() {
 229                 throw new UnsupportedOperationException();
 230             }
 231 
 232             public boolean updateCatalogs(boolean force) {
 233                 throw new UnsupportedOperationException();
 234             }
 235 
 236         };
 237     }
 238 }
--- EOF ---