1 package com.atlassian.plugin;
2
3 import java.util.List;
4
5 import junit.framework.TestCase;
6
7 import org.dom4j.Document;
8 import org.dom4j.DocumentException;
9 import org.dom4j.DocumentHelper;
10
11 import com.atlassian.plugin.elements.ResourceDescriptor;
12 import com.atlassian.plugin.elements.ResourceLocation;
13
14 public class TestResources extends TestCase
15 {
16 private static final String RESOURCE_DOC = "<foo>" + "<resource type=\"velocity\" name=\"view\">the content</resource>" + "<resource type=\"velocity\" name=\"edit\" />"
17 + "<resource type=\"image\" name=\"view\" />" + "</foo>";
18
19 public void testMultipleResources() throws DocumentException, PluginParseException
20 {
21 final Resources resources = makeTestResources();
22
23 final List descriptors = resources.getResourceDescriptors();
24 assertEquals(3, descriptors.size());
25
26 assertDescriptorMatches((ResourceDescriptor) descriptors.get(0), "velocity", "view");
27 assertDescriptorMatches((ResourceDescriptor) descriptors.get(1), "velocity", "edit");
28 assertDescriptorMatches((ResourceDescriptor) descriptors.get(2), "image", "view");
29 }
30
31 public void testGetResourceDescriptorsByType() throws DocumentException, PluginParseException
32 {
33 final Resources resources = makeTestResources();
34
35 assertEquals(0, resources.getResourceDescriptors("blah").size());
36
37 final List velocityResources = resources.getResourceDescriptors("velocity");
38 assertEquals(2, velocityResources.size());
39
40 assertDescriptorMatches((ResourceDescriptor) velocityResources.get(0), "velocity", "view");
41 assertDescriptorMatches((ResourceDescriptor) velocityResources.get(1), "velocity", "edit");
42 }
43
44 public void testNullTypeThrows() throws PluginParseException, DocumentException
45 {
46 final Resources resources = makeTestResources();
47 try
48 {
49 resources.getResourceDescriptors(null);
50 fail("IllegalArgumentException expected");
51 }
52 catch (final IllegalArgumentException expected)
53 {}
54 }
55
56 public void testGetResourceDescriptor() throws DocumentException, PluginParseException
57 {
58 final Resources resources = makeTestResources();
59
60 assertNull(resources.getResourceLocation("image", "edit"));
61 assertNull(resources.getResourceLocation("fish", "view"));
62 assertNull(resources.getResourceLocation(null, "view"));
63 assertNull(resources.getResourceLocation("image", null));
64
65 assertLocationMatches(resources.getResourceLocation("image", "view"), "image", "view");
66
67 }
68
69 public void testMultipleResourceWithClashingKeysFail() throws DocumentException
70 {
71 final Document document = DocumentHelper.parseText("<foo>" + "<resource type=\"velocity\" name=\"view\">the content</resource>"
72 + "<resource type=\"velocity\" name=\"view\" />" + "</foo>");
73
74 try
75 {
76
77 Resources.fromXml(document.getRootElement());
78 fail("Should have thrown exception about duplicate resources.");
79 }
80 catch (final PluginParseException e)
81 {
82 assertEquals("Duplicate resource with type 'velocity' and name 'view' found", e.getMessage());
83 }
84 }
85
86 public void testParsingNullElementThrowsException() throws Exception
87 {
88 try
89 {
90 Resources.fromXml(null);
91 fail("Expected exception when parsing null element");
92 }
93 catch (final IllegalArgumentException expected)
94 {}
95 }
96
97 public void testEmptyResources() throws Exception
98 {
99 final Resources resources = Resources.EMPTY_RESOURCES;
100 assertTrue("Empty resources should be empty", resources.getResourceDescriptors().isEmpty());
101 assertTrue("Empty resources should be empty by type", resources.getResourceDescriptors("i18n").isEmpty());
102 assertNull("Empty resources should return null for any resource", resources.getResourceLocation("i18n", "i18n.properties"));
103 }
104
105 private void assertLocationMatches(final ResourceLocation first, final String type, final String name)
106 {
107 assertEquals(type, first.getType());
108 assertEquals(name, first.getName());
109 }
110
111 private void assertDescriptorMatches(final ResourceDescriptor first, final String type, final String name)
112 {
113 assertEquals(type, first.getType());
114 assertEquals(name, first.getName());
115 }
116
117 private Resources makeTestResources() throws DocumentException, PluginParseException
118 {
119 final Document document = DocumentHelper.parseText(RESOURCE_DOC);
120 return Resources.fromXml(document.getRootElement());
121 }
122 }