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;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.net.MalformedURLException;
023: import java.text.ParseException;
024: import java.util.ArrayList;
025: import java.util.Collection;
026:
027: import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor;
028: import org.apache.ivy.core.module.id.ModuleRevisionId;
029: import org.apache.ivy.core.report.ResolveReport;
030: import org.apache.ivy.core.settings.IvySettings;
031: import org.apache.ivy.plugins.repository.file.FileResource;
032: import org.apache.ivy.plugins.resolver.DependencyResolver;
033: import org.apache.ivy.plugins.resolver.util.ResolvedResource;
034:
035: /**
036: * Fixture easing the development of tests requiring to set up a simple repository with some
037: * modules, using micro ivy format to describe the repository. <br/> Example of use:
038: *
039: * <pre>
040: * public class MyTest extends TestCase {
041: * private TestFixture fixture;
042: *
043: * protected void setUp() throws Exception {
044: * fixture = new TestFixture();
045: * // additional setup here
046: * }
047: *
048: * protected void tearDown() throws Exception {
049: * fixture.clean();
050: * }
051: *
052: * public void testXXX() throws Exception {
053: * fixture
054: * .addMD("#A;1-> { #B;[1.5,1.6] #C;2.5 }")
055: * .addMD("#B;1.5->#D;2.0")
056: * .addMD("#B;1.6->#D;2.0")
057: * .addMD("#C;2.5->#D;[1.0,1.6]")
058: * .addMD("#D;1.5").addMD("#D;1.6").addMD("#D;2.0")
059: * .init();
060: * ResolveReport r = fixture.resolve("#A;1");
061: * // assertions go here
062: * }
063: * }
064: * </pre>
065: */
066: public class TestFixture {
067:
068: private Collection mds = new ArrayList();
069: private Ivy ivy;
070:
071: public TestFixture() {
072: try {
073: this .ivy = new Ivy();
074: IvySettings settings = new IvySettings();
075: settings.defaultInit();
076: ivy.setSettings(settings);
077: TestHelper.loadTestSettings(ivy.getSettings());
078: ivy.bind();
079: } catch (Exception e) {
080: throw new RuntimeException(e);
081: }
082: }
083:
084: public TestFixture addMD(String microIvy) {
085: mds.add(TestHelper.parseMicroIvyDescriptor(microIvy));
086: return this ;
087: }
088:
089: public TestFixture init() throws IOException {
090: TestHelper.fillRepository(getTestRepository(), mds);
091: return this ;
092: }
093:
094: private DependencyResolver getTestRepository() {
095: return ivy.getSettings().getResolver("test");
096: }
097:
098: public IvySettings getSettings() {
099: return ivy.getSettings();
100: }
101:
102: public Ivy getIvy() {
103: return ivy;
104: }
105:
106: public void clean() {
107: TestHelper.cleanTest();
108: }
109:
110: public File getIvyFile(String mrid) {
111: ResolvedResource r = getTestRepository().findIvyFileRef(
112: new DefaultDependencyDescriptor(ModuleRevisionId
113: .parse(mrid), false),
114: TestHelper.newResolveData(getSettings()));
115: if (r == null) {
116: throw new IllegalStateException("module not found: " + mrid);
117: }
118: return ((FileResource) r.getResource()).getFile();
119: }
120:
121: public ResolveReport resolve(String mrid)
122: throws MalformedURLException, ParseException, IOException {
123: return ivy.resolve(getIvyFile(mrid).toURL(), TestHelper
124: .newResolveOptions(getSettings()));
125: }
126:
127: }
|