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