01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.ivy;
19:
20: import java.io.File;
21:
22: import org.apache.ivy.core.IvyContext;
23: import org.apache.ivy.core.cache.DefaultResolutionCacheManager;
24: import org.apache.ivy.core.report.ResolveReport;
25: import org.apache.ivy.core.resolve.ResolveOptions;
26: import org.apache.ivy.core.resolve.ResolveTest;
27: import org.apache.ivy.core.settings.IvySettings;
28: import org.apache.ivy.util.Message;
29: import org.apache.ivy.util.MockMessageLogger;
30:
31: import junit.framework.TestCase;
32:
33: public class IvyTest extends TestCase {
34: public void testMultipleInstances() throws Exception {
35: // this test checks that IvyContext is properly set and unset when using multiple instances
36: // of Ivy. We also check logging, because it heavily relies on IvyContext.
37:
38: // we start by loading one ivy instance and using it to resolve some dependencies
39: MockMessageLogger mockLogger = new MockMessageLogger();
40: Ivy ivy = Ivy.newInstance();
41: ivy.getLoggerEngine().setDefaultLogger(mockLogger);
42: ivy.configure(new File("test/repositories/ivysettings.xml"));
43: assertFalse(
44: "IvyContext should be cleared and return a default Ivy instance",
45: IvyContext.getContext().getIvy() == ivy);
46:
47: File cache = new File("build/cache");
48: ResolveReport report = ivy.resolve(new File(
49: "test/repositories/1/org1/mod1.1/ivys/ivy-1.0.xml")
50: .toURL(), getResolveOptions(ivy, new String[] { "*" }));
51: assertNotNull(report);
52: assertFalse(report.hasError());
53: mockLogger.assertLogContains("mod1.1");
54: assertFalse(
55: "IvyContext should be cleared and return a default Ivy instance",
56: IvyContext.getContext().getIvy() == ivy);
57:
58: // then we load another instance, and use it for another resolution
59: MockMessageLogger mockLogger2 = new MockMessageLogger();
60: Ivy ivy2 = new Ivy();
61: ivy2.getLoggerEngine().setDefaultLogger(mockLogger2);
62: ivy2.configure(new File(
63: "test/repositories/norev/ivysettings.xml").toURL());
64: report = ivy2.resolve(new File(
65: "test/repositories/norev/ivy.xml").toURL(),
66: getResolveOptions(ivy2, new String[] { "*" }));
67: assertNotNull(report);
68: assertFalse(report.hasError());
69: mockLogger2.assertLogContains("norev/ivysettings.xml");
70: assertFalse(
71: "IvyContext should be cleared and return a default Ivy instance",
72: IvyContext.getContext().getIvy() == ivy2);
73:
74: // finally we reuse the first instance to make another resolution
75: report = ivy.resolve(new File(
76: "test/repositories/1/org6/mod6.1/ivys/ivy-0.3.xml")
77: .toURL(), getResolveOptions(ivy,
78: new String[] { "extension" }));
79: assertNotNull(report);
80: assertFalse(report.hasError());
81: mockLogger.assertLogContains("mod6.1");
82: assertFalse(
83: "IvyContext should be cleared and return a default Ivy instance",
84: IvyContext.getContext().getIvy() == ivy);
85: }
86:
87: private ResolveOptions getResolveOptions(Ivy ivy, String[] confs) {
88: return getResolveOptions(ivy.getSettings(), confs);
89: }
90:
91: private ResolveOptions getResolveOptions(IvySettings settings,
92: String[] confs) {
93: return new ResolveOptions().setConfs(confs);
94: }
95: }
|