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 junit.framework.Test;
013: import junit.framework.TestSuite;
014:
015: import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
016: import org.eclipse.pde.internal.core.text.bundle.ImportPackageHeader;
017: import org.eclipse.pde.internal.core.text.bundle.ImportPackageObject;
018: import org.eclipse.pde.internal.core.text.bundle.PackageObject;
019: import org.eclipse.text.edits.TextEdit;
020: import org.osgi.framework.Constants;
021:
022: public class ImportPackageTestCase extends PackageHeaderTestCase {
023:
024: public ImportPackageTestCase() {
025: super (Constants.IMPORT_PACKAGE);
026: }
027:
028: public static Test suite() {
029: return new TestSuite(ImportPackageTestCase.class);
030: }
031:
032: public void testReadOptionalPackage() {
033: StringBuffer buffer = new StringBuffer();
034: buffer.append("Manifest-Version: 1.0\n");
035: buffer.append("Bundle-ManifestVersion: 2\n");
036: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
037: buffer.append(fHeaderName
038: + ": com.example.abc;resolution:=optional\n");
039: fDocument.set(buffer.toString());
040: load();
041:
042: IManifestHeader header = fModel.getBundle().getManifestHeader(
043: fHeaderName);
044: assertNotNull(header);
045:
046: ImportPackageObject object = (ImportPackageObject) getPackage(
047: header, "com.example.abc");
048: assertNotNull(object);
049: assertTrue(object.isOptional());
050: }
051:
052: public void testMakePackageOptional() throws Exception {
053: StringBuffer buffer = new StringBuffer();
054: buffer.append("Manifest-Version: 1.0\n");
055: buffer.append("Bundle-ManifestVersion: 2\n");
056: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
057: buffer.append(fHeaderName + ": org.osgi.framework\n");
058: fDocument.set(buffer.toString());
059: load(true);
060: IManifestHeader header = fModel.getBundle().getManifestHeader(
061: fHeaderName);
062: assertNotNull(header);
063:
064: ImportPackageObject object = (ImportPackageObject) getPackage(
065: header, "org.osgi.framework");
066: assertNotNull(object);
067: object.setOptional(true);
068:
069: TextEdit[] ops = fListener.getTextOperations();
070: assertEquals(1, ops.length);
071:
072: ops[0].apply(fDocument);
073: assertEquals(5, fDocument.getNumberOfLines());
074: assertEquals(0, fDocument.getLineLength(4));
075:
076: int pos = fDocument.getLineOffset(3);
077: int length = fDocument.getLineLength(3);
078: assertEquals(fHeaderName
079: + ": org.osgi.framework;resolution:=optional\n",
080: fDocument.get(pos, length));
081: }
082:
083: public void testRemoveOptionalDirective() throws Exception {
084: StringBuffer buffer = new StringBuffer();
085: buffer.append("Manifest-Version: 1.0\n");
086: buffer.append("Bundle-ManifestVersion: 2\n");
087: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
088: buffer.append(fHeaderName
089: + ": org.osgi.framework;resolution:=optional\n");
090: fDocument.set(buffer.toString());
091: load(true);
092: IManifestHeader header = fModel.getBundle().getManifestHeader(
093: fHeaderName);
094: assertNotNull(header);
095:
096: ImportPackageObject object = (ImportPackageObject) getPackage(
097: header, "org.osgi.framework");
098: assertNotNull(object);
099: object.setOptional(false);
100:
101: TextEdit[] ops = fListener.getTextOperations();
102: assertEquals(1, ops.length);
103:
104: ops[0].apply(fDocument);
105: assertEquals(5, fDocument.getNumberOfLines());
106: assertEquals(0, fDocument.getLineLength(4));
107:
108: int pos = fDocument.getLineOffset(3);
109: int length = fDocument.getLineLength(3);
110: assertEquals(fHeaderName + ": org.osgi.framework\n", fDocument
111: .get(pos, length));
112: }
113:
114: protected void addPackage(IManifestHeader header, String packageName) {
115: ((ImportPackageHeader) header).addPackage(packageName);
116:
117: }
118:
119: protected PackageObject getPackage(IManifestHeader header,
120: String packageName) {
121: return ((ImportPackageHeader) header).getPackage(packageName);
122: }
123: }
|