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