View Javadoc

1   package org.apache.torque.generator.variable;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNull;
24  
25  import org.apache.torque.generator.BaseTest;
26  import org.apache.torque.generator.qname.QualifiedName;
27  import org.apache.torque.generator.qname.QualifiedNameMap;
28  import org.apache.torque.generator.variable.Variable;
29  import org.apache.torque.generator.variable.VariableStore;
30  import org.junit.Test;
31  
32  public class VariableStoreTest extends BaseTest
33  {
34      @Test
35      public void testVariableScopePrecedence()
36      {
37          VariableStore store = new VariableStore();
38          store.startOutlet();
39          QualifiedName qualifiedName
40              = new QualifiedName("org.apache.torque.name");
41  
42          // fill store
43          store.set(new Variable(
44                  qualifiedName,
45                  "org.apache.torque.GENERATOR",
46                  Variable.Scope.OUTLET));
47          store.set(new Variable(
48                  qualifiedName,
49                  "org.apache.torque.CHILDREN1",
50                  Variable.Scope.CHILDREN));
51          store.startOutlet();
52          store.set(new Variable(
53                  qualifiedName,
54                  "org.apache.torque.CHILDREN2",
55                  Variable.Scope.CHILDREN));
56          store.set(new Variable(
57                  qualifiedName,
58                  "org.apache.torque.FILE",
59                  Variable.Scope.FILE));
60          store.set(new Variable(
61                  qualifiedName,
62                  "org.apache.torque.GLOBAL",
63                  Variable.Scope.GLOBAL));
64  
65          assertEquals(
66                  "org.apache.torque.GENERATOR",
67                  store.getInHierarchy(qualifiedName).getValue());
68          assertEquals(
69                  "org.apache.torque.GENERATOR",
70                  store.getContent().get(qualifiedName).getValue());
71  
72          store.remove(store.getInHierarchy(qualifiedName));
73  
74          assertEquals(
75                  "org.apache.torque.CHILDREN2",
76                  store.getInHierarchy(qualifiedName).getValue());
77          assertEquals(
78                  "org.apache.torque.CHILDREN2",
79                  store.getContent().get(qualifiedName).getValue());
80  
81          store.endOutlet();
82  
83          assertEquals(
84                  "org.apache.torque.CHILDREN1",
85                  store.getInHierarchy(qualifiedName).getValue());
86          assertEquals(
87                  "org.apache.torque.CHILDREN1",
88                  store.getContent().get(qualifiedName).getValue());
89  
90          store.endOutlet();
91  
92          assertEquals(
93                  "org.apache.torque.FILE",
94                  store.getInHierarchy(qualifiedName).getValue());
95          assertEquals(
96                  "org.apache.torque.FILE",
97                  store.getContent().get(qualifiedName).getValue());
98  
99          store.endFile();
100 
101         assertEquals(
102                 "org.apache.torque.GLOBAL",
103                 store.getInHierarchy(qualifiedName).getValue());
104         assertEquals(
105                 "org.apache.torque.GLOBAL",
106                 store.getContent().get(qualifiedName).getValue());
107 
108         store.endGeneration();
109 
110         assertNull(store.getInHierarchy(qualifiedName));
111         assertNull(store.getContent().get(qualifiedName));
112     }
113 
114     public void testNamespaceVisibility()
115     {
116         VariableStore store = new VariableStore();
117         store.set(new Variable(
118                 new QualifiedName("org.apache.torque.name"),
119                 "org.apache.torque.GENERATOR",
120                 Variable.Scope.OUTLET));
121         store.set(new Variable(
122                 new QualifiedName("org.apache.name"),
123                 "org.apache.FILE",
124                 Variable.Scope.FILE));
125         QualifiedName qualifiedName
126             = new QualifiedName("org.apache.torque.name");
127         assertEquals(
128                 "org.apache.torque.GENERATOR",
129                 store.getInHierarchy(qualifiedName).getValue());
130 
131         store.clear();
132         store.set(new Variable(
133                 new QualifiedName("org.apache.name"),
134                 "org.apache.GENERATOR",
135                 Variable.Scope.OUTLET));
136         store.set(new Variable(
137                 new QualifiedName("org.apache.torque.name"),
138                 "org.apache.torque.FILE",
139                 Variable.Scope.FILE));
140         assertEquals(
141                 "org.apache.torque.FILE",
142                 store.getInHierarchy(qualifiedName).getValue());
143 
144     }
145 
146     public void testGetInHierarchy()
147     {
148         VariableStore store = new VariableStore();
149         store.set(new Variable(
150                 new QualifiedName("org.apache.torque.name"),
151                 "org.apache.torque",
152                 Variable.Scope.OUTLET));
153         QualifiedName qualifiedName
154             = new QualifiedName("org.apache.torque.name");
155         assertEquals(
156                 "org.apache.torque",
157                 store.getInHierarchy(qualifiedName).getValue());
158         qualifiedName
159             = new QualifiedName("org.apache.torque.generator.name");
160         assertEquals(
161                 "org.apache.torque",
162                 store.getInHierarchy(qualifiedName).getValue());
163         qualifiedName
164             = new QualifiedName("org.apache.name");
165         assertNull(store.getInHierarchy(qualifiedName));
166     }
167 
168     public void testGetContents()
169     {
170         VariableStore store = new VariableStore();
171         store.startOutlet();
172         store.set(new Variable(
173                 new QualifiedName("org.apache.torque.generator"),
174                 "org.apache.torque.generator",
175                 Variable.Scope.OUTLET));
176         store.set(new Variable(
177                 new QualifiedName("org.apache.torque.children1"),
178                 "org.apache.torque.children1",
179                 Variable.Scope.CHILDREN));
180         store.startOutlet();
181         store.set(new Variable(
182                 new QualifiedName("org.apache.torque.children2"),
183                 "org.apache.torque.children2",
184                 Variable.Scope.CHILDREN));
185         store.set(new Variable(
186                 new QualifiedName("org.apache.torque.file"),
187                 "org.apache.torque.file",
188                 Variable.Scope.FILE));
189         store.set(new Variable(
190                 new QualifiedName("org.apache.torque.global"),
191                 "org.apache.torque.global",
192                 Variable.Scope.GLOBAL));
193 
194         QualifiedNameMap<Variable> storeContent = store.getContent();
195         assertEquals("storeContent should contain 5 entries",
196                 5,
197                 storeContent.size());
198 
199         {
200             Variable variable
201                     = storeContent.get(
202                         new QualifiedName("org.apache.torque.generator"));
203             assertEquals(
204                     "org.apache.torque.generator",
205                     variable.getValue());
206         }
207         {
208             Variable variable
209                     = storeContent.get(
210                         new QualifiedName("org.apache.torque.children1"));
211             assertEquals(
212                     "org.apache.torque.children1",
213                     variable.getValue());
214         }
215         {
216             Variable variable
217                     = storeContent.get(
218                         new QualifiedName("org.apache.torque.children2"));
219             assertEquals(
220                     "org.apache.torque.children2",
221                     variable.getValue());
222         }
223         {
224             Variable variable
225                     = storeContent.get(
226                         new QualifiedName("org.apache.torque.file"));
227             assertEquals(
228                     "org.apache.torque.file",
229                     variable.getValue());
230         }
231         {
232             Variable variable
233                     = storeContent.get(
234                         new QualifiedName("org.apache.torque.global"));
235             assertEquals(
236                     "org.apache.torque.global",
237                     variable.getValue());
238         }
239     }
240 
241     public void testRemove()
242     {
243         VariableStore store = new VariableStore();
244         store.startOutlet();
245         QualifiedName qualifiedName
246             = new QualifiedName("org.apache.torque.name");
247 
248         // fill store
249         store.set(new Variable(
250                 qualifiedName,
251                 "org.apache.torque.GENERATOR",
252                 Variable.Scope.OUTLET));
253         store.set(new Variable(
254                 qualifiedName,
255                 "org.apache.torque.CHILDREN",
256                 Variable.Scope.CHILDREN));
257         store.set(new Variable(
258                 qualifiedName,
259                 "org.apache.torque.FILE",
260                 Variable.Scope.FILE));
261         store.set(new Variable(
262                 qualifiedName,
263                 "org.apache.torque.GLOBAL",
264                 Variable.Scope.GLOBAL));
265 
266         assertEquals(
267                 "org.apache.torque.GENERATOR",
268                 store.getInHierarchy(qualifiedName).getValue());
269 
270         store.remove(store.getInHierarchy(qualifiedName));
271 
272         assertEquals(
273                 "org.apache.torque.CHILDREN",
274                 store.getInHierarchy(qualifiedName).getValue());
275 
276         store.remove(store.getInHierarchy(qualifiedName));
277 
278         assertEquals(
279                 "org.apache.torque.FILE",
280                 store.getInHierarchy(qualifiedName).getValue());
281 
282         store.remove(store.getInHierarchy(qualifiedName));
283 
284         assertEquals(
285                 "org.apache.torque.GLOBAL",
286                 store.getInHierarchy(qualifiedName).getValue());
287 
288         store.remove(store.getInHierarchy(qualifiedName));
289 
290         assertNull(store.getInHierarchy(qualifiedName));
291 
292         // test whether we can remove hidden variables
293         Variable childrenVariable = new Variable(
294                 qualifiedName,
295                 "org.apache.torque.CHILDREN",
296                 Variable.Scope.CHILDREN);
297 
298         store.set(new Variable(
299                 qualifiedName,
300                 "org.apache.torque.GENERATOR",
301                 Variable.Scope.OUTLET));
302         store.set(childrenVariable);
303         store.set(new Variable(
304                 qualifiedName,
305                 "org.apache.torque.FILE",
306                 Variable.Scope.FILE));
307 
308         assertEquals(
309                 "org.apache.torque.GENERATOR",
310                 store.getInHierarchy(qualifiedName).getValue());
311 
312         store.remove(childrenVariable);
313 
314         assertEquals(
315                 "org.apache.torque.GENERATOR",
316                 store.getInHierarchy(qualifiedName).getValue());
317 
318         store.remove(store.getInHierarchy(qualifiedName));
319 
320         assertEquals(
321                 "org.apache.torque.FILE",
322                 store.getInHierarchy(qualifiedName).getValue());
323 
324     }
325 }