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.plugins.resolver;
019:
020: import java.io.File;
021: import java.util.Arrays;
022: import java.util.GregorianCalendar;
023:
024: import junit.framework.TestCase;
025:
026: import org.apache.ivy.core.cache.DefaultResolutionCacheManager;
027: import org.apache.ivy.core.event.EventManager;
028: import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor;
029: import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
030: import org.apache.ivy.core.module.id.ModuleRevisionId;
031: import org.apache.ivy.core.resolve.ResolveData;
032: import org.apache.ivy.core.resolve.ResolveEngine;
033: import org.apache.ivy.core.resolve.ResolveOptions;
034: import org.apache.ivy.core.resolve.ResolvedModuleRevision;
035: import org.apache.ivy.core.settings.IvySettings;
036: import org.apache.ivy.core.settings.XmlSettingsParser;
037: import org.apache.ivy.core.sort.SortEngine;
038:
039: /**
040: * Test for DualResolver
041: */
042: public class DualResolverTest extends AbstractDependencyResolverTest {
043: private IvySettings _settings;
044:
045: private ResolveEngine _engine;
046:
047: private ResolveData _data;
048:
049: private File _cache;
050:
051: protected void setUp() throws Exception {
052: _settings = new IvySettings();
053: _engine = new ResolveEngine(_settings, new EventManager(),
054: new SortEngine(_settings));
055: _cache = new File("build/cache");
056: _data = new ResolveData(_engine, new ResolveOptions());
057: _cache.mkdirs();
058: _settings.setDefaultCache(_cache);
059: }
060:
061: public void testFromConf() throws Exception {
062: new XmlSettingsParser(_settings).parse(DualResolverTest.class
063: .getResource("dualresolverconf.xml"));
064:
065: DependencyResolver resolver = _settings.getResolver("dualok");
066: assertNotNull(resolver);
067: assertTrue(resolver instanceof DualResolver);
068: DualResolver dual = (DualResolver) resolver;
069: assertNotNull(dual.getIvyResolver());
070: assertEquals("ivy", dual.getIvyResolver().getName());
071: assertNotNull(dual.getArtifactResolver());
072: assertEquals("artifact", dual.getArtifactResolver().getName());
073:
074: resolver = _settings.getResolver("dualnotenough");
075: assertNotNull(resolver);
076: assertTrue(resolver instanceof DualResolver);
077: dual = (DualResolver) resolver;
078: assertNotNull(dual.getIvyResolver());
079: assertNull(dual.getArtifactResolver());
080: }
081:
082: public void testFromBadConf() throws Exception {
083: try {
084: new XmlSettingsParser(_settings)
085: .parse(DualResolverTest.class
086: .getResource("dualresolverconf-bad.xml"));
087: fail("bad dual resolver configuration should raise exception");
088: } catch (Exception ex) {
089: // ok -> bad conf has raised an exception
090: }
091: }
092:
093: public void testBad() throws Exception {
094: DualResolver dual = new DualResolver();
095: dual.setIvyResolver(new IBiblioResolver());
096: DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(
097: ModuleRevisionId.newInstance("org", "mod", "rev"),
098: false);
099: try {
100: dual.getDependency(dd, _data);
101: fail("bad dual resolver configuration should raise exception");
102: } catch (Exception ex) {
103: // ok -> should have raised an exception
104: }
105: }
106:
107: public void testResolve() throws Exception {
108: DualResolver dual = new DualResolver();
109: MockResolver ivyResolver = MockResolver.buildMockResolver(
110: "ivy", true, new GregorianCalendar(2005, 1, 20)
111: .getTime());
112: MockResolver artifactResolver = MockResolver.buildMockResolver(
113: "artifact", false, new GregorianCalendar(2005, 1, 20)
114: .getTime());
115: dual.setIvyResolver(ivyResolver);
116: dual.setArtifactResolver(artifactResolver);
117: DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(
118: ModuleRevisionId.newInstance("org", "mod", "rev"),
119: false);
120: ResolvedModuleRevision rmr = dual.getDependency(dd, _data);
121:
122: assertNotNull(rmr);
123: assertEquals(dual, rmr.getArtifactResolver());
124: assertEquals(Arrays.asList(new DependencyDescriptor[] { dd }),
125: ivyResolver.askedDeps);
126: assertTrue(artifactResolver.askedDeps.isEmpty());
127: }
128:
129: public void testResolveFromArtifact() throws Exception {
130: DualResolver dual = new DualResolver();
131: MockResolver ivyResolver = MockResolver.buildMockResolver(
132: "ivy", false, new GregorianCalendar(2005, 1, 20)
133: .getTime());
134: MockResolver artifactResolver = MockResolver.buildMockResolver(
135: "artifact", true, new GregorianCalendar(2005, 1, 20)
136: .getTime());
137: dual.setIvyResolver(ivyResolver);
138: dual.setArtifactResolver(artifactResolver);
139: DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(
140: ModuleRevisionId.newInstance("org", "mod", "rev"),
141: false);
142: ResolvedModuleRevision rmr = dual.getDependency(dd, _data);
143:
144: assertNotNull(rmr);
145: assertEquals(artifactResolver, rmr.getResolver());
146: assertEquals(Arrays.asList(new DependencyDescriptor[] { dd }),
147: ivyResolver.askedDeps);
148: assertEquals(Arrays.asList(new DependencyDescriptor[] { dd }),
149: artifactResolver.askedDeps);
150: }
151:
152: public void testResolveFail() throws Exception {
153: DualResolver dual = new DualResolver();
154: MockResolver ivyResolver = MockResolver.buildMockResolver(
155: "ivy", false, new GregorianCalendar(2005, 1, 20)
156: .getTime());
157: MockResolver artifactResolver = MockResolver.buildMockResolver(
158: "artifact", false, new GregorianCalendar(2005, 1, 20)
159: .getTime());
160: dual.setIvyResolver(ivyResolver);
161: dual.setArtifactResolver(artifactResolver);
162: DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(
163: ModuleRevisionId.newInstance("org", "mod", "rev"),
164: false);
165: ResolvedModuleRevision rmr = dual.getDependency(dd, _data);
166:
167: assertNull(rmr);
168: assertEquals(Arrays.asList(new DependencyDescriptor[] { dd }),
169: ivyResolver.askedDeps);
170: assertEquals(Arrays.asList(new DependencyDescriptor[] { dd }),
171: artifactResolver.askedDeps);
172: }
173: }
|