View Javadoc

1   package org.apache.torque.generator.source;
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.assertFalse;
24  import static org.junit.Assert.assertSame;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import java.io.File;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import org.apache.torque.generator.configuration.paths.Maven2DirectoryProjectPaths;
33  import org.apache.torque.generator.configuration.paths.ProjectPaths;
34  import org.apache.torque.generator.control.ControllerState;
35  import org.apache.torque.generator.source.stream.FileSource;
36  import org.apache.torque.generator.source.stream.PropertiesSourceFormat;
37  import org.junit.Before;
38  import org.junit.Test;
39  
40  public class RichSourceElementImplTest
41  {
42      private SourceElement rootSourceElement;
43  
44      @Before
45      public void setUp()
46      {
47          rootSourceElement = new SourceElement("parent");
48          SourceElement child1 = new SourceElement("child1");
49          List<SourceElement> children = rootSourceElement.getChildren();
50          children.add(child1);
51          SourceElement child2 = new SourceElement("child2");
52          children.add(child2);
53          SourceElement child3 = new SourceElement("child3");
54          children.add(child3);
55      }
56  
57      @Test
58      public void testGetParents()
59      {
60          SourceElement child1 = rootSourceElement.getChildren().get(0);
61          List<SourceElement> parentsOfChild1 = child1.getParents();
62          assertEquals(1, parentsOfChild1.size());
63          assertSame(rootSourceElement, parentsOfChild1.get(0));
64      }
65  
66      @Test
67      public void testAddChild()
68      {
69          SourceElement child1 = rootSourceElement.getChildren().get(0);
70          SourceElement child2 = rootSourceElement.getChildren().get(1);
71          SourceElement child3 = rootSourceElement.getChildren().get(2);
72          SourceElement child4 = new SourceElement("child4");
73          rootSourceElement.getChildren().add(child4);
74  
75          assertEquals("parent should have 4 children",
76                  4,
77                  rootSourceElement.getChildren().size());
78          assertSame(child1, rootSourceElement.getChildren().get(0));
79          assertSame(child2, rootSourceElement.getChildren().get(1));
80          assertSame(child3, rootSourceElement.getChildren().get(2));
81          assertSame(child4, rootSourceElement.getChildren().get(3));
82          assertSame(rootSourceElement, child4.getParent());
83          assertEquals(1, child4.getParents().size());
84      }
85  
86      @Test
87      public void testAddChildAtIndex()
88      {
89          SourceElement child1 = rootSourceElement.getChildren().get(0);
90          SourceElement child2 = rootSourceElement.getChildren().get(1);
91          SourceElement child3 = rootSourceElement.getChildren().get(2);
92          SourceElement child4 = new SourceElement("child4");
93          List<SourceElement> children = rootSourceElement.getChildren();
94          children.add(1, child4);
95          assertSame(child1, rootSourceElement.getChildren().get(0));
96          assertSame(child4, rootSourceElement.getChildren().get(1));
97          assertSame(child2, rootSourceElement.getChildren().get(2));
98          assertSame(child3, rootSourceElement.getChildren().get(3));
99          assertSame(rootSourceElement, child4.getParent());
100         assertEquals(1, child4.getParents().size());
101     }
102 
103     @Test
104     public void testRemoveChild()
105     {
106         List<SourceElement> children = rootSourceElement.getChildren();
107         SourceElement child1 = children.get(0);
108         SourceElement child2 = children.get(1);
109         SourceElement child3 = children.get(2);
110         boolean result = children.remove(child2);
111         assertTrue(result);
112         assertEquals("parent should have 2 children",
113                 2,
114                 rootSourceElement.getChildren().size());
115         assertSame(child1, rootSourceElement.getChildren().get(0));
116         assertSame(child3, rootSourceElement.getChildren().get(1));
117         assertEquals(0, child2.getParents().size());
118         assertEquals(null, child2.getParent());
119     }
120 
121     @Test
122     public void testRemoveChildViaIterator()
123     {
124         Iterator<SourceElement> childIt
125                 = rootSourceElement.getChildren().iterator();
126         List<SourceElement> children = rootSourceElement.getChildren();
127         SourceElement child1 = children.get(0);
128         SourceElement child2 = children.get(1);
129         SourceElement child3 = children.get(2);
130         childIt.next(); //child1
131         childIt.next(); //child2
132         childIt.remove();
133 
134         assertEquals(2, children.size());
135         assertEquals(child1, children.get(0));
136         assertEquals(child3, children.get(1));
137         assertEquals(0, child2.getParents().size());
138         assertEquals(null, child2.getParent());
139     }
140 
141     @Test
142     public void testRemoveNonExistingChild()
143     {
144         List<SourceElement> children = rootSourceElement.getChildren();
145         SourceElement child1 = children.get(0);
146         SourceElement child2 = children.get(1);
147         SourceElement child3 = children.get(2);
148         SourceElement child4 = new SourceElement("child4");
149 
150         boolean result = children.remove(child4);
151         assertFalse(result);
152         assertEquals("parent should have 3 children",
153                 3,
154                 rootSourceElement.getChildren().size());
155         assertSame(child1, rootSourceElement.getChildren().get(0));
156         assertSame(child2, rootSourceElement.getChildren().get(1));
157         assertSame(child3, rootSourceElement.getChildren().get(2));
158     }
159 
160     @Test
161     public void testGetChildIndex()
162     {
163         SourceElement child2 = rootSourceElement.getChildren().get(1);
164 
165         assertEquals("child index should be 1",
166                 1,
167                 rootSourceElement.getChildren().indexOf(child2));
168     }
169 
170     @Test
171     public void testGetNonExistingChildIndex()
172     {
173         SourceElement child4 = new SourceElement("child4");
174         assertEquals("child index should be -1",
175                 -1,
176                 rootSourceElement.getChildren().indexOf(child4));
177     }
178 
179     @Test
180     public void testAddParent()
181     {
182         SourceElement child = new SourceElement("child");
183         List<SourceElement> parentsOfChild = child.getParents();
184         SourceElement parent1 = new SourceElement("parent1");
185         parentsOfChild.add(parent1);
186         SourceElement parent2 = new SourceElement("parent2");
187         parentsOfChild.add(parent2);
188 
189         assertEquals("child should have 2 parents",
190                 2,
191                 parentsOfChild.size());
192         assertSame("child has wrong primary parent",
193                 parent1,
194                 child.getParent());
195         assertSame("parent1 has wrong child",
196                 child,
197                 parent1.getChildren().get(0));
198         assertSame("child has wrong second parent",
199                 parent2,
200                 child.getParents().get(1));
201         assertSame("parent2 has wrong child",
202                 child,
203                 parent2.getChildren().get(0));
204     }
205 
206     @Test
207     public void testTreeEquals()
208     {
209         SourceElement original = new SourceElement("root");
210         SourceElement toCompare
211                 = new SourceElement("wrongName");
212         if (original.graphEquals(toCompare))
213         {
214             fail("SourceElements with different names must not be equal");
215         }
216 
217         toCompare = new SourceElement("root");
218         if (!original.graphEquals(toCompare))
219         {
220             fail("empty SourceElements with equal names must be equal");
221         }
222 
223         original.setAttribute("attributeName", "attributeValue");
224         if (original.graphEquals(toCompare))
225         {
226             fail("SourceElements with different attribute names"
227                     + " must not be equal");
228         }
229 
230         toCompare.setAttribute("attributeName", "anotherAttributeValue");
231         if (original.graphEquals(toCompare))
232         {
233             fail("SourceElements with different attribute content"
234                     + " must not be equal(1)");
235         }
236 
237         toCompare.setAttribute("attributeName", null);
238         if (original.graphEquals(toCompare))
239         {
240             fail("SourceElements with different attribute content"
241                     + " must not be equal(2)");
242         }
243 
244         toCompare.setAttribute("attributeName", "attributeValue");
245         if (!original.graphEquals(toCompare))
246         {
247             fail("SourceElements with equal attribute content"
248                     + " must be equal(1)");
249         }
250 
251         original.setAttribute("attributeName", null);
252         if (original.graphEquals(toCompare))
253         {
254             fail("SourceElements with different attribute content"
255                     + " must not be equal(3)");
256         }
257 
258         toCompare.setAttribute("attributeName", null);
259         if (!original.graphEquals(toCompare))
260         {
261             fail("SourceElements with equal attribute content"
262                     + " must be equal(2)");
263         }
264 
265         original.getChildren().add(new SourceElement("child"));
266         if (original.graphEquals(toCompare))
267         {
268             fail("SourceElements with different children"
269                     + " must not be equal(1)");
270         }
271 
272         toCompare.getChildren().add(new SourceElement("wrongChild"));
273         if (original.graphEquals(toCompare))
274         {
275             fail("SourceElements with different children"
276                     + " must not be equal(2)");
277         }
278 
279         toCompare = new SourceElement("root");
280         toCompare.setAttribute("attributeName", null);
281         toCompare.getChildren().add(new SourceElement("child"));
282         if (!original.graphEquals(toCompare))
283         {
284             fail("SourceElements with equal children"
285                     + " must be equal(1)");
286         }
287 
288         original.getChildren().add(original);
289         if (original.graphEquals(toCompare))
290         {
291             fail("SourceElements with different children"
292                     + " must not be equal(3)");
293         }
294 
295         toCompare.getChildren().add(toCompare);
296         if (!original.graphEquals(toCompare))
297         {
298             fail("SourceElements with equal children"
299                     + " must be equal(2)");
300         }
301     }
302 
303     @Test
304     public void testSetAttribute()
305     {
306         SourceElement sourceElement = new SourceElement("root");
307         assertEquals(sourceElement.getAttributeNames().size(), 0);
308         assertEquals(null, sourceElement.getAttribute("attributeName"));
309 
310         sourceElement.setAttribute("attributeName", "123");
311         assertEquals(1, sourceElement.getAttributeNames().size());
312         assertEquals("123", sourceElement.getAttribute("attributeName"));
313     }
314 
315     @Test
316     public void testSetAttributeToNull()
317     {
318         SourceElement sourceElement = new SourceElement("root");
319         sourceElement.setAttribute("attributeName", "123");
320         assertEquals(1, sourceElement.getAttributeNames().size());
321 
322         sourceElement.setAttribute("attributeName", null);
323         assertEquals(0, sourceElement.getAttributeNames().size());
324     }
325 
326     @Test
327     public void testToString() throws Exception
328     {
329         SourceElement sourceElement = new SourceElement("root");
330         sourceElement.setAttribute("attributeName", "123");
331         sourceElement.getChildren().add(new SourceElement("child"));
332         sourceElement.getChildren().add(sourceElement);
333         String expected
334                 = "(name=root,attributes=(attributeName=123),"
335                     + "children=((name=child,attributes=(),children=()),"
336                     + "<<loop detected>>))";
337         assertEquals(expected, sourceElement.toString());
338     }
339 
340     @Test
341     public void testCopySourceElement() throws Exception
342     {
343         ProjectPaths projectPaths
344                 = new Maven2DirectoryProjectPaths(
345                     new File("src/test/propertyToJava"));
346         File propertiesFile
347                 = new File(
348                     projectPaths.getDefaultSourcePath(),
349                     "propertiesData.properties");
350         FileSource fileSource
351                 = new FileSource(
352                         new PropertiesSourceFormat(),
353                         propertiesFile,
354                         new ControllerState());
355         SourceElement rootElement
356                 = (SourceElement) fileSource.getRootElement();
357 
358         SourceElement copiedRootElement = rootElement.copy();
359         if (!rootElement.graphEquals(copiedRootElement))
360         {
361             fail("copied tree does not equal original tree");
362         }
363     }
364 }