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.plugins.circular;
19:
20: import junit.framework.TestCase;
21:
22: import org.apache.ivy.TestHelper;
23: import org.apache.ivy.core.IvyContext;
24: import org.apache.ivy.core.event.EventManager;
25: import org.apache.ivy.core.resolve.ResolveData;
26: import org.apache.ivy.core.resolve.ResolveEngine;
27: import org.apache.ivy.core.resolve.ResolveOptions;
28: import org.apache.ivy.core.settings.IvySettings;
29: import org.apache.ivy.core.sort.SortEngine;
30: import org.apache.ivy.util.Message;
31: import org.apache.ivy.util.MockMessageLogger;
32:
33: public class WarnCircularDependencyStrategyTest extends TestCase {
34: private CircularDependencyStrategy strategy;
35: private MockMessageLogger mockMessageImpl;
36:
37: protected void setUp() throws Exception {
38: strategy = WarnCircularDependencyStrategy.getInstance();
39:
40: resetLogger();
41: }
42:
43: private void resetLogger() {
44: mockMessageImpl = new MockMessageLogger();
45: Message.setDefaultLogger(mockMessageImpl);
46: }
47:
48: public void testLog() throws Exception {
49: strategy.handleCircularDependency(TestHelper
50: .parseMridsToArray("#A;1.0, #B;1.0"));
51:
52: mockMessageImpl
53: .assertLogWarningContains("circular dependency found: #A;1.0->#B;1.0");
54: }
55:
56: public void testRemoveDuplicates() throws Exception {
57: strategy.handleCircularDependency(TestHelper
58: .parseMridsToArray("#A;1.1, #B;1.0"));
59: strategy.handleCircularDependency(TestHelper
60: .parseMridsToArray("#A;1.1, #B;1.0"));
61:
62: // should only log the circular dependency once
63: assertEquals(1, mockMessageImpl.getLogs().size());
64: }
65:
66: public void testRemoveDuplicates2() throws Exception {
67: setResolveContext("1");
68: resetLogger();
69: strategy.handleCircularDependency(TestHelper
70: .parseMridsToArray("#A;1.1, #B;1.0"));
71: strategy.handleCircularDependency(TestHelper
72: .parseMridsToArray("#A;1.1, #B;1.0"));
73:
74: // should only log the circular dependency once
75: assertEquals(1, mockMessageImpl.getLogs().size());
76:
77: setResolveContext("2");
78: resetLogger();
79: strategy.handleCircularDependency(TestHelper
80: .parseMridsToArray("#A;1.1, #B;1.0"));
81: // should log the message
82: assertEquals(1, mockMessageImpl.getLogs().size());
83:
84: strategy.handleCircularDependency(TestHelper
85: .parseMridsToArray("#A;1.1, #B;1.0"));
86:
87: // should not log the message again
88: assertEquals(1, mockMessageImpl.getLogs().size());
89: }
90:
91: private void setResolveContext(String resolveId) {
92: IvySettings settings = new IvySettings();
93: IvyContext.getContext().setResolveData(
94: new ResolveData(new ResolveEngine(settings,
95: new EventManager(), new SortEngine(settings)),
96: new ResolveOptions().setResolveId(resolveId)));
97: }
98: }
|