View Javadoc

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