001: package org.apache.maven.project.interpolation;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import junit.framework.TestCase;
023: import org.apache.maven.model.Dependency;
024: import org.apache.maven.model.Model;
025: import org.apache.maven.model.Organization;
026: import org.apache.maven.model.Repository;
027: import org.apache.maven.model.Scm;
028:
029: import java.io.IOException;
030: import java.util.Collections;
031: import java.util.Map;
032: import java.util.Properties;
033:
034: /**
035: * @author jdcasey
036: * @version $Id: RegexBasedModelInterpolatorTest.java 549786 2007-06-22 11:34:53Z kenney $
037: */
038: public class RegexBasedModelInterpolatorTest extends TestCase {
039: private Map context;
040:
041: protected void setUp() throws Exception {
042: super .setUp();
043:
044: context = Collections.singletonMap("basedir", "myBasedir");
045: }
046:
047: public void testShouldNotThrowExceptionOnReferenceToNonExistentValue()
048: throws IOException, ModelInterpolationException {
049: Model model = new Model();
050:
051: Scm scm = new Scm();
052: scm.setConnection("${test}/somepath");
053:
054: model.setScm(scm);
055:
056: Model out = new RegexBasedModelInterpolator().interpolate(
057: model, context);
058:
059: assertEquals("${test}/somepath", out.getScm().getConnection());
060: }
061:
062: public void testShouldThrowExceptionOnRecursiveScmConnectionReference()
063: throws IOException {
064: Model model = new Model();
065:
066: Scm scm = new Scm();
067: scm.setConnection("${project.scm.connection}/somepath");
068:
069: model.setScm(scm);
070:
071: try {
072: Model out = new RegexBasedModelInterpolator().interpolate(
073: model, context);
074:
075: fail("The interpolator should not allow self-referencing expressions in POM.");
076: } catch (ModelInterpolationException e) {
077:
078: }
079: }
080:
081: public void testShouldNotThrowExceptionOnReferenceToValueContainingNakedExpression()
082: throws IOException, ModelInterpolationException {
083: Model model = new Model();
084:
085: Scm scm = new Scm();
086: scm.setConnection("${test}/somepath");
087:
088: model.setScm(scm);
089:
090: model.addProperty("test", "test");
091:
092: Model out = new RegexBasedModelInterpolator().interpolate(
093: model, context);
094:
095: assertEquals("test/somepath", out.getScm().getConnection());
096: }
097:
098: public void testShouldInterpolateOrganizationNameCorrectly()
099: throws Exception {
100: String orgName = "MyCo";
101:
102: Model model = new Model();
103: model.setName("${pom.organization.name} Tools");
104:
105: Organization org = new Organization();
106: org.setName(orgName);
107:
108: model.setOrganization(org);
109:
110: Model out = new RegexBasedModelInterpolator().interpolate(
111: model, context);
112:
113: assertEquals(orgName + " Tools", out.getName());
114: }
115:
116: public void testShouldInterpolateDependencyVersionToSetSameAsProjectVersion()
117: throws Exception {
118: Model model = new Model();
119: model.setVersion("3.8.1");
120:
121: Dependency dep = new Dependency();
122: dep.setVersion("${version}");
123:
124: Dependency dep2 = new Dependency();
125: dep2.setVersion("${pom.version}");
126:
127: model.addDependency(dep);
128: model.addDependency(dep2);
129:
130: Model out = new RegexBasedModelInterpolator().interpolate(
131: model, context);
132:
133: assertEquals("3.8.1", ((Dependency) out.getDependencies()
134: .get(0)).getVersion());
135: assertEquals("3.8.1", ((Dependency) out.getDependencies()
136: .get(1)).getVersion());
137: }
138:
139: public void testShouldNotInterpolateDependencyVersionWithInvalidReference()
140: throws Exception {
141: Model model = new Model();
142: model.setVersion("3.8.1");
143:
144: Dependency dep = new Dependency();
145: dep.setVersion("${something}");
146:
147: model.addDependency(dep);
148:
149: /*
150: // This is the desired behaviour, however there are too many crappy poms in the repo and an issue with the
151: // timing of executing the interpolation
152:
153: try
154: {
155: new RegexBasedModelInterpolator().interpolate( model, context );
156: fail( "Should have failed to interpolate with invalid reference" );
157: }
158: catch ( ModelInterpolationException expected )
159: {
160: assertTrue( true );
161: }
162: */
163:
164: Model out = new RegexBasedModelInterpolator().interpolate(
165: model, context);
166:
167: assertEquals("${something}", ((Dependency) out
168: .getDependencies().get(0)).getVersion());
169: }
170:
171: public void testTwoReferences() throws Exception {
172: Model model = new Model();
173: model.setVersion("3.8.1");
174: model.setArtifactId("foo");
175:
176: Dependency dep = new Dependency();
177: dep.setVersion("${artifactId}-${version}");
178:
179: Dependency dep2 = new Dependency();
180: dep2.setVersion("${pom.artifactId}-${pom.version}");
181:
182: model.addDependency(dep);
183: model.addDependency(dep2);
184:
185: Model out = new RegexBasedModelInterpolator().interpolate(
186: model, context);
187:
188: assertEquals("foo-3.8.1", ((Dependency) out.getDependencies()
189: .get(0)).getVersion());
190: assertEquals("foo-3.8.1", ((Dependency) out.getDependencies()
191: .get(1)).getVersion());
192: }
193:
194: public void testBasedir() throws Exception {
195: Model model = new Model();
196: model.setVersion("3.8.1");
197: model.setArtifactId("foo");
198:
199: Repository repository = new Repository();
200:
201: repository.setUrl("file://localhost/${basedir}/temp-repo");
202:
203: model.addRepository(repository);
204:
205: assertNotNull(context.get("basedir"));
206:
207: Model out = new RegexBasedModelInterpolator().interpolate(
208: model, context);
209:
210: assertEquals("file://localhost/myBasedir/temp-repo",
211: ((Repository) out.getRepositories().get(0)).getUrl());
212: }
213:
214: public void testExpressionThatEvaluatesToNullReturnsTheLiteralString()
215: throws Exception {
216: Model model = new Model();
217:
218: Properties modelProperties = new Properties();
219:
220: modelProperties.setProperty("outputDirectory",
221: "${DOES_NOT_EXIST}");
222:
223: model.setProperties(modelProperties);
224:
225: Model out = new RegexBasedModelInterpolator().interpolate(
226: model, context);
227:
228: assertEquals(
229: out.getProperties().getProperty("outputDirectory"),
230: "${DOES_NOT_EXIST}");
231: }
232:
233: public void testPOMExpressionDoesNotUseSystemProperty()
234: throws Exception {
235: Model model = new Model();
236: model.setVersion("1.0");
237:
238: Properties modelProperties = new Properties();
239: modelProperties.setProperty("version", "prop version");
240: modelProperties.setProperty("foo.version", "prop foo.version");
241: modelProperties.setProperty("pom.version", "prop pom.version");
242: modelProperties.setProperty("project.version",
243: "prop project.version");
244:
245: model.setProperties(modelProperties);
246:
247: Dependency dep = new Dependency();
248: model.addDependency(dep);
249:
250: checkDep("prop version", "${version}", model);
251: checkDep("1.0", "${pom.version}", model);
252: checkDep("1.0", "${project.version}", model);
253: checkDep("prop foo.version", "${foo.version}", model);
254: }
255:
256: private void checkDep(String expectedVersion,
257: String depVersionExpr, Model model) throws Exception {
258: ((Dependency) model.getDependencies().get(0))
259: .setVersion(depVersionExpr);
260: Model out = new RegexBasedModelInterpolator().interpolate(
261: model, context);
262: String result = ((Dependency) out.getDependencies().get(0))
263: .getVersion();
264: assertEquals("Expected '" + expectedVersion
265: + "' for version expression '" + depVersionExpr
266: + "', but was '" + result + "'", expectedVersion,
267: result);
268: }
269:
270: }
|