1 /*
   2  * Copyright (c) 2017, 2018, 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 
  25 package jdk.tools.jaotc.test.collect;
  26 
  27 import java.net.MalformedURLException;
  28 import java.nio.file.Path;
  29 import java.util.HashSet;
  30 import java.util.Set;
  31 
  32 import jdk.tools.jaotc.collect.FileSupport;
  33 
  34 public class FakeFileSupport extends FileSupport {
  35     private final Set<String> exists = new HashSet<>();
  36     private final Set<String> directories = new HashSet<>();
  37 
  38     private final Set<String> checkedExists = new HashSet<>();
  39     private final Set<String> checkedDirectory = new HashSet<>();
  40     private final Set<String> checkedJarFileSystemRoots = new HashSet<>();
  41     private final Set<String> classloaderPaths = new HashSet<>();
  42 
  43     private Path jarFileSystemRoot = null;
  44     private final ClassLoader classLoader;
  45 
  46     public FakeFileSupport(Set<String> existing, Set<String> directories) {
  47         this.exists.addAll(existing);
  48         this.directories.addAll(directories);
  49 
  50         classLoader = new ClassLoader() {
  51             @Override
  52             public Class<?> loadClass(String name) throws ClassNotFoundException {
  53                 return null;
  54             }
  55         };
  56     }
  57 
  58     public void setJarFileSystemRoot(Path path) {
  59         jarFileSystemRoot = path;
  60     }
  61 
  62     @Override
  63     public boolean exists(Path path) {
  64         checkedExists.add(path.toString());
  65         return exists.contains(path.toString());
  66     }
  67 
  68     @Override
  69     public boolean isDirectory(Path path) {
  70         checkedDirectory.add(path.toString());
  71         return directories.contains(path.toString());
  72     }
  73 
  74     @Override
  75     public ClassLoader createClassLoader(Path path) throws MalformedURLException {
  76         classloaderPaths.add(path.toString());
  77         return classLoader;
  78     }
  79 
  80     @Override
  81     public Path getJarFileSystemRoot(Path jarFile) {
  82         checkedJarFileSystemRoots.add(jarFile.toString());
  83         return jarFileSystemRoot;
  84     }
  85 
  86     @Override
  87     public boolean isAbsolute(Path entry) {
  88         return entry.toString().startsWith("/");
  89     }
  90 
  91     public void addExist(String name) {
  92         exists.add(name);
  93     }
  94 
  95     public void addDirectory(String name) {
  96         directories.add(name);
  97     }
  98 
  99     public Set<String> getCheckedExists() {
 100         return checkedExists;
 101     }
 102 
 103     public Set<String> getCheckedDirectory() {
 104         return checkedDirectory;
 105     }
 106 
 107     public Set<String> getCheckedJarFileSystemRoots() {
 108         return checkedJarFileSystemRoots;
 109     }
 110 
 111     public Set<String> getClassloaderPaths() {
 112         return classloaderPaths;
 113     }
 114 }