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.DirectoryScanner;
027: import org.apache.tools.ant.Project;
028: import org.apache.tools.ant.taskdefs.Delete;
029: import org.apache.tools.ant.types.FileSet;
030:
031: public class IvyCacheFilesetTest extends TestCase {
032: private File cache;
033:
034: private IvyCacheFileset fileset;
035:
036: private Project project;
037:
038: protected void setUp() throws Exception {
039: createCache();
040: project = new Project();
041: project.setProperty("ivy.settings.file",
042: "test/repositories/ivysettings.xml");
043:
044: fileset = new IvyCacheFileset();
045: fileset.setProject(project);
046: System.setProperty("ivy.cache.dir", cache.getAbsolutePath());
047: }
048:
049: private void createCache() {
050: cache = new File("build/cache");
051: cache.mkdirs();
052: }
053:
054: protected void tearDown() throws Exception {
055: cleanCache();
056: }
057:
058: private void cleanCache() {
059: Delete del = new Delete();
060: del.setProject(new Project());
061: del.setDir(cache);
062: del.execute();
063: }
064:
065: public void testSimple() throws Exception {
066: project.setProperty("ivy.dep.file",
067: "test/java/org/apache/ivy/ant/ivy-simple.xml");
068: fileset.setSetid("simple-setid");
069: fileset.execute();
070: Object ref = project.getReference("simple-setid");
071: assertNotNull(ref);
072: assertTrue(ref instanceof FileSet);
073: FileSet fs = (FileSet) ref;
074: DirectoryScanner directoryScanner = fs
075: .getDirectoryScanner(project);
076: assertEquals(1, directoryScanner.getIncludedFiles().length);
077: assertEquals(getArchiveFileInCache("org1", "mod1.2", "2.0",
078: "mod1.2", "jar", "jar").getAbsolutePath(), new File(
079: directoryScanner.getBasedir(), directoryScanner
080: .getIncludedFiles()[0]).getAbsolutePath());
081: }
082:
083: private File getArchiveFileInCache(String organisation,
084: String module, String revision, String artifact,
085: String type, String ext) {
086: return TestHelper.getArchiveFileInCache(fileset
087: .getIvyInstance(), organisation, module, revision,
088: artifact, type, ext);
089: }
090:
091: private File getArchiveFileInCache(String organisation,
092: String module, String revision, String artifact,
093: String type, String ext, File cache) {
094: return TestHelper.getArchiveFileInCache(fileset
095: .getIvyInstance(), organisation, module, revision,
096: artifact, type, ext);
097: }
098:
099: public void testEmptyConf() throws Exception {
100: project.setProperty("ivy.dep.file",
101: "test/java/org/apache/ivy/ant/ivy-108.xml");
102: fileset.setSetid("emptyconf-setid");
103: fileset.setConf("empty");
104: fileset.execute();
105: Object ref = project.getReference("emptyconf-setid");
106: assertNotNull(ref);
107: assertTrue(ref instanceof FileSet);
108: FileSet fs = (FileSet) ref;
109: DirectoryScanner directoryScanner = fs
110: .getDirectoryScanner(project);
111: assertEquals(0, directoryScanner.getIncludedFiles().length);
112: }
113:
114: public void testFailure() throws Exception {
115: try {
116: project.setProperty("ivy.dep.file",
117: "test/java/org/apache/ivy/ant/ivy-failure.xml");
118: fileset.setSetid("failure-setid");
119: fileset.execute();
120: fail("failure didn't raised an exception with default haltonfailure setting");
121: } catch (BuildException ex) {
122: // ok => should raised an exception
123: }
124: }
125:
126: public void testHaltOnFailure() throws Exception {
127: try {
128: project.setProperty("ivy.dep.file",
129: "test/java/org/apache/ivy/ant/ivy-failure.xml");
130: fileset.setSetid("haltfailure-setid");
131: fileset.setHaltonfailure(false);
132: fileset.execute();
133: } catch (BuildException ex) {
134: fail("failure raised an exception with haltonfailure set to false");
135: }
136: }
137:
138: public void testWithoutPreviousResolveAndNonDefaultCache()
139: throws Exception {
140: File cache2 = new File("build/cache2");
141: cache2.mkdirs();
142:
143: try {
144: project.setProperty("ivy.dep.file",
145: "test/java/org/apache/ivy/ant/ivy-simple.xml");
146: fileset.setSetid("simple-setid");
147: System.setProperty("ivy.cache.dir", cache2
148: .getAbsolutePath());
149: fileset.execute();
150: Object ref = project.getReference("simple-setid");
151: assertNotNull(ref);
152: assertTrue(ref instanceof FileSet);
153: FileSet fs = (FileSet) ref;
154: DirectoryScanner directoryScanner = fs
155: .getDirectoryScanner(project);
156: assertEquals(1, directoryScanner.getIncludedFiles().length);
157: assertEquals(getArchiveFileInCache("org1", "mod1.2", "2.0",
158: "mod1.2", "jar", "jar", cache2).getAbsolutePath(),
159: new File(directoryScanner.getBasedir(),
160: directoryScanner.getIncludedFiles()[0])
161: .getAbsolutePath());
162: } finally {
163: Delete del = new Delete();
164: del.setProject(new Project());
165: del.setDir(cache2);
166: del.execute();
167: }
168: }
169:
170: }
|