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.pde.internal.core.ibundle.IManifestHeader;
013: import org.eclipse.pde.internal.core.text.bundle.BasePackageHeader;
014: import org.eclipse.pde.internal.core.text.bundle.PackageObject;
015: import org.eclipse.text.edits.TextEdit;
016:
017: public abstract class PackageHeaderTestCase extends
018: MultiLineHeaderTestCase {
019:
020: public PackageHeaderTestCase(String headerName) {
021: super (headerName);
022: }
023:
024: public void testAddExportPackageHeader() throws Exception {
025: StringBuffer buffer = new StringBuffer();
026: buffer.append("Manifest-Version: 1.0\n");
027: buffer.append("Bundle-ManifestVersion: 2\n");
028: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
029: fDocument.set(buffer.toString());
030: load(true);
031: fModel.getBundle().setHeader(fHeaderName, "com.example.abc");
032:
033: IManifestHeader header = fModel.getBundle().getManifestHeader(
034: fHeaderName);
035: assertNotNull(header);
036: assertEquals(fHeaderName + ": com.example.abc\n", header
037: .write());
038:
039: TextEdit[] ops = fListener.getTextOperations();
040: assertEquals(1, ops.length);
041:
042: ops[0].apply(fDocument);
043: assertEquals(buffer.toString() + header.write(), fDocument
044: .get());
045: }
046:
047: public void testRemoveExistingExportPackageHeader()
048: throws Exception {
049: StringBuffer buffer = new StringBuffer();
050: buffer.append("Manifest-Version: 1.0\n");
051: buffer.append("Bundle-ManifestVersion: 2\n");
052: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
053: buffer.append(fHeaderName);
054: buffer.append(": com.example.abc\n");
055: fDocument.set(buffer.toString());
056: load(true);
057: IManifestHeader header = fModel.getBundle().getManifestHeader(
058: fHeaderName);
059: assertNotNull(header);
060:
061: ((BasePackageHeader) header).removePackage("com.example.abc");
062:
063: TextEdit[] ops = fListener.getTextOperations();
064: assertEquals(1, ops.length);
065:
066: ops[0].apply(fDocument);
067: assertEquals(4, fDocument.getNumberOfLines());
068: }
069:
070: public void testAddPackage() throws Exception {
071: StringBuffer buffer = new StringBuffer();
072: buffer.append("Manifest-Version: 1.0\n");
073: buffer.append("Bundle-ManifestVersion: 2\n");
074: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
075: buffer.append(fHeaderName);
076: buffer.append(": com.example.abc\n");
077: fDocument.set(buffer.toString());
078: load(true);
079:
080: IManifestHeader header = fModel.getBundle().getManifestHeader(
081: fHeaderName);
082: addPackage(header, "com.example.abc.actions");
083: TextEdit[] ops = fListener.getTextOperations();
084: assertEquals(1, ops.length);
085:
086: ops[0].apply(fDocument);
087: assertEquals(6, fDocument.getNumberOfLines());
088: assertEquals(0, fDocument.getLineLength(5));
089:
090: int pos = fDocument.getLineOffset(3);
091: int length = fDocument.getLineLength(3)
092: + fDocument.getLineLength(4);
093:
094: StringBuffer expected = new StringBuffer(fHeaderName
095: + ": com.example.abc,\n");
096: expected.append(" com.example.abc.actions\n");
097: assertEquals(expected.toString(), fDocument.get(pos, length));
098: }
099:
100: public void testAddImportPackageHeader() throws Exception {
101: StringBuffer buffer = new StringBuffer();
102: buffer.append("Manifest-Version: 1.0\n");
103: buffer.append("Bundle-ManifestVersion: 2\n");
104: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
105: fDocument.set(buffer.toString());
106: load(true);
107: fModel.getBundle().setHeader(fHeaderName, "com.example.abc");
108:
109: IManifestHeader header = fModel.getBundle().getManifestHeader(
110: fHeaderName);
111: assertNotNull(header);
112: assertEquals(fHeaderName + ": com.example.abc\n", header
113: .write());
114:
115: TextEdit[] ops = fListener.getTextOperations();
116: assertEquals(1, ops.length);
117:
118: ops[0].apply(fDocument);
119: assertEquals(buffer.toString() + header.write(), fDocument
120: .get());
121: }
122:
123: public void testAddMultiplePackages() throws Exception {
124: StringBuffer buffer = new StringBuffer();
125: buffer.append("Manifest-Version: 1.0\n");
126: buffer.append("Bundle-ManifestVersion: 2\n");
127: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
128: buffer.append(fHeaderName);
129: buffer.append(": com.example.abc\n");
130: fDocument.set(buffer.toString());
131: load(true);
132: IManifestHeader header = fModel.getBundle().getManifestHeader(
133: fHeaderName);
134: addPackage(header, "com.example.abc.views");
135: addPackage(header, "com.example.abc.imports");
136: addPackage(header, "com.example.abc.exports");
137:
138: TextEdit[] ops = fListener.getTextOperations();
139: assertEquals(1, ops.length);
140:
141: ops[0].apply(fDocument);
142: assertEquals(8, fDocument.getNumberOfLines());
143: assertEquals(0, fDocument.getLineLength(7));
144:
145: int pos = fDocument.getLineOffset(3);
146: int length = fDocument.getLineOffset(7)
147: - fDocument.getLineOffset(3);
148: StringBuffer expected = new StringBuffer(fHeaderName
149: + ": com.example.abc,\n");
150: expected.append(" com.example.abc.exports,\n");
151: expected.append(" com.example.abc.imports,\n");
152: expected.append(" com.example.abc.views\n");
153: assertEquals(expected.toString(), fDocument.get(pos, length));
154: }
155:
156: public void testRemovePackage() throws Exception {
157: StringBuffer buffer = new StringBuffer();
158: buffer.append("Manifest-Version: 1.0\n");
159: buffer.append("Bundle-ManifestVersion: 2\n");
160: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
161: buffer.append(fHeaderName);
162: buffer.append(": com.example.abc,\n");
163: buffer.append(" com.example.abc.actions,\n");
164: buffer.append(" com.example.abc.refactoring\n");
165: fDocument.set(buffer.toString());
166: load(true);
167: IManifestHeader header = fModel.getBundle().getManifestHeader(
168: fHeaderName);
169: ((BasePackageHeader) header)
170: .removePackage("com.example.abc.actions");
171:
172: TextEdit[] ops = fListener.getTextOperations();
173: assertEquals(1, ops.length);
174:
175: ops[0].apply(fDocument);
176: assertEquals(6, fDocument.getNumberOfLines());
177: assertEquals(0, fDocument.getLineLength(5));
178:
179: int pos = fDocument.getLineOffset(3);
180: int length = fDocument.getLineOffset(5)
181: - fDocument.getLineOffset(3);
182: StringBuffer expected = new StringBuffer(fHeaderName
183: + ": com.example.abc,\n");
184: expected.append(" com.example.abc.refactoring\n");
185: assertEquals(expected.toString(), fDocument.get(pos, length));
186: }
187:
188: public void testRemoveMultiplePackages() throws Exception {
189: StringBuffer buffer = new StringBuffer();
190: buffer.append("Manifest-Version: 1.0\n");
191: buffer.append("Bundle-ManifestVersion: 2\n");
192: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
193: buffer.append(fHeaderName);
194: buffer.append(": com.example.abc,\n");
195: buffer.append(" com.example.abc.actions,\n");
196: buffer.append(" com.example.abc.refactoring\n");
197: fDocument.set(buffer.toString());
198: load(true);
199: IManifestHeader header = fModel.getBundle().getManifestHeader(
200: fHeaderName);
201: ((BasePackageHeader) header)
202: .removePackage("com.example.abc.actions");
203: ((BasePackageHeader) header).removePackage("com.example.abc");
204:
205: TextEdit[] ops = fListener.getTextOperations();
206: assertEquals(1, ops.length);
207:
208: ops[0].apply(fDocument);
209: assertEquals(5, fDocument.getNumberOfLines());
210: assertEquals(0, fDocument.getLineLength(4));
211:
212: int pos = fDocument.getLineOffset(3);
213: int length = fDocument.getLineLength(3);
214: assertEquals(fHeaderName + ": com.example.abc.refactoring\n",
215: fDocument.get(pos, length));
216: }
217:
218: public void testReadPackageWithVersion() {
219: StringBuffer buffer = new StringBuffer();
220: buffer.append("Manifest-Version: 1.0\n");
221: buffer.append("Bundle-ManifestVersion: 2\n");
222: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
223: buffer.append(fHeaderName);
224: buffer.append(": org.osgi.framework;version=\"1.3.0\"\n");
225: fDocument.set(buffer.toString());
226: load();
227:
228: IManifestHeader header = fModel.getBundle().getManifestHeader(
229: fHeaderName);
230: assertNotNull(header);
231:
232: PackageObject object = getPackage(header, "org.osgi.framework");
233: assertNotNull(object);
234: assertEquals("1.3.0", object.getVersion());
235: }
236:
237: public void testAddVersionToPackage() throws Exception {
238: StringBuffer buffer = new StringBuffer();
239: buffer.append("Manifest-Version: 1.0\n");
240: buffer.append("Bundle-ManifestVersion: 2\n");
241: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
242: buffer.append(fHeaderName);
243: buffer.append(": org.osgi.framework\n");
244: fDocument.set(buffer.toString());
245: load(true);
246: IManifestHeader header = fModel.getBundle().getManifestHeader(
247: fHeaderName);
248: assertNotNull(header);
249:
250: PackageObject object = getPackage(header, "org.osgi.framework");
251: assertNotNull(object);
252: object.setVersion("1.3.0");
253:
254: TextEdit[] ops = fListener.getTextOperations();
255: assertEquals(1, ops.length);
256:
257: ops[0].apply(fDocument);
258: assertEquals(5, fDocument.getNumberOfLines());
259: assertEquals(0, fDocument.getLineLength(4));
260:
261: int pos = fDocument.getLineOffset(3);
262: int length = fDocument.getLineLength(3);
263: assertEquals(fHeaderName
264: + ": org.osgi.framework;version=\"1.3.0\"\n", fDocument
265: .get(pos, length));
266: }
267:
268: public void testRemoveVersionFromPackage() throws Exception {
269: StringBuffer buffer = new StringBuffer();
270: buffer.append("Manifest-Version: 1.0\n");
271: buffer.append("Bundle-ManifestVersion: 2\n");
272: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
273: buffer.append(fHeaderName);
274: buffer.append(": org.osgi.framework\n");
275: fDocument.set(buffer.toString());
276: load(true);
277: IManifestHeader header = fModel.getBundle().getManifestHeader(
278: fHeaderName);
279: assertNotNull(header);
280:
281: PackageObject object = getPackage(header, "org.osgi.framework");
282: assertNotNull(object);
283: object.setVersion(null);
284:
285: TextEdit[] ops = fListener.getTextOperations();
286: assertEquals(1, ops.length);
287:
288: ops[0].apply(fDocument);
289: assertEquals(5, fDocument.getNumberOfLines());
290: assertEquals(0, fDocument.getLineLength(4));
291:
292: int pos = fDocument.getLineOffset(3);
293: int length = fDocument.getLineLength(3);
294: assertEquals(fHeaderName + ": org.osgi.framework\n", fDocument
295: .get(pos, length));
296: }
297:
298: public void testAddPackageWithWindowsDelimiter() throws Exception {
299: StringBuffer buffer = new StringBuffer();
300: buffer.append("Manifest-Version: 1.0\r\n");
301: buffer.append("Bundle-ManifestVersion: 2\r\n");
302: buffer.append("Bundle-SymoblicName: com.example.xyz\r\n");
303: buffer.append(fHeaderName);
304: buffer.append(": com.example.abc\r\n");
305: fDocument.set(buffer.toString());
306: load(true);
307:
308: IManifestHeader header = fModel.getBundle().getManifestHeader(
309: fHeaderName);
310: addPackage(header, "com.example.abc.actions");
311: TextEdit[] ops = fListener.getTextOperations();
312: assertEquals(1, ops.length);
313:
314: ops[0].apply(fDocument);
315: assertEquals(6, fDocument.getNumberOfLines());
316: assertEquals(0, fDocument.getLineLength(5));
317:
318: int pos = fDocument.getLineOffset(3);
319: int length = fDocument.getLineLength(3)
320: + fDocument.getLineLength(4);
321:
322: StringBuffer expected = new StringBuffer(fHeaderName
323: + ": com.example.abc,\r\n");
324: expected.append(" com.example.abc.actions\r\n");
325: assertEquals(expected.toString(), fDocument.get(pos, length));
326: }
327:
328: public void testRemovePackageWithWindowsDelimiter()
329: throws Exception {
330: StringBuffer buffer = new StringBuffer();
331: buffer.append("Manifest-Version: 1.0\r\n");
332: buffer.append("Bundle-ManifestVersion: 2\r\n");
333: buffer.append("Bundle-SymoblicName: com.example.xyz\r\n");
334: buffer.append(fHeaderName);
335: buffer.append(": com.example.abc,\r\n");
336: buffer.append(" com.example.abc.actions,\r\n");
337: buffer.append(" com.example.abc.refactoring\r\n");
338: fDocument.set(buffer.toString());
339: load(true);
340: IManifestHeader header = fModel.getBundle().getManifestHeader(
341: fHeaderName);
342: ((BasePackageHeader) header)
343: .removePackage("com.example.abc.actions");
344:
345: TextEdit[] ops = fListener.getTextOperations();
346: assertEquals(1, ops.length);
347:
348: ops[0].apply(fDocument);
349: assertEquals(6, fDocument.getNumberOfLines());
350: assertEquals(0, fDocument.getLineLength(5));
351:
352: int pos = fDocument.getLineOffset(3);
353: int length = fDocument.getLineOffset(5)
354: - fDocument.getLineOffset(3);
355: StringBuffer expected = new StringBuffer(fHeaderName
356: + ": com.example.abc,\r\n");
357: expected.append(" com.example.abc.refactoring\r\n");
358: assertEquals(expected.toString(), fDocument.get(pos, length));
359: }
360:
361: public void testPreserveSpacing() throws Exception {
362: StringBuffer buffer = new StringBuffer();
363: buffer.append("Manifest-Version: 1.0\n");
364: buffer.append("Bundle-ManifestVersion: 2\n");
365: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
366: buffer.append(fHeaderName);
367: buffer.append(": \n");
368: buffer.append(" com.example.abc\n");
369: fDocument.set(buffer.toString());
370: load(true);
371:
372: IManifestHeader header = fModel.getBundle().getManifestHeader(
373: fHeaderName);
374: addPackage(header, "com.example.abc.actions");
375: TextEdit[] ops = fListener.getTextOperations();
376: assertEquals(1, ops.length);
377:
378: ops[0].apply(fDocument);
379: assertEquals(7, fDocument.getNumberOfLines());
380: assertEquals(0, fDocument.getLineLength(6));
381:
382: int pos = fDocument.getLineOffset(3);
383: int length = fDocument.getLineOffset(6)
384: - fDocument.getLineOffset(3);
385:
386: StringBuffer expected = new StringBuffer(fHeaderName + ": \n");
387: expected.append(" com.example.abc,\n");
388: expected.append(" com.example.abc.actions\n");
389: assertEquals(expected.toString(), fDocument.get(pos, length));
390:
391: }
392:
393: protected abstract void addPackage(IManifestHeader header,
394: String packageName);
395:
396: protected abstract PackageObject getPackage(IManifestHeader header,
397: String packageName);
398: }
|