View Javadoc

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