1 package org.kuali.spring.util;
2
3 import java.io.IOException;
4 import java.util.Properties;
5
6 import junit.framework.Assert;
7
8 import org.junit.Test;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
12
13 public class PlaceholderStringResolverTest {
14
15 final Logger logger = LoggerFactory.getLogger(PlaceholderStringResolverTest.class);
16
17 @Test
18 public void setters() throws IOException {
19 PropertyLogger plogger = new PropertyLogger();
20 PlaceholderStringResolver resolver = new PlaceholderStringResolver();
21 resolver.setPlaceholderPrefix("foo");
22 resolver.setPlaceholderSuffix("bar");
23 resolver.setPlogger(plogger);
24 resolver.setSimplePrefix("o");
25
26 Assert.assertEquals("foo", resolver.getPlaceholderPrefix());
27 Assert.assertEquals("bar", resolver.getPlaceholderSuffix());
28 Assert.assertEquals("o", resolver.getSimplePrefix());
29 Assert.assertNotNull(resolver.getPlogger());
30 }
31
32 @Test
33 public void valueSeparator() throws IOException {
34 Properties properties = new Properties();
35 properties.setProperty("a", "bar");
36 PropertiesRetriever retriever = new PropertiesRetriever(properties);
37 PlaceholderStringResolver resolver = new PlaceholderStringResolver();
38
39
40 resolver.setValueSeparator("=");
41
42
43 String text = "Hello ${c=foo} World";
44 Assert.assertEquals("Hello foo World", resolver.resolve(text, retriever));
45
46
47
48 String text2 = "Hello ${a=foo} World";
49 Assert.assertEquals("Hello bar World", resolver.resolve(text2, retriever));
50
51
52 String text3 = "Hello ${b} World";
53 resolver.setIgnoreUnresolvablePlaceholders(true);
54 Assert.assertEquals("Hello ${b} World", resolver.resolve(text3, retriever));
55 }
56
57 @Test
58 public void unresolvablePlaceholder() throws IOException {
59 Properties properties = new Properties();
60 PropertiesRetriever retriever = new PropertiesRetriever(properties);
61 PlaceholderStringResolver resolver = new PlaceholderStringResolver();
62 String text = "Hello ${c} World";
63 try {
64 resolver.resolve(text, retriever);
65 Assert.fail("Should have thrown an exception due to an unresolvable placeholder");
66 } catch (IllegalArgumentException e) {
67
68 }
69
70 resolver.setIgnoreUnresolvablePlaceholders(true);
71
72 Assert.assertEquals(text, resolver.resolve(text, retriever));
73
74 }
75
76 @Test
77 public void noMatchingSuffix() throws IOException {
78 Properties properties = new Properties();
79 PropertiesRetriever retriever = new PropertiesRetriever(properties);
80 PlaceholderStringResolver resolver = new PlaceholderStringResolver();
81 String text = "Hello ${a World";
82 Assert.assertEquals(text, resolver.resolve(text, retriever));
83
84 }
85
86 @Test
87 public void constructorTest() throws IOException {
88 PlaceholderStringResolver resolver = new PlaceholderStringResolver("foo", "bar", "=", true);
89 Assert.assertEquals("foo", resolver.getPlaceholderPrefix());
90 Assert.assertEquals("bar", resolver.getPlaceholderSuffix());
91 Assert.assertEquals("=", resolver.getValueSeparator());
92 Assert.assertEquals(resolver.getSimplePrefix(), resolver.getPlaceholderPrefix());
93
94 PlaceholderStringResolver resolver2 = new PlaceholderStringResolver("foo2", "bar2");
95 Assert.assertEquals("foo2", resolver2.getPlaceholderPrefix());
96 Assert.assertEquals("bar2", resolver2.getPlaceholderSuffix());
97 Assert.assertNull(resolver2.getValueSeparator());
98 Assert.assertEquals(PlaceholderStringResolver.DEFAULT_IS_IGNORE_UNRESOLVABLE_PLACEHOLDERS,
99 resolver2.isIgnoreUnresolvablePlaceholders());
100
101 PlaceholderStringResolver resolver3 = new PlaceholderStringResolver();
102 Assert.assertEquals(PropertyPlaceholderConfigurer.DEFAULT_PLACEHOLDER_PREFIX, resolver3.getPlaceholderPrefix());
103 Assert.assertEquals(PropertyPlaceholderConfigurer.DEFAULT_PLACEHOLDER_SUFFIX, resolver3.getPlaceholderSuffix());
104 }
105
106 @Test
107 public void circularReference() throws IOException {
108 Properties properties = new Properties();
109 properties.setProperty("a", "${b}");
110 properties.setProperty("b", "${a}");
111 PropertiesRetriever retriever = new PropertiesRetriever(properties);
112 PlaceholderStringResolver resolver = new PlaceholderStringResolver();
113 String text = "${a}";
114 try {
115 resolver.resolve(text, retriever);
116 Assert.fail("Should have thrown a circular reference exception");
117 } catch (IllegalArgumentException e) {
118
119 }
120 }
121
122 @Test
123 public void resolve2() throws IOException {
124 Properties properties = new Properties();
125 properties.setProperty("a", "1");
126 properties.setProperty("b", "${a}");
127 PropertiesRetriever retriever = new PropertiesRetriever(properties);
128 PlaceholderStringResolver resolver = new PlaceholderStringResolver();
129 String text = "${b}";
130 Assert.assertEquals("1", resolver.resolve(text, retriever));
131 }
132
133 @Test
134 public void resolve() throws IOException {
135 Properties properties = new Properties();
136 properties.setProperty("a", "1");
137 properties.setProperty("b", "2");
138 properties.setProperty("1.2", "blue");
139 PropertiesRetriever retriever = new PropertiesRetriever(properties);
140 PlaceholderStringResolver resolver = new PlaceholderStringResolver();
141 String text = "The sky is ${${a}.${b}}";
142 Assert.assertEquals("The sky is blue", resolver.resolve(text, retriever));
143 }
144
145 @Test
146 public void process() throws IOException {
147 Properties properties = new Properties();
148 properties.setProperty("foo", "bar");
149 PropertiesRetriever retriever = new PropertiesRetriever(properties);
150 PlaceholderStringResolver resolver = new PlaceholderStringResolver();
151 String text = "${foo}";
152 Assert.assertEquals("bar", resolver.resolve(text, retriever));
153 }
154
155 @Test
156 public void processNested() throws IOException {
157 Properties properties = new Properties();
158 properties.setProperty("a", "1");
159 properties.setProperty("b", "2");
160 properties.setProperty("c", "3");
161 properties.setProperty("1.2", "beans");
162 PropertiesRetriever retriever = new PropertiesRetriever(properties);
163 PlaceholderStringResolver resolver = new PlaceholderStringResolver();
164 String text = "I like ${${a}.${b}}";
165 Assert.assertEquals("I like beans", resolver.resolve(text, retriever));
166 }
167
168 }