1 package org.apache.archiva.redback.components.evaluator.sources;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23 import org.apache.archiva.redback.components.evaluator.DefaultExpressionEvaluator;
24 import org.apache.archiva.redback.components.evaluator.EvaluatorException;
25 import org.apache.archiva.redback.components.evaluator.sources.PropertiesExpressionSource;
26 import org.apache.archiva.redback.components.evaluator.sources.SystemPropertyExpressionSource;
27 import org.apache.archiva.redback.components.evaluator.ExpressionEvaluator;
28
29 import java.util.Properties;
30
31
32
33
34
35
36
37 public class DefaultExpressionEvaluatorTest
38 extends TestCase
39 {
40 private ExpressionEvaluator evaluator;
41
42 protected void setUp()
43 throws Exception
44 {
45 super.setUp();
46
47 evaluator = new DefaultExpressionEvaluator();
48 }
49
50 public void testSimple()
51 throws EvaluatorException
52 {
53 Properties props = new Properties();
54 props.setProperty( "fruit", "apple" );
55
56 PropertiesExpressionSource propsSource = new PropertiesExpressionSource();
57 propsSource.setProperties( props );
58 evaluator.addExpressionSource( propsSource );
59
60 String expression = "${fruit}";
61 String expected = "apple";
62
63 String actual = evaluator.expand( expression );
64 assertEquals( expected, actual );
65 }
66
67 public void testSimpleStartOfLine()
68 throws EvaluatorException
69 {
70 Properties props = new Properties();
71 props.setProperty( "fruit", "apple" );
72
73 PropertiesExpressionSource propsSource = new PropertiesExpressionSource();
74 propsSource.setProperties( props );
75 evaluator.addExpressionSource( propsSource );
76
77 String expression = "${fruit} is good for you.";
78 String expected = "apple is good for you.";
79
80 String actual = evaluator.expand( expression );
81 assertEquals( expected, actual );
82 }
83
84 public void testSimpleEndOfLine()
85 throws EvaluatorException
86 {
87 Properties props = new Properties();
88 props.setProperty( "fruit", "apple" );
89
90 PropertiesExpressionSource propsSource = new PropertiesExpressionSource();
91 propsSource.setProperties( props );
92 evaluator.addExpressionSource( propsSource );
93
94 String expression = "watch out for the worm in the ${fruit}";
95 String expected = "watch out for the worm in the apple";
96
97 String actual = evaluator.expand( expression );
98 assertEquals( expected, actual );
99 }
100
101 public void testSimpleSystemProperty()
102 throws EvaluatorException
103 {
104 evaluator.addExpressionSource( new SystemPropertyExpressionSource() );
105
106 String userHome = System.getProperty( "user.home" );
107 String expression = "My HOME directory is ${user.home}";
108 String expected = "My HOME directory is " + userHome;
109
110 String actual = evaluator.expand( expression );
111 assertEquals( expected, actual );
112 }
113
114 public void testMultiExpression()
115 throws EvaluatorException
116 {
117 evaluator.addExpressionSource( new SystemPropertyExpressionSource() );
118
119 String userName = System.getProperty( "user.name" );
120 String userHome = System.getProperty( "user.home" );
121 String expression = "${user.name}'s home directory is ${user.home}";
122 String expected = userName + "'s home directory is " + userHome;
123
124 String actual = evaluator.expand( expression );
125 assertEquals( expected, actual );
126 }
127
128
129
130
131
132
133
134 public void testMultiExpressionVersionBug()
135 throws EvaluatorException
136 {
137 Properties props = new Properties();
138 props.setProperty( "prj.ver.maj", "2" );
139 props.setProperty( "prj.ver.min", "0" );
140 props.setProperty( "prj.ver.inc", "4" );
141 props.setProperty( "prj.ver.suf", "-SNAPSHOT" );
142
143 PropertiesExpressionSource propsSource = new PropertiesExpressionSource();
144 propsSource.setProperties( props );
145 evaluator.addExpressionSource( propsSource );
146
147 String expression = "${prj.ver.maj}.${prj.ver.min}.${prj.ver.inc}${prj.ver.suf}";
148 String expected = "2.0.4-SNAPSHOT";
149
150 String actual = evaluator.expand( expression );
151 assertEquals( expected, actual );
152 }
153
154 public void testEscaping()
155 throws EvaluatorException
156 {
157 evaluator.addExpressionSource( new SystemPropertyExpressionSource() );
158
159 String userName = System.getProperty( "user.name" );
160 String userHome = System.getProperty( "user.home" );
161 String expression = "${user.name}'s home directory is ${user.home} (fetched via $${user.home} expression)";
162 String expected = userName + "'s home directory is " + userHome + " (fetched via ${user.home} expression)";
163
164 String actual = evaluator.expand( expression );
165 assertEquals( expected, actual );
166 }
167
168 public void testRecursiveSimple()
169 throws EvaluatorException
170 {
171 PropertiesExpressionSource propsource = new PropertiesExpressionSource();
172 Properties props = new Properties();
173
174
175 props.setProperty( "main.dir", "${target.dir}/classes" );
176 props.setProperty( "target.dir", "./target" );
177
178 propsource.setProperties( props );
179
180 evaluator.addExpressionSource( propsource );
181 evaluator.addExpressionSource( new SystemPropertyExpressionSource() );
182
183 String expression = "My classes directory is ${main.dir}";
184 String expected = "My classes directory is ./target/classes";
185
186 String actual = evaluator.expand( expression );
187 assertEquals( expected, actual );
188 }
189
190 public void testRecursiveCycle()
191 {
192 PropertiesExpressionSource propsource = new PropertiesExpressionSource();
193 Properties props = new Properties();
194
195
196 props.setProperty( "main.dir", "${test.dir}/target/classes" );
197 props.setProperty( "test.dir", "${main.dir}/target/test-classes" );
198
199 propsource.setProperties( props );
200
201 evaluator.addExpressionSource( propsource );
202 evaluator.addExpressionSource( new SystemPropertyExpressionSource() );
203
204 try
205 {
206 evaluator.expand( "My main dir is ${main.dir}" );
207 fail( "Should have thrown an EvaluatorException due to recursive cycle." );
208 }
209 catch ( EvaluatorException e )
210 {
211
212 }
213 }
214 }