View Javadoc

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