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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/**
*
* Copyright 2006 The Apache Software Foundation
*
* Licensed 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.core.loader;
import junit.framework.TestCase;
import org.apache.tuscany.core.builder.ObjectFactory;
import org.apache.tuscany.core.config.ConfigurationLoadException;
import org.apache.tuscany.core.loader.impl.StringParserPropertyFactory;
import org.apache.tuscany.model.assembly.AssemblyFactory;
import org.apache.tuscany.model.assembly.Property;
import org.apache.tuscany.model.assembly.impl.AssemblyFactoryImpl;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.beans.PropertyEditorManager;
import java.beans.PropertyEditorSupport;
import java.io.StringReader;
import java.net.URI;
import java.util.Arrays;
/**
* @version $Rev$ $Date$
*/
public class StringParserPropertyFactoryTestCase extends TestCase {
private StringParserPropertyFactory factory;
private XMLInputFactory xmlFactory;
private Property property;
public void testSimpleString() throws XMLStreamException, ConfigurationLoadException {
String instance = getInstance(String.class, "<foo>Hello World</foo>");
assertEquals("Hello World", instance);
}
public void testByteArray() throws XMLStreamException, ConfigurationLoadException {
byte[] instance = getInstance(byte[].class, "<foo>01020304</foo>");
assertTrue(Arrays.equals(new byte[]{1, 2, 3, 4}, instance));
}
public void testInteger() throws XMLStreamException, ConfigurationLoadException {
Integer instance = getInstance(Integer.class, "<foo>1234</foo>");
assertEquals(Integer.valueOf(1234), instance);
}
public void testInt() throws XMLStreamException, ConfigurationLoadException {
int instance = getInstance(Integer.TYPE, "<foo>1234</foo>");
assertEquals(1234, instance);
}
public void testBoolean() throws XMLStreamException, ConfigurationLoadException {
Boolean instance = getInstance(Boolean.class, "<foo>true</foo>");
assertSame(Boolean.TRUE, instance);
}
public void testConstructor() throws XMLStreamException, ConfigurationLoadException {
// java.net.URI has a ctr that takes a String
URI instance = getInstance(URI.class, "<foo>http://www.apache.org</foo>");
assertEquals(URI.create("http://www.apache.org"), instance);
}
public void testPropertyEditor() throws XMLStreamException, ConfigurationLoadException {
// register a property editor for java.lang.Class
PropertyEditorManager.registerEditor(Class.class, ClassEditor.class);
try {
Class<?> instance = getInstance(Class.class, "<foo>java.lang.Integer</foo>");
assertEquals(Integer.class, instance);
} finally{
PropertyEditorManager.registerEditor(Class.class, null);
}
}
private <T> T getInstance(Class<T> type, String xml) throws XMLStreamException, ConfigurationLoadException {
property.setType(type);
XMLStreamReader reader = xmlFactory.createXMLStreamReader(new StringReader(xml));
reader.next();
ObjectFactory<T> objectFactory = (ObjectFactory<T>) factory.createObjectFactory(reader, property);
return objectFactory.getInstance();
}
protected void setUp() throws Exception {
super.setUp();
factory = new StringParserPropertyFactory();
xmlFactory = XMLInputFactory.newInstance();
AssemblyFactory assemblyFactory = new AssemblyFactoryImpl();
property = assemblyFactory.createProperty();
}
public static class ClassEditor extends PropertyEditorSupport {
public void setAsText(String text) throws IllegalArgumentException {
try {
setValue(Class.forName(text));
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(text);
}
}
}
}
|