View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.launcher;
8   
9   import org.mule.config.Preferred;
10  import org.mule.module.launcher.descriptor.ApplicationDescriptor;
11  import org.mule.module.launcher.descriptor.DescriptorParser;
12  import org.mule.tck.junit4.AbstractMuleTestCase;
13  
14  import java.io.File;
15  import java.io.IOException;
16  
17  import org.apache.commons.collections.MultiMap;
18  import org.apache.commons.collections.map.MultiValueMap;
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  
25  public class DescriptorParserTestCase extends AbstractMuleTestCase
26  {
27  
28      @Test
29      public void testOverridePreferred() throws Exception
30      {
31          DefaultAppBloodhound bh = new DefaultAppBloodhound();
32          MultiMap overrides = new MultiValueMap();
33          overrides.put("properties", new TestDescriptorParserDefault());
34  
35          // test with default annotation values
36          bh.mergeParserOverrides(overrides);
37          assertEquals(1, bh.parserRegistry.size());
38          DescriptorParser result = bh.parserRegistry.get("properties");
39          assertNotNull(result);
40          assertTrue("@Preferred implementation ignored", result instanceof TestDescriptorParserDefault);
41      }
42  
43      @Test
44      public void testBothPreferredWithWeight() throws Exception
45      {
46          DefaultAppBloodhound bh = new DefaultAppBloodhound();
47          MultiMap overrides = new MultiValueMap();
48          overrides.put("properties", new TestDescriptorParserDefault());
49          overrides.put("properties", new TestDescriptorParserPreferred());
50  
51          // test with weigh attribute (we have 3 candidates now)
52          bh.mergeParserOverrides(overrides);
53          assertEquals(1, bh.parserRegistry.size());
54          DescriptorParser result = bh.parserRegistry.get("properties");
55          assertNotNull(result);
56          assertTrue("@Preferred implementation ignored", result instanceof TestDescriptorParserPreferred);
57      }
58  
59      @Test
60      public void testOverrideWithoutPreferred() throws Exception
61      {
62          DefaultAppBloodhound bh = new DefaultAppBloodhound();
63          MultiMap overrides = new MultiValueMap();
64          overrides.put("properties", new TestDescriptorParserNoAnnotation());
65  
66          // test with weigh attribute (we have 3 candidates now)
67          bh.mergeParserOverrides(overrides);
68          assertEquals(1, bh.parserRegistry.size());
69          DescriptorParser result = bh.parserRegistry.get("properties");
70          assertNotNull(result);
71          assertTrue("@Preferred implementation ignored", result instanceof TestDescriptorParserNoAnnotation);
72      }
73  
74      @Test
75      public void testMixedOverrides() throws Exception
76      {
77          DefaultAppBloodhound bh = new DefaultAppBloodhound();
78          MultiMap overrides = new MultiValueMap();
79          overrides.put("properties", new TestDescriptorParserNoAnnotation());
80          overrides.put("properties", new TestDescriptorParserDefault());
81  
82          // test with weigh attribute (we have 3 candidates now)
83          bh.mergeParserOverrides(overrides);
84          assertEquals(1, bh.parserRegistry.size());
85          DescriptorParser result = bh.parserRegistry.get("properties");
86          assertNotNull(result);
87          assertTrue("@Preferred implementation ignored", result instanceof TestDescriptorParserDefault);
88      }
89  
90  
91      /**
92       * Test parser with annotation default
93       */
94      @Preferred()
95      class TestDescriptorParserDefault implements DescriptorParser
96      {
97  
98          public ApplicationDescriptor parse(File descriptor) throws IOException
99          {
100             return null;
101         }
102 
103         public String getSupportedFormat()
104         {
105             return "properties";
106         }
107     }
108 
109     /**
110      * Test parser with weigh annotation
111      */
112     @Preferred(weight = 10)
113     class TestDescriptorParserPreferred implements DescriptorParser
114     {
115 
116         public ApplicationDescriptor parse(File descriptor) throws IOException
117         {
118             return null;
119         }
120 
121         public String getSupportedFormat()
122         {
123             return "properties";
124         }
125 
126     }
127 
128 
129     class TestDescriptorParserNoAnnotation implements DescriptorParser
130     {
131 
132         public ApplicationDescriptor parse(File descriptor) throws IOException
133         {
134             return null;
135         }
136 
137         public String getSupportedFormat()
138         {
139             return "properties";
140         }
141 
142     }
143 
144 }