1 | /* |
2 | * Created on Sep 11, 2004 |
3 | * |
4 | * TODO To change the template for this generated file go to |
5 | * Window - Preferences - Java - Code Style - Code Templates |
6 | */ |
7 | package biz.xsoftware.manifest; |
8 | |
9 | import java.io.File; |
10 | import java.net.URL; |
11 | import java.util.jar.Manifest; |
12 | |
13 | import junit.framework.TestCase; |
14 | |
15 | /** |
16 | * This is purely so emma always always reports code coverage |
17 | * numbers on everything |
18 | */ |
19 | public class TestManifestInfo extends TestCase { |
20 | |
21 | private final static String DUMMY = "dummy"; |
22 | |
23 | private File jarFile; |
24 | |
25 | /** |
26 | * @param arg0 |
27 | */ |
28 | public TestManifestInfo(String arg0) { |
29 | super(arg0); |
30 | } |
31 | public void setUp() { |
32 | String jarLoc = System.getProperty("jar.name"); |
33 | jarFile = new File(jarLoc); |
34 | } |
35 | |
36 | public void testManifestInfo() throws Throwable { |
37 | FakeJarLocator mock = new FakeJarLocator(jarFile, "should.not.matter.class"); |
38 | |
39 | ManifestInfo.setJarLocator(mock); |
40 | |
41 | ManifestInfo.main(new String[] {"-version"}); |
42 | |
43 | ManifestInfo.main(new String[] {"-manifest"}); |
44 | |
45 | String version = new ManifestInfo().getVersion(); |
46 | assertEquals("version from build.xml should equal version in jar", System.getProperty("version"), version); |
47 | } |
48 | |
49 | public void testRunMainClass() throws Throwable { |
50 | FakeJarLocator mock = new FakeJarLocator(jarFile, TestManifestInfo.class.getName()); |
51 | ManifestInfo.setJarLocator(mock); |
52 | ManifestInfo.main(new String[] {DUMMY}); |
53 | |
54 | assertTrue("should have one argument", argsLen == 1); |
55 | assertEquals("variable should have been passed through", DUMMY, dummyArg); |
56 | } |
57 | |
58 | public void testExceptionFromMainClass() throws Throwable { |
59 | FakeJarLocator mock = new FakeJarLocator(jarFile, ManifestUtilImpl.class.getName()); |
60 | ManifestInfo.setJarLocator(mock); |
61 | ManifestInfo.main(new String[0]); |
62 | } |
63 | |
64 | public void testClassNotFound() throws Throwable { |
65 | FakeJarLocator mock = new FakeJarLocator(jarFile, "this.class.is.not.found"); |
66 | ManifestInfo.setJarLocator(mock); |
67 | ManifestInfo.main(new String[0]); |
68 | } |
69 | public void testTOOLSJAVAMain() throws Throwable { |
70 | FakeJarLocator mock = new FakeJarLocator(jarFile, " TOOLS.JAVA.Main "); |
71 | ManifestInfo.setJarLocator(mock); |
72 | ManifestInfo.main(new String[0]); |
73 | } |
74 | public void testNullClassName() throws Throwable { |
75 | FakeJarLocator mock = new FakeJarLocator(jarFile, null); |
76 | ManifestInfo.setJarLocator(mock); |
77 | ManifestInfo.main(new String[0]); |
78 | } |
79 | |
80 | public void testGetMainClass() { |
81 | Manifest mf = new Manifest(); |
82 | mf.getMainAttributes().putValue("SubMain-Class", " xx "); |
83 | ManifestUtilImpl util = new ManifestUtilImpl(); |
84 | String s = util.getMainClass(mf); |
85 | assertEquals("should have trimmed the value", "xx", s); |
86 | } |
87 | |
88 | public void testGetFile() throws Exception { |
89 | File f = new File("tools"+File.separator+"buildtemplate.jar"); |
90 | assertTrue("file should exist before we test this", f.exists()); |
91 | URL url = f.toURL(); |
92 | URL urlToClassFile = new URL("file", null, -1, url+"!/biz/xsoftware/buildtemplate/Util.class"); |
93 | File tmp = new ManifestUtilImpl().getFile(urlToClassFile); |
94 | |
95 | assertNotNull("Should return a real file", tmp); |
96 | assertTrue("file should still exist", tmp.exists()); |
97 | } |
98 | |
99 | private class FakeJarLocator implements ManifestInfo.ManifestUtil { |
100 | private File file; |
101 | private String mainClass; |
102 | public FakeJarLocator(File f, String mainClass) { |
103 | this.file = f; |
104 | this.mainClass = mainClass; |
105 | } |
106 | public File getFile(URL url) { |
107 | return file; |
108 | } |
109 | public void exit(int code) { |
110 | //do nothing!!! |
111 | } |
112 | public String getMainClass(Manifest manifest) { |
113 | return mainClass; |
114 | } |
115 | } |
116 | |
117 | //dummy main method for testing!!! |
118 | public static void main(String[] args) { |
119 | argsLen = args.length; |
120 | dummyArg = args[0]; |
121 | } |
122 | private static int argsLen; |
123 | private static String dummyArg; |
124 | } |