View Javadoc
1   package com.atlassian.plugin.osgi.container.felix;
2   
3   import com.atlassian.plugin.util.ClassLoaderStack;
4   import com.google.common.collect.ImmutableMap;
5   import org.apache.commons.io.IOUtils;
6   import org.junit.Test;
7   import org.osgi.framework.Version;
8   
9   import java.io.File;
10  import java.io.IOException;
11  import java.io.PrintStream;
12  import java.net.MalformedURLException;
13  import java.net.URL;
14  import java.util.Map;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertTrue;
18  
19  public class TestExportsBuilderUtils {
20  
21      @Test
22      public void testParseExportWithVersions() throws IOException {
23          Map<String, String> expected = ImmutableMap.of(
24                  "com.atlassian.dummy", "1.2.3",
25                  "com.atlassian.dummy.sub", "4.5.6",
26                  "com.atlassian.wahaha", "1.3.6.SNAPSHOT");
27  
28          runParsingTest(expected, " # comment",
29                  "com.atlassian.dummy  =1.2.3",
30                  "com.atlassian.dummy.sub=  4.5.6",
31                  "com.atlassian.wahaha= 1.3.6-SNAPSHOT");
32      }
33  
34      @Test
35      public void testParseExportNoVersions() throws IOException {
36          Map<String, String> expected = ImmutableMap.of(
37                  "com.atlassian.dummy", Version.emptyVersion.toString(),
38                  "com.atlassian.dummy.sub", Version.emptyVersion.toString(),
39                  "com.atlassian.wahaha", Version.emptyVersion.toString());
40  
41          runParsingTest(expected, " # comment",
42                  "com.atlassian.dummy",
43                  "com.atlassian.dummy.sub       ",
44                  "      com.atlassian.wahaha");
45      }
46  
47      @Test
48      public void testConstructJdkExportsFromTestResource() {
49          Map<String, String> content = ExportBuilderUtils.parseExportFile("jdk-packages.test.txt");
50          Map<String, String> expected = ImmutableMap.of("foo.bar", Version.emptyVersion.toString(),
51                  "foo.baz", Version.emptyVersion.toString());
52  
53          assertEquals(expected, content);
54      }
55  
56      @Test
57      public void testConstructJdkExports() {
58          Map<String, String> content = ExportBuilderUtils.parseExportFile(ExportsBuilder.JDK8_PACKAGES_PATH);
59          assertTrue(content.containsKey("org.xml.sax"));
60      }
61  
62      private String createExportFile(String[] lines) throws IOException {
63          File file = File.createTempFile("TestExportsBuilderUtils", "txt");
64          file.deleteOnExit();
65  
66          PrintStream ps = null;
67          try {
68              ps = new PrintStream(file);
69  
70              for (String line : lines) {
71                  ps.println(line);
72              }
73          } finally {
74              IOUtils.closeQuietly(ps);
75          }
76  
77          return file.getAbsolutePath();
78      }
79  
80      private void runParsingTest(Map<String, String> expected, String... inputLines) throws IOException {
81          String exportFile = createExportFile(inputLines);
82  
83          ClassLoaderStack.push(new MockedClassLoader(exportFile));
84          Map<String, String> output;
85  
86          try {
87              output = ExportBuilderUtils.parseExportFile(exportFile);
88          } finally {
89              ClassLoaderStack.pop();
90          }
91  
92          assertEquals(expected, output);
93      }
94  
95      static class MockedClassLoader extends ClassLoader {
96          private String filePath;
97  
98          MockedClassLoader(String filePath) {
99              super(null);
100             this.filePath = filePath;
101         }
102 
103         @Override
104         public URL getResource(String name) {
105             try {
106                 return new File(name).toURI().toURL();
107             } catch (MalformedURLException e) {
108                 throw new RuntimeException(e);
109             }
110         }
111     }
112 }