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