1   package org.codehaus.dataforge.engine.configuration;
2   
3   /*
4    * Copyright 2001-2004 Ben Walding
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.List;
20  
21  import junit.framework.TestCase;
22  
23  import org.codehaus.dataforge.engine.DataForge;
24  import org.codehaus.dataforge.engine.context.Property;
25  import org.codehaus.dataforge.engine.resolvers.ClassResolver;
26  import org.codehaus.dataforge.engine.transformers.AbstractTransformer;
27  import org.codehaus.dataforge.engine.transformers.Bind;
28  import org.codehaus.dataforge.engine.transformers.Link;
29  import org.codehaus.dataforge.engine.transformers.StaticTransformer;
30  import org.codehaus.dataforge.engine.transformers.Transformer;
31  
32  /***
33   * @author Ben Walding
34   *
35   */
36  public class XMLConfigurationTest extends TestCase
37  {
38  
39      
40      public void testSimpleConfig() throws Exception
41      {
42          DataForge df = XMLConfiguration.quickConfig(getClass(), "simpleconfig.df");
43          verifySimpleConfig(df, "simpleconfig.df");
44      }
45  
46      public void testInvalidClass() throws Exception
47      {
48          DataForgeConfig dfc = XMLConfiguration.quickParse(new ClassResolver(this.getClass()), "invalidclass.df");
49          //Should not throw an exception.
50          
51          try
52          {
53              new DataForge(dfc);
54              fail("should have thrown a ClassNotFoundException");
55          }
56          catch (RuntimeException e)
57          {
58              if (!(e.getCause() instanceof ClassNotFoundException))
59              {
60                  throw e;
61              }
62              else
63              {
64                  //Success
65              }
66          }
67      }
68  
69      /***
70       * @param df
71       * @param source the expected source on the DataForgeConfig
72       */
73      public static void verifySimpleConfig(DataForge df, String source)
74      {
75          DataForgeConfig dfc = df.getDataForgeConfig();
76  
77          List transformers = dfc.getTransformers();
78          assertEquals("dfc.getTitle()", "xmltesttitle", dfc.getTitle());
79          assertNotNull("dfc.getResolver()", dfc.getResolver());
80          
81          assertEquals("dfc.getSource()", source, dfc.getSource());
82  
83          assertEquals("transformers.size()", 2, transformers.size());
84          assertEquals("dfc.getProperty(test)", "no real purpose except to exercise some more code paths", dfc
85                          .getProperty("test"));
86          TransformerConfig tform0 = (TransformerConfig) transformers.get(0);
87          assertEquals("tform0.getType()", StaticTransformer.class.getName(), tform0.getType());
88          assertEquals("tform0.getName()", "A", tform0.getName());
89          assertEquals("tform0.getDescription()", "This is transformer A", tform0.getDescription());
90          assertEquals("tform0.getScope()", "public", tform0.getScope());
91          assertEquals("tform0.getDebug()", "1", tform0.getDebug());
92          TransformerConfig gen1 = (TransformerConfig) transformers.get(1);
93          assertEquals("gen1.getType()", "org.codehaus.dataforge.engine.row.MockRowSource", gen1.getType());
94          assertEquals("gen1.getName()", "B", gen1.getName());
95          assertEquals("gen1.getScope()", "private", gen1.getScope());
96  
97          TransformerConfig gen_subforge_0 = dfc.resolveTransformer("subforge.SUB_A");
98          assertEquals("gen_subforge_0.getType()", StaticTransformer.class.getName(), gen_subforge_0.getType());
99          assertEquals("gen_subforge_0.getName()", "SUB_A", gen_subforge_0.getName());
100         assertEquals("gen_subforge_0.getScope()", "public", gen_subforge_0.getScope());
101 
102         {
103             List binds = dfc.getBinds();
104             assertEquals("binds.size()", 2, binds.size());
105             BindConfig bind0 = (BindConfig) dfc.getBinds().get(0);
106             assertEquals("bind0.getName()", "testbind", bind0.getName());
107             List links = bind0.getLinks();
108             assertEquals("links.getLinks().size()", 1, links.size());
109             LinkConfig link0 = (LinkConfig) links.get(0);
110             assertEquals("link0.getName()", "testlink", link0.getName());
111             assertEquals("link0.getSource()", "A", link0.getSource());
112             assertEquals("bind0.getTarget()", "B", bind0.getTarget());
113 
114         }
115 
116         {
117             BindConfig bind1 = (BindConfig) dfc.getBinds().get(1);
118             assertEquals("bind0.getName()", "testbind2", bind1.getName());
119             List links = bind1.getLinks();
120             assertEquals("links.getLinks().size()", 2, links.size());
121             LinkConfig link0 = (LinkConfig) links.get(0);
122             LinkConfig link1 = (LinkConfig) links.get(1);
123             assertEquals("link0.getName()", "testlink2", link0.getName());
124             assertEquals("link0.getSource()", "A", link0.getSource());
125             assertEquals("link1.getName()", "testlink3", link1.getName());
126             assertEquals("link1.getSource()", "B", link1.getSource());
127             assertEquals("bind0.getTarget()", "B", bind1.getTarget());
128         }
129         //      Now check that we can find a named link
130 
131         SubForgeConfig subforge = dfc.resolveSubForgeConfig("subforge");
132         assertEquals("subforge.getNamespace()", "subforge", subforge.getNamespace());
133         DataForgeConfig subforge_dfc = subforge.getConfig();
134         assertEquals("subforge_dfc.getNamespace()", "subforge", subforge_dfc.getNamespace());
135 
136         TransformerConfig A_cfg = dfc.resolveTransformer("A");
137         assertEquals("A_cfg.getNamespace()", null, A_cfg.getNamespace());
138         assertEquals("A_cfg.getFullyQualifiedName()", "A", A_cfg.getFullyQualifiedName());
139 
140         Transformer a = df.resolveTransformer("A");
141         Transformer b = df.resolveTransformer("B");
142         assertEquals("subForge_SUB_A.getNameSpace()", null, a.getNamespace());
143         assertEquals("A.getFullyQualifiedName()", "A", a.getFullyQualifiedName());
144         assertEquals("A.getDebug()", "1", a.getDebug());
145 
146         //Now check that transformers from subforges are getting their namespaces set, 
147         // and calculating fully qualified names correctly.
148         TransformerConfig subForge_SUB_A_cfg = dfc.resolveTransformer("subforge.SUB_A");
149         assertEquals("subForge_SUB_A_cfg.getNamespace()", "subforge", subForge_SUB_A_cfg.getNamespace());
150         assertEquals("subForge_SUB_A_cfg.getFullyQualifiedName()", "subforge.SUB_A", subForge_SUB_A_cfg
151                         .getFullyQualifiedName());
152 
153         Transformer subForge_SUB_A = df.resolveTransformer("subforge.SUB_A");
154         assertEquals("subForge_SUB_A.getNameSpace()", "subforge", subForge_SUB_A.getNamespace());
155         assertEquals("subForge_SUB_A.getFullyQualifiedName()", "subforge.SUB_A", subForge_SUB_A.getFullyQualifiedName());
156 
157         Bind binder0 = (Bind) b.getBinds().get(0);
158         assertNotNull("binder0", binder0);
159         Link l = binder0.resolveLink("testlink");
160         assertEquals("l.getName()", "testlink", l.getName());
161 
162         Bind binder1 = ((AbstractTransformer) b).getBind(1);
163         assertNotNull("binder1", binder1);
164         Link l2 = binder1.resolveLink("testlink2");
165         assertEquals("l.getName()", "testlink2", l2.getName());
166 
167         //Check that it fails when link can't be found
168         try
169         {
170             binder0.resolveLink("testlinknotreal");
171             fail("resolving link testlinknotreal should have failed");
172         }
173         catch (IllegalArgumentException iaex)
174         {
175             assertEquals(IllegalArgumentException.class, iaex.getClass());
176         }
177 
178         assertEquals("annotations.size()", 1, dfc.getAnnotations().size());
179         AnnotationConfig ac0 = (AnnotationConfig) dfc.getAnnotations().get(0);
180         assertEquals("annotation.test", ac0.getName());
181         assertEquals("THIS IS THE ANNOTATION FOR TRANSFORMER.A", ac0.getText());
182         List targets = ac0.getTargets();
183         assertEquals(1, targets.size());
184         String target0_ac0 = (String) targets.get(0);
185         assertEquals("transformer.A", target0_ac0);
186     }
187 
188     public void testForgeResolution1() throws Exception
189     {
190         DataForge df = XMLConfiguration.quickConfig(getClass(), "simpleconfig.df");
191         DataForgeConfig dfc = df.getDataForgeConfig();
192 
193         try
194         {
195             dfc.resolveSubForgeConfig("doesnotexist");
196             fail("should have thrown an IllegalArgumentException");
197         }
198         catch (IllegalArgumentException e)
199         {
200             //Success!
201         }
202     }
203 
204     public void testForgeResolution2() throws Exception
205     {
206         DataForge df = XMLConfiguration.quickConfig(getClass(), "simpleconfig.df");
207         DataForgeConfig dfc = df.getDataForgeConfig();
208 
209         SubForgeConfig sdfc = dfc.resolveSubForgeConfig("subforge");
210         assertEquals("subforge", sdfc.getNamespace());
211     }
212 
213     public void testForgeResolution3() throws Exception
214     {
215         DataForge df = XMLConfiguration.quickConfig(getClass(), "simpleconfig.df");
216         DataForgeConfig dfc = df.getDataForgeConfig();
217 
218         SubForgeConfig sdfc = dfc.resolveSubForgeConfig("subforge.subforge2");
219         assertEquals("subforge2", sdfc.getNamespace());
220         assertEquals("subconfig2.df", sdfc.getSource());
221     }
222 
223     public void testTransformerResolution() throws Exception
224     {
225         DataForge df = XMLConfiguration.quickConfig(getClass(), "simpleconfig.df");
226         DataForgeConfig dfc = df.getDataForgeConfig();
227 
228         TransformerConfig tcSUB_SUB2_SUB2_A = dfc.resolveTransformer("subforge.subforge2.sub2_a");
229 
230         assertNotNull("tcSUB_SUB2_SUB2_A", tcSUB_SUB2_SUB2_A);
231         assertEquals("tcSUB_SUB2_SUB2_A", "SELECT * FROM ONESMALLTABLE",
232                         ((Property) tcSUB_SUB2_SUB2_A.getProperty("sql")).getValueAsString().trim());
233 
234     }
235 
236     public void testTransformerResolution2() throws Exception
237     {
238         DataForge df = XMLConfiguration.quickConfig(getClass(), "simpleconfig.df");
239         DataForgeConfig dfc = df.getDataForgeConfig();
240 
241         try
242         {
243             dfc.resolveTransformer("subforge.subforge2.sub2_aasd");
244             fail("Should have throw exception trying to resolve subforge.subforge2.sub2_aasd");
245         }
246         catch (Exception e)
247         {
248             //Success
249         }
250     }
251 
252     public void testValidation1() throws Exception
253     {
254         try
255         {
256             XMLConfiguration.quickConfig(getClass(), "XMLConfigurationValidate1.df");
257             fail("An exception should have been thrown due to duplicate transformer names");
258         }
259         catch (Exception e)
260         {
261             assertEquals("e.class", IllegalStateException.class, e.getClass());
262         }
263 
264     }
265 
266     /***
267      * An exception should not be thrown due to a subforge trying to import
268      * another subforge with the same namespace as itself
269      * @throws Exception
270      */
271     public void testValidation2() throws Exception
272     {
273         XMLConfiguration.quickConfig(getClass(), "XMLConfigurationValidate2a.df");
274     }
275 
276     public void testValidation3() throws Exception
277     {
278         try
279         {
280             XMLConfiguration.quickConfig(getClass(), "XMLConfigurationValidate3a.df");
281             fail("Namespaces for subforges must not be empty");
282         }
283         catch (Exception e)
284         {
285             assertEquals("e.getClass()", IllegalStateException.class, e.getClass());
286         }
287     }
288 
289     public void testInvalidResource() throws Exception
290     {
291         try
292         {
293             XMLConfiguration.quickConfig(getClass(), "shrubberynubbery.flubbery");
294             fail("Should have thrown an exception");
295         }
296         catch (IllegalArgumentException e)
297         {
298             //Do nothing.
299         }
300     }
301 
302 }