/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.apache.ivy; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.regex.Matcher; import java.util.regex.Pattern; import junit.framework.Assert; import org.apache.ivy.core.cache.DefaultRepositoryCacheManager; import org.apache.ivy.core.event.EventManager; import org.apache.ivy.core.module.descriptor.Artifact; import org.apache.ivy.core.module.descriptor.DefaultArtifact; import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor; import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor; import org.apache.ivy.core.module.descriptor.ModuleDescriptor; import org.apache.ivy.core.module.id.ModuleRevisionId; import org.apache.ivy.core.resolve.ResolveData; import org.apache.ivy.core.resolve.ResolveEngine; import org.apache.ivy.core.resolve.ResolveOptions; import org.apache.ivy.core.settings.IvySettings; import org.apache.ivy.core.sort.SortEngine; import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorWriter; import org.apache.ivy.plugins.resolver.DependencyResolver; import org.apache.ivy.plugins.resolver.FileSystemResolver; import org.apache.ivy.util.FileUtil; public class TestHelper { public static DefaultArtifact newArtifact(String organisation, String module, String revision, String artifact, String type, String ext) { return new DefaultArtifact(ModuleRevisionId.newInstance(organisation, module, revision), new Date(), artifact, type, ext); } public static File getArchiveFileInCache(Ivy ivy, String mrid, String artifactName, String type, String ext) { DefaultArtifact artifact = new DefaultArtifact(ModuleRevisionId.parse(mrid), new Date(), artifactName, type, ext); return getRepositoryCacheManager(ivy, artifact.getModuleRevisionId()) .getArchiveFileInCache(artifact); } public static File getArchiveFileInCache(Ivy ivy, String organisation, String module, String revision, String artifactName, String type, String ext) { DefaultArtifact artifact = newArtifact(organisation, module, revision, artifactName, type, ext); return getRepositoryCacheManager(ivy, artifact.getModuleRevisionId()) .getArchiveFileInCache(artifact); } public static DefaultRepositoryCacheManager getRepositoryCacheManager(Ivy ivy, ModuleRevisionId id) { // WARN: this doesn't work if the resolver registered is a compound resolver (chain or dual) // and a sub resolver doesn't use the same cache manager as the parent return (DefaultRepositoryCacheManager) ivy.getSettings().getResolver(id) .getRepositoryCacheManager(); } /** * Assertion utility methods to test if a collection of {@link ModuleRevisionId} matches a given * expected set of mrids. *
* Expected mrids is given as a String of comma separated string representations of
* {@link ModuleRevisionId}.
*
* @param expectedMrids
* the expected set of mrids
* @param mrids
* the3 mrids to test
*/
public static void assertModuleRevisionIds(String expectedMrids,
Collection/*
* Examples:
*
*
* #A;1
*
*
*
*
*
* #A;2-> #B;[1.0,1.5]
*
*
*
*
*
* #A;3-> { #B;[1.0,1.5] #C;[2.0,2.5] }
*
*
*
* When finished you should call {@link #cleanTestRepository()} *
*/ public static FileSystemResolver newTestRepository() { FileSystemResolver testRepository = new FileSystemResolver(); testRepository.setName("test"); String testRepoDir = new File("build/test/test-repo").getAbsolutePath(); testRepository.addIvyPattern(testRepoDir + "/[organisation]/[module]/[revision]/[artifact].[ext]"); testRepository.addArtifactPattern(testRepoDir + "/[organisation]/[module]/[revision]/[artifact].[ext]"); return testRepository; } /** * Cleans up the test repository. * * @see #newTestRepository() */ public static void cleanTestRepository() { FileUtil.forceDelete(new File("build/test/test-repo")); } /** * Cleans up the test repository and cache. * * @see #newTestSettings() */ public static void cleanTest() { cleanTestRepository(); FileUtil.forceDelete(new File("build/test/cache")); } /** * Init a test resolver as default, useful combined with * {@link #fillRepository(DependencyResolver, Collection)}. * * @param settings * the settings to initialize * @return test settings */ public static IvySettings loadTestSettings(IvySettings settings) { settings.setDefaultCache(new File("build/test/cache")); settings.addResolver(newTestRepository()); settings.setDefaultResolver("test"); return settings; } /** * Create basic resolve data using the given settings * * @param settings * the settings to use to create the resolve data * @return basic resolve data useful for testing */ public static ResolveData newResolveData(IvySettings settings) { return new ResolveData(new ResolveEngine(settings, new EventManager(), new SortEngine( settings)), newResolveOptions(settings)); } /** * Create basic resolve options using the given settings * * @param settings * the settings to use to create the resolve options * @return the basic resolve options, useful for testing */ public static ResolveOptions newResolveOptions(IvySettings settings) { return new ResolveOptions(); } }