1/* 2 * Copyright (c) 2014, 2015, 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 24import java.io.IOException; 25import java.lang.module.ModuleReference; 26import java.lang.module.ModuleFinder; 27import java.lang.module.ModuleDescriptor; 28import java.lang.module.ModuleReader; 29import java.net.URI; 30import java.util.HashMap; 31import java.util.HashSet; 32import java.util.Map; 33import java.util.Optional; 34import java.util.Set; 35 36/** 37 * A container of modules that acts as a ModuleFinder for testing 38 * purposes. 39 */ 40 41class ModuleLibrary implements ModuleFinder { 42 private final Map<String, ModuleReference> namesToReference = new HashMap<>(); 43 44 private ModuleLibrary() { } 45 46 void add(ModuleDescriptor... descriptors) { 47 for (ModuleDescriptor descriptor: descriptors) { 48 String name = descriptor.name(); 49 if (!namesToReference.containsKey(name)) { 50 //modules.add(descriptor); 51 52 URI uri = URI.create("module:/" + descriptor.name()); 53 54 ModuleReference mref = new ModuleReference(descriptor, uri) { 55 @Override 56 public ModuleReader open() { 57 throw new UnsupportedOperationException(); 58 } 59 }; 60 61 namesToReference.put(name, mref); 62 } 63 } 64 } 65 66 static ModuleLibrary of(ModuleDescriptor... descriptors) { 67 ModuleLibrary ml = new ModuleLibrary(); 68 ml.add(descriptors); 69 return ml; 70 } 71 72 @Override 73 public Optional<ModuleReference> find(String name) { 74 return Optional.ofNullable(namesToReference.get(name)); 75 } 76 77 @Override 78 public Set<ModuleReference> findAll() { 79 return new HashSet<>(namesToReference.values()); 80 } 81} 82 83