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.core.publish;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.text.ParseException;
023: import java.util.Arrays;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.ivy.core.cache.DefaultResolutionCacheManager;
028: import org.apache.ivy.core.event.EventManager;
029: import org.apache.ivy.core.module.descriptor.Artifact;
030: import org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor;
031: import org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor;
032: import org.apache.ivy.core.module.id.ModuleRevisionId;
033: import org.apache.ivy.core.resolve.ResolveData;
034: import org.apache.ivy.core.resolve.ResolveEngine;
035: import org.apache.ivy.core.resolve.ResolveOptions;
036: import org.apache.ivy.core.resolve.ResolvedModuleRevision;
037: import org.apache.ivy.core.settings.IvySettings;
038: import org.apache.ivy.core.sort.SortEngine;
039: import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorWriter;
040: import org.apache.ivy.plugins.resolver.FileSystemResolver;
041: import org.apache.ivy.util.FileUtil;
042:
043: public class PublishEngineTest extends TestCase {
044: protected void setUp() throws Exception {
045: System.setProperty("ivy.cache.dir", new File(
046: "build/test/publish/cache").getAbsolutePath());
047: FileUtil.forceDelete(new File("build/test/publish"));
048: }
049:
050: protected void tearDown() throws Exception {
051: FileUtil.forceDelete(new File("build/test/publish"));
052: }
053:
054: public void testAtomicity() throws Exception {
055: IvySettings settings = new IvySettings();
056: final PublishEngine engine = new PublishEngine(settings,
057: new EventManager());
058: final int[] counter = new int[] { 0 };
059:
060: final DefaultModuleDescriptor md = DefaultModuleDescriptor
061: .newDefaultInstance(ModuleRevisionId.parse("#A;1.0"));
062: final FileSystemResolver resolver = new FileSystemResolver() {
063: public void publish(Artifact artifact, File src,
064: boolean overwrite) throws IOException {
065: super .publish(artifact, src, overwrite);
066: synchronized (PublishEngineTest.this ) {
067: counter[0]++;
068: }
069: sleepSilently(50);
070: synchronized (PublishEngineTest.this ) {
071: counter[0]++;
072: }
073: }
074: };
075: resolver.setName("test");
076: resolver.setSettings(settings);
077: resolver
078: .addIvyPattern("build/test/publish/repo/[module]/[revision]/[artifact].[ext]");
079: resolver
080: .addArtifactPattern("build/test/publish/repo/[module]/[revision]/[artifact].[ext]");
081:
082: FileUtil.copy(new File(
083: "test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"),
084: new File("build/test/publish/module/A.jar"), null);
085: XmlModuleDescriptorWriter.write(md, new File(
086: "build/test/publish/module/ivy.xml"));
087:
088: resolveAndAssertNotFound(settings, resolver,
089: "#A;latest.integration", "before publishing");
090:
091: // run publish asynchronously
092: new Thread() {
093: public void run() {
094: try {
095: engine
096: .publish(
097: md,
098: Arrays
099: .asList(new String[] { "build/test/publish/module/[artifact].[ext]" }),
100: resolver,
101: new PublishOptions()
102: .setSrcIvyPattern("build/test/publish/module/[artifact].[ext]"));
103: synchronized (PublishEngineTest.this ) {
104: counter[0]++;
105: }
106: } catch (IOException e) {
107: throw new RuntimeException(e);
108: }
109: }
110: }.start();
111:
112: while (true) {
113: sleepSilently(5);
114: synchronized (this ) {
115: if (counter[0] == 5) {
116: break;
117: } else if (counter[0] < 4) {
118: resolveAndAssertNotFound(settings, resolver,
119: "#A;latest.integration", "after "
120: + (counter[0] / 2)
121: + " artifacts published");
122: }
123: }
124: }
125: resolveAndAssertFound(settings, resolver, "#A;1.0");
126: }
127:
128: private void resolveAndAssertNotFound(IvySettings settings,
129: FileSystemResolver resolver, String module, String context)
130: throws ParseException {
131: ResolvedModuleRevision rmr = resolveModule(settings, resolver,
132: module);
133: assertNull("module found " + context + ". module=" + rmr, rmr);
134: }
135:
136: private void resolveAndAssertFound(IvySettings settings,
137: FileSystemResolver resolver, String module)
138: throws ParseException {
139: ResolvedModuleRevision rmr = resolveModule(settings, resolver,
140: module);
141: assertNotNull(rmr);
142: assertEquals(module, rmr.getId().toString());
143: }
144:
145: private ResolvedModuleRevision resolveModule(IvySettings settings,
146: FileSystemResolver resolver, String module)
147: throws ParseException {
148: return resolver.getDependency(new DefaultDependencyDescriptor(
149: ModuleRevisionId.parse(module), false),
150: new ResolveData(new ResolveEngine(settings,
151: new EventManager(), new SortEngine(settings)),
152: new ResolveOptions()));
153: }
154:
155: private void sleepSilently(int timeout) {
156: try {
157: Thread.sleep(timeout);
158: } catch (InterruptedException e) {
159: }
160: }
161: }
|