1 /*
2 * Copyright The Apache Software Foundation. All rights reserved.
3 *
4 * This software is published under the terms of the Apache Software License
5 * version 1.1, a copy of which has been included with this distribution in
6 * the LICENSE.txt file.
7 */
8 package org.codehaus.spice.configkit;
9
10 import java.io.InputStream;
11 import java.util.HashMap;
12 import java.util.Properties;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import junit.framework.TestCase;
16 import org.w3c.dom.Comment;
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19 import org.w3c.dom.Text;
20
21 /***
22 * Basic unit tests for the PropertyExpander.
23 *
24 * @author Peter Donald
25 */
26 public final class PropertyExpanderTestCase
27 extends TestCase
28 {
29 private final PropertyExpander m_emptyExpander = new PropertyExpander(
30 PropertyExpander.EMPTY_ON_UNDEFINED );
31 private final PropertyExpander m_exceptExpander = new PropertyExpander();
32 private final PropertyExpander m_leaveExpander = new PropertyExpander(
33 PropertyExpander.LEAVE_UNDEFINED );
34
35 public void testStringExpansion()
36 throws Exception
37 {
38 final HashMap data = new HashMap();
39 data.put( "app.name", "MyApp" );
40 assertEquals( "Expand with no vars",
41 "foo",
42 m_emptyExpander.expandValues( "foo", data ) );
43 assertEquals( "Expand with one var",
44 "MyApp",
45 m_emptyExpander.expandValues( "${app.name}", data ) );
46 assertEquals( "Expand with one var n start text",
47 "xMyApp",
48 m_emptyExpander.expandValues( "x${app.name}", data ) );
49 assertEquals( "Expand with one var n end text",
50 "MyAppx",
51 m_emptyExpander.expandValues( "${app.name}x", data ) );
52 assertEquals( "Expand with two var",
53 "MyAppMyApp",
54 m_emptyExpander.expandValues( "${app.name}${app.name}",
55 data ) );
56 assertEquals( "Expand with two var inner text",
57 "MyAppxMyApp",
58 m_emptyExpander.expandValues( "${app.name}x${app.name}",
59 data ) );
60 assertEquals( "Expand with two var inner n outer text",
61 "xMyAppxMyAppx",
62 m_emptyExpander.expandValues(
63 "x${app.name}x${app.name}x", data ) );
64 assertEquals( "No exist and empty policy",
65 "",
66 m_emptyExpander.expandValues( "${noexist}", data ) );
67 assertEquals( "No exist and leave policy",
68 "${noexist}",
69 m_leaveExpander.expandValues( "${noexist}", data ) );
70 try
71 {
72 m_exceptExpander.expandValues( "${noexist}", data );
73 fail( "Expected exception on non existent property" );
74 }
75 catch( Exception e )
76 {
77 }
78
79 try
80 {
81 m_emptyExpander.expandValues( "${noexist", data );
82 fail( "Expected exception on badly formed property" );
83 }
84 catch( Exception e )
85 {
86 }
87 }
88
89 public void testElementExpansion()
90 throws Exception
91 {
92 final HashMap data = new HashMap();
93 data.put( "app.name", "MyApp" );
94
95 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
96 final DocumentBuilder builder = factory.newDocumentBuilder();
97 final Document document = builder.newDocument();
98 final Element root = document.createElement( "root" );
99 root.setAttribute( "attr", "${app.name}" );
100
101 final Text text1 = document.createTextNode( "${app.name}" );
102 root.appendChild( text1 );
103
104 final Element child = document.createElement( "child" );
105 child.setAttribute( "attr2", "${app.name}" );
106
107 final Text text2 = document.createTextNode( "${app.name}" );
108 child.appendChild( text2 );
109
110 final Comment comment = document.createComment( "some random comment" );
111 child.appendChild( comment );
112
113 root.appendChild( child );
114
115 m_emptyExpander.expandValues( root, data );
116
117 assertEquals( "root/@attr", "MyApp", root.getAttribute( "attr" ) );
118 assertEquals( "root/#content", "MyApp", text1.getData() );
119 assertEquals( "root/child/@attr2",
120 "MyApp",
121 child.getAttribute( "attr2" ) );
122 assertEquals( "root/child/#content", "MyApp", text2.getData() );
123 }
124
125 public void testPropertiesExpansion()
126 throws Exception
127 {
128 final HashMap data = new HashMap();
129 data.put( "app.name", "MyApp" );
130
131 final Properties input = new Properties();
132 final InputStream inputStream = getClass().getResourceAsStream(
133 "test/test.properties" );
134 assertNotNull( "Input data", inputStream );
135 input.load( inputStream );
136
137 final Properties output =
138 m_emptyExpander.expandValues( input, data );
139
140 assertEquals( "${app.name}.description=Foo",
141 "Foo",
142 output.getProperty( "MyApp.description" ) );
143 assertEquals( "location=${app.name}",
144 "MyApp",
145 output.getProperty( "location" ) );
146 }
147 }
148
This page was automatically generated by Maven