1 package com.atlassian.plugin.test;
2
3 import org.apache.commons.io.IOUtils;
4 import org.junit.Test;
5
6 import java.io.File;
7 import java.net.URL;
8 import java.net.URLClassLoader;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 public class TestPluginJarBuilder {
15
16 @Test
17 public void testBuild() throws Exception {
18 File jar = new PluginJarBuilder("foo")
19 .addJava("my.Foo", "package my; public class Foo { public String hi() {return \"hi\";}}")
20 .addResource("foo.txt", "Some text")
21 .addPluginInformation("someKey", "someName", "1.33")
22 .build();
23 assertNotNull(jar);
24
25 URLClassLoader cl = new URLClassLoader(new URL[]{jar.toURI().toURL()}, null);
26 Class<?> cls = cl.loadClass("my.Foo");
27 assertNotNull(cls);
28 Object foo = cls.getConstructor().newInstance();
29 String result = (String) cls.getMethod("hi").invoke(foo);
30 assertEquals("hi", result);
31 assertEquals("Some text", IOUtils.toString(cl.getResourceAsStream("foo.txt")));
32 assertNotNull(cl.getResource("META-INF/MANIFEST.MF"));
33
34 String xml = IOUtils.toString(cl.getResourceAsStream("atlassian-plugin.xml"));
35 assertTrue(xml.indexOf("someKey") > 0);
36 assertTrue(xml.indexOf("someName") > 0);
37 assertTrue(xml.indexOf("1.33") > 0);
38 }
39 }