View Javadoc
1   /*-
2    * #%L
3    * io.earcam.instrumental.archive.osgi
4    * %%
5    * Copyright (C) 2018 earcam
6    * %%
7    * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
8    * 
9    * You <b>must</b> choose to accept, in full - any individual or combination of 
10   * the following licenses:
11   * <ul>
12   * 	<li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
13   * 	<li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
14   * 	<li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
15   * 	<li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
16   * </ul>
17   * #L%
18   */
19  package io.earcam.instrumental.archive.osgi.auto;
20  
21  import static io.earcam.instrumental.archive.Archive.archive;
22  import static io.earcam.instrumental.archive.AsJar.asJar;
23  import static io.earcam.instrumental.archive.osgi.AsOsgiBundle.asOsgiBundle;
24  import static io.earcam.instrumental.module.auto.Classpaths.PROPERTY_CLASS_PATH;
25  import static java.io.File.pathSeparator;
26  import static java.nio.charset.StandardCharsets.UTF_8;
27  import static java.util.stream.Collectors.joining;
28  import static java.util.stream.Collectors.toList;
29  import static org.hamcrest.MatcherAssert.assertThat;
30  import static org.hamcrest.Matchers.containsInAnyOrder;
31  
32  import java.nio.file.Path;
33  import java.nio.file.Paths;
34  import java.util.Arrays;
35  import java.util.List;
36  import java.util.Set;
37  import java.util.UUID;
38  
39  import org.junit.jupiter.api.Test;
40  
41  import io.earcam.instrumental.module.osgi.BundleInfo;
42  import io.earcam.instrumental.module.osgi.Clause;
43  
44  public class ClasspathBundlesTest {
45  
46  	@Test
47  	void findsBundlesOnClasspath()
48  	{
49  		Path baseDir = Paths.get(".", "target", getClass().getCanonicalName(), "findsBundlesOnClasspath", UUID.randomUUID().toString());
50  		Path otherDir = baseDir.resolve("other");
51  		otherDir.toFile().mkdirs();
52  
53  		Path aJar = writeBundle(baseDir, "bundle.a");
54  		Path bJar = writeJar(baseDir, "vanilla-b");
55  		Path cJar = writeExplodedBundle(baseDir, "bundle.c");
56  		Path dJar = writeZip(baseDir, "de_nadda");
57  
58  		String oldClasspath = System.getProperty(PROPERTY_CLASS_PATH);
59  		try {
60  			System.setProperty(PROPERTY_CLASS_PATH, constructPath(aJar, bJar, cJar, dJar));
61  
62  			ClasspathBundles mapper = new ClasspathBundles();
63  
64  			List<String> classpathModuleNames = mapper.bundles().stream()
65  					.map(BundleInfo::symbolicName)
66  					.map(Clause::uniqueNames)
67  					.flatMap(Set::stream)
68  					.collect(toList());
69  
70  			assertThat(classpathModuleNames, containsInAnyOrder("bundle.a", "bundle.c"));
71  		} finally {
72  			System.setProperty(PROPERTY_CLASS_PATH, oldClasspath);
73  		}
74  	}
75  
76  
77  	private Path writeBundle(Path baseDir, String symbolicName)
78  	{
79  
80  		return archive()
81  				.configured(asOsgiBundle()
82  						.named(symbolicName))
83  				.to(baseDir.resolve(symbolicName + ".jar"));
84  	}
85  
86  
87  	private Path writeJar(Path baseDir, String filename)
88  	{
89  
90  		return archive()
91  				.configured(asJar()).to(baseDir.resolve(filename + ".jar"));
92  	}
93  
94  
95  	private Path writeExplodedBundle(Path baseDir, String symbolicName)
96  	{
97  
98  		return archive()
99  				.configured(asOsgiBundle()
100 						.named(symbolicName))
101 				.explodeTo(baseDir.resolve(symbolicName + "_exploded"));
102 	}
103 
104 
105 	private Path writeZip(Path baseDir, String name)
106 	{
107 		return archive()
108 				.with("some.txt", "blah blah".getBytes(UTF_8))
109 				.explodeTo(baseDir.resolve(name + ".zip"));
110 	}
111 
112 
113 	private static String constructPath(Path... jars)
114 	{
115 		return Arrays.stream(jars)
116 				.map(Path::toAbsolutePath)
117 				.map(Path::toString)
118 				.collect(joining(pathSeparator));
119 	}
120 }