2 * Copyright 2005-2011 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.xmlrouter.impl;
18 import static junit.framework.Assert.*;
20 import java.util.Collection;
21 import java.util.LinkedHashMap;
24 import javax.xml.transform.dom.DOMSource;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.wamblee.xmlrouter.common.Id;
29 import org.wamblee.xmlrouter.config.Transformation;
31 public class TransformationsTest {
33 public static class MyTransformation implements Transformation {
38 public MyTransformation(String aFrom, String aTo) {
44 public String getName() {
49 public String getFromType() {
54 public String getToType() {
59 public DOMSource transform(DOMSource aDocument) {
64 public String toString() {
65 return from + "->" + to;
69 private Transformations transformations;
73 transformations = new Transformations();
76 private Map<Id<Transformation>, Transformation> createTransformations(
77 long aStartId, Transformation... aTransformations) {
78 Map<Id<Transformation>, Transformation> res = new LinkedHashMap<Id<Transformation>, Transformation>();
81 for (Transformation t : aTransformations) {
82 res.put(new Id<Transformation>(id++), t);
88 public void testOneTransformation() {
89 transformations.replaceTransformations(createTransformations(10L,
90 new MyTransformation("A", "B")));
92 System.out.println(transformations.toString());
93 TransformationPath path = transformations.getPath("A", "B");
94 assertEquals(1, path.size());
95 assertNull(transformations.getPath("B", "A"));
97 assertEquals(0, transformations.getPath("A", "A").size());
98 assertEquals(0, transformations.getPath("B", "B").size());
100 Collection<String> possibleTargets = transformations
101 .getPossibleTargetTypes("A");
102 assertEquals(2, possibleTargets.size());
103 assertTrue(possibleTargets.contains("A"));
104 assertTrue(possibleTargets.contains("B"));
108 public void testMultipleTransformations() {
109 transformations.replaceTransformations(createTransformations(10L,
110 new MyTransformation("A", "B"), new MyTransformation("B", "C"),
111 new MyTransformation("C", "A")));
113 System.out.println(transformations);
114 assertEquals(2, transformations.getPath("C", "B").size());
116 transformations.replaceTransformations(createTransformations(10L,
117 new MyTransformation("B", "C"), new MyTransformation("C", "A")));
119 assertNull(transformations.getPath("C", "B"));
123 public void testUnknownDestination() {
124 transformations.replaceTransformations(createTransformations(10L,
125 new MyTransformation("A", "B")));
126 assertNull(transformations.getPath("A", "D"));
130 public void testWithoutTransformations() {
131 Collection<String> res = transformations.getPossibleTargetTypes("a");
132 assertEquals(1, res.size());
134 TransformationPath path = transformations.getPath("A", "A");
136 assertEquals(0, path.size());
138 assertNull(transformations.getPath("B", "C"));