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