1 /*******************************************************************************
2  * Copyright (c) 2004, 2013 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.osgi.tests.resolver;
15 
16 import org.eclipse.osgi.service.resolver.*;
17 import org.eclipse.osgi.tests.services.resolver.AbstractStateTest;
18 import org.osgi.framework.BundleException;
19 
20 public class TestCycle_002 extends AbstractStateTest {
TestCycle_002(String testName)21 	public TestCycle_002(String testName) {
22 		super(testName);
23 	}
24 
25 	BundleDescription bundle_1 = null;
26 	BundleDescription bundle_2 = null;
27 	BundleDescription bundle_3 = null;
28 
testTest_002()29 	public void testTest_002() {
30 		State state = buildEmptyState();
31 		StateObjectFactory sof = StateObjectFactory.defaultFactory;
32 
33 		bundle_1 = create_bundle_1(sof);
34 		bundle_2 = create_bundle_2(sof);
35 		bundle_3 = create_bundle_3(sof);
36 		//***************************************************
37 		// stage a
38 		// expect to pass =true
39 		//***************************************************
40 		addBundlesToState_a(state);
41 		//***************************************************
42 		try {
43 			state.resolve();
44 		} catch (Throwable t) {
45 			fail("unexpected exception class=" + t.getClass().getName() + " message=" + t.getMessage());
46 			return;
47 		}
48 		checkBundlesResolved_a();
49 		checkWiring_a();
50 	} // end of method
51 
checkWiringState_1()52 	public void checkWiringState_1() {
53 		BundleDescription[] requires = bundle_1.getResolvedRequires();
54 		assertNotNull("requires array is unexpectedly null", requires);
55 		assertTrue("requires array is unexpectedly empty", requires.length > 0);
56 		for (BundleDescription require : requires) {
57 			String requiresName = require.getName();
58 			assertNotNull("package name is null", requiresName);
59 			if (requiresName.equals("B")) {
60 				assertNotNull("Require [B] is not wired when it should be ", require);
61 				assertEquals("Require [B] is wired incorrectly ", require, bundle_2);
62 			} else if (requiresName.equals("C")) {
63 				assertNotNull("Require [C] is not wired when it should be ", require);
64 				assertEquals("Require [C] is wired incorrectly ", require, bundle_3);
65 			}
66 		} // end for
67 	} // end method
68 
checkWiringState_2()69 	public void checkWiringState_2() {
70 		BundleDescription[] requires = bundle_2.getResolvedRequires();
71 		assertNotNull("requires array is unexpectedly null", requires);
72 		assertTrue("requires array is unexpectedly empty", requires.length > 0);
73 		for (BundleDescription require : requires) {
74 			String requiresName = require.getName();
75 			assertNotNull("package name is null", requiresName);
76 			if (requiresName.equals("A")) {
77 				assertNotNull("Require [A] is not wired when it should be ", require);
78 				assertEquals("Require [A] is wired incorrectly ", require, bundle_1);
79 			} else if (requiresName.equals("C")) {
80 				assertNotNull("Require [C] is not wired when it should be ", require);
81 				assertEquals("Require [C] is wired incorrectly ", require, bundle_3);
82 			}
83 		} // end for
84 	} // end method
85 
checkWiringState_3()86 	public void checkWiringState_3() {
87 		BundleDescription[] requires = bundle_3.getResolvedRequires();
88 		assertNotNull("requires array is unexpectedly null", requires);
89 		assertTrue("requires array is unexpectedly empty", requires.length > 0);
90 		for (BundleDescription require : requires) {
91 			String requiresName = require.getName();
92 			assertNotNull("package name is null", requiresName);
93 			if (requiresName.equals("A")) {
94 				assertNotNull("Require [A] is not wired when it should be ", require);
95 				assertEquals("Require [A] is wired incorrectly ", require, bundle_1);
96 			} else if (requiresName.equals("B")) {
97 				assertNotNull("Require [B] is not wired when it should be ", require);
98 				assertEquals("Require [B] is wired incorrectly ", require, bundle_2);
99 			}
100 		} // end for
101 	} // end method
102 
checkWiring_a()103 	public void checkWiring_a() {
104 		checkWiringState_1();
105 		checkWiringState_2();
106 		checkWiringState_3();
107 	} // end method
108 
addBundlesToState_a(State state)109 	public void addBundlesToState_a(State state) {
110 		boolean added = false;
111 		added = state.addBundle(bundle_1);
112 		assertTrue("failed to add bundle ", added);
113 		added = state.addBundle(bundle_2);
114 		assertTrue("failed to add bundle ", added);
115 		added = state.addBundle(bundle_3);
116 		assertTrue("failed to add bundle ", added);
117 	} // end method
118 
checkBundlesResolved_a()119 	public void checkBundlesResolved_a() {
120 		assertTrue("unexpected bundle resolution state", bundle_1.isResolved());
121 		assertTrue("unexpected bundle resolution state", bundle_2.isResolved());
122 		assertTrue("unexpected bundle resolution state", bundle_3.isResolved());
123 	} // end method
124 
create_bundle_1(StateObjectFactory sof)125 	public BundleDescription create_bundle_1(StateObjectFactory sof) {
126 		java.util.Dictionary dictionary_1 = new java.util.Properties();
127 		BundleDescription bundle = null;
128 		dictionary_1.put("Bundle-ManifestVersion", "2");
129 		dictionary_1.put("Bundle-SymbolicName", "A");
130 		dictionary_1.put("Require-Bundle", "B, C");
131 		try {
132 			bundle = sof.createBundleDescription(dictionary_1, "bundle_1", 1);
133 		} catch (BundleException be) {
134 			fail(be.getMessage());
135 		}
136 		return bundle;
137 	} // end of method
138 
create_bundle_2(StateObjectFactory sof)139 	public BundleDescription create_bundle_2(StateObjectFactory sof) {
140 		java.util.Dictionary dictionary_2 = new java.util.Properties();
141 		BundleDescription bundle = null;
142 		dictionary_2.put("Bundle-ManifestVersion", "2");
143 		dictionary_2.put("Bundle-SymbolicName", "B");
144 		dictionary_2.put("Require-Bundle", "A, C");
145 		try {
146 			bundle = sof.createBundleDescription(dictionary_2, "bundle_2", 2);
147 		} catch (BundleException be) {
148 			fail(be.getMessage());
149 		}
150 		return bundle;
151 	} // end of method
152 
create_bundle_3(StateObjectFactory sof)153 	public BundleDescription create_bundle_3(StateObjectFactory sof) {
154 		java.util.Dictionary dictionary_3 = new java.util.Properties();
155 		BundleDescription bundle = null;
156 		dictionary_3.put("Bundle-ManifestVersion", "2");
157 		dictionary_3.put("Bundle-SymbolicName", "C");
158 		dictionary_3.put("Require-Bundle", "A, B");
159 		try {
160 			bundle = sof.createBundleDescription(dictionary_3, "bundle_3", 3);
161 		} catch (BundleException be) {
162 			fail(be.getMessage());
163 		}
164 		return bundle;
165 	} // end of method
166 
167 } // end of testcase
168