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.BundleDescription;
17 import org.eclipse.osgi.service.resolver.ExportPackageDescription;
18 import org.eclipse.osgi.service.resolver.State;
19 import org.eclipse.osgi.service.resolver.StateObjectFactory;
20 import org.eclipse.osgi.tests.services.resolver.AbstractStateTest;
21 import org.osgi.framework.BundleException;
22 
23 
24 public class TestRFC79_001 extends AbstractStateTest {
TestRFC79_001(String testName)25 	public TestRFC79_001(String testName) {
26 		super(testName);
27 	}
28 
29 	BundleDescription bundle_1 = null;
30 	BundleDescription bundle_2 = null;
31 	BundleDescription bundle_3 = null;
32 
33 
testTest_001()34 	public void testTest_001() {
35 		State state = buildEmptyState();
36 		StateObjectFactory sof = StateObjectFactory.defaultFactory;
37 
38 		bundle_1 = create_bundle_1(sof);
39 		bundle_2 = create_bundle_2(sof);
40 		bundle_3 = create_bundle_3(sof);
41 		//***************************************************
42 		// stage a
43 		// expect to pass =true
44 		//***************************************************
45 		addBundlesToState_a(state);
46 		//***************************************************
47 		try {
48 			state.resolve();
49 		} catch (Throwable t) {
50 			fail("unexpected exception class=" + t.getClass().getName()
51 					+ " message=" + t.getMessage());
52 			return;
53 		}
54 		checkBundlesResolved_a();
55 		checkWiring_a();
56 	} // end of method
57 
58 
checkWiringState_1()59 	public void checkWiringState_1() {
60 		ExportPackageDescription[] exports = bundle_1.getResolvedImports();
61 		assertNotNull("export array is unexpectedly null", exports);
62 		assertTrue("export array is unexpectedly empty", exports.length > 0);
63 		for (ExportPackageDescription export : exports) {
64 			String exportPackageName = export.getName();
65 			assertNotNull("package name is null", exportPackageName);
66 			if (exportPackageName.equals("javax.servlet")) {
67 				assertNotNull("Package [javax.servlet] is not wired when it should be ", export.getExporter());
68 				assertEquals("Package [javax.servlet] is wired incorrectly ", export.getExporter(), bundle_2);
69 			}
70 		} // end for
71 	} // end method
72 
checkWiringState_2()73 	public void checkWiringState_2() {
74 		ExportPackageDescription[] exports = bundle_2.getResolvedImports();
75 		assertNotNull("export array is unexpectedly null", exports);
76 		assertTrue("export array is unexpectedly empty", exports.length > 0);
77 		for (ExportPackageDescription export : exports) {
78 			String exportPackageName = export.getName();
79 			assertNotNull("package name is null", exportPackageName);
80 			if (exportPackageName.equals("javax.servlet")) {
81 				assertNotNull("Package [javax.servlet] is not wired when it should be ", export.getExporter());
82 				assertEquals("Package [javax.servlet] is wired incorrectly ", export.getExporter(), bundle_2);
83 			}
84 		} // end for
85 	} // end method
86 
checkWiringState_3()87 	public void checkWiringState_3() {
88 		ExportPackageDescription[] exports = bundle_3.getResolvedImports();
89 		assertNotNull("export array is unexpectedly null", exports);
90 		assertTrue("export array is unexpectedly empty", exports.length > 0);
91 		for (ExportPackageDescription export : exports) {
92 			String exportPackageName = export.getName();
93 			assertNotNull("package name is null", exportPackageName);
94 			if (exportPackageName.equals("javax.servlet")) {
95 				assertNotNull("Package [javax.servlet] is not wired when it should be ", export.getExporter());
96 				assertEquals("Package [javax.servlet] is wired incorrectly ", export.getExporter(), bundle_2);
97 			}
98 		} // end for
99 	} // end method
100 
101 
checkWiring_a()102 	public void checkWiring_a() {
103 		checkWiringState_1();
104 		checkWiringState_2();
105 		checkWiringState_3();
106 	} // end method
107 
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 
119 
checkBundlesResolved_a()120 	public void checkBundlesResolved_a() {
121 		assertTrue("unexpected bundle resolution state", bundle_1.isResolved());
122 		assertTrue("unexpected bundle resolution state", bundle_2.isResolved());
123 		assertTrue("unexpected bundle resolution state", bundle_3.isResolved());
124 	} // end method
125 
126 
create_bundle_1(StateObjectFactory sof)127 	public BundleDescription create_bundle_1(StateObjectFactory sof) {
128 		java.util.Dictionary dictionary_1 = new java.util.Properties();
129 		BundleDescription bundle = null;
130 		dictionary_1.put("Bundle-ManifestVersion", "2");
131 		dictionary_1.put("Bundle-SymbolicName", "A");
132 		dictionary_1.put("Export-Package", "javax.servlet; version=2.1.0");
133 		dictionary_1.put("Import-Package", "javax.servlet; version=2.1.0");
134 		try {
135 			bundle = sof.createBundleDescription(dictionary_1, "bundle_1", 1);
136 		} catch (BundleException be) {
137 			fail(be.getMessage());
138 		}
139 		return bundle;
140 	} // end of method
141 
create_bundle_2(StateObjectFactory sof)142 	public BundleDescription create_bundle_2(StateObjectFactory sof) {
143 		java.util.Dictionary dictionary_2 = new java.util.Properties();
144 		BundleDescription bundle = null;
145 		dictionary_2.put("Bundle-ManifestVersion", "2");
146 		dictionary_2.put("Bundle-SymbolicName", "B");
147 		dictionary_2.put("Export-Package", "javax.servlet; version=2.2.0");
148 		dictionary_2.put("Import-Package", "javax.servlet; version=2.2.0");
149 		try {
150 			bundle = sof.createBundleDescription(dictionary_2, "bundle_2", 2);
151 		} catch (BundleException be) {
152 			fail(be.getMessage());
153 		}
154 		return bundle;
155 	} // end of method
156 
create_bundle_3(StateObjectFactory sof)157 	public BundleDescription create_bundle_3(StateObjectFactory sof) {
158 		java.util.Dictionary dictionary_3 = new java.util.Properties();
159 		BundleDescription bundle = null;
160 		dictionary_3.put("Bundle-ManifestVersion", "2");
161 		dictionary_3.put("Bundle-SymbolicName", "C");
162 		dictionary_3.put("Import-Package", "javax.servlet; version=2.1.0");
163 		try {
164 			bundle = sof.createBundleDescription(dictionary_3, "bundle_3", 3);
165 		} catch (BundleException be) {
166 			fail(be.getMessage());
167 		}
168 		return bundle;
169 	} // end of method
170 
create_bundle_4(StateObjectFactory sof)171 	public BundleDescription create_bundle_4(StateObjectFactory sof) {
172 		java.util.Dictionary dictionary_4 = new java.util.Properties();
173 		BundleDescription bundle = null;
174 		dictionary_4.put("Bundle-ManifestVersion", "2");
175 		dictionary_4.put("Bundle-SymbolicName", "D");
176 		dictionary_4.put("Import-Package", "foo; version=1.0; c=z");
177 		try {
178 			bundle = sof.createBundleDescription(dictionary_4, "bundle_4", 4);
179 		} catch (BundleException be) {
180 			fail(be.getMessage());
181 		}
182 		return bundle;
183 	} // end of method
184 
create_bundle_5(StateObjectFactory sof)185 	public BundleDescription create_bundle_5(StateObjectFactory sof) {
186 		java.util.Dictionary dictionary_5 = new java.util.Properties();
187 		BundleDescription bundle = null;
188 		dictionary_5.put("Bundle-ManifestVersion", "2");
189 		dictionary_5.put("Bundle-SymbolicName", "E");
190 		dictionary_5.put("Import-Package", "foo; version=1.0; a=y");
191 		try {
192 			bundle = sof.createBundleDescription(dictionary_5, "bundle_5", 5);
193 		} catch (BundleException be) {
194 			fail(be.getMessage());
195 		}
196 		return bundle;
197 	} // end of method
198 
199 } // end of testcase
200