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.ivy.TestHelper;
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.Project;
027: import org.apache.tools.ant.taskdefs.Delete;
028: import org.apache.tools.ant.types.Path;
029:
030: public class IvyCachePathTest extends TestCase {
031: private File cache;
032:
033: private IvyCachePath path;
034:
035: private Project project;
036:
037: protected void setUp() throws Exception {
038: createCache();
039: project = new Project();
040: project.setProperty("ivy.settings.file",
041: "test/repositories/ivysettings.xml");
042:
043: path = new IvyCachePath();
044: path.setProject(project);
045: System.setProperty("ivy.cache.dir", cache.getAbsolutePath());
046: }
047:
048: private void createCache() {
049: cache = new File("build/cache");
050: cache.mkdirs();
051: }
052:
053: protected void tearDown() throws Exception {
054: cleanCache();
055: }
056:
057: private void cleanCache() {
058: Delete del = new Delete();
059: del.setProject(new Project());
060: del.setDir(cache);
061: del.execute();
062: }
063:
064: public void testSimple() throws Exception {
065: project.setProperty("ivy.dep.file",
066: "test/java/org/apache/ivy/ant/ivy-simple.xml");
067: path.setPathid("simple-pathid");
068: path.execute();
069: Object ref = project.getReference("simple-pathid");
070: assertNotNull(ref);
071: assertTrue(ref instanceof Path);
072: Path p = (Path) ref;
073: assertEquals(1, p.size());
074: assertEquals(getArchiveFileInCache("org1", "mod1.2", "2.0",
075: "mod1.2", "jar", "jar").getAbsolutePath(), new File(p
076: .list()[0]).getAbsolutePath());
077: }
078:
079: public void testInline1() throws Exception {
080: // we first resolve another ivy file
081: IvyResolve resolve = new IvyResolve();
082: resolve.setProject(project);
083: resolve.setFile(new File(
084: "test/java/org/apache/ivy/ant/ivy-latest.xml"));
085: resolve.execute();
086:
087: assertTrue(getArchiveFileInCache("org1", "mod1.2", "2.2",
088: "mod1.2", "jar", "jar").exists());
089:
090: // then we resolve a dependency directly
091: path.setOrganisation("org1");
092: path.setModule("mod1.2");
093: path.setRevision("2.0");
094: path.setInline(true);
095: path.setPathid("simple-pathid");
096: path.execute();
097: Object ref = project.getReference("simple-pathid");
098: assertNotNull(ref);
099: assertTrue(ref instanceof Path);
100: Path p = (Path) ref;
101: assertEquals(1, p.size());
102: assertEquals(getArchiveFileInCache("org1", "mod1.2", "2.0",
103: "mod1.2", "jar", "jar").getAbsolutePath(), new File(p
104: .list()[0]).getAbsolutePath());
105: }
106:
107: public void testInline2() throws Exception {
108: // we first resolve a dependency directly
109: path.setOrganisation("org1");
110: path.setModule("mod1.2");
111: path.setRevision("2.0");
112: path.setInline(true);
113: path.setPathid("simple-pathid");
114: path.execute();
115: Object ref = project.getReference("simple-pathid");
116: assertNotNull(ref);
117: assertTrue(ref instanceof Path);
118: Path p = (Path) ref;
119: assertEquals(1, p.size());
120: assertEquals(getArchiveFileInCache("org1", "mod1.2", "2.0",
121: "mod1.2", "jar", "jar").getAbsolutePath(), new File(p
122: .list()[0]).getAbsolutePath());
123:
124: // we then resolve another ivy file
125: IvyResolve resolve = new IvyResolve();
126: resolve.setProject(project);
127: resolve.setFile(new File(
128: "test/java/org/apache/ivy/ant/ivy-latest.xml"));
129: resolve.execute();
130:
131: assertTrue(getArchiveFileInCache("org1", "mod1.2", "2.2",
132: "mod1.2", "jar", "jar").exists());
133: }
134:
135: public void testEmptyConf() throws Exception {
136: project.setProperty("ivy.dep.file",
137: "test/java/org/apache/ivy/ant/ivy-108.xml");
138: path.setPathid("emptyconf-pathid");
139: path.setConf("empty");
140: path.execute();
141: Object ref = project.getReference("emptyconf-pathid");
142: assertNotNull(ref);
143: assertTrue(ref instanceof Path);
144: Path p = (Path) ref;
145: assertEquals(0, p.size());
146: }
147:
148: public void testFailure() throws Exception {
149: try {
150: project.setProperty("ivy.dep.file",
151: "test/java/org/apache/ivy/ant/ivy-failure.xml");
152: path.setPathid("failure-pathid");
153: path.execute();
154: fail("failure didn't raised an exception with default haltonfailure setting");
155: } catch (BuildException ex) {
156: // ok => should raised an exception
157: }
158: }
159:
160: public void testHaltOnFailure() throws Exception {
161: try {
162: project.setProperty("ivy.dep.file",
163: "test/java/org/apache/ivy/ant/ivy-failure.xml");
164: path.setPathid("haltfailure-pathid");
165: path.setHaltonfailure(false);
166: path.execute();
167: } catch (BuildException ex) {
168: fail("failure raised an exception with haltonfailure set to false");
169: }
170: }
171:
172: public void testWithResolveId() throws Exception {
173: IvyResolve resolve = new IvyResolve();
174: resolve.setProject(project);
175: resolve.setFile(new File(
176: "test/java/org/apache/ivy/ant/ivy-simple.xml"));
177: resolve.setResolveId("withResolveId");
178: resolve.execute();
179:
180: // resolve another ivy file
181: resolve = new IvyResolve();
182: resolve.setProject(project);
183: resolve.setFile(new File(
184: "test/java/org/apache/ivy/ant/ivy-latest.xml"));
185: resolve.execute();
186:
187: path.setResolveId("withResolveId");
188: path.setPathid("withresolveid-pathid");
189: path.execute();
190:
191: Object ref = project.getReference("withresolveid-pathid");
192: assertNotNull(ref);
193: assertTrue(ref instanceof Path);
194: Path p = (Path) ref;
195: assertEquals(1, p.size());
196: assertEquals(getArchiveFileInCache("org1", "mod1.2", "2.0",
197: "mod1.2", "jar", "jar").getAbsolutePath(), new File(p
198: .list()[0]).getAbsolutePath());
199: }
200:
201: public void testWithResolveIdWithoutResolve() throws Exception {
202: Project otherProject = new Project();
203: otherProject.setProperty("ivy.settings.file",
204: "test/repositories/ivysettings.xml");
205:
206: IvyResolve resolve = new IvyResolve();
207: resolve.setProject(otherProject);
208: resolve.setFile(new File(
209: "test/java/org/apache/ivy/ant/ivy-simple.xml"));
210: resolve.setResolveId("withResolveId");
211: resolve.execute();
212:
213: // resolve another ivy file
214: resolve = new IvyResolve();
215: resolve.setProject(project);
216: resolve.setFile(new File(
217: "test/java/org/apache/ivy/ant/ivy-latest.xml"));
218: resolve.execute();
219:
220: path.setResolveId("withResolveId");
221: path.setPathid("withresolveid-pathid");
222: path.setFile(new File(
223: "test/java/org/apache/ivy/ant/ivy-simple.xml"));
224: path.execute();
225:
226: Object ref = project.getReference("withresolveid-pathid");
227: assertNotNull(ref);
228: assertTrue(ref instanceof Path);
229: Path p = (Path) ref;
230: assertEquals(1, p.size());
231: assertEquals(getArchiveFileInCache("org1", "mod1.2", "2.0",
232: "mod1.2", "jar", "jar").getAbsolutePath(), new File(p
233: .list()[0]).getAbsolutePath());
234: }
235:
236: public void testWithResolveIdAndMissingConfs() throws Exception {
237: Project otherProject = new Project();
238: otherProject.setProperty("ivy.settings.file",
239: "test/repositories/ivysettings.xml");
240:
241: IvyResolve resolve = new IvyResolve();
242: resolve.setProject(otherProject);
243: resolve.setFile(new File(
244: "test/java/org/apache/ivy/ant/ivy-multiconf.xml"));
245: resolve.setResolveId("testWithResolveIdAndMissingConfs");
246: resolve.setConf("default");
247: resolve.execute();
248:
249: // resolve another ivy file
250: resolve = new IvyResolve();
251: resolve.setProject(project);
252: resolve.setFile(new File(
253: "test/java/org/apache/ivy/ant/ivy-latest.xml"));
254: resolve.execute();
255:
256: project.setProperty("ivy.dep.file",
257: "test/java/org/apache/ivy/ant/ivy-multiconf.xml");
258:
259: path.setResolveId("testWithResolveIdAndMissingConfs");
260: path.setPathid("withresolveid-pathid");
261: path.setConf("default,compile");
262: path.setFile(new File(
263: "test/java/org/apache/ivy/ant/ivy-multiconf.xml"));
264: path.execute();
265: }
266:
267: private File getArchiveFileInCache(String organisation,
268: String module, String revision, String artifact,
269: String type, String ext) {
270: return TestHelper.getArchiveFileInCache(path.getIvyInstance(),
271: organisation, module, revision, artifact, type, ext);
272: }
273: }
|