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