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.config.spring.parsers.processors;
8   
9   import org.mule.config.spring.parsers.PreProcessor;
10  
11  import javax.xml.parsers.ParserConfigurationException;
12  
13  import org.junit.Test;
14  
15  public class CheckRequiredAttributesTestCase extends AbstractPreProcessorTestCase
16  {
17  
18      @Test
19      public void testSingleSetSingleAttribute() throws ParserConfigurationException
20      {
21          String[][] groups = new String[][] {
22              new String[]{ "a1" }
23          };
24          String text = "must have all attributes for one of the sets: [a1]";
25          
26          // no attributes
27          assertBad(groups, "", text);
28          
29          // optional attribute
30          assertBad(groups, "x", text);
31          
32          // required attribute
33          assertOk(groups, "a1");
34          assertOk(groups, "a1 x");
35          assertOk(groups, "x a1");
36      }
37  
38      @Test
39      public void testSingleSetMultipleAttributes() throws ParserConfigurationException
40      {
41          String[][] groups = new String[][] {
42              new String[] { "b1", "b2" }
43          };
44          String text = "must have all attributes for one of the sets: [b1, b2]";
45  
46          // no attributes
47          assertBad(groups, "", text);
48  
49          // optional attribute
50          assertBad(groups, "x", text);
51          
52          // one of the required attributes
53          assertBad(groups, "b1", text);
54          assertBad(groups, "b2", text);
55          
56          // one of the required attributes and an optional attribute
57          assertBad(groups, "x b1", text);
58          assertBad(groups, "x b2", text);
59  
60          // both required attributes
61          assertOk(groups, "b1 b2");
62          
63          // both required attributes and an optional attribute
64          assertOk(groups, "x b1 b2");
65      }
66  
67      @Test
68      public void testTwoSetsSingleAttribute() throws ParserConfigurationException
69      {
70          String[][] groups = new String[][] {
71              new String[] { "a1" }, 
72              new String[] { "b1" }
73          };
74          String text = "must have all attributes for one of the sets: [a1] [b1]";
75          
76          // empty set
77          assertBad(groups, "", text);
78          
79          // only optional attributes
80          assertBad(groups, "x", text);
81          assertBad(groups, "x y", text);
82          
83          // one attribute from a required set
84          assertOk(groups, "a1");
85          assertOk(groups, "b1");
86          
87          // one attribute from a required set and optional attributes
88          assertOk(groups, "a1 x");
89          assertOk(groups, "x a1");
90          assertOk(groups, "b1 x");
91          assertOk(groups, "x b1");
92  
93          // attributes from both sets, assuming this is OK, too as both groups are fully satisfied
94          assertOk(groups, "a1 b1");
95      }
96      
97      @Test
98      public void testTwoSetsEmptySecondSet() throws ParserConfigurationException
99      {
100         String[][] groups = new String[][] {
101             new String[] { "a1" },
102             new String[] {}
103         };
104         String text = "must have all attributes for one of the sets: [a1]";
105         
106         // no attributes
107         assertBad(groups, "", text);
108         
109         // only optional attributes
110         assertBad(groups, "x", text);
111         assertBad(groups, "x b1", text);
112 
113         // required attribute
114         assertOk(groups, "a1");
115         assertOk(groups, "a1 x");
116         assertOk(groups, "x a1");
117     }
118     
119     @Test
120     public void testTwoSetsMultipleAttributes() throws ParserConfigurationException
121     {
122         String[][] groups = new String[][] {
123             new String[] { "a1", "a2" },
124             new String[] { "b1", "b2" }
125         };
126         String text = "must have all attributes for one of the sets: [a1, a2] [b1, b2]";
127         
128         // no attributes
129         assertBad(groups, "", text);
130         
131         // only optional attributes
132         assertBad(groups, "x", text);
133 
134         // only one attribute from the required set
135         assertBad(groups, "a1", text);
136         assertBad(groups, "a2", text);
137         assertBad(groups, "b1", text);
138         assertBad(groups, "b2", text);
139         assertBad(groups, "a1 x", text);
140         assertBad(groups, "a2 x", text);
141         assertBad(groups, "b1 x", text);
142         assertBad(groups, "b2 x", text);
143         assertBad(groups, "x a1", text);
144         assertBad(groups, "x a2", text);
145         assertBad(groups, "x b1", text);
146         assertBad(groups, "x b2", text);
147         assertBad(groups, "a1 b1", text);
148         
149         // attributes from one required set
150         assertOk(groups, "a1 a2");
151         assertOk(groups, "x a1 a2");
152         assertOk(groups, "a1 x a2");
153         assertOk(groups, "a1 a2 x");
154         assertOk(groups, "b1 b2");
155         assertOk(groups, "x b1 b2");
156         assertOk(groups, "b1 x b2");
157         assertOk(groups, "b1 b2 x");
158         
159         // attributes from both required sets
160         assertOk(groups, "a1 a2 b1");
161         assertOk(groups, "x a1 a2 b1");
162         assertOk(groups, "a1 x a2 b1");
163         assertOk(groups, "a1 a2 x b1");
164         assertOk(groups, "a1 a2 b1 x");
165         assertOk(groups, "a1 a2 b2");
166         assertOk(groups, "x a1 a2 b2");
167         assertOk(groups, "a1 x a2 b2");
168         assertOk(groups, "a1 a2 x b2");
169         assertOk(groups, "a1 a2 b2 x");
170         assertOk(groups, "b1 b2 a1");
171         assertOk(groups, "x b1 b2 a1");
172         assertOk(groups, "b1 x b2 a1");
173         assertOk(groups, "b1 b2 x a1");
174         assertOk(groups, "b1 b2 a1 x");
175         assertOk(groups, "b1 b2 a2");
176         assertOk(groups, "x b1 b2 a2");
177         assertOk(groups, "b1 x b2 a2");
178         assertOk(groups, "b1 b2 x a2");
179         assertOk(groups, "b1 b2 a2 x");
180     }
181 
182     @Test
183     public void testTwoSetsOverlappingAttributes() throws ParserConfigurationException
184     {
185         String[][] groups = new String[][] {
186             new String[] { "a1", "a2" },
187             new String[] { "a1", "b1" }
188         };
189         String text = "must have all attributes for one of the sets: [a1, a2] [a1, b1]";
190         
191         // no attributes
192         assertBad(groups, "", text);
193         
194         // only optional attributes
195         assertBad(groups, "x", text);
196 
197         // attributes from first group
198         assertOk(groups, "a1 a2");
199         assertOk(groups, "x a1 a2");
200         assertOk(groups, "a1 x a2");
201         assertOk(groups, "a1 a2 x");
202         
203         // attributes from second group
204         assertOk(groups, "a1 b1");
205         assertOk(groups, "x a1 b1");
206         assertOk(groups, "a1 x b1");
207         assertOk(groups, "a1 b1 x");
208         
209         // attributes from both groups
210         assertOk(groups, "a1 a2 b1");
211     }
212     
213     @Test
214     public void testRealWorld() throws ParserConfigurationException
215     {
216         String[][] groups = new String[][] {
217             new String[] { "address" },
218             new String[] { "ref" },
219             new String[] { "type", "from" },
220             new String[] { "type", "recipient" }
221         };
222         
223         assertOk(groups, "from id name type");
224     }
225 
226     protected PreProcessor createCheck(String[][] constraint)
227     {
228         return new CheckRequiredAttributes(constraint);
229     }
230 }