1 package org.apache.torque.generator.configuration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.assertArrayEquals;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 import java.io.File;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Set;
37
38 import org.apache.commons.io.FileUtils;
39 import org.apache.commons.lang.StringUtils;
40 import org.apache.torque.generator.BaseTest;
41 import org.apache.torque.generator.configuration.controller.Loglevel;
42 import org.apache.torque.generator.configuration.controller.OutletReference;
43 import org.apache.torque.generator.configuration.controller.Output;
44 import org.apache.torque.generator.configuration.mergepoint.MergepointMapping;
45 import org.apache.torque.generator.configuration.option.MapOptionsConfiguration;
46 import org.apache.torque.generator.configuration.option.OptionsConfiguration;
47 import org.apache.torque.generator.configuration.outlet.OutletConfiguration;
48 import org.apache.torque.generator.configuration.paths.CustomProjectPaths;
49 import org.apache.torque.generator.configuration.paths.DefaultTorqueGeneratorPaths;
50 import org.apache.torque.generator.configuration.paths.Maven2DirectoryProjectPaths;
51 import org.apache.torque.generator.configuration.paths.ProjectPaths;
52 import org.apache.torque.generator.configuration.source.EntityReferences;
53 import org.apache.torque.generator.control.ControllerState;
54 import org.apache.torque.generator.control.action.ApplyAction;
55 import org.apache.torque.generator.control.action.MergepointAction;
56 import org.apache.torque.generator.control.action.TraverseAllAction;
57 import org.apache.torque.generator.file.Fileset;
58 import org.apache.torque.generator.java.JavaOutlet;
59 import org.apache.torque.generator.option.Option;
60 import org.apache.torque.generator.option.OptionImpl;
61 import org.apache.torque.generator.option.Options;
62 import org.apache.torque.generator.outlet.Outlet;
63 import org.apache.torque.generator.qname.QualifiedName;
64 import org.apache.torque.generator.source.SourceProcessConfiguration;
65 import org.apache.torque.generator.source.SourceTransformerDefinition;
66 import org.apache.torque.generator.source.jdbc.JdbcMetadataSourceProvider;
67 import org.apache.torque.generator.source.stream.FileSourceProvider;
68 import org.apache.torque.generator.source.stream.PropertiesSourceFormat;
69 import org.apache.torque.generator.source.stream.XmlSourceFormat;
70 import org.apache.torque.generator.template.velocity.VelocityOutlet;
71 import org.junit.Before;
72 import org.junit.Test;
73
74
75
76
77
78 public class ReadConfigurationTest extends BaseTest
79 {
80 private ControllerState controllerState;
81
82 @Before
83 public void setUp()
84 {
85 controllerState = new ControllerState();
86 UnitConfiguration unitConfiguration = new UnitConfiguration();
87 controllerState.setUnitConfiguration(unitConfiguration);
88 Options options = new Options();
89 unitConfiguration.setOptions(options);
90 }
91
92 @Test
93 public void testReadConfiguration() throws Exception
94 {
95 ProjectPaths projectPaths = new Maven2DirectoryProjectPaths(
96 new File("src/test/configuration"));
97 UnitDescriptor unitDescriptor = new UnitDescriptor(
98 UnitDescriptor.Packaging.DIRECTORY,
99 projectPaths,
100 new DefaultTorqueGeneratorPaths());
101 UnitConfigurationReader configurationReader
102 = new UnitConfigurationReader();
103 ConfigurationHandlers configurationHandlers
104 = new ConfigurationHandlers();
105 UnitConfiguration unitConfiguration
106 = configurationReader.read(
107 unitDescriptor,
108 configurationHandlers);
109 {
110 File defaultTargetDirectory
111 = unitConfiguration.getOutputDirectory(null);
112 File expected = new File(
113 "src/test/configuration/target/generated-sources");
114 assertEquals(expected, defaultTargetDirectory);
115 }
116 {
117 File modifiableTargetDirectory
118 = unitConfiguration.getOutputDirectory("modifiable");
119 File expected = new File(
120 "src/test/configuration/src/main/generated-java");
121 assertEquals(expected, modifiableTargetDirectory);
122 }
123 try
124 {
125 unitConfiguration.getOutputDirectory("notExistingKey");
126 fail("Exception expected");
127 }
128 catch (IllegalStateException e)
129 {
130
131 }
132
133 assertEquals(Loglevel.DEBUG, unitConfiguration.getLoglevel());
134
135 {
136 Options expectedOptions = new Options();
137 Set<Option> expectedOptionSet = new HashSet<Option>();
138 expectedOptionSet.add(new OptionImpl(
139 new QualifiedName("org.apache.torque.generator.optionWithNamespace"),
140 "optionWithNamespaceValue"));
141 expectedOptionSet.add(new OptionImpl(
142 new QualifiedName("org.apache.optionWithNamespace"),
143 "org.apache.optionWithNamespaceValue"));
144 expectedOptionSet.add(new OptionImpl(
145 new QualifiedName("optionWithoutNamespace", ""),
146 "optionWithoutNamespaceValue"));
147 expectedOptionSet.add(new OptionImpl(
148 new QualifiedName("optionPrecedenceLastFile"),
149 "value from xml"));
150 expectedOptionSet.add(new OptionImpl(
151 new QualifiedName("jdbcUrl"),
152 "jdbc.url.option.value"));
153 expectedOptionSet.add(new OptionImpl(
154 new QualifiedName("jdbcDriver"),
155 "jdbc.driver.option.value"));
156 expectedOptions.addGlobalOptions(expectedOptionSet);
157 assertOptionsEquals(
158 expectedOptions,
159 unitConfiguration.getOptions());
160 }
161
162 {
163 EntityReferences entityReferences
164 = unitConfiguration.getEntityReferences();
165 Map<String, byte[]> actual = entityReferences.getEntityReferences();
166 assertEquals(2, actual.size());
167 assertArrayEquals(
168 FileUtils.readFileToByteArray(
169 new File("src/test/configuration/src/main/torque-gen/resources/override.xsd")),
170 actual.get("http://db.apache.org/torque/some.xsd"));
171 assertArrayEquals(
172 FileUtils.readFileToByteArray(
173 new File("src/test/configuration/src/main/torque-gen/resources/new.xsd")),
174 actual.get("http://db.apache.org/torque/new.xsd"));
175 }
176
177 {
178 List<Output> outputFiles = unitConfiguration.getOutputList();
179 assertEquals(3, outputFiles.size());
180
181 {
182 Output output = outputFiles.get(0);
183 assertEquals(
184 new QualifiedName("org.apache.torque.generator.firstOutput"),
185 output.getName());
186 assertEquals(
187 "skip",
188 output.getExistingTargetStrategy());
189 assertEquals(
190 null,
191 output.getOutputDirKey());
192 assertNull(output.getFilename());
193
194 {
195 OutletReference outletConfiguration
196 = output.getContentOutlet();
197 assertEquals(
198 new QualifiedName(
199 "org.apache.torque.generator.test.readConfiguration.javaOutlet"),
200 outletConfiguration.getName());
201 }
202
203 {
204 JavaOutlet filenameOutlet
205 = (JavaOutlet) output.getFilenameOutlet();
206 assertEquals("Foo", filenameOutlet.getFoo());
207 assertEquals("Bar", filenameOutlet.getBar());
208 assertEquals(
209 new QualifiedName(
210 "org.apache.torque.generator.configuration.filenameOutlet"),
211 filenameOutlet.getName());
212 }
213
214 {
215 Fileset sourceFileset = new Fileset(
216 projectPaths.getDefaultSourcePath(),
217 new HashSet<String>(),
218 new HashSet<String>());
219 FileSourceProvider sourceProvider = new FileSourceProvider(
220 new XmlSourceFormat(),
221 sourceFileset,
222 null);
223 sourceProvider.init(
224 configurationHandlers,
225 controllerState);
226 output.getSourceProvider().init(
227 configurationHandlers,
228 controllerState);
229 assertFileSourceProviderEquals(
230 sourceProvider,
231 (FileSourceProvider) output.getSourceProvider());
232 }
233 assertSourceProcessConfigurationEquals(
234 new SourceProcessConfiguration(),
235 output.getSourceProcessConfiguration());
236 }
237
238 {
239 Output output = outputFiles.get(1);
240 assertEquals(
241 new QualifiedName("secondOutput"),
242 output.getName());
243 assertEquals(
244 "replace",
245 output.getExistingTargetStrategy());
246 assertEquals(
247 "secondOutputDirKey",
248 output.getOutputDirKey());
249 assertNull(output.getFilename());
250
251 {
252 OutletReference outletConfiguration
253 = output.getContentOutlet();
254 assertEquals(
255 new QualifiedName(
256 "org.apache.torque.generator.test.readConfiguration.anotherOutlet"),
257 outletConfiguration.getName());
258 }
259
260 {
261 VelocityOutlet filenameOutlet
262 = (VelocityOutlet) output.getFilenameOutlet();
263 String templateContent
264 = filenameOutlet.getContent(controllerState);
265
266
267 String templateContentLicenseRemoved
268 = StringUtils.substringAfterLast(templateContent, "\r\n");
269 assertEquals(
270 "test template output",
271 templateContentLicenseRemoved);
272 assertEquals(
273 new QualifiedName(
274 "org.apache.torque.generator.configuration.filenameOutlet"),
275 filenameOutlet.getName());
276 assertTrue(filenameOutlet.isOptionsInContext());
277 assertTrue(filenameOutlet.isSourceAttributesInContext());
278 assertTrue(filenameOutlet.isVariablesInContext());
279 }
280
281 {
282 Fileset sourceFileset = new Fileset(
283 projectPaths.getDefaultSourcePath(),
284 createSetFrom("second.source.path.properties"),
285 createSetFrom("second.excluded.properties"));
286 FileSourceProvider sourceProvider = new FileSourceProvider(
287 new PropertiesSourceFormat(),
288 sourceFileset,
289 true);
290 sourceProvider.init(
291 configurationHandlers,
292 controllerState);
293 output.getSourceProvider().init(
294 configurationHandlers,
295 controllerState);
296 assertFileSourceProviderEquals(
297 sourceProvider,
298 (FileSourceProvider) output.getSourceProvider());
299 }
300 {
301 SourceProcessConfiguration sourceProcessConfiguration
302 = new SourceProcessConfiguration();
303 sourceProcessConfiguration.setStartElementsPath(
304 "properties/entry");
305 List<SourceTransformerDefinition> transformerDefinitions
306 = new ArrayList<SourceTransformerDefinition>();
307
308 transformerDefinitions.add(
309 new SourceTransformerDefinition(
310 new ConfigurationTestTransformer(),
311 null));
312 transformerDefinitions.add(
313 new SourceTransformerDefinition(
314 new OtherConfigurationTestTransformer(),
315 "database"));
316 sourceProcessConfiguration.setSourceTransformerDefinitions(
317 transformerDefinitions);
318 sourceProcessConfiguration.setSkipDecider(
319 "org.apache.torque.generator.configuration.ConfigurationTestSkipDecider");
320 assertSourceProcessConfigurationEquals(
321 sourceProcessConfiguration,
322 output.getSourceProcessConfiguration());
323 }
324 }
325 {
326 Output output = outputFiles.get(2);
327 assertEquals(
328 new QualifiedName("thirdOutput"),
329 output.getName());
330 assertEquals(
331 "append",
332 output.getExistingTargetStrategy());
333 assertEquals(
334 "thirdOutputDirKey",
335 output.getOutputDirKey());
336 assertEquals("outputFileName", output.getFilename());
337 assertNull(output.getFilenameOutlet());
338
339 {
340 OutletReference outletConfiguration
341 = output.getContentOutlet();
342 assertEquals(
343 new QualifiedName(
344 "org.apache.torque.generator.test.readConfiguration.thirdOutlet"),
345 outletConfiguration.getName());
346 }
347
348 {
349 JdbcMetadataSourceProvider sourceProvider
350 = new JdbcMetadataSourceProvider(
351 "jdbcUrl",
352 "jdbcDriver",
353 "jdbcUsername",
354 "jdbcPassword",
355 "jdbcSchema");
356 sourceProvider.init(
357 configurationHandlers,
358 controllerState);
359 output.getSourceProvider().init(
360 configurationHandlers,
361 controllerState);
362 assertJdbcMetadataSourceProviderEquals(
363 sourceProvider,
364 (JdbcMetadataSourceProvider) output.getSourceProvider());
365 }
366 assertSourceProcessConfigurationEquals(
367 new SourceProcessConfiguration(),
368 output.getSourceProcessConfiguration());
369 }
370 }
371
372 {
373 OutletConfiguration outletConfiguration
374 = unitConfiguration.getOutletConfiguration();
375 Map<QualifiedName, Outlet> outletMap
376 = outletConfiguration.getOutlets();
377 assertEquals(2, outletMap.size());
378 {
379 JavaOutlet outlet
380 = (JavaOutlet) outletMap.get(new QualifiedName(
381 "org.apache.torque.generator.test.readConfiguration.javaOutlet"));
382 assertEquals("Foo2", outlet.getFoo());
383 assertEquals("Bar2", outlet.getBar());
384 Map<String, MergepointMapping> mergepointMappings
385 = outlet.getMergepointMappings();
386 assertEquals(3, mergepointMappings.size());
387 {
388 MergepointMapping mergepointMapping
389 = mergepointMappings.get("properties");
390 assertEquals(1, mergepointMapping.getActions().size());
391 MergepointAction action
392 = mergepointMapping.getActions().get(0);
393 assertEquals(
394 new TraverseAllAction(
395 "entry",
396 "org.apache.torque.generator.velocity.propertyCopy",
397 true),
398 action);
399 assertEquals("inputElement", outlet.getInputElementName());
400 }
401 {
402
403 MergepointMapping mergepointMapping
404 = mergepointMappings.get("mergepointName");
405 assertEquals(1, mergepointMapping.getActions().size());
406 MergepointAction action
407 = mergepointMapping.getActions().get(0);
408 assertEquals(
409 new ApplyAction(
410 null,
411 "someOutletAction",
412 false),
413 action);
414 assertEquals("inputElement", outlet.getInputElementName());
415 }
416 {
417
418 MergepointMapping mergepointMapping
419 = mergepointMappings.get("mergepointFromParent");
420 assertEquals(1, mergepointMapping.getActions().size());
421 MergepointAction action
422 = mergepointMapping.getActions().get(0);
423 assertEquals(
424 new ApplyAction(
425 null,
426 "newOutletAction",
427 true),
428 action);
429 assertEquals("inputElement", outlet.getInputElementName());
430 }
431 }
432
433 {
434 VelocityOutlet outlet
435 = (VelocityOutlet) outletMap.get(new QualifiedName(
436 "org.apache.torque.generator.test.readConfiguration.anotherOutlet"));
437 String templateContent
438 = outlet.getContent(controllerState);
439
440
441 String templateContentLicenseRemoved
442 = StringUtils.substringAfterLast(templateContent, "\r\n");
443 assertEquals(
444 "test template output",
445 templateContentLicenseRemoved);
446 assertEquals(0, outlet.getMergepointMappings().size());
447 assertNull(outlet.getInputElementName());
448 assertFalse(outlet.isOptionsInContext());
449 assertFalse(outlet.isSourceAttributesInContext());
450 assertFalse(outlet.isVariablesInContext());
451 }
452 }
453 }
454
455 @Test
456 public void testReadConfigFromClasspath() throws Exception
457 {
458 Map<String, File> outputDirMap = new HashMap<String, File>();
459 outputDirMap.put(null, new File("generated-sources"));
460 ProjectPaths projectPaths = new CustomProjectPaths(
461 null,
462 "org.apache.torque.generator.test.readfromclasspath",
463 new File("src"),
464 outputDirMap,
465 new File("work"));
466 UnitDescriptor unitDescriptor = new UnitDescriptor(
467 UnitDescriptor.Packaging.CLASSPATH,
468 projectPaths,
469 new DefaultTorqueGeneratorPaths());
470 ConfigurationHandlers configurationHandlers
471 = new ConfigurationHandlers();
472 UnitConfigurationReader configurationReader
473 = new UnitConfigurationReader();
474 UnitConfiguration unitConfiguration
475 = configurationReader.read(
476 unitDescriptor,
477 configurationHandlers);
478
479
480 {
481 Options options = unitConfiguration.getOptions();
482 Option option = options.getInHierarchy(
483 new QualifiedName("configuration"));
484 assertNotNull("option configuration should be set", option);
485 assertEquals("fromClasspath", option.getValue());
486 }
487
488
489 {
490 OutletConfiguration outletConfiguration
491 = unitConfiguration.getOutletConfiguration();
492 Map<QualifiedName, Outlet> outletMap
493 = outletConfiguration.getOutlets();
494 assertEquals(2, outletMap.size());
495 }
496 }
497
498 @Test
499 public void testOverrideOptions() throws Exception
500 {
501 ProjectPaths projectPaths = new Maven2DirectoryProjectPaths(
502 new File("src/test/configuration"));
503 Map<String, String> overrideOptions = new HashMap<String, String>();
504 overrideOptions.put("optionWithoutNamespace", "overriddenValue");
505 overrideOptions.put("newOption", "newValue");
506 OptionsConfiguration optionConfiguration
507 = new MapOptionsConfiguration(overrideOptions);
508
509 UnitDescriptor unitDescriptor = new UnitDescriptor(
510 UnitDescriptor.Packaging.DIRECTORY,
511 projectPaths,
512 new DefaultTorqueGeneratorPaths());
513 unitDescriptor.setOverrideOptions(optionConfiguration);
514 ConfigurationHandlers configurationHandlers
515 = new ConfigurationHandlers();
516 UnitConfigurationReader configurationReader
517 = new UnitConfigurationReader();
518 UnitConfiguration unitConfiguration
519 = configurationReader.read(
520 unitDescriptor,
521 configurationHandlers);
522 Options options = unitConfiguration.getOptions();
523 {
524 Option option = options.getInHierarchy(
525 new QualifiedName("optionWithoutNamespace"));
526 assertEquals("overriddenValue", option.getValue());
527 }
528 {
529 Option option = options.getInHierarchy(
530 new QualifiedName("newOption"));
531 assertEquals("newValue", option.getValue());
532 }
533 }
534
535
536 @Test
537 public void testInheritance() throws Exception
538 {
539 CustomProjectPaths projectPaths;
540 UnitConfiguration unitConfiguration;
541 ConfigurationHandlers configurationHandlers
542 = new ConfigurationHandlers();
543 {
544 CustomProjectPaths parentProjectPaths
545 = new CustomProjectPaths(
546 new Maven2DirectoryProjectPaths(
547 new File("src/test/configuration")));
548 parentProjectPaths.setConfigurationDir(
549 new File("src/test/configuration/src/main/torque-gen-parent"));
550 parentProjectPaths.setOutputDirectory(
551 null,
552 new File("src/test/configuration/target/parentCustom"));
553 parentProjectPaths.setOutputDirectory(
554 "modifiable",
555 new File("src/test/configuration/src/main/parentCustom"));
556 UnitDescriptor parentUnitDescriptor = new UnitDescriptor(
557 UnitDescriptor.Packaging.DIRECTORY,
558 parentProjectPaths,
559 new DefaultTorqueGeneratorPaths());
560
561 projectPaths = new CustomProjectPaths(
562 new Maven2DirectoryProjectPaths(
563 new File("src/test/configuration")));
564 projectPaths.setOutputDirectory(
565 null,
566 new File("src/test/configuration/target/custom"));
567 projectPaths.setOutputDirectory(
568 "modifiable",
569 new File("src/test/configuration/src/main/custom"));
570 UnitDescriptor unitDescriptor = new UnitDescriptor(
571 UnitDescriptor.Packaging.DIRECTORY,
572 projectPaths,
573 new DefaultTorqueGeneratorPaths());
574 unitDescriptor.setInheritsFrom(parentUnitDescriptor);
575 UnitConfigurationReader configurationReader
576 = new UnitConfigurationReader();
577 unitConfiguration
578 = configurationReader.read(
579 unitDescriptor,
580 configurationHandlers);
581 }
582
583 assertEquals(Loglevel.DEBUG, unitConfiguration.getLoglevel());
584
585 {
586 File newFileTargetDirectory
587 = unitConfiguration.getOutputDirectory(null);
588 File expected = new File("src/test/configuration/target/custom");
589 assertEquals(expected, newFileTargetDirectory);
590 }
591 {
592 File modifiableFileTargetDirectory
593 = unitConfiguration.getOutputDirectory("modifiable");
594 File expected = new File("src/test/configuration/src/main/custom");
595 assertEquals(expected, modifiableFileTargetDirectory);
596 }
597
598 {
599 Options expectedOptions = new Options();
600 Set<Option> expectedOptionSet = new HashSet<Option>();
601 expectedOptionSet.add(new OptionImpl(
602 new QualifiedName("org.apache.torque.generator.optionWithNamespace"),
603 "optionWithNamespaceValue"));
604 expectedOptionSet.add(new OptionImpl(
605 new QualifiedName("org.apache.optionWithNamespace"),
606 "org.apache.optionWithNamespaceValue"));
607 expectedOptionSet.add(new OptionImpl(
608 new QualifiedName("optionWithoutNamespace", ""),
609 "optionWithoutNamespaceValue"));
610 expectedOptionSet.add(new OptionImpl(
611 new QualifiedName("optionPrecedenceLastFile"),
612 "value from xml"));
613 expectedOptionSet.add(new OptionImpl(
614 new QualifiedName("jdbcUrl"),
615 "jdbc.url.option.value"));
616 expectedOptionSet.add(new OptionImpl(
617 new QualifiedName("jdbcDriver"),
618 "jdbc.driver.option.value"));
619 expectedOptionSet.add(new OptionImpl(
620 new QualifiedName("parentOptionWithoutNamespace", ""),
621 "parentOptionWithoutNamespaceParentValue"));
622 expectedOptionSet.add(new OptionImpl(
623 new QualifiedName("org.apache.torque.generator.parentOptionWithNamespace"),
624 "parentOptionWithNamespaceParentValue"));
625 expectedOptions.addGlobalOptions(expectedOptionSet);
626 assertOptionsEquals(
627 expectedOptions,
628 unitConfiguration.getOptions());
629 }
630
631 {
632 EntityReferences entityReferences
633 = unitConfiguration.getEntityReferences();
634 Map<String, byte[]> actual = entityReferences.getEntityReferences();
635 assertEquals(3, actual.size());
636 assertArrayEquals(
637 FileUtils.readFileToByteArray(
638 new File("src/test/configuration/src/main/torque-gen-parent/resources/parent.xsd")),
639 actual.get("http://db.apache.org/torque/parent.xsd"));
640 assertArrayEquals(
641 FileUtils.readFileToByteArray(
642 new File("src/test/configuration/src/main/torque-gen/resources/override.xsd")),
643 actual.get("http://db.apache.org/torque/some.xsd"));
644 assertArrayEquals(
645 FileUtils.readFileToByteArray(
646 new File("src/test/configuration/src/main/torque-gen/resources/new.xsd")),
647 actual.get("http://db.apache.org/torque/new.xsd"));
648 }
649
650 {
651 List<Output> outputList = unitConfiguration.getOutputList();
652 assertEquals(4, outputList.size());
653
654 {
655 Output output = outputList.get(0);
656 assertEquals(
657 new QualifiedName("parentOutput"),
658 output.getName());
659 assertEquals(
660 "replace",
661 output.getExistingTargetStrategy());
662 assertEquals(
663 "parentOutputDirKeyFromParent",
664 output.getOutputDirKey());
665 assertNull(output.getFilename());
666 {
667 OutletReference outletConfiguration
668 = output.getContentOutlet();
669 assertEquals(
670 new QualifiedName(
671 "org.apache.torque.generator.test.readConfiguration.testParentOutlet"),
672 outletConfiguration.getName());
673 }
674 {
675 JavaOutlet filenameOutlet
676 = (JavaOutlet) output.getFilenameOutlet();
677 assertEquals("ParentFoo", filenameOutlet.getFoo());
678 assertEquals("ParentBar", filenameOutlet.getBar());
679 assertEquals(
680 new QualifiedName(
681 "org.apache.torque.generator.configuration.filenameOutlet"),
682 filenameOutlet.getName());
683 }
684
685 {
686 Fileset sourceFileset = new Fileset(
687 projectPaths.getDefaultSourcePath(),
688 createSetFrom("parentSource"),
689 new HashSet<String>());
690 FileSourceProvider sourceProvider = new FileSourceProvider(
691 new XmlSourceFormat(),
692 sourceFileset,
693 false);
694 output.getSourceProvider().init(
695 configurationHandlers,
696 controllerState);
697 sourceProvider.init(
698 configurationHandlers,
699 controllerState);
700 assertFileSourceProviderEquals(
701 sourceProvider,
702 (FileSourceProvider) output.getSourceProvider());
703 }
704 {
705 SourceProcessConfiguration sourceProcessConfiguration
706 = new SourceProcessConfiguration();
707 sourceProcessConfiguration.setStartElementsPath("parentSourceElement");
708 List<SourceTransformerDefinition> transformerDefinitions
709 = new ArrayList<SourceTransformerDefinition>();
710
711 transformerDefinitions.add(
712 new SourceTransformerDefinition(
713 new OtherConfigurationTestTransformer(),
714 "parent/database"));
715 sourceProcessConfiguration.setSourceTransformerDefinitions(
716 transformerDefinitions);
717 sourceProcessConfiguration.setSkipDecider(
718 "org.apache.torque.generator.configuration.OtherConfigurationTestSkipDecider");
719 assertSourceProcessConfigurationEquals(
720 sourceProcessConfiguration,
721 output.getSourceProcessConfiguration());
722 }
723 }
724 {
725 Output output = outputList.get(1);
726 assertEquals(
727 new QualifiedName("org.apache.torque.generator.firstOutput"),
728 output.getName());
729 assertEquals(
730 "skip",
731 output.getExistingTargetStrategy());
732 assertEquals(
733 null,
734 output.getOutputDirKey());
735 assertNull(output.getFilename());
736
737 {
738 OutletReference outletConfiguration
739 = output.getContentOutlet();
740 assertEquals(
741 new QualifiedName(
742 "org.apache.torque.generator.test.readConfiguration.javaOutlet"),
743 outletConfiguration.getName());
744 }
745 {
746 JavaOutlet filenameOutlet
747 = (JavaOutlet) output.getFilenameOutlet();
748 assertEquals("Foo", filenameOutlet.getFoo());
749 assertEquals("Bar", filenameOutlet.getBar());
750 assertEquals(
751 new QualifiedName(
752 "org.apache.torque.generator.configuration.filenameOutlet"),
753 filenameOutlet.getName());
754 }
755
756 {
757 Fileset sourceFileset = new Fileset(
758 projectPaths.getDefaultSourcePath(),
759 new HashSet<String>(),
760 new HashSet<String>());
761 FileSourceProvider sourceProvider = new FileSourceProvider(
762 new XmlSourceFormat(),
763 sourceFileset,
764 null);
765 sourceProvider.init(
766 configurationHandlers,
767 controllerState);
768 output.getSourceProvider().init(
769 configurationHandlers,
770 controllerState);
771 assertFileSourceProviderEquals(
772 sourceProvider,
773 (FileSourceProvider) output.getSourceProvider());
774 }
775 assertSourceProcessConfigurationEquals(
776 new SourceProcessConfiguration(),
777 output.getSourceProcessConfiguration());
778 }
779
780 {
781 Output output = outputList.get(2);
782 assertEquals(
783 new QualifiedName("secondOutput"),
784 output.getName());
785 assertEquals(
786 "replace",
787 output.getExistingTargetStrategy());
788 assertEquals(
789 output.getOutputDirKey(),
790 "secondOutputDirKey");
791 assertNull(output.getFilename());
792
793 {
794 OutletReference outletConfiguration
795 = output.getContentOutlet();
796 assertEquals(
797 new QualifiedName(
798 "org.apache.torque.generator.test.readConfiguration.anotherOutlet"),
799 outletConfiguration.getName());
800 }
801 {
802 VelocityOutlet filenameOutlet
803 = (VelocityOutlet) output.getFilenameOutlet();
804 String templateContent
805 = filenameOutlet.getContent(controllerState);
806
807
808 String templateContentLicenseRemoved
809 = StringUtils.substringAfterLast(templateContent, "\r\n");
810 assertEquals(
811 "test template output",
812 templateContentLicenseRemoved);
813 assertEquals(
814 new QualifiedName(
815 "org.apache.torque.generator.configuration.filenameOutlet"),
816 filenameOutlet.getName());
817 }
818
819 {
820 Fileset sourceFileset = new Fileset(
821 projectPaths.getDefaultSourcePath(),
822 createSetFrom("second.source.path.properties"),
823 createSetFrom("second.excluded.properties"));
824 FileSourceProvider sourceProvider = new FileSourceProvider(
825 new PropertiesSourceFormat(),
826 sourceFileset,
827 true);
828 sourceProvider.init(
829 configurationHandlers,
830 controllerState);
831 output.getSourceProvider().init(
832 configurationHandlers,
833 controllerState);
834 assertFileSourceProviderEquals(
835 sourceProvider,
836 (FileSourceProvider) output.getSourceProvider());
837 }
838 {
839 SourceProcessConfiguration sourceProcessConfiguration
840 = new SourceProcessConfiguration();
841 sourceProcessConfiguration.setStartElementsPath("properties/entry");
842 List<SourceTransformerDefinition> transformerDefinitions
843 = new ArrayList<SourceTransformerDefinition>();
844 transformerDefinitions.add(
845 new SourceTransformerDefinition(
846 new ConfigurationTestTransformer(),
847 null));
848 transformerDefinitions.add(
849 new SourceTransformerDefinition(
850 new OtherConfigurationTestTransformer(),
851 "database"));
852 sourceProcessConfiguration.setSourceTransformerDefinitions(
853 transformerDefinitions);
854 sourceProcessConfiguration.setSkipDecider(
855 "org.apache.torque.generator.configuration.ConfigurationTestSkipDecider");
856 assertSourceProcessConfigurationEquals(
857 sourceProcessConfiguration,
858 output.getSourceProcessConfiguration());
859 }
860 }
861 {
862 Output output = outputList.get(3);
863 assertEquals(
864 new QualifiedName("thirdOutput"),
865 output.getName());
866 assertEquals(
867 "append",
868 output.getExistingTargetStrategy());
869 assertEquals(
870 "thirdOutputDirKey",
871 output.getOutputDirKey());
872 assertEquals("outputFileName", output.getFilename());
873 assertNull(output.getFilenameOutlet());
874
875 {
876 OutletReference outletConfiguration
877 = output.getContentOutlet();
878 assertEquals(
879 new QualifiedName(
880 "org.apache.torque.generator.test.readConfiguration.thirdOutlet"),
881 outletConfiguration.getName());
882 }
883
884 {
885 JdbcMetadataSourceProvider sourceProvider
886 = new JdbcMetadataSourceProvider(
887 "jdbcUrl",
888 "jdbcDriver",
889 "jdbcUsername",
890 "jdbcPassword",
891 "jdbcSchema");
892 sourceProvider.init(
893 configurationHandlers,
894 controllerState);
895 output.getSourceProvider().init(
896 configurationHandlers,
897 controllerState);
898 assertJdbcMetadataSourceProviderEquals(
899 sourceProvider,
900 (JdbcMetadataSourceProvider) output.getSourceProvider());
901 }
902 assertSourceProcessConfigurationEquals(
903 new SourceProcessConfiguration(),
904 output.getSourceProcessConfiguration());
905 }
906 }
907
908 {
909 OutletConfiguration outletConfiguration
910 = unitConfiguration.getOutletConfiguration();
911 Map<QualifiedName, Outlet> outletMap
912 = outletConfiguration.getOutlets();
913 assertEquals(3, outletMap.size());
914 {
915 JavaOutlet outlet
916 = (JavaOutlet) outletMap.get(new QualifiedName(
917 "org.apache.torque.generator.test.readConfiguration.javaOutlet"));
918 assertEquals("Foo2", outlet.getFoo());
919 assertEquals("Bar2", outlet.getBar());
920 Map<String, MergepointMapping> mergepointMappings
921 = outlet.getMergepointMappings();
922 assertEquals(3, mergepointMappings.size());
923 {
924 MergepointMapping mergepointMapping
925 = mergepointMappings.get("properties");
926 assertEquals(1, mergepointMapping.getActions().size());
927 MergepointAction action
928 = mergepointMapping.getActions().get(0);
929 assertEquals(
930 new TraverseAllAction(
931 "entry",
932 "org.apache.torque.generator.velocity.propertyCopy",
933 true),
934 action);
935 assertEquals("inputElement", outlet.getInputElementName());
936 }
937 {
938
939 MergepointMapping mergepointMapping
940 = mergepointMappings.get("mergepointName");
941 assertEquals(1, mergepointMapping.getActions().size());
942 MergepointAction action
943 = mergepointMapping.getActions().get(0);
944 assertEquals(
945 new ApplyAction(
946 null,
947 "someOutletAction",
948 false),
949 action);
950 assertEquals("inputElement", outlet.getInputElementName());
951 }
952 {
953
954 MergepointMapping mergepointMapping
955 = mergepointMappings.get("mergepointFromParent");
956 assertEquals(1, mergepointMapping.getActions().size());
957 MergepointAction action
958 = mergepointMapping.getActions().get(0);
959 assertEquals(
960 new ApplyAction(
961 null,
962 "newOutletAction",
963 true),
964 action);
965 assertEquals("inputElement", outlet.getInputElementName());
966 }
967 }
968
969 {
970 VelocityOutlet outlet
971 = (VelocityOutlet) outletMap.get(new QualifiedName(
972 "org.apache.torque.generator.test.readConfiguration.anotherOutlet"));
973 String templateContent
974 = outlet.getContent(controllerState);
975
976
977 String templateContentLicenseRemoved
978 = StringUtils.substringAfterLast(templateContent, "\r\n");
979 assertEquals(
980 "test template output",
981 templateContentLicenseRemoved);
982 assertEquals(0, outlet.getMergepointMappings().size());
983 }
984 {
985 VelocityOutlet outlet
986 = (VelocityOutlet) outletMap.get(new QualifiedName(
987 "org.apache.torque.generator.test.readConfiguration.parentOutlet"));
988 String templateContent
989 = outlet.getContent(controllerState);
990
991
992 String templateContentLicenseRemoved
993 = StringUtils.substringAfterLast(templateContent, "\r\n");
994 assertEquals(
995 "parent test template output",
996 templateContentLicenseRemoved);
997 assertEquals(0, outlet.getMergepointMappings().size());
998 }
999 }
1000 }
1001
1002 private static void assertFileSourceProviderEquals(
1003 FileSourceProvider expected,
1004 FileSourceProvider actual)
1005 {
1006 assertEquals(
1007 expected.getSourceFormat(),
1008 actual.getSourceFormat());
1009 assertEquals(
1010 expected.getSourceFileset().getIncludes(),
1011 actual.getSourceFileset().getIncludes());
1012 assertEquals(
1013 expected.getSourceFileset().getExcludes(),
1014 actual.getSourceFileset().getExcludes());
1015 assertEquals(
1016 expected.getSourceFileset().getBasedir(),
1017 actual.getSourceFileset().getBasedir());
1018 assertEquals(
1019 expected.getPaths(),
1020 actual.getPaths());
1021 assertEquals(
1022 expected.getCombineFiles(),
1023 actual.getCombineFiles());
1024 }
1025
1026 private static void assertJdbcMetadataSourceProviderEquals(
1027 JdbcMetadataSourceProvider expected,
1028 JdbcMetadataSourceProvider actual)
1029 {
1030 assertEquals(
1031 expected.getDriverOption(),
1032 actual.getDriverOption());
1033 assertEquals(
1034 expected.getPasswordOption(),
1035 actual.getPasswordOption());
1036 assertEquals(
1037 expected.getSchemaOption(),
1038 actual.getSchemaOption());
1039 assertEquals(
1040 expected.getUrlOption(),
1041 actual.getUrlOption());
1042 assertEquals(
1043 expected.getUsernameOption(),
1044 actual.getUsernameOption());
1045 assertEquals(
1046 expected.getDriver(),
1047 actual.getDriver());
1048 assertEquals(
1049 expected.getPassword(),
1050 actual.getPassword());
1051 assertEquals(
1052 expected.getSchema(),
1053 actual.getSchema());
1054 assertEquals(
1055 expected.getUrl(),
1056 actual.getUrl());
1057 assertEquals(
1058 expected.getUsername(),
1059 actual.getUsername());
1060 }
1061
1062 private static void assertOptionsEquals(Options expected, Options actual)
1063 {
1064 Map<QualifiedName, Option> expectedMap
1065 = expected.getGlobalScope();
1066 Map<QualifiedName, Option> actualMap
1067 = actual.getGlobalScope();
1068 assertEquals(expectedMap.size(), actualMap.size());
1069
1070 for (QualifiedName expectedKey : expectedMap.keySet())
1071 {
1072 assertTrue(expectedMap.containsKey(expectedKey));
1073 Object expectedValue = expectedMap.get(expectedKey).getValue();
1074 Object actualValue = actualMap.get(expectedKey).getValue();
1075 assertEquals(expectedValue, actualValue);
1076 }
1077 }
1078
1079 private void assertSourceProcessConfigurationEquals(
1080 SourceProcessConfiguration expected,
1081 SourceProcessConfiguration actual)
1082 {
1083 if (expected.getSkipDecider() != null)
1084 {
1085 assertEquals(
1086 expected.getSkipDecider().getClass(),
1087 actual.getSkipDecider().getClass());
1088 }
1089 else
1090 {
1091 assertNull(actual.getSkipDecider());
1092 }
1093 assertEquals(
1094 expected.getStartElementsPath(),
1095 actual.getStartElementsPath());
1096 assertEquals(
1097 expected.getTransformerDefinitions(),
1098 actual.getTransformerDefinitions());
1099 }
1100
1101
1102
1103
1104
1105
1106
1107
1108 private static Set<String> createSetFrom(String... content)
1109 {
1110 Set<String> result = new HashSet<String>();
1111 for (String part : content)
1112 {
1113 result.add(part);
1114 }
1115 return result;
1116 }
1117 }