001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.registry;
038:
039: import java.awt.Component;
040: import java.util.Collections;
041: import java.util.HashSet;
042: import java.util.LinkedList;
043: import java.util.List;
044: import java.util.Set;
045: import java.util.Stack;
046: import junit.framework.TestCase;
047: import org.netbeans.installer.product.components.Product;
048: import org.netbeans.installer.utils.exceptions.UnresolvedDependencyException;
049:
050: /*
051: * Created on 29 Àâãóñò 2006 ã., 16:45
052: */
053:
054: /**
055: *
056: * @author Danila_Dugurov
057: */
058:
059: public class TestCircles extends TestCase {
060:
061: private List<Product> list;
062:
063: public void testThingInSelf() {
064: try {
065: list = new LinkedList<Product>();
066: Product component = new Product();
067: component.addRequirement(component);
068: list.add(component);
069: checkCircles();
070: fail();
071: } catch (UnresolvedDependencyException ex) {
072: }
073: }
074:
075: public void testMoreSofisticatedRequirements() {
076: try {
077: list = new LinkedList<Product>();
078: Product component = new Product();
079: Product depp = new Product();
080: Product jony = new Product();
081: list.add(component);
082: list.add(depp);
083: list.add(jony);
084: component.addRequirement(depp);
085: depp.addRequirement(jony);
086: jony.addRequirement(component);
087: checkCircles();
088: fail();
089: } catch (UnresolvedDependencyException ex) {
090: }
091: }
092:
093: public void testRequirementsAndConflicts() {
094: try {
095: list = new LinkedList<Product>();
096: Product component = new Product();
097: Product depp = new Product();
098: Product jonny = new Product();
099: list.add(component);
100: list.add(depp);
101: list.add(jonny);
102: component.addRequirement(depp);
103: depp.addRequirement(jonny);
104: jonny.addConflict(component);
105: checkCircles();
106: fail();
107: } catch (UnresolvedDependencyException ex) {
108: }
109: }
110:
111: public void testSofisticatedRequirementsAndConflicts() {
112: try {
113: list = new LinkedList<Product>();
114: Product root = new Product();
115: Product depp = new Product();
116: Product jonny = new Product();
117: Product independant = new Product();
118: list.add(root);
119: list.add(depp);
120: list.add(jonny);
121: list.add(independant);
122: root.addRequirement(depp);
123: root.addRequirement(jonny);
124: jonny.addConflict(depp);
125: jonny.addRequirement(independant);
126: depp.addRequirement(independant);
127: checkCircles();
128: fail();
129: } catch (UnresolvedDependencyException ex) {
130: }
131: }
132:
133: public void testOkConflicts() {
134: try {
135: list = new LinkedList<Product>();
136: Product root = new Product();
137: Product depp = new Product();
138: Product jonny = new Product();
139: list.add(depp);
140: list.add(jonny);
141: list.add(root);
142: root.addConflict(depp);
143: root.addConflict(jonny);
144: jonny.addRequirement(depp);
145: checkCircles();
146: } catch (UnresolvedDependencyException ex) {
147: fail();
148: }
149: }
150:
151: private void checkCircles() throws UnresolvedDependencyException {
152: for (Product component : list) {
153: final Stack<Product> visited = new Stack<Product>();
154: final Set<Product> conflictSet = new HashSet<Product>();
155: final Set<Product> requirementSet = new HashSet<Product>();
156: checkCircles(component, visited, conflictSet,
157: requirementSet);
158: }
159: }
160:
161: private void checkCircles(Product component,
162: Stack<Product> visited, Set<Product> conflictSet,
163: Set<Product> requirementSet)
164: throws UnresolvedDependencyException {
165: if (visited.contains(component)
166: || conflictSet.contains(component))
167: throw new UnresolvedDependencyException("circles found");
168: visited.push(component);
169: requirementSet.add(component);
170: if (!Collections.disjoint(requirementSet, component
171: .getConflicts()))
172: throw new UnresolvedDependencyException("circles found");
173: conflictSet.addAll(component.getConflicts());
174: for (Product comp : component.getRequirements())
175: checkCircles(comp, visited, conflictSet, requirementSet);
176: visited.pop();
177: }
178: }
|