Reduced the time during which the configuration is locked. Computation of paths now...
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / TransformationPathsTest.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.HashMap;
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24
25 import javax.xml.transform.dom.DOMSource;
26
27 import org.junit.Test;
28 import org.wamblee.xmlrouter.common.Id;
29 import org.wamblee.xmlrouter.config.Transformation;
30
31 public class TransformationPathsTest {
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 TransformationPaths transformations;
70
71     private Map<Id<Transformation>, Transformation> createTransformations(
72         long aStartId, Transformation... aTransformations) {
73         Map<Id<Transformation>, Transformation> res = new LinkedHashMap<Id<Transformation>, Transformation>();
74
75         long id = aStartId;
76         for (Transformation t : aTransformations) {
77             res.put(new Id<Transformation>(id++), t);
78         }
79         return res;
80     }
81
82     @Test
83     public void testOneTransformation() {
84         transformations = new TransformationPaths(createTransformations(10L,
85             new MyTransformation("A", "B")));
86
87         System.out.println(transformations.toString());
88         TransformationPath path = transformations.getPath("A", "B");
89         assertEquals(1, path.size());
90         assertNull(transformations.getPath("B", "A"));
91
92         assertEquals(0, transformations.getPath("A", "A").size());
93         assertEquals(0, transformations.getPath("B", "B").size());
94
95         Collection<String> possibleTargets = transformations
96             .getPossibleTargetTypes("A");
97         assertEquals(2, possibleTargets.size());
98         assertTrue(possibleTargets.contains("A"));
99         assertTrue(possibleTargets.contains("B"));
100     }
101
102     @Test
103     public void testMultipleTransformations() {
104         transformations = new TransformationPaths(createTransformations(10L,
105             new MyTransformation("A", "B"), new MyTransformation("B", "C"),
106             new MyTransformation("C", "A")));
107
108         System.out.println(transformations);
109         assertEquals(2, transformations.getPath("C", "B").size());
110
111         transformations = new TransformationPaths(createTransformations(10L,
112             new MyTransformation("B", "C"), new MyTransformation("C", "A")));
113
114         assertNull(transformations.getPath("C", "B"));
115     }
116
117     @Test
118     public void testUnknownDestination() {
119         transformations = new TransformationPaths(createTransformations(10L,
120             new MyTransformation("A", "B")));
121         assertNull(transformations.getPath("A", "D"));
122     }
123
124     @Test
125     public void testWithoutTransformations() {
126         transformations = new TransformationPaths(
127             new HashMap<Id<Transformation>, Transformation>());
128         Collection<String> res = transformations.getPossibleTargetTypes("a");
129         assertEquals(1, res.size());
130
131         TransformationPath path = transformations.getPath("A", "A");
132         assertNotNull(path);
133         assertEquals(0, path.size());
134
135         assertNull(transformations.getPath("B", "C"));
136     }
137 }