1 package com.atlassian.plugin;
2
3 import com.atlassian.plugin.elements.ResourceDescriptor;
4 import com.atlassian.plugin.elements.ResourceLocation;
5 import org.dom4j.Document;
6 import org.dom4j.DocumentException;
7 import org.dom4j.DocumentHelper;
8 import org.junit.Rule;
9 import org.junit.Test;
10 import org.junit.rules.ExpectedException;
11
12 import java.util.List;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.assertTrue;
17
18 public class TestResources {
19 private static final String RESOURCE_DOC = "<foo>" + "<resource type=\"velocity\" name=\"view\">the content</resource>" + "<resource type=\"velocity\" name=\"edit\" />"
20 + "<resource type=\"image\" name=\"view\" />" + "</foo>";
21
22 @Rule
23 public final ExpectedException expectedException = ExpectedException.none();
24
25 @Test
26 public void testMultipleResources() throws DocumentException, PluginParseException {
27 final Resources resources = makeTestResources();
28
29 final List descriptors = resources.getResourceDescriptors();
30 assertEquals(3, descriptors.size());
31
32 assertDescriptorMatches((ResourceDescriptor) descriptors.get(0), "velocity", "view");
33 assertDescriptorMatches((ResourceDescriptor) descriptors.get(1), "velocity", "edit");
34 assertDescriptorMatches((ResourceDescriptor) descriptors.get(2), "image", "view");
35 }
36
37 @Test
38 public void testGetResourceDescriptor() throws DocumentException, PluginParseException {
39 final Resources resources = makeTestResources();
40
41 assertNull(resources.getResourceLocation("image", "edit"));
42 assertNull(resources.getResourceLocation("fish", "view"));
43 assertNull(resources.getResourceLocation(null, "view"));
44 assertNull(resources.getResourceLocation("image", null));
45
46 assertLocationMatches(resources.getResourceLocation("image", "view"), "image", "view");
47 }
48
49 @Test
50 public void testMultipleResourceWithClashingKeysFail() throws DocumentException {
51 expectedException.expect(PluginParseException.class);
52 expectedException.expectMessage("Duplicate resource with type 'velocity' and name 'view' found");
53 final Document document = DocumentHelper.parseText("<foo>" + "<resource type=\"velocity\" name=\"view\">the content</resource>"
54 + "<resource type=\"velocity\" name=\"view\" />" + "</foo>");
55
56 Resources.fromXml(document.getRootElement());
57 }
58
59 @Test
60 public void testParsingNullElementThrowsException() {
61 expectedException.expect(IllegalArgumentException.class);
62
63 Resources.fromXml(null);
64 }
65
66 @Test
67 public void testEmptyResources() {
68 final Resources resources = Resources.EMPTY_RESOURCES;
69 assertTrue("Empty resources should be empty", resources.getResourceDescriptors().isEmpty());
70 assertNull("Empty resources should return null for any resource", resources.getResourceLocation("i18n", "i18n.properties"));
71 }
72
73 private void assertLocationMatches(final ResourceLocation first, final String type, final String name) {
74 assertEquals(type, first.getType());
75 assertEquals(name, first.getName());
76 }
77
78 private void assertDescriptorMatches(final ResourceDescriptor first, final String type, final String name) {
79 assertEquals(type, first.getType());
80 assertEquals(name, first.getName());
81 }
82
83 private Resources makeTestResources() throws DocumentException, PluginParseException {
84 final Document document = DocumentHelper.parseText(RESOURCE_DOC);
85 return Resources.fromXml(document.getRootElement());
86 }
87 }