View Javadoc

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