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.ExportPackageHeader;
017: import org.eclipse.pde.internal.core.text.bundle.ExportPackageObject;
018: import org.eclipse.pde.internal.core.text.bundle.PackageFriend;
019: import org.eclipse.pde.internal.core.text.bundle.PackageObject;
020: import org.eclipse.text.edits.TextEdit;
021: import org.osgi.framework.Constants;
022:
023: public class ExportPackageTestCase extends PackageHeaderTestCase {
024:
025: public ExportPackageTestCase() {
026: super (Constants.EXPORT_PACKAGE);
027: }
028:
029: public static Test suite() {
030: return new TestSuite(ExportPackageTestCase.class);
031: }
032:
033: protected void addPackage(IManifestHeader header, String packageName) {
034: PackageObject packageObject = new PackageObject(
035: (ExportPackageHeader) header, packageName, null, null);
036: ((ExportPackageHeader) header).addPackage(packageObject);
037: }
038:
039: protected PackageObject getPackage(IManifestHeader header,
040: String packageName) {
041: return ((ExportPackageHeader) header).getPackage(packageName);
042: }
043:
044: public void testReadInternalPackage() {
045: StringBuffer buffer = new StringBuffer();
046: buffer.append("Manifest-Version: 1.0\n");
047: buffer.append("Bundle-ManifestVersion: 2\n");
048: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
049: buffer.append(fHeaderName
050: + ": com.example.abc;x-internal:=true\n");
051: fDocument.set(buffer.toString());
052: load();
053:
054: IManifestHeader header = fModel.getBundle().getManifestHeader(
055: fHeaderName);
056: assertNotNull(header);
057:
058: ExportPackageObject object = (ExportPackageObject) getPackage(
059: header, "com.example.abc");
060: assertNotNull(object);
061: assertEquals(object.getFriends().length, 0);
062: assertTrue(object.isInternal());
063: }
064:
065: public void testReadOneFriend() {
066: StringBuffer buffer = new StringBuffer();
067: buffer.append("Manifest-Version: 1.0\n");
068: buffer.append("Bundle-ManifestVersion: 2\n");
069: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
070: buffer.append(fHeaderName
071: + ": com.example.abc;x-friends:=com.example.xyz\n");
072: fDocument.set(buffer.toString());
073: load();
074:
075: IManifestHeader header = fModel.getBundle().getManifestHeader(
076: fHeaderName);
077: assertNotNull(header);
078:
079: ExportPackageObject object = (ExportPackageObject) getPackage(
080: header, "com.example.abc");
081: assertNotNull(object);
082: assertTrue(object.isInternal());
083: PackageFriend[] friends = object.getFriends();
084: assertEquals(friends.length, 1);
085: assertTrue(friends[0].getName().equals("com.example.xyz"));
086: }
087:
088: public void testReadMultipleFriend() {
089: StringBuffer buffer = new StringBuffer();
090: buffer.append("Manifest-Version: 1.0\n");
091: buffer.append("Bundle-ManifestVersion: 2\n");
092: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
093: buffer
094: .append(fHeaderName
095: + ": com.example.abc;x-friends:=\"com.example.xxx,com.example.yyy,com.example.zzz\"\n");
096: fDocument.set(buffer.toString());
097: load();
098:
099: IManifestHeader header = fModel.getBundle().getManifestHeader(
100: fHeaderName);
101: assertNotNull(header);
102:
103: ExportPackageObject object = (ExportPackageObject) getPackage(
104: header, "com.example.abc");
105: assertNotNull(object);
106: assertTrue(object.isInternal());
107: PackageFriend[] friends = object.getFriends();
108: assertEquals(friends.length, 3);
109: assertTrue(friends[0].getName().equals("com.example.xxx"));
110: assertTrue(friends[1].getName().equals("com.example.yyy"));
111: assertTrue(friends[2].getName().equals("com.example.zzz"));
112: }
113:
114: public void testSetPackageInternal() throws Exception {
115: StringBuffer buffer = new StringBuffer();
116: buffer.append("Manifest-Version: 1.0\n");
117: buffer.append("Bundle-ManifestVersion: 2\n");
118: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
119: buffer.append(fHeaderName + ": org.osgi.framework\n");
120: fDocument.set(buffer.toString());
121: load(true);
122: IManifestHeader header = fModel.getBundle().getManifestHeader(
123: fHeaderName);
124: assertNotNull(header);
125:
126: ExportPackageObject object = (ExportPackageObject) getPackage(
127: header, "org.osgi.framework");
128: assertNotNull(object);
129: object.setInternal(true);
130:
131: TextEdit[] ops = fListener.getTextOperations();
132: assertEquals(1, ops.length);
133:
134: ops[0].apply(fDocument);
135: assertEquals(5, fDocument.getNumberOfLines());
136: assertEquals(0, fDocument.getLineLength(4));
137:
138: int pos = fDocument.getLineOffset(3);
139: int length = fDocument.getLineLength(3);
140: assertEquals(fHeaderName
141: + ": org.osgi.framework;x-internal:=true\n", fDocument
142: .get(pos, length));
143: }
144:
145: public void testAddPackageFriend() throws Exception {
146: StringBuffer buffer = new StringBuffer();
147: buffer.append("Manifest-Version: 1.0\n");
148: buffer.append("Bundle-ManifestVersion: 2\n");
149: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
150: buffer.append(fHeaderName + ": org.osgi.framework\n");
151: fDocument.set(buffer.toString());
152: load(true);
153: IManifestHeader header = fModel.getBundle().getManifestHeader(
154: fHeaderName);
155: assertNotNull(header);
156:
157: ExportPackageObject object = (ExportPackageObject) getPackage(
158: header, "org.osgi.framework");
159: assertNotNull(object);
160: object
161: .addFriend(new PackageFriend(object,
162: "org.eclipse.pde.ui"));
163: TextEdit[] ops = fListener.getTextOperations();
164: assertEquals(1, ops.length);
165:
166: ops[0].apply(fDocument);
167: assertEquals(5, fDocument.getNumberOfLines());
168: assertEquals(0, fDocument.getLineLength(4));
169:
170: int pos = fDocument.getLineOffset(3);
171: int length = fDocument.getLineLength(3);
172: assertEquals(
173: fHeaderName
174: + ": org.osgi.framework;x-friends:=\"org.eclipse.pde.ui\"\n",
175: fDocument.get(pos, length));
176: }
177:
178: public void testAddPackageFriends() throws Exception {
179: StringBuffer buffer = new StringBuffer();
180: buffer.append("Manifest-Version: 1.0\n");
181: buffer.append("Bundle-ManifestVersion: 2\n");
182: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
183: buffer.append(fHeaderName + ": org.osgi.framework\n");
184: fDocument.set(buffer.toString());
185: load(true);
186: IManifestHeader header = fModel.getBundle().getManifestHeader(
187: fHeaderName);
188: assertNotNull(header);
189:
190: ExportPackageObject object = (ExportPackageObject) getPackage(
191: header, "org.osgi.framework");
192: assertNotNull(object);
193: object.addFriend(new PackageFriend(object,
194: "org.eclipse.pde.core"));
195: object
196: .addFriend(new PackageFriend(object,
197: "org.eclipse.pde.ui"));
198: TextEdit[] ops = fListener.getTextOperations();
199: assertEquals(1, ops.length);
200:
201: ops[0].apply(fDocument);
202: assertEquals(5, fDocument.getNumberOfLines());
203: assertEquals(0, fDocument.getLineLength(4));
204:
205: int pos = fDocument.getLineOffset(3);
206: int length = fDocument.getLineLength(3);
207: assertEquals(
208: fHeaderName
209: + ": org.osgi.framework;x-friends:=\"org.eclipse.pde.core,org.eclipse.pde.ui\"\n",
210: fDocument.get(pos, length));
211: }
212: }
|