Turned the API packages into bundles.
[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.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23
24 import javax.xml.transform.dom.DOMSource;
25
26 import org.junit.Test;
27 import org.wamblee.xmlrouter.common.Id;
28 import org.wamblee.xmlrouter.config.Transformation;
29
30 public class TransformationPathsTest {
31
32     public static class MyTransformation implements Transformation {
33
34         private String from;
35         private String to;
36
37         public MyTransformation(String aFrom, String aTo) {
38             from = aFrom;
39             to = aTo;
40         }
41
42         @Override
43         public Id<Transformation> getId() {
44             return new Id<Transformation>("myname");
45         }
46
47         @Override
48         public String getFromType() {
49             return from;
50         }
51
52         @Override
53         public String getToType() {
54             return to;
55         }
56
57         @Override
58         public DOMSource transform(DOMSource aDocument) {
59             return null;
60         }
61
62         @Override
63         public String toString() {
64             return from + "->" + to;
65         }
66     }
67
68     private TransformationPaths transformations;
69
70     private List<Transformation> createTransformations(long aStartId,
71         Transformation... aTransformations) {
72         List<Transformation> res = new ArrayList<Transformation>();
73
74         for (Transformation t : aTransformations) {
75             res.add(t);
76         }
77         return res;
78     }
79
80     @Test
81     public void testOneTransformation() {
82         transformations = new TransformationPaths(createTransformations(10L,
83             new MyTransformation("A", "B")));
84
85         System.out.println(transformations.toString());
86         TransformationPath path = transformations.getPath("A", "B");
87         assertEquals(1, path.size());
88         assertNull(transformations.getPath("B", "A"));
89
90         assertEquals(0, transformations.getPath("A", "A").size());
91         assertEquals(0, transformations.getPath("B", "B").size());
92
93         Collection<String> possibleTargets = transformations
94             .getPossibleTargetTypes("A");
95         assertEquals(2, possibleTargets.size());
96         assertTrue(possibleTargets.contains("A"));
97         assertTrue(possibleTargets.contains("B"));
98     }
99
100     @Test
101     public void testMultipleTransformations() {
102         transformations = new TransformationPaths(createTransformations(10L,
103             new MyTransformation("A", "B"), new MyTransformation("B", "C"),
104             new MyTransformation("C", "A")));
105
106         System.out.println(transformations);
107         assertEquals(2, transformations.getPath("C", "B").size());
108
109         transformations = new TransformationPaths(createTransformations(10L,
110             new MyTransformation("B", "C"), new MyTransformation("C", "A")));
111
112         assertNull(transformations.getPath("C", "B"));
113     }
114
115     @Test
116     public void testUnknownDestination() {
117         transformations = new TransformationPaths(createTransformations(10L,
118             new MyTransformation("A", "B")));
119         assertNull(transformations.getPath("A", "D"));
120     }
121
122     @Test
123     public void testWithoutTransformations() {
124         transformations = new TransformationPaths(
125             new ArrayList<Transformation>());
126         Collection<String> res = transformations.getPossibleTargetTypes("a");
127         assertEquals(1, res.size());
128
129         TransformationPath path = transformations.getPath("A", "A");
130         assertNotNull(path);
131         assertEquals(0, path.size());
132
133         assertNull(transformations.getPath("B", "C"));
134     }
135 }