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 TestOptional_001 extends AbstractStateTest {
TestOptional_001(String testName)25 	public TestOptional_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_002()34 	public void testTest_002() {
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 exp : exports) {
64 			String exportPackageName = exp.getName();
65 			assertNotNull("package name is null", exportPackageName);
66 			if (exportPackageName.equals("q")) {
67 				assertNotNull("Package [q] is not wired when it should be ", exp.getExporter());
68 				assertEquals("Package [q] is wired incorrectly ", exp.getExporter(), bundle_3);
69 			} else if (exportPackageName.equals("r")) {
70 				assertNotNull("Package [r] is not wired when it should be ", exp.getExporter());
71 				assertEquals("Package [r] is wired incorrectly ", exp.getExporter(), bundle_3);
72 			} else if (exportPackageName.equals("p")) {
73 				assertNull("Package [p] is wired when it should not be ", exp);
74 			}
75 		} // end for
76 	} // end method
77 
checkWiringState_2()78 	public void checkWiringState_2() {
79 	} // end method
80 
checkWiringState_3()81 	public void checkWiringState_3() {
82 	} // end method
83 
84 
checkWiring_a()85 	public void checkWiring_a() {
86 		checkWiringState_1();
87 		checkWiringState_2();
88 		checkWiringState_3();
89 	} // end method
90 
91 
addBundlesToState_a(State state)92 	public void addBundlesToState_a(State state) {
93 		boolean added = false;
94 		added = state.addBundle(bundle_1);
95 		assertTrue("failed to add bundle ", added);
96 		added = state.addBundle(bundle_2);
97 		assertTrue("failed to add bundle ", added);
98 		added = state.addBundle(bundle_3);
99 		assertTrue("failed to add bundle ", added);
100 	} // end method
101 
102 
checkBundlesResolved_a()103 	public void checkBundlesResolved_a() {
104 		assertTrue("unexpected bundle resolution state", bundle_1.isResolved());
105 		assertTrue("unexpected bundle resolution state", bundle_2.isResolved());
106 		assertTrue("unexpected bundle resolution state", bundle_3.isResolved());
107 	} // end method
108 
109 
create_bundle_1(StateObjectFactory sof)110 	public BundleDescription create_bundle_1(StateObjectFactory sof) {
111 		java.util.Dictionary dictionary_1 = new java.util.Properties();
112 		BundleDescription bundle = null;
113 		dictionary_1.put("Bundle-ManifestVersion", "2");
114 		dictionary_1.put("Bundle-SymbolicName", "A");
115 		dictionary_1.put("Import-Package", "p; resolution:=optional, q, r");
116 		try {
117 			bundle = sof.createBundleDescription(dictionary_1, "bundle_1", 1);
118 		} catch (BundleException be) {
119 			fail(be.getMessage());
120 		}
121 		return bundle;
122 	} // end of method
123 
create_bundle_2(StateObjectFactory sof)124 	public BundleDescription create_bundle_2(StateObjectFactory sof) {
125 		java.util.Dictionary dictionary_2 = new java.util.Properties();
126 		BundleDescription bundle = null;
127 		dictionary_2.put("Bundle-ManifestVersion", "2");
128 		dictionary_2.put("Bundle-SymbolicName", "B");
129 		dictionary_2.put("Export-Package", "p; r; uses:=\"p,r\"");
130 		try {
131 			bundle = sof.createBundleDescription(dictionary_2, "bundle_2", 2);
132 		} catch (BundleException be) {
133 			fail(be.getMessage());
134 		}
135 		return bundle;
136 	} // end of method
137 
create_bundle_3(StateObjectFactory sof)138 	public BundleDescription create_bundle_3(StateObjectFactory sof) {
139 		java.util.Dictionary dictionary_3 = new java.util.Properties();
140 		BundleDescription bundle = null;
141 		dictionary_3.put("Bundle-ManifestVersion", "2");
142 		dictionary_3.put("Bundle-SymbolicName", "C");
143 		dictionary_3.put("Export-Package", "q; r; uses:=\"q,r\"");
144 		try {
145 			bundle = sof.createBundleDescription(dictionary_3, "bundle_3", 3);
146 		} catch (BundleException be) {
147 			fail(be.getMessage());
148 		}
149 		return bundle;
150 	} // end of method
151 
152 } // end of testcase
153