001: package org.apache.maven.project.injection;
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.DependencyManagement;
025: import org.apache.maven.model.Exclusion;
026: import org.apache.maven.model.Model;
027:
028: import java.util.List;
029:
030: /**
031: * @author jdcasey
032: */
033: public class DefaultModelDefaultsInjectorTest extends TestCase {
034: public void testShouldConstructWithNoParams() {
035: new DefaultModelDefaultsInjector();
036: }
037:
038: public void testShouldMergeManagedDependencyOfTypeEJBToDependencyList() {
039: Model model = new Model();
040:
041: Dependency managedDep = new Dependency();
042:
043: managedDep.setGroupId("group");
044: managedDep.setArtifactId("artifact");
045: managedDep.setVersion("1.0");
046: managedDep.setType("ejb");
047:
048: DependencyManagement depMgmt = new DependencyManagement();
049:
050: depMgmt.addDependency(managedDep);
051:
052: model.setDependencyManagement(depMgmt);
053:
054: Dependency dep = new Dependency();
055:
056: dep.setGroupId("group");
057: dep.setArtifactId("artifact");
058:
059: // looks like groupId:artifactId:type is the minimum for identification, where
060: // type is defaulted to "jar".
061: dep.setType("ejb");
062:
063: model.addDependency(dep);
064:
065: new DefaultModelDefaultsInjector().injectDefaults(model);
066:
067: List resultingDeps = model.getDependencies();
068:
069: assertEquals(1, resultingDeps.size());
070:
071: Dependency result = (Dependency) resultingDeps.get(0);
072:
073: assertEquals("1.0", result.getVersion());
074: }
075:
076: public void testShouldSucceedInMergingDependencyWithDependency() {
077: Model model = new Model();
078:
079: Dependency dep = new Dependency();
080: dep.setGroupId("myGroup");
081: dep.setArtifactId("myArtifact");
082:
083: model.addDependency(dep);
084:
085: Dependency def = new Dependency();
086: def.setGroupId(dep.getGroupId());
087: def.setArtifactId(dep.getArtifactId());
088: def.setVersion("1.0.1");
089: def.setScope("scope");
090:
091: DependencyManagement depMgmt = new DependencyManagement();
092:
093: depMgmt.addDependency(def);
094:
095: model.setDependencyManagement(depMgmt);
096:
097: new DefaultModelDefaultsInjector().injectDefaults(model);
098:
099: List deps = model.getDependencies();
100: assertEquals(1, deps.size());
101:
102: Dependency result = (Dependency) deps.get(0);
103: assertEquals(def.getVersion(), result.getVersion());
104: }
105:
106: public void testShouldMergeDependencyExclusionsFromDefaultsToDependency() {
107: Model model = new Model();
108:
109: Dependency dep = new Dependency();
110: dep.setGroupId("myGroup");
111: dep.setArtifactId("myArtifact");
112:
113: model.addDependency(dep);
114:
115: Dependency def = new Dependency();
116: def.setGroupId(dep.getGroupId());
117: def.setArtifactId(dep.getArtifactId());
118: def.setVersion("1.0.1");
119: def.setScope("scope");
120:
121: Exclusion exc = new Exclusion();
122: exc.setArtifactId("mydep");
123: exc.setGroupId("mygrp");
124:
125: def.addExclusion(exc);
126:
127: DependencyManagement depMgmt = new DependencyManagement();
128:
129: depMgmt.addDependency(def);
130:
131: model.setDependencyManagement(depMgmt);
132:
133: new DefaultModelDefaultsInjector().injectDefaults(model);
134:
135: List deps = model.getDependencies();
136: assertEquals(1, deps.size());
137:
138: Dependency result = (Dependency) deps.get(0);
139: assertEquals(def.getVersion(), result.getVersion());
140:
141: List resultExclusions = result.getExclusions();
142: assertNotNull(resultExclusions);
143: assertEquals(1, resultExclusions.size());
144:
145: Exclusion resultExclusion = (Exclusion) resultExclusions.get(0);
146: assertEquals("mydep", resultExclusion.getArtifactId());
147: assertEquals("mygrp", resultExclusion.getGroupId());
148: }
149:
150: public void testShouldMergeDefaultUrlAndArtifactWhenDependencyDoesntSupplyVersion() {
151: Model model = new Model();
152:
153: Dependency dep = new Dependency();
154: dep.setGroupId("myGroup");
155: dep.setArtifactId("myArtifact");
156:
157: model.addDependency(dep);
158:
159: Dependency def = new Dependency();
160: def.setGroupId(dep.getGroupId());
161: def.setArtifactId(dep.getArtifactId());
162: def.setVersion("1.0.1");
163:
164: DependencyManagement depMgmt = new DependencyManagement();
165:
166: depMgmt.addDependency(def);
167:
168: model.setDependencyManagement(depMgmt);
169:
170: new DefaultModelDefaultsInjector().injectDefaults(model);
171:
172: List deps = model.getDependencies();
173: assertEquals(1, deps.size());
174:
175: Dependency result = (Dependency) deps.get(0);
176: assertEquals(def.getVersion(), result.getVersion());
177: }
178:
179: public void testShouldNotMergeDefaultUrlOrArtifactWhenDependencySuppliesVersion() {
180: Model model = new Model();
181:
182: Dependency dep = new Dependency();
183: dep.setGroupId("myGroup");
184: dep.setArtifactId("myArtifact");
185: dep.setVersion("1.0.1");
186:
187: model.addDependency(dep);
188:
189: Dependency def = new Dependency();
190: def.setGroupId(dep.getGroupId());
191: def.setArtifactId(dep.getArtifactId());
192:
193: DependencyManagement depMgmt = new DependencyManagement();
194:
195: depMgmt.addDependency(def);
196:
197: model.setDependencyManagement(depMgmt);
198:
199: new DefaultModelDefaultsInjector().injectDefaults(model);
200:
201: List deps = model.getDependencies();
202: assertEquals(1, deps.size());
203:
204: Dependency result = (Dependency) deps.get(0);
205: assertEquals(dep.getVersion(), result.getVersion());
206: }
207:
208: public void testShouldMergeDefaultPropertiesWhenDependencyDoesntSupplyProperties() {
209: Model model = new Model();
210:
211: Dependency dep = new Dependency();
212: dep.setGroupId("myGroup");
213: dep.setArtifactId("myArtifact");
214: dep.setVersion("1.0.1");
215:
216: model.addDependency(dep);
217:
218: Dependency def = new Dependency();
219: def.setGroupId(dep.getGroupId());
220: def.setArtifactId(dep.getArtifactId());
221:
222: DependencyManagement depMgmt = new DependencyManagement();
223:
224: depMgmt.addDependency(def);
225:
226: model.setDependencyManagement(depMgmt);
227:
228: new DefaultModelDefaultsInjector().injectDefaults(model);
229:
230: List deps = model.getDependencies();
231: assertEquals(1, deps.size());
232: }
233:
234: public void testShouldNotMergeDefaultPropertiesWhenDependencySuppliesProperties() {
235: Model model = new Model();
236:
237: Dependency dep = new Dependency();
238: dep.setGroupId("myGroup");
239: dep.setArtifactId("myArtifact");
240: dep.setVersion("1.0.1");
241:
242: model.addDependency(dep);
243:
244: Dependency def = new Dependency();
245: def.setGroupId(dep.getGroupId());
246: def.setArtifactId(dep.getArtifactId());
247:
248: DependencyManagement depMgmt = new DependencyManagement();
249:
250: depMgmt.addDependency(def);
251:
252: model.setDependencyManagement(depMgmt);
253:
254: new DefaultModelDefaultsInjector().injectDefaults(model);
255:
256: List deps = model.getDependencies();
257: assertEquals(1, deps.size());
258: }
259:
260: public void testShouldMergeDefaultScopeWhenDependencyDoesntSupplyScope() {
261: Model model = new Model();
262:
263: Dependency dep = new Dependency();
264: dep.setGroupId("myGroup");
265: dep.setArtifactId("myArtifact");
266: dep.setVersion("1.0.1");
267: dep.setScope("scope");
268:
269: model.addDependency(dep);
270:
271: Dependency def = new Dependency();
272: def.setGroupId(dep.getGroupId());
273: def.setArtifactId(dep.getArtifactId());
274:
275: DependencyManagement depMgmt = new DependencyManagement();
276:
277: depMgmt.addDependency(def);
278:
279: model.setDependencyManagement(depMgmt);
280:
281: new DefaultModelDefaultsInjector().injectDefaults(model);
282:
283: List deps = model.getDependencies();
284: assertEquals(1, deps.size());
285:
286: Dependency result = (Dependency) deps.get(0);
287:
288: assertEquals("scope", result.getScope());
289: }
290:
291: public void testShouldNotMergeDefaultScopeWhenDependencySuppliesScope() {
292: Model model = new Model();
293:
294: Dependency dep = new Dependency();
295: dep.setGroupId("myGroup");
296: dep.setArtifactId("myArtifact");
297: dep.setVersion("1.0.1");
298: dep.setScope("scope");
299:
300: model.addDependency(dep);
301:
302: Dependency def = new Dependency();
303: def.setGroupId(dep.getGroupId());
304: def.setArtifactId(dep.getArtifactId());
305: def.setScope("default");
306:
307: DependencyManagement depMgmt = new DependencyManagement();
308:
309: depMgmt.addDependency(def);
310:
311: model.setDependencyManagement(depMgmt);
312:
313: new DefaultModelDefaultsInjector().injectDefaults(model);
314:
315: List deps = model.getDependencies();
316: assertEquals(1, deps.size());
317:
318: Dependency result = (Dependency) deps.get(0);
319: assertEquals("scope", result.getScope());
320: }
321:
322: public void testShouldRejectDependencyWhereNoVersionIsFoundAfterDefaultsInjection() {
323: Model model = new Model();
324:
325: Dependency dep = new Dependency();
326: dep.setGroupId("myGroup");
327: dep.setArtifactId("myArtifact");
328:
329: model.addDependency(dep);
330:
331: Dependency def = new Dependency();
332: def.setGroupId(dep.getGroupId());
333: def.setArtifactId(dep.getArtifactId());
334:
335: DependencyManagement depMgmt = new DependencyManagement();
336:
337: depMgmt.addDependency(def);
338:
339: model.setDependencyManagement(depMgmt);
340:
341: // try
342: // {
343: new DefaultModelDefaultsInjector().injectDefaults(model);
344: Dependency dependency = (Dependency) model.getDependencies()
345: .get(0);
346: assertNull("check version is null", dependency.getVersion());
347: // fail("Should fail to validate dependency without a version.");
348: // }
349: // catch ( IllegalStateException e )
350: // {
351: // // should throw when it detects a missing version in the test dependency.
352: // }
353: }
354:
355: }
|