Now using RouterConfig internally inside the XML Router.
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / TransformationsTest.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.xmlrouter.impl;
17
18 import static junit.framework.Assert.*;
19
20 import java.util.Collection;
21 import java.util.LinkedHashMap;
22 import java.util.Map;
23
24 import javax.xml.transform.dom.DOMSource;
25
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.wamblee.xmlrouter.common.Id;
29 import org.wamblee.xmlrouter.config.Transformation;
30
31 public class TransformationsTest {
32
33     public static class MyTransformation implements Transformation {
34
35         private String from;
36         private String to;
37
38         public MyTransformation(String aFrom, String aTo) {
39             from = aFrom;
40             to = aTo;
41         }
42
43         @Override
44         public String getName() {
45             return "myname";
46         }
47
48         @Override
49         public String getFromType() {
50             return from;
51         }
52
53         @Override
54         public String getToType() {
55             return to;
56         }
57
58         @Override
59         public DOMSource transform(DOMSource aDocument) {
60             return null;
61         }
62
63         @Override
64         public String toString() {
65             return from + "->" + to;
66         }
67     }
68
69     private Transformations transformations;
70
71     @Before
72     public void setUp() {
73         transformations = new Transformations();
74     }
75
76     private Map<Id<Transformation>, Transformation> createTransformations(
77         long aStartId, Transformation... aTransformations) {
78         Map<Id<Transformation>, Transformation> res = new LinkedHashMap<Id<Transformation>, Transformation>();
79
80         long id = aStartId;
81         for (Transformation t : aTransformations) {
82             res.put(new Id<Transformation>(id++), t);
83         }
84         return res;
85     }
86
87     @Test
88     public void testOneTransformation() {
89         transformations.replaceTransformations(createTransformations(10L,
90             new MyTransformation("A", "B")));
91
92         System.out.println(transformations.toString());
93         TransformationPath path = transformations.getPath("A", "B");
94         assertEquals(1, path.size());
95         assertNull(transformations.getPath("B", "A"));
96
97         assertEquals(0, transformations.getPath("A", "A").size());
98         assertEquals(0, transformations.getPath("B", "B").size());
99
100         Collection<String> possibleTargets = transformations
101             .getPossibleTargetTypes("A");
102         assertEquals(2, possibleTargets.size());
103         assertTrue(possibleTargets.contains("A"));
104         assertTrue(possibleTargets.contains("B"));
105     }
106
107     @Test
108     public void testMultipleTransformations() {
109         transformations.replaceTransformations(createTransformations(10L,
110             new MyTransformation("A", "B"), new MyTransformation("B", "C"),
111             new MyTransformation("C", "A")));
112
113         System.out.println(transformations);
114         assertEquals(2, transformations.getPath("C", "B").size());
115
116         transformations.replaceTransformations(createTransformations(10L,
117             new MyTransformation("B", "C"), new MyTransformation("C", "A")));
118
119         assertNull(transformations.getPath("C", "B"));
120     }
121
122     @Test
123     public void testUnknownDestination() {
124         transformations.replaceTransformations(createTransformations(10L,
125             new MyTransformation("A", "B")));
126         assertNull(transformations.getPath("A", "D"));
127     }
128
129     @Test
130     public void testWithoutTransformations() {
131         Collection<String> res = transformations.getPossibleTargetTypes("a");
132         assertEquals(1, res.size());
133
134         TransformationPath path = transformations.getPath("A", "A");
135         assertNotNull(path);
136         assertEquals(0, path.size());
137
138         assertNull(transformations.getPath("B", "C"));
139     }
140 }