001: /*******************************************************************************
002: * Copyright (c) 2005, 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.RequireBundleHeader;
014: import org.eclipse.pde.internal.core.text.bundle.RequireBundleObject;
015: import org.eclipse.text.edits.TextEdit;
016: import org.osgi.framework.Constants;
017:
018: import junit.framework.Test;
019: import junit.framework.TestSuite;
020:
021: public class RequireBundleTestCase extends MultiLineHeaderTestCase {
022:
023: public RequireBundleTestCase() {
024: super (Constants.REQUIRE_BUNDLE);
025: }
026:
027: public static Test suite() {
028: return new TestSuite(RequireBundleTestCase.class);
029: }
030:
031: public void testAddRequireBundleHeader() throws Exception {
032: StringBuffer buffer = new StringBuffer();
033: buffer.append("Manifest-Version: 1.0\n");
034: buffer.append("Bundle-ManifestVersion: 2\n");
035: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
036: fDocument.set(buffer.toString());
037: load(true);
038: fModel.getBundle().setHeader(Constants.REQUIRE_BUNDLE,
039: "com.example.abc");
040:
041: IManifestHeader header = fModel.getBundle().getManifestHeader(
042: Constants.REQUIRE_BUNDLE);
043: assertNotNull(header);
044: assertEquals("Require-Bundle: com.example.abc\n", header
045: .write());
046:
047: TextEdit[] ops = fListener.getTextOperations();
048: assertEquals(1, ops.length);
049:
050: ops[0].apply(fDocument);
051: assertEquals(buffer.toString() + header.write(), fDocument
052: .get());
053: }
054:
055: public void testRemoveExistingRequireBundleHeader()
056: throws Exception {
057: StringBuffer buffer = new StringBuffer();
058: buffer.append("Manifest-Version: 1.0\n");
059: buffer.append("Bundle-ManifestVersion: 2\n");
060: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
061: buffer.append("Require-Bundle: com.example.abc\n");
062: fDocument.set(buffer.toString());
063: load(true);
064: IManifestHeader header = fModel.getBundle().getManifestHeader(
065: Constants.REQUIRE_BUNDLE);
066: assertNotNull(header);
067:
068: ((RequireBundleHeader) header).removeBundle("com.example.abc");
069:
070: TextEdit[] ops = fListener.getTextOperations();
071: assertEquals(1, ops.length);
072:
073: ops[0].apply(fDocument);
074: assertEquals(4, fDocument.getNumberOfLines());
075: }
076:
077: public void testAddBundle() throws Exception {
078: StringBuffer buffer = new StringBuffer();
079: buffer.append("Manifest-Version: 1.0\n");
080: buffer.append("Bundle-ManifestVersion: 2\n");
081: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
082: buffer.append("Require-Bundle: com.example.abc\n");
083: fDocument.set(buffer.toString());
084: load(true);
085:
086: IManifestHeader header = fModel.getBundle().getManifestHeader(
087: Constants.REQUIRE_BUNDLE);
088: ((RequireBundleHeader) header).addBundle("com.example.core");
089: TextEdit[] ops = fListener.getTextOperations();
090: assertEquals(1, ops.length);
091:
092: ops[0].apply(fDocument);
093: assertEquals(6, fDocument.getNumberOfLines());
094: assertEquals(0, fDocument.getLineLength(5));
095:
096: int pos = fDocument.getLineOffset(3);
097: int length = fDocument.getLineLength(3)
098: + fDocument.getLineLength(4);
099:
100: StringBuffer expected = new StringBuffer(
101: "Require-Bundle: com.example.abc,\n");
102: expected.append(" com.example.core\n");
103: assertEquals(expected.toString(), fDocument.get(pos, length));
104: }
105:
106: public void testAddMultipleBundles() throws Exception {
107: StringBuffer buffer = new StringBuffer();
108: buffer.append("Manifest-Version: 1.0\n");
109: buffer.append("Bundle-ManifestVersion: 2\n");
110: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
111: buffer.append("Require-Bundle: com.example.abc\n");
112: fDocument.set(buffer.toString());
113: load(true);
114: IManifestHeader header = fModel.getBundle().getManifestHeader(
115: Constants.REQUIRE_BUNDLE);
116: ((RequireBundleHeader) header).addBundle("com.example.core");
117: ((RequireBundleHeader) header).addBundle("com.example.ui");
118: ((RequireBundleHeader) header).addBundle("com.example");
119:
120: TextEdit[] ops = fListener.getTextOperations();
121: assertEquals(1, ops.length);
122:
123: ops[0].apply(fDocument);
124: assertEquals(8, fDocument.getNumberOfLines());
125: assertEquals(0, fDocument.getLineLength(7));
126:
127: int pos = fDocument.getLineOffset(3);
128: int length = fDocument.getLineOffset(7)
129: - fDocument.getLineOffset(3);
130: StringBuffer expected = new StringBuffer(
131: "Require-Bundle: com.example.abc,\n");
132: expected.append(" com.example.core,\n");
133: expected.append(" com.example.ui,\n");
134: expected.append(" com.example\n");
135: assertEquals(expected.toString(), fDocument.get(pos, length));
136: }
137:
138: public void testRemoveBundle() throws Exception {
139: StringBuffer buffer = new StringBuffer();
140: buffer.append("Manifest-Version: 1.0\n");
141: buffer.append("Bundle-ManifestVersion: 2\n");
142: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
143: buffer.append("Require-Bundle: com.example.abc,\n");
144: buffer.append(" com.example.core,\n");
145: buffer.append(" com.example.ui\n");
146: fDocument.set(buffer.toString());
147: load(true);
148: IManifestHeader header = fModel.getBundle().getManifestHeader(
149: Constants.REQUIRE_BUNDLE);
150: ((RequireBundleHeader) header).removeBundle("com.example.core");
151:
152: TextEdit[] ops = fListener.getTextOperations();
153: assertEquals(1, ops.length);
154:
155: ops[0].apply(fDocument);
156: assertEquals(6, fDocument.getNumberOfLines());
157: assertEquals(0, fDocument.getLineLength(5));
158:
159: int pos = fDocument.getLineOffset(3);
160: int length = fDocument.getLineOffset(5)
161: - fDocument.getLineOffset(3);
162: StringBuffer expected = new StringBuffer(
163: "Require-Bundle: com.example.abc,\n");
164: expected.append(" com.example.ui\n");
165: assertEquals(expected.toString(), fDocument.get(pos, length));
166: }
167:
168: public void testRemoveMultipleBundles() throws Exception {
169: StringBuffer buffer = new StringBuffer();
170: buffer.append("Manifest-Version: 1.0\n");
171: buffer.append("Bundle-ManifestVersion: 2\n");
172: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
173: buffer.append("Require-Bundle: com.example.abc,\n");
174: buffer.append(" com.example.core,\n");
175: buffer.append(" com.example.ui\n");
176: fDocument.set(buffer.toString());
177: load(true);
178: IManifestHeader header = fModel.getBundle().getManifestHeader(
179: Constants.REQUIRE_BUNDLE);
180: ((RequireBundleHeader) header).removeBundle("com.example.core");
181: ((RequireBundleHeader) header).removeBundle("com.example.abc");
182:
183: TextEdit[] ops = fListener.getTextOperations();
184: assertEquals(1, ops.length);
185:
186: ops[0].apply(fDocument);
187: assertEquals(5, fDocument.getNumberOfLines());
188: assertEquals(0, fDocument.getLineLength(4));
189:
190: int pos = fDocument.getLineOffset(3);
191: int length = fDocument.getLineLength(3);
192: assertEquals("Require-Bundle: com.example.ui\n", fDocument.get(
193: pos, length));
194: }
195:
196: public void testReadOptionalBundle() {
197: StringBuffer buffer = new StringBuffer();
198: buffer.append("Manifest-Version: 1.0\n");
199: buffer.append("Bundle-ManifestVersion: 2\n");
200: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
201: buffer
202: .append("Require-Bundle: com.example.abc;resolution:=optional\n");
203: fDocument.set(buffer.toString());
204: load();
205:
206: IManifestHeader header = fModel.getBundle().getManifestHeader(
207: Constants.REQUIRE_BUNDLE);
208: assertNotNull(header);
209:
210: RequireBundleObject bundle = getBundle(
211: (RequireBundleHeader) header, "com.example.abc");
212: assertNotNull(bundle);
213: assertTrue(bundle.isOptional());
214: }
215:
216: public void testSetGetVersion() {
217: StringBuffer buffer = new StringBuffer();
218: buffer.append("Manifest-Version: 1.0\n");
219: buffer.append("Bundle-ManifestVersion: 2\n");
220: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
221: buffer
222: .append("Require-Bundle: org.eclipse.osgi;bundle-version=\"[3.1.0,4.0.0)\"\n");
223: fDocument.set(buffer.toString());
224: load();
225:
226: IManifestHeader header = fModel.getBundle().getManifestHeader(
227: Constants.REQUIRE_BUNDLE);
228: assertNotNull(header);
229:
230: RequireBundleObject bundle = getBundle(
231: (RequireBundleHeader) header, "org.eclipse.osgi");
232: assertNotNull(bundle);
233: assertEquals("[3.1.0,4.0.0)", bundle.getVersion());
234:
235: bundle.setVersion("(1.9.9,3.0.9]");
236: assertEquals("(1.9.9,3.0.9]", bundle.getVersion());
237: }
238:
239: public void testReadBundleWithVersion() {
240: StringBuffer buffer = new StringBuffer();
241: buffer.append("Manifest-Version: 1.0\n");
242: buffer.append("Bundle-ManifestVersion: 2\n");
243: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
244: buffer
245: .append("Require-Bundle: org.eclipse.osgi;bundle-version=\"3.2.0\"\n");
246: fDocument.set(buffer.toString());
247: load();
248:
249: IManifestHeader header = fModel.getBundle().getManifestHeader(
250: Constants.REQUIRE_BUNDLE);
251: assertNotNull(header);
252:
253: RequireBundleObject bundle = getBundle(
254: (RequireBundleHeader) header, "org.eclipse.osgi");
255: assertNotNull(bundle);
256: assertEquals("3.2.0", bundle.getVersion());
257: }
258:
259: public void testMakeBundleOptional() throws Exception {
260: StringBuffer buffer = new StringBuffer();
261: buffer.append("Manifest-Version: 1.0\n");
262: buffer.append("Bundle-ManifestVersion: 2\n");
263: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
264: buffer.append("Require-Bundle: org.eclipse.osgi\n");
265: fDocument.set(buffer.toString());
266: load(true);
267: IManifestHeader header = fModel.getBundle().getManifestHeader(
268: Constants.REQUIRE_BUNDLE);
269: assertNotNull(header);
270:
271: RequireBundleObject bundle = getBundle(
272: (RequireBundleHeader) header, "org.eclipse.osgi");
273: assertNotNull(bundle);
274: bundle.setOptional(true);
275:
276: TextEdit[] ops = fListener.getTextOperations();
277: assertEquals(1, ops.length);
278:
279: ops[0].apply(fDocument);
280: assertEquals(5, fDocument.getNumberOfLines());
281: assertEquals(0, fDocument.getLineLength(4));
282:
283: int pos = fDocument.getLineOffset(3);
284: int length = fDocument.getLineLength(3);
285: assertEquals(
286: "Require-Bundle: org.eclipse.osgi;resolution:=optional\n",
287: fDocument.get(pos, length));
288: }
289:
290: public void testRemoveOptionalDirective() throws Exception {
291: StringBuffer buffer = new StringBuffer();
292: buffer.append("Manifest-Version: 1.0\n");
293: buffer.append("Bundle-ManifestVersion: 2\n");
294: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
295: buffer
296: .append("Require-Bundle: org.eclipse.osgi;resolution:=optional\n");
297: fDocument.set(buffer.toString());
298: load(true);
299: IManifestHeader header = fModel.getBundle().getManifestHeader(
300: Constants.REQUIRE_BUNDLE);
301: assertNotNull(header);
302:
303: RequireBundleObject object = getBundle(
304: (RequireBundleHeader) header, "org.eclipse.osgi");
305: assertNotNull(object);
306: object.setOptional(false);
307:
308: TextEdit[] ops = fListener.getTextOperations();
309: assertEquals(1, ops.length);
310:
311: ops[0].apply(fDocument);
312: assertEquals(5, fDocument.getNumberOfLines());
313: assertEquals(0, fDocument.getLineLength(4));
314:
315: int pos = fDocument.getLineOffset(3);
316: int length = fDocument.getLineLength(3);
317: assertEquals("Require-Bundle: org.eclipse.osgi\n", fDocument
318: .get(pos, length));
319: }
320:
321: public void testAddVersionToBundle() throws Exception {
322: StringBuffer buffer = new StringBuffer();
323: buffer.append("Manifest-Version: 1.0\n");
324: buffer.append("Bundle-ManifestVersion: 2\n");
325: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
326: buffer.append("Require-Bundle: org.eclipse.osgi\n");
327: fDocument.set(buffer.toString());
328: load(true);
329: IManifestHeader header = fModel.getBundle().getManifestHeader(
330: Constants.REQUIRE_BUNDLE);
331: assertNotNull(header);
332:
333: RequireBundleObject object = getBundle(
334: (RequireBundleHeader) header, "org.eclipse.osgi");
335: assertNotNull(object);
336: object.setVersion("3.2.0");
337:
338: TextEdit[] ops = fListener.getTextOperations();
339: assertEquals(1, ops.length);
340:
341: ops[0].apply(fDocument);
342: assertEquals(5, fDocument.getNumberOfLines());
343: assertEquals(0, fDocument.getLineLength(4));
344:
345: int pos = fDocument.getLineOffset(3);
346: int length = fDocument.getLineLength(3);
347: assertEquals(
348: "Require-Bundle: org.eclipse.osgi;bundle-version=\"3.2.0\"\n",
349: fDocument.get(pos, length));
350: }
351:
352: public void testRemoveVersionFromBundle() throws Exception {
353: StringBuffer buffer = new StringBuffer();
354: buffer.append("Manifest-Version: 1.0\n");
355: buffer.append("Bundle-ManifestVersion: 2\n");
356: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
357: buffer.append("Require-Bundle: org.eclipse.osgi\n");
358: fDocument.set(buffer.toString());
359: load(true);
360: IManifestHeader header = fModel.getBundle().getManifestHeader(
361: Constants.REQUIRE_BUNDLE);
362: assertNotNull(header);
363:
364: RequireBundleObject object = getBundle(
365: (RequireBundleHeader) header, "org.eclipse.osgi");
366: assertNotNull(object);
367: object.setVersion(null);
368:
369: TextEdit[] ops = fListener.getTextOperations();
370: assertEquals(1, ops.length);
371:
372: ops[0].apply(fDocument);
373: assertEquals(5, fDocument.getNumberOfLines());
374: assertEquals(0, fDocument.getLineLength(4));
375:
376: int pos = fDocument.getLineOffset(3);
377: int length = fDocument.getLineLength(3);
378: assertEquals("Require-Bundle: org.eclipse.osgi\n", fDocument
379: .get(pos, length));
380: }
381:
382: public void testAddBundleWithWindowsDelimiter() throws Exception {
383: StringBuffer buffer = new StringBuffer();
384: buffer.append("Manifest-Version: 1.0\r\n");
385: buffer.append("Bundle-ManifestVersion: 2\r\n");
386: buffer.append("Bundle-SymoblicName: com.example.xyz\r\n");
387: buffer.append("Require-Bundle: com.example.abc\r\n");
388: fDocument.set(buffer.toString());
389: load(true);
390:
391: IManifestHeader header = fModel.getBundle().getManifestHeader(
392: Constants.REQUIRE_BUNDLE);
393: ((RequireBundleHeader) header).addBundle("com.example.core");
394: TextEdit[] ops = fListener.getTextOperations();
395: assertEquals(1, ops.length);
396:
397: ops[0].apply(fDocument);
398: assertEquals(6, fDocument.getNumberOfLines());
399: assertEquals(0, fDocument.getLineLength(5));
400:
401: int pos = fDocument.getLineOffset(3);
402: int length = fDocument.getLineLength(3)
403: + fDocument.getLineLength(4);
404:
405: StringBuffer expected = new StringBuffer(
406: "Require-Bundle: com.example.abc,\r\n");
407: expected.append(" com.example.core\r\n");
408: assertEquals(expected.toString(), fDocument.get(pos, length));
409: }
410:
411: public void testRemoveBundleWithWindowsDelimiter() throws Exception {
412: StringBuffer buffer = new StringBuffer();
413: buffer.append("Manifest-Version: 1.0\r\n");
414: buffer.append("Bundle-ManifestVersion: 2\r\n");
415: buffer.append("Bundle-SymoblicName: com.example.xyz\r\n");
416: buffer.append("Require-Bundle: com.example.abc,\r\n");
417: buffer.append(" com.example.core,\r\n");
418: buffer.append(" com.example.ui\r\n");
419: fDocument.set(buffer.toString());
420: load(true);
421: IManifestHeader header = fModel.getBundle().getManifestHeader(
422: Constants.REQUIRE_BUNDLE);
423: ((RequireBundleHeader) header).removeBundle("com.example.core");
424:
425: TextEdit[] ops = fListener.getTextOperations();
426: assertEquals(1, ops.length);
427:
428: ops[0].apply(fDocument);
429: assertEquals(6, fDocument.getNumberOfLines());
430: assertEquals(0, fDocument.getLineLength(5));
431:
432: int pos = fDocument.getLineOffset(3);
433: int length = fDocument.getLineOffset(5)
434: - fDocument.getLineOffset(3);
435: StringBuffer expected = new StringBuffer(
436: "Require-Bundle: com.example.abc,\r\n");
437: expected.append(" com.example.ui\r\n");
438: assertEquals(expected.toString(), fDocument.get(pos, length));
439: }
440:
441: public void testPreserveSpacing() throws Exception {
442: StringBuffer buffer = new StringBuffer();
443: buffer.append("Manifest-Version: 1.0\n");
444: buffer.append("Bundle-ManifestVersion: 2\n");
445: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
446: buffer.append("Require-Bundle: \n");
447: buffer.append(" com.example.abc\n");
448: fDocument.set(buffer.toString());
449: load(true);
450:
451: IManifestHeader header = fModel.getBundle().getManifestHeader(
452: Constants.REQUIRE_BUNDLE);
453: ((RequireBundleHeader) header).addBundle("com.example.core");
454: TextEdit[] ops = fListener.getTextOperations();
455: assertEquals(1, ops.length);
456:
457: ops[0].apply(fDocument);
458: assertEquals(7, fDocument.getNumberOfLines());
459: assertEquals(0, fDocument.getLineLength(6));
460:
461: int pos = fDocument.getLineOffset(3);
462: int length = fDocument.getLineOffset(6)
463: - fDocument.getLineOffset(3);
464:
465: StringBuffer expected = new StringBuffer("Require-Bundle: \n");
466: expected.append(" com.example.abc,\n");
467: expected.append(" com.example.core\n");
468: assertEquals(expected.toString(), fDocument.get(pos, length));
469:
470: }
471:
472: public void testReadBundleReExport() {
473: StringBuffer buffer = new StringBuffer();
474: buffer.append("Manifest-Version: 1.0\n");
475: buffer.append("Bundle-ManifestVersion: 2\n");
476: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
477: buffer
478: .append("Require-Bundle: com.example.abc;visibility:=reexport\n");
479: fDocument.set(buffer.toString());
480: load();
481:
482: IManifestHeader header = fModel.getBundle().getManifestHeader(
483: Constants.REQUIRE_BUNDLE);
484: assertNotNull(header);
485:
486: RequireBundleObject bundle = getBundle(
487: (RequireBundleHeader) header, "com.example.abc");
488: assertNotNull(bundle);
489: assertTrue(bundle.isReexported());
490: }
491:
492: public void testMakeBundleReExport() throws Exception {
493: StringBuffer buffer = new StringBuffer();
494: buffer.append("Manifest-Version: 1.0\n");
495: buffer.append("Bundle-ManifestVersion: 2\n");
496: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
497: buffer.append("Require-Bundle: org.eclipse.osgi\n");
498: fDocument.set(buffer.toString());
499: load(true);
500: IManifestHeader header = fModel.getBundle().getManifestHeader(
501: Constants.REQUIRE_BUNDLE);
502: assertNotNull(header);
503:
504: RequireBundleObject bundle = getBundle(
505: (RequireBundleHeader) header, "org.eclipse.osgi");
506: assertNotNull(bundle);
507: bundle.setReexported(true);
508:
509: TextEdit[] ops = fListener.getTextOperations();
510: assertEquals(1, ops.length);
511:
512: ops[0].apply(fDocument);
513: assertEquals(5, fDocument.getNumberOfLines());
514: assertEquals(0, fDocument.getLineLength(4));
515:
516: int pos = fDocument.getLineOffset(3);
517: int length = fDocument.getLineLength(3);
518: assertEquals(
519: "Require-Bundle: org.eclipse.osgi;visibility:=reexport\n",
520: fDocument.get(pos, length));
521: }
522:
523: public void testRemoveReExport() throws Exception {
524: StringBuffer buffer = new StringBuffer();
525: buffer.append("Manifest-Version: 1.0\n");
526: buffer.append("Bundle-ManifestVersion: 2\n");
527: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
528: buffer
529: .append("Require-Bundle: org.eclipse.osgi;visibility:=reexport\n");
530: fDocument.set(buffer.toString());
531: load(true);
532: IManifestHeader header = fModel.getBundle().getManifestHeader(
533: Constants.REQUIRE_BUNDLE);
534: assertNotNull(header);
535:
536: RequireBundleObject object = getBundle(
537: (RequireBundleHeader) header, "org.eclipse.osgi");
538: assertNotNull(object);
539: object.setReexported(false);
540:
541: TextEdit[] ops = fListener.getTextOperations();
542: assertEquals(1, ops.length);
543:
544: ops[0].apply(fDocument);
545: assertEquals(5, fDocument.getNumberOfLines());
546: assertEquals(0, fDocument.getLineLength(4));
547:
548: int pos = fDocument.getLineOffset(3);
549: int length = fDocument.getLineLength(3);
550: assertEquals("Require-Bundle: org.eclipse.osgi\n", fDocument
551: .get(pos, length));
552: }
553:
554: private static RequireBundleObject getBundle(
555: RequireBundleHeader header, String id) {
556: RequireBundleObject[] bundles = header.getRequiredBundles();
557: for (int i = 0; i < bundles.length; i++)
558: if (bundles[i].getId().equals(id))
559: return bundles[i];
560: return null;
561: }
562:
563: }
|