|
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 */ |
|
23 |
|
24 /** |
|
25 * @test |
|
26 * @requires vm.aot |
|
27 * @modules jdk.aot/jdk.tools.jaotc |
|
28 * jdk.aot/jdk.tools.jaotc.collect |
|
29 * @run junit/othervm jdk.tools.jaotc.test.collect.ClassSearchTest |
|
30 */ |
|
31 |
|
32 |
|
33 |
|
34 package jdk.tools.jaotc.test.collect; |
|
35 |
|
36 import java.util.ArrayList; |
|
37 import java.util.HashSet; |
|
38 import java.util.List; |
|
39 import java.util.Set; |
|
40 import java.util.function.BiConsumer; |
|
41 import java.util.function.BiFunction; |
|
42 |
|
43 import org.junit.Assert; |
|
44 import org.junit.Test; |
|
45 |
|
46 import jdk.tools.jaotc.LoadedClass; |
|
47 import jdk.tools.jaotc.collect.ClassSearch; |
|
48 import jdk.tools.jaotc.collect.ClassSource; |
|
49 import jdk.tools.jaotc.collect.SearchFor; |
|
50 import jdk.tools.jaotc.collect.SearchPath; |
|
51 import jdk.tools.jaotc.collect.SourceProvider; |
|
52 |
|
53 public class ClassSearchTest { |
|
54 @Test(expected = InternalError.class) |
|
55 public void itShouldThrowExceptionIfNoProvidersAvailable() { |
|
56 ClassSearch target = new ClassSearch(); |
|
57 SearchPath searchPath = new SearchPath(); |
|
58 target.search(list(new SearchFor("foo")), searchPath); |
|
59 } |
|
60 |
|
61 @Test |
|
62 public void itShouldFindAProviderForEachEntry() { |
|
63 Set<String> searched = new HashSet<>(); |
|
64 ClassSearch target = new ClassSearch(); |
|
65 target.addProvider(provider("", (name, searchPath) -> { |
|
66 searched.add(name); |
|
67 return new NoopSource(); |
|
68 })); |
|
69 target.search(searchForList("foo", "bar", "foobar"), null); |
|
70 Assert.assertEquals(hashset("foo", "bar", "foobar"), searched); |
|
71 } |
|
72 |
|
73 private static SourceProvider provider(String supports, BiFunction<String, SearchPath, ClassSource> fn) { |
|
74 return new SourceProvider() { |
|
75 @Override |
|
76 public ClassSource findSource(String name, SearchPath searchPath) { |
|
77 return fn.apply(name, searchPath); |
|
78 } |
|
79 |
|
80 @Override |
|
81 public boolean supports(String type) { |
|
82 return supports.equals(type); |
|
83 } |
|
84 }; |
|
85 } |
|
86 |
|
87 @Test |
|
88 public void itShouldOnlySearchSupportedProvidersForKnownType() { |
|
89 Set<String> visited = new HashSet<>(); |
|
90 ClassSearch target = new ClassSearch(); |
|
91 |
|
92 target.addProvider(provider("jar", (name, searchPath) -> { |
|
93 visited.add("jar"); |
|
94 return null; |
|
95 })); |
|
96 |
|
97 target.addProvider(provider("dir", (name, searchPath) -> { |
|
98 visited.add("dir"); |
|
99 return null; |
|
100 })); |
|
101 |
|
102 try { |
|
103 target.search(list(new SearchFor("some", "dir")), null); |
|
104 } catch (InternalError e) { |
|
105 // throws because no provider gives a source |
|
106 } |
|
107 |
|
108 Assert.assertEquals(hashset("dir"), visited); |
|
109 } |
|
110 |
|
111 @Test(expected = InternalError.class) |
|
112 public void itShouldThrowErrorIfMultipleSourcesAreAvailable() { |
|
113 ClassSearch target = new ClassSearch(); |
|
114 target.addProvider(provider("", (name, searchPath) -> consumer -> Assert.fail())); |
|
115 target.addProvider(provider("", (name, searchPath) -> consumer -> Assert.fail())); |
|
116 |
|
117 target.search(searchForList("somethign"), null); |
|
118 } |
|
119 |
|
120 @Test |
|
121 public void itShouldSearchAllProvidersForUnknownType() { |
|
122 Set<String> visited = new HashSet<>(); |
|
123 ClassSearch target = new ClassSearch(); |
|
124 target.addProvider(provider("", (name, searchPath) -> { |
|
125 visited.add("1"); |
|
126 return null; |
|
127 })); |
|
128 target.addProvider(provider("", (name, searchPath) -> { |
|
129 visited.add("2"); |
|
130 return null; |
|
131 })); |
|
132 |
|
133 try { |
|
134 target.search(searchForList("foo"), null); |
|
135 } catch (InternalError e) { |
|
136 // throws because no provider gives a source |
|
137 } |
|
138 |
|
139 Assert.assertEquals(hashset("1", "2"), visited); |
|
140 } |
|
141 |
|
142 @Test |
|
143 public void itShouldTryToLoadSaidClassFromClassLoader() { |
|
144 Set<String> loaded = new HashSet<>(); |
|
145 |
|
146 ClassSearch target = new ClassSearch(); |
|
147 target.addProvider(new SourceProvider() { |
|
148 @Override |
|
149 public boolean supports(String type) { |
|
150 return true; |
|
151 } |
|
152 |
|
153 @Override |
|
154 public ClassSource findSource(String name, SearchPath searchPath) { |
|
155 return new ClassSource() { |
|
156 @Override |
|
157 public void eachClass(BiConsumer<String, ClassLoader> consumer) { |
|
158 consumer.accept("foo.Bar", new ClassLoader() { |
|
159 @Override |
|
160 public Class<?> loadClass(String nm) throws ClassNotFoundException { |
|
161 loaded.add(nm); |
|
162 return null; |
|
163 } |
|
164 }); |
|
165 } |
|
166 }; |
|
167 } |
|
168 }); |
|
169 |
|
170 java.util.List<LoadedClass> search = target.search(searchForList("/tmp/something"), null); |
|
171 Assert.assertEquals(list(new LoadedClass("foo.Bar", null)), search); |
|
172 } |
|
173 |
|
174 @Test(expected = InternalError.class) |
|
175 public void itShouldThrowInternalErrorWhenClassLoaderFails() { |
|
176 ClassLoader classLoader = new ClassLoader() { |
|
177 @Override |
|
178 public Class<?> loadClass(String name1) throws ClassNotFoundException { |
|
179 throw new ClassNotFoundException("failed to find " + name1); |
|
180 } |
|
181 }; |
|
182 |
|
183 ClassSearch target = new ClassSearch(); |
|
184 target.addProvider(provider("", (name, searchPath) -> consumer -> consumer.accept("foo.Bar", classLoader))); |
|
185 target.search(searchForList("foobar"), null); |
|
186 } |
|
187 |
|
188 private static List<SearchFor> searchForList(String... entries) { |
|
189 List<SearchFor> list = new ArrayList<>(); |
|
190 for (String entry : entries) { |
|
191 list.add(new SearchFor(entry)); |
|
192 } |
|
193 return list; |
|
194 } |
|
195 |
|
196 @SafeVarargs |
|
197 private static <T> List<T> list(T... entries) { |
|
198 List<T> list = new ArrayList<>(); |
|
199 for (T entry : entries) { |
|
200 list.add(entry); |
|
201 } |
|
202 return list; |
|
203 } |
|
204 |
|
205 @SafeVarargs |
|
206 private static <T> Set<T> hashset(T... entries) { |
|
207 Set<T> set = new HashSet<>(); |
|
208 for (T entry : entries) { |
|
209 set.add(entry); |
|
210 } |
|
211 return set; |
|
212 } |
|
213 |
|
214 private static class NoopSource implements ClassSource { |
|
215 @Override |
|
216 public void eachClass(BiConsumer<String, ClassLoader> consumer) { |
|
217 } |
|
218 } |
|
219 } |