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 org.apache.ivy.Ivy;
023: import org.apache.ivy.core.settings.IvySettings;
024: import org.apache.ivy.plugins.resolver.DependencyResolver;
025: import org.apache.ivy.plugins.resolver.IBiblioResolver;
026: import org.apache.ivy.plugins.resolver.IvyRepResolver;
027: import org.apache.tools.ant.BuildException;
028: import org.apache.tools.ant.Project;
029:
030: import junit.framework.TestCase;
031:
032: public class IvyAntSettingsTest extends TestCase {
033: private IvyAntSettings antSettings;
034: private Project project;
035:
036: protected void setUp() throws Exception {
037: project = new Project();
038: project.setProperty("myproperty", "myvalue");
039:
040: antSettings = new IvyAntSettings();
041: antSettings.setProject(project);
042: }
043:
044: private Ivy getIvyInstance() {
045: return antSettings.getConfiguredIvyInstance();
046: }
047:
048: public void testDefault() throws Exception {
049: // by default settings look in the current directory for an ivysettings.xml file...
050: // but Ivy itself has one, and we don't want to use it
051: antSettings.getProject().setProperty("ivy.settings.file",
052: "no/settings/will/use/default.xml");
053: antSettings.execute();
054:
055: IvySettings settings = getIvyInstance().getSettings();
056: assertNotNull(settings.getDefaultResolver());
057:
058: DependencyResolver publicResolver = settings
059: .getResolver("public");
060: assertNotNull(publicResolver);
061: assertTrue(publicResolver instanceof IBiblioResolver);
062: IBiblioResolver ibiblio = (IBiblioResolver) publicResolver;
063: assertTrue(ibiblio.isM2compatible());
064: }
065:
066: public void testDefault14() throws Exception {
067: // by default settings look in the current directory for an ivysettings.xml file...
068: // but Ivy itself has one, and we don't want to use it
069: antSettings.getProject().setProperty("ivy.settings.file",
070: "no/settings/will/use/default.xml");
071: antSettings.getProject().setProperty("ivy.14.compatible",
072: "true");
073: antSettings.execute();
074:
075: IvySettings settings = getIvyInstance().getSettings();
076: assertNotNull(settings.getDefaultResolver());
077:
078: DependencyResolver publicResolver = settings
079: .getResolver("public");
080: assertTrue(publicResolver instanceof IvyRepResolver);
081: }
082:
083: public void testFile() throws Exception {
084: antSettings.setFile(new File(
085: "test/repositories/ivysettings.xml"));
086:
087: antSettings.execute();
088:
089: Ivy ivy = getIvyInstance();
090: assertNotNull(ivy);
091: IvySettings settings = ivy.getSettings();
092: assertNotNull(settings);
093:
094: assertEquals(new File("build/cache"), settings
095: .getDefaultCache());
096: assertEquals(new File("test/repositories/ivysettings.xml")
097: .getAbsolutePath(), settings.getVariables()
098: .getVariable("ivy.settings.file"));
099: assertEquals(new File("test/repositories/ivysettings.xml")
100: .toURL().toExternalForm(), settings.getVariables()
101: .getVariable("ivy.settings.url"));
102: assertEquals(new File("test/repositories").getAbsolutePath(),
103: settings.getVariables().getVariable("ivy.settings.dir"));
104: assertEquals("myvalue", settings.getVariables().getVariable(
105: "myproperty"));
106: }
107:
108: public void testURL() throws Exception {
109: String confUrl = new File("test/repositories/ivysettings.xml")
110: .toURL().toExternalForm();
111: String confDirUrl = new File("test/repositories").toURL()
112: .toExternalForm();
113: if (confDirUrl.endsWith("/")) {
114: confDirUrl = confDirUrl.substring(0,
115: confDirUrl.length() - 1);
116: }
117: antSettings.setUrl(confUrl);
118:
119: antSettings.execute();
120:
121: IvySettings settings = getIvyInstance().getSettings();
122:
123: assertEquals(new File("build/cache"), settings
124: .getDefaultCache());
125: assertEquals(confUrl, settings.getVariables().getVariable(
126: "ivy.settings.url"));
127: assertEquals(confDirUrl, settings.getVariables().getVariable(
128: "ivy.settings.dir"));
129: assertEquals("myvalue", settings.getVariables().getVariable(
130: "myproperty"));
131: }
132:
133: public void testAntProperties() throws Exception {
134: String confUrl = IvyConfigureTest.class.getResource(
135: "ivysettings-test.xml").toExternalForm();
136: antSettings.setUrl(confUrl);
137:
138: antSettings.execute();
139:
140: IvySettings settings = getIvyInstance().getSettings();
141: assertNotNull(settings);
142:
143: assertEquals("myvalue", settings.getVariables().getVariable(
144: "myproperty"));
145: assertEquals("myvalue", settings.getDefaultCache().getName());
146: }
147:
148: public void testOverrideVariables() throws Exception {
149: String confUrl = IvyConfigureTest.class.getResource(
150: "ivysettings-props.xml").toExternalForm();
151: antSettings.setUrl(confUrl);
152:
153: antSettings.execute();
154:
155: IvySettings settings = getIvyInstance().getSettings();
156: assertNotNull(settings);
157:
158: assertEquals("lib/test/[artifact]-[revision].[ext]", settings
159: .getVariables().getVariable("ivy.retrieve.pattern"));
160: }
161:
162: public void testExposeAntProperties() throws Exception {
163: String confUrl = IvyConfigureTest.class.getResource(
164: "ivysettings-props.xml").toExternalForm();
165: antSettings.setUrl(confUrl);
166: antSettings.setId("this.id");
167:
168: antSettings.execute();
169:
170: assertNotNull(getIvyInstance());
171:
172: assertEquals("value", antSettings.getProject().getProperty(
173: "ivy.test.variable"));
174: assertEquals("value", antSettings.getProject().getProperty(
175: "ivy.test.variable.this.id"));
176: }
177:
178: public void testIncludeTwice() throws Exception {
179: // IVY-601
180: antSettings
181: .setFile(new File(
182: "test/java/org/apache/ivy/ant/ivysettings-include-twice.xml"));
183:
184: antSettings.execute();
185:
186: assertNotNull(getIvyInstance());
187: }
188:
189: public void testOverrideTrue() throws Exception {
190: antSettings.setFile(new File(
191: "test/repositories/ivysettings.xml"));
192: antSettings.execute();
193:
194: Ivy ivy = getIvyInstance();
195: assertNotNull(ivy);
196:
197: antSettings = new IvyAntSettings();
198: antSettings.setProject(project);
199: antSettings.setOverride(IvyAntSettings.OVERRIDE_TRUE);
200: antSettings.setFile(new File(
201: "test/repositories/ivysettings.xml"));
202: antSettings.execute();
203: assertNotNull(getIvyInstance());
204:
205: assertTrue(ivy != getIvyInstance());
206: }
207:
208: public void testOverrideFalse() throws Exception {
209: antSettings.setFile(new File(
210: "test/repositories/ivysettings.xml"));
211: antSettings.execute();
212:
213: Ivy ivy = getIvyInstance();
214: assertNotNull(ivy);
215:
216: IvyAntSettings newAntSettings = new IvyAntSettings();
217: newAntSettings.setProject(project);
218: newAntSettings.setOverride(IvyAntSettings.OVERRIDE_FALSE);
219: newAntSettings.setFile(new File(
220: "test/repositories/ivysettings.xml"));
221: newAntSettings.execute();
222:
223: assertTrue(antSettings == project.getReference(newAntSettings
224: .getId()));
225: }
226:
227: public void testOverrideNotAllowed() throws Exception {
228: antSettings.setFile(new File(
229: "test/repositories/ivysettings.xml"));
230: antSettings.execute();
231:
232: Ivy ivy = getIvyInstance();
233: assertNotNull(ivy);
234:
235: antSettings = new IvyAntSettings();
236: antSettings.setProject(project);
237: antSettings.setOverride(IvyAntSettings.OVERRIDE_NOT_ALLOWED);
238: antSettings.setFile(new File(
239: "test/repositories/ivysettings.xml"));
240:
241: try {
242: antSettings.execute();
243: fail("calling settings twice with the same id with "
244: + "override=notallowed should raise an exception");
245: } catch (BuildException e) {
246: assertTrue(e.getMessage().indexOf("notallowed") != -1);
247: assertTrue(e.getMessage().indexOf(antSettings.getId()) != -1);
248: }
249: }
250:
251: public void testInvalidOverride() throws Exception {
252: try {
253: antSettings.setOverride("unknown");
254: fail("settings override with invalid value should raise an exception");
255: } catch (Exception e) {
256: assertTrue(e.getMessage().indexOf("unknown") != -1);
257: }
258: }
259:
260: }
|