View Javadoc

1   /*
2    * $Id: DescriptorParserTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.module.launcher.descriptor.ApplicationDescriptor;
14  import org.mule.module.launcher.descriptor.DescriptorParser;
15  import org.mule.module.launcher.descriptor.Preferred;
16  import org.mule.tck.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  
24  public class DescriptorParserTestCase extends AbstractMuleTestCase
25  {
26  
27      public void testOverridePreferred() throws Exception
28      {
29          DefaultAppBloodhound bh = new DefaultAppBloodhound();
30          MultiMap overrides = new MultiValueMap();
31          overrides.put("properties", new TestDescriptorParserDefault());
32  
33          // test with default annotation values
34          bh.mergeParserOverrides(overrides);
35          assertEquals(1, bh.parserRegistry.size());
36          DescriptorParser result = bh.parserRegistry.get("properties");
37          assertNotNull(result);
38          assertTrue("@Preferred implementation ignored", result instanceof TestDescriptorParserDefault);
39      }
40  
41      public void testBothPreferredWithWeight() throws Exception
42      {
43          DefaultAppBloodhound bh = new DefaultAppBloodhound();
44          MultiMap overrides = new MultiValueMap();
45          overrides.put("properties", new TestDescriptorParserDefault());
46          overrides.put("properties", new TestDescriptorParserPreferred());
47  
48          // test with weigh attribute (we have 3 candidates now)
49          bh.mergeParserOverrides(overrides);
50          assertEquals(1, bh.parserRegistry.size());
51          DescriptorParser result = bh.parserRegistry.get("properties");
52          assertNotNull(result);
53          assertTrue("@Preferred implementation ignored", result instanceof TestDescriptorParserPreferred);
54      }
55  
56      public void testOverrideWithoutPreferred() throws Exception
57      {
58          DefaultAppBloodhound bh = new DefaultAppBloodhound();
59          MultiMap overrides = new MultiValueMap();
60          overrides.put("properties", new TestDescriptorParserNoAnnotation());
61  
62          // test with weigh attribute (we have 3 candidates now)
63          bh.mergeParserOverrides(overrides);
64          assertEquals(1, bh.parserRegistry.size());
65          DescriptorParser result = bh.parserRegistry.get("properties");
66          assertNotNull(result);
67          assertTrue("@Preferred implementation ignored", result instanceof TestDescriptorParserNoAnnotation);
68      }
69  
70      public void testMixedOverrides() throws Exception
71      {
72          DefaultAppBloodhound bh = new DefaultAppBloodhound();
73          MultiMap overrides = new MultiValueMap();
74          overrides.put("properties", new TestDescriptorParserNoAnnotation());
75          overrides.put("properties", new TestDescriptorParserDefault());
76  
77          // test with weigh attribute (we have 3 candidates now)
78          bh.mergeParserOverrides(overrides);
79          assertEquals(1, bh.parserRegistry.size());
80          DescriptorParser result = bh.parserRegistry.get("properties");
81          assertNotNull(result);
82          assertTrue("@Preferred implementation ignored", result instanceof TestDescriptorParserDefault);
83      }
84  
85  
86      /**
87       * Test parser with annotation default
88       */
89      @Preferred()
90      class TestDescriptorParserDefault implements DescriptorParser
91      {
92  
93          public ApplicationDescriptor parse(File descriptor) throws IOException
94          {
95              return null;
96          }
97  
98          public String getSupportedFormat()
99          {
100             return "properties";
101         }
102     }
103 
104     /**
105      * Test parser with weigh annotation
106      */
107     @Preferred(weight = 10)
108     class TestDescriptorParserPreferred implements DescriptorParser
109     {
110 
111         public ApplicationDescriptor parse(File descriptor) throws IOException
112         {
113             return null;
114         }
115 
116         public String getSupportedFormat()
117         {
118             return "properties";
119         }
120 
121     }
122 
123 
124     class TestDescriptorParserNoAnnotation implements DescriptorParser
125     {
126 
127         public ApplicationDescriptor parse(File descriptor) throws IOException
128         {
129             return null;
130         }
131 
132         public String getSupportedFormat()
133         {
134             return "properties";
135         }
136 
137     }
138 
139 }