1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tuscany.sca.databinding.xml;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import org.apache.tuscany.sca.databinding.DataPipe;
import org.apache.tuscany.sca.databinding.DataPipeTransformer;
import org.apache.tuscany.sca.databinding.impl.DOMHelper;
import org.apache.tuscany.sca.databinding.impl.PipedTransformer;
import org.junit.Assert;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/**
* Test case for DataPipe
*
* @version $Rev$ $Date$
*/
public class DataPipeTestCase {
@Test
public final void testStreamPipe() throws IOException {
byte[] bytes = new byte[] {1, 2, 3};
DataPipeTransformer<OutputStream, InputStream> pipe = new StreamDataPipe();
DataPipe<OutputStream, InputStream> dataPipe = pipe.newInstance();
OutputStream os = dataPipe.getSink();
os.write(bytes);
byte[] newBytes = new byte[16];
int count = dataPipe.getResult().read(newBytes);
Assert.assertEquals(3, count);
for (int i = 0; i < bytes.length; i++) {
Assert.assertEquals(bytes[i], newBytes[i]);
}
}
@Test
public final void testWriter2ReaderPipe() throws IOException {
String str = "ABC";
Writer2ReaderDataPipe pipe = new Writer2ReaderDataPipe();
Assert.assertSame(Writer.class, pipe.getSourceType());
Assert.assertSame(Reader.class, pipe.getTargetType());
DataPipe<Writer, Reader> dataPipe = pipe.newInstance();
dataPipe.getSink().write(str);
char[] buf = new char[16];
int count = dataPipe.getResult().read(buf);
Assert.assertEquals(3, count);
for (int i = 0; i < str.length(); i++) {
Assert.assertEquals(str.charAt(i), buf[i]);
}
}
@Test
public final void testPiped() throws Exception {
Node2Writer node2Writer = new Node2Writer();
Writer2ReaderDataPipe pipe = new Writer2ReaderDataPipe();
PipedTransformer<Node, Writer, Reader> transformer =
new PipedTransformer<Node, Writer, Reader>(node2Writer, pipe);
Document document = DOMHelper.newDocument();
Element element = document.createElementNS("http://ns1", "root");
document.appendChild(element);
Reader reader = transformer.transform(document, null);
Assert.assertEquals(transformer.getWeight(), node2Writer.getWeight() + pipe.getWeight());
Assert.assertEquals(transformer.getSourceDataBinding(), node2Writer.getSourceDataBinding());
Assert.assertEquals(transformer.getTargetDataBinding(), pipe.getTargetDataBinding());
char[] buf = new char[120];
int count = reader.read(buf);
String xml = new String(buf, 0, count);
Assert.assertTrue(xml.contains("<root xmlns=\"http://ns1\"/>"));
}
}
|