001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.ant;
019:
020: import java.io.File;
021:
022: import junit.framework.TestCase;
023:
024: import org.apache.tools.ant.BuildException;
025: import org.apache.tools.ant.Project;
026: import org.apache.tools.ant.taskdefs.Delete;
027:
028: public class IvyArtifactPropertyTest extends TestCase {
029: private File cache;
030:
031: private IvyArtifactProperty prop;
032:
033: private Project project;
034:
035: protected void setUp() throws Exception {
036: createCache();
037: project = new Project();
038: project.setProperty("ivy.settings.file",
039: "test/repositories/ivysettings.xml");
040:
041: prop = new IvyArtifactProperty();
042: prop.setProject(project);
043: System.setProperty("ivy.cache.dir", cache.getAbsolutePath());
044: }
045:
046: private void createCache() {
047: cache = new File("build/cache");
048: cache.mkdirs();
049: }
050:
051: protected void tearDown() throws Exception {
052: cleanCache();
053: }
054:
055: private void cleanCache() {
056: Delete del = new Delete();
057: del.setProject(new Project());
058: del.setDir(cache);
059: del.execute();
060: }
061:
062: public void testSimple() throws Exception {
063: project.setProperty("ivy.dep.file",
064: "test/java/org/apache/ivy/ant/ivy-simple.xml");
065: prop.setName("[module].[artifact]-[revision]");
066: prop
067: .setValue("${cache.dir}/[module]/[artifact]-[revision].[type]");
068: prop.execute();
069: String val = project.getProperty("mod1.2.mod1.2-2.0");
070: assertNotNull(val);
071: assertEquals("build/cache/mod1.2/mod1.2-2.0.jar", val);
072: }
073:
074: public void testWithResolveId() throws Exception {
075: IvyResolve resolve = new IvyResolve();
076: resolve.setProject(project);
077: resolve.setFile(new File(
078: "test/java/org/apache/ivy/ant/ivy-simple.xml"));
079: resolve.setResolveId("abc");
080: resolve.execute();
081:
082: // resolve another ivy file
083: resolve = new IvyResolve();
084: resolve.setProject(project);
085: resolve.setFile(new File(
086: "test/java/org/apache/ivy/ant/ivy-latest.xml"));
087: resolve.execute();
088:
089: prop.setName("[module].[artifact]-[revision]");
090: prop
091: .setValue("${cache.dir}/[module]/[artifact]-[revision].[type]");
092: prop.setResolveId("abc");
093: prop.execute();
094:
095: String val = project.getProperty("mod1.2.mod1.2-2.0");
096: assertNotNull(val);
097: assertEquals("build/cache/mod1.2/mod1.2-2.0.jar", val);
098: }
099:
100: public void testWithResolveIdWithoutResolve() throws Exception {
101: try {
102: prop.setName("[module].[artifact]-[revision]");
103: prop
104: .setValue("${cache.dir}/[module]/[artifact]-[revision].[type]");
105: prop.setResolveId("abc");
106: prop.execute();
107: fail("Task should have failed because no resolve was performed!");
108: } catch (BuildException e) {
109: // this is expected!
110: }
111: }
112: }
|