001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.ui.tests.model.bundle;
011:
012: import org.eclipse.jdt.launching.JavaRuntime;
013: import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
014: import org.eclipse.jdt.launching.environments.IExecutionEnvironmentsManager;
015: import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
016: import org.eclipse.pde.internal.core.text.bundle.ExecutionEnvironment;
017: import org.eclipse.pde.internal.core.text.bundle.RequiredExecutionEnvironmentHeader;
018: import org.eclipse.text.edits.TextEdit;
019: import org.osgi.framework.Constants;
020:
021: import junit.framework.Test;
022: import junit.framework.TestSuite;
023:
024: public class ExecutionEnvironmentTestCase extends
025: MultiLineHeaderTestCase {
026:
027: public ExecutionEnvironmentTestCase() {
028: super (Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
029: }
030:
031: public static Test suite() {
032: return new TestSuite(ExecutionEnvironmentTestCase.class);
033: }
034:
035: public void testAddExecutionEnvironmentHeader() throws Exception {
036: StringBuffer buffer = new StringBuffer();
037: buffer.append("Manifest-Version: 1.0\n");
038: buffer.append("Bundle-ManifestVersion: 2\n");
039: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
040: fDocument.set(buffer.toString());
041: load(true);
042: fModel.getBundle().setHeader(
043: Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT,
044: "J2SE-1.4");
045:
046: IManifestHeader header = fModel.getBundle().getManifestHeader(
047: Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
048: assertNotNull(header);
049: assertEquals("Bundle-RequiredExecutionEnvironment: J2SE-1.4\n",
050: header.write());
051:
052: TextEdit[] ops = fListener.getTextOperations();
053: assertEquals(1, ops.length);
054:
055: ops[0].apply(fDocument);
056: assertEquals(buffer.toString() + header.write(), fDocument
057: .get());
058: }
059:
060: public void testRemoveExistingExecutionEnvironment()
061: throws Exception {
062: StringBuffer buffer = new StringBuffer();
063: buffer.append("Manifest-Version: 1.0\n");
064: buffer.append("Bundle-ManifestVersion: 2\n");
065: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
066: buffer
067: .append("Bundle-RequiredExecutionEnvironment: J2SE-1.4\n");
068: fDocument.set(buffer.toString());
069: load(true);
070: IManifestHeader header = fModel.getBundle().getManifestHeader(
071: Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
072: assertNotNull(header);
073:
074: ExecutionEnvironment env = ((RequiredExecutionEnvironmentHeader) header)
075: .getEnvironments()[0];
076: ((RequiredExecutionEnvironmentHeader) header)
077: .removeExecutionEnvironment(env);
078:
079: TextEdit[] ops = fListener.getTextOperations();
080: assertEquals(1, ops.length);
081:
082: ops[0].apply(fDocument);
083: assertEquals(4, fDocument.getNumberOfLines());
084: }
085:
086: public void testAddExecutionEnvironment() throws Exception {
087: StringBuffer buffer = new StringBuffer();
088: buffer.append("Manifest-Version: 1.0\n");
089: buffer.append("Bundle-ManifestVersion: 2\n");
090: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
091: buffer
092: .append("Bundle-RequiredExecutionEnvironment: J2SE-1.4\n");
093: fDocument.set(buffer.toString());
094: load(true);
095:
096: IManifestHeader header = fModel.getBundle().getManifestHeader(
097: Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
098: IExecutionEnvironment env = JavaRuntime
099: .getExecutionEnvironmentsManager().getEnvironment(
100: "J2SE-1.5");
101: ((RequiredExecutionEnvironmentHeader) header)
102: .addExecutionEnvironment(env);
103: TextEdit[] ops = fListener.getTextOperations();
104: assertEquals(1, ops.length);
105:
106: ops[0].apply(fDocument);
107: assertEquals(6, fDocument.getNumberOfLines());
108: assertEquals(0, fDocument.getLineLength(5));
109:
110: int pos = fDocument.getLineOffset(3);
111: int length = fDocument.getLineLength(3)
112: + fDocument.getLineLength(4);
113:
114: StringBuffer expected = new StringBuffer(
115: "Bundle-RequiredExecutionEnvironment: J2SE-1.4,\n");
116: expected.append(" J2SE-1.5\n");
117: assertEquals(expected.toString(), fDocument.get(pos, length));
118: }
119:
120: public void testAddMulitplieExecutionEnvironmnets()
121: throws Exception {
122: StringBuffer buffer = new StringBuffer();
123: buffer.append("Manifest-Version: 1.0\n");
124: buffer.append("Bundle-ManifestVersion: 2\n");
125: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
126: buffer
127: .append("Bundle-RequiredExecutionEnvironment: J2SE-1.4\n");
128: fDocument.set(buffer.toString());
129: load(true);
130: IManifestHeader header = fModel.getBundle().getManifestHeader(
131: Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
132: IExecutionEnvironmentsManager manager = JavaRuntime
133: .getExecutionEnvironmentsManager();
134: IExecutionEnvironment env1 = manager
135: .getEnvironment("CDC-1.1/Foundation-1.1");
136: IExecutionEnvironment env2 = manager.getEnvironment("J2SE-1.5");
137: IExecutionEnvironment env3 = manager
138: .getEnvironment("OSGi/Minimum-1.1");
139: ((RequiredExecutionEnvironmentHeader) header)
140: .addExecutionEnvironment(env1);
141: ((RequiredExecutionEnvironmentHeader) header)
142: .addExecutionEnvironment(env2);
143: ((RequiredExecutionEnvironmentHeader) header)
144: .addExecutionEnvironment(env3);
145:
146: TextEdit[] ops = fListener.getTextOperations();
147: assertEquals(1, ops.length);
148:
149: ops[0].apply(fDocument);
150: assertEquals(8, fDocument.getNumberOfLines());
151: assertEquals(0, fDocument.getLineLength(7));
152:
153: int pos = fDocument.getLineOffset(3);
154: int length = fDocument.getLineOffset(7)
155: - fDocument.getLineOffset(3);
156: StringBuffer expected = new StringBuffer(
157: "Bundle-RequiredExecutionEnvironment: J2SE-1.4,\n");
158: expected.append(" CDC-1.1/Foundation-1.1,\n");
159: expected.append(" J2SE-1.5,\n");
160: expected.append(" OSGi/Minimum-1.1\n");
161: assertEquals(expected.toString(), fDocument.get(pos, length));
162: }
163:
164: public void testRemoveExecutionEnvironment() throws Exception {
165: StringBuffer buffer = new StringBuffer();
166: buffer.append("Manifest-Version: 1.0\n");
167: buffer.append("Bundle-ManifestVersion: 2\n");
168: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
169: buffer
170: .append("Bundle-RequiredExecutionEnvironment: J2SE-1.4,\n");
171: buffer.append(" CDC-1.1/Foundation-1.1,\n");
172: buffer.append(" OSGi/Minimum-1.1\n");
173: fDocument.set(buffer.toString());
174: load(true);
175: IManifestHeader header = fModel.getBundle().getManifestHeader(
176: Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
177: ExecutionEnvironment env = ((RequiredExecutionEnvironmentHeader) header)
178: .getEnvironments()[1];
179: ((RequiredExecutionEnvironmentHeader) header)
180: .removeExecutionEnvironment(env);
181:
182: TextEdit[] ops = fListener.getTextOperations();
183: assertEquals(1, ops.length);
184:
185: ops[0].apply(fDocument);
186: assertEquals(6, fDocument.getNumberOfLines());
187: assertEquals(0, fDocument.getLineLength(5));
188:
189: int pos = fDocument.getLineOffset(3);
190: int length = fDocument.getLineOffset(5)
191: - fDocument.getLineOffset(3);
192: StringBuffer expected = new StringBuffer(
193: "Bundle-RequiredExecutionEnvironment: J2SE-1.4,\n");
194: expected.append(" OSGi/Minimum-1.1\n");
195: assertEquals(expected.toString(), fDocument.get(pos, length));
196: }
197:
198: public void testRemoveMultipleExecutionEnvironments()
199: throws Exception {
200: StringBuffer buffer = new StringBuffer();
201: buffer.append("Manifest-Version: 1.0\n");
202: buffer.append("Bundle-ManifestVersion: 2\n");
203: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
204: buffer
205: .append("Bundle-RequiredExecutionEnvironment: J2SE-1.4,\n");
206: buffer.append(" CDC-1.1/Foundation-1.1,\n");
207: buffer.append(" OSGi/Minimum-1.1\n");
208: fDocument.set(buffer.toString());
209: load(true);
210: IManifestHeader header = fModel.getBundle().getManifestHeader(
211: Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
212: ExecutionEnvironment[] envs = ((RequiredExecutionEnvironmentHeader) header)
213: .getEnvironments();
214: ((RequiredExecutionEnvironmentHeader) header)
215: .removeExecutionEnvironment(envs[1]);
216: ((RequiredExecutionEnvironmentHeader) header)
217: .removeExecutionEnvironment(envs[0]);
218:
219: TextEdit[] ops = fListener.getTextOperations();
220: assertEquals(1, ops.length);
221:
222: ops[0].apply(fDocument);
223: assertEquals(5, fDocument.getNumberOfLines());
224: assertEquals(0, fDocument.getLineLength(4));
225:
226: int pos = fDocument.getLineOffset(3);
227: int length = fDocument.getLineLength(3);
228: assertEquals(
229: "Bundle-RequiredExecutionEnvironment: OSGi/Minimum-1.1\n",
230: fDocument.get(pos, length));
231: }
232:
233: public void testPreserveSpacing() throws Exception {
234: StringBuffer buffer = new StringBuffer();
235: buffer.append("Manifest-Version: 1.0\n");
236: buffer.append("Bundle-ManifestVersion: 2\n");
237: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
238: buffer.append("Bundle-RequiredExecutionEnvironment: \n");
239: buffer.append(" J2SE-1.4\n");
240: fDocument.set(buffer.toString());
241: load(true);
242:
243: IManifestHeader header = fModel.getBundle().getManifestHeader(
244: Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
245: IExecutionEnvironment env = JavaRuntime
246: .getExecutionEnvironmentsManager().getEnvironment(
247: "OSGi/Minimum-1.1");
248: ((RequiredExecutionEnvironmentHeader) header)
249: .addExecutionEnvironment(env);
250: TextEdit[] ops = fListener.getTextOperations();
251: assertEquals(1, ops.length);
252:
253: ops[0].apply(fDocument);
254: assertEquals(7, fDocument.getNumberOfLines());
255: assertEquals(0, fDocument.getLineLength(6));
256:
257: int pos = fDocument.getLineOffset(3);
258: int length = fDocument.getLineOffset(6)
259: - fDocument.getLineOffset(3);
260:
261: StringBuffer expected = new StringBuffer(
262: "Bundle-RequiredExecutionEnvironment: \n");
263: expected.append(" J2SE-1.4,\n");
264: expected.append(" OSGi/Minimum-1.1\n");
265: assertEquals(expected.toString(), fDocument.get(pos, length));
266:
267: }
268:
269: }
|