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.BundleSymbolicNameHeader;
017: import org.eclipse.text.edits.TextEdit;
018: import org.osgi.framework.Constants;
019:
020: public class BundleSymbolicNameTestCase extends BundleModelTestCase {
021:
022: public BundleSymbolicNameTestCase() {
023: super (Constants.BUNDLE_SYMBOLICNAME);
024: }
025:
026: public static Test suite() {
027: return new TestSuite(BundleSymbolicNameTestCase.class);
028: }
029:
030: public void testAbsentHeader() {
031: StringBuffer buffer = new StringBuffer();
032: buffer.append("Manifest-Version: 1.0\n");
033: buffer.append("Bundle-ManifestVersion: 2\n");
034: fDocument.set(buffer.toString());
035: load();
036:
037: assertNull(fModel.getBundle().getManifestHeader(
038: Constants.BUNDLE_SYMBOLICNAME));
039: }
040:
041: public void testPresentHeader() {
042: StringBuffer buffer = new StringBuffer();
043: buffer.append("Manifest-Version: 1.0\n");
044: buffer.append("Bundle-ManifestVersion: 2\n");
045: buffer.append("Bundle-SymbolicName: com.example.xyz\n");
046: fDocument.set(buffer.toString());
047: load();
048:
049: assertNotNull(fModel.getBundle().getManifestHeader(
050: Constants.BUNDLE_SYMBOLICNAME));
051: }
052:
053: public void testHeaderOffset1() throws Exception {
054: StringBuffer buffer = new StringBuffer();
055: buffer.append("Manifest-Version: 1.0\n");
056: buffer.append("Bundle-ManifestVersion: 2\n");
057: buffer.append("Bundle-SymbolicName: com.example.xyz\n");
058: fDocument.set(buffer.toString());
059: load();
060:
061: IManifestHeader header = fModel.getBundle().getManifestHeader(
062: Constants.BUNDLE_SYMBOLICNAME);
063: assertEquals(fDocument.getLineOffset(2), header.getOffset());
064: }
065:
066: public void testHeaderOffset2() throws Exception {
067: StringBuffer buffer = new StringBuffer();
068: buffer.append("Manifest-Version: 1.0\n");
069: buffer.append("Bundle-ManifestVersion: 2\n");
070: buffer.append("Bundle-SymbolicName: com.example.xyz\n");
071: buffer.append("Require-Bundle: com.example.abc\n");
072: fDocument.set(buffer.toString());
073: load();
074:
075: IManifestHeader header = fModel.getBundle().getManifestHeader(
076: Constants.BUNDLE_SYMBOLICNAME);
077: assertEquals(fDocument.getLineOffset(2), header.getOffset());
078: }
079:
080: public void testHeaderLength() throws Exception {
081: StringBuffer buffer = new StringBuffer();
082: buffer.append("Manifest-Version: 1.0\n");
083: buffer.append("Bundle-ManifestVersion: 2\n");
084: buffer.append("Bundle-SymbolicName: com.example.xyz\n");
085: fDocument.set(buffer.toString());
086: load();
087:
088: IManifestHeader header = fModel.getBundle().getManifestHeader(
089: Constants.BUNDLE_SYMBOLICNAME);
090: assertEquals(fDocument.getLineLength(2), header.getLength());
091: }
092:
093: public void testHeaderLengthWithWindowsDelimiter() throws Exception {
094: StringBuffer buffer = new StringBuffer();
095: buffer.append("Manifest-Version: 1.0\r\n");
096: buffer.append("Bundle-ManifestVersion: 2\r\n");
097: buffer.append("Bundle-SymbolicName: com.example.xyz\r\n");
098: fDocument.set(buffer.toString());
099: load();
100:
101: IManifestHeader header = fModel.getBundle().getManifestHeader(
102: Constants.BUNDLE_SYMBOLICNAME);
103: assertEquals(fDocument.getLineLength(2), header.getLength());
104: }
105:
106: public void testAddBundleSymbolicName() 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("Require-Bundle: com.example.xyz\n");
111: fDocument.set(buffer.toString());
112: load(true);
113: fModel.getBundle().setHeader(Constants.BUNDLE_SYMBOLICNAME,
114: "com.example.abc");
115:
116: IManifestHeader header = fModel.getBundle().getManifestHeader(
117: Constants.BUNDLE_SYMBOLICNAME);
118: assertNotNull(header);
119: assertEquals("Bundle-SymbolicName: com.example.abc\n", header
120: .write());
121:
122: TextEdit[] ops = fListener.getTextOperations();
123: assertEquals(1, ops.length);
124:
125: ops[0].apply(fDocument);
126: assertEquals(buffer.toString() + header.write(), fDocument
127: .get());
128: }
129:
130: public void testRemoveBundleSymbolicName() throws Exception {
131: StringBuffer buffer = new StringBuffer();
132: buffer.append("Manifest-Version: 1.0\n");
133: buffer.append("Bundle-ManifestVersion: 2\n");
134: buffer.append("Bundle-SymbolicName: com.example.xyz\n");
135: buffer.append("Require-Bundle: com.example.abc\n");
136: fDocument.set(buffer.toString());
137: load(true);
138: IManifestHeader header = fModel.getBundle().getManifestHeader(
139: Constants.BUNDLE_SYMBOLICNAME);
140: assertNotNull(header);
141:
142: ((BundleSymbolicNameHeader) header).setId("");
143:
144: TextEdit[] ops = fListener.getTextOperations();
145: assertEquals(1, ops.length);
146:
147: ops[0].apply(fDocument);
148: assertEquals(4, fDocument.getNumberOfLines());
149: }
150:
151: public void testChangeBundleSymbolicName() throws Exception {
152: StringBuffer buffer = new StringBuffer();
153: buffer.append("Manifest-Version: 1.0\n");
154: buffer.append("Bundle-ManifestVersion: 2\n");
155: buffer.append("Bundle-SymbolicName: com.example.xyz\n");
156: buffer.append("Require-Bundle: com.example.abc\n");
157: fDocument.set(buffer.toString());
158: load(true);
159:
160: IManifestHeader header = fModel.getBundle().getManifestHeader(
161: Constants.BUNDLE_SYMBOLICNAME);
162: ((BundleSymbolicNameHeader) header).setId("com.example.core");
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(2);
171: int length = fDocument.getLineLength(2);
172:
173: assertEquals("Bundle-SymbolicName: com.example.core\n",
174: fDocument.get(pos, length));
175: }
176:
177: public void testReadSingletonDirective() {
178: StringBuffer buffer = new StringBuffer();
179: buffer.append("Manifest-Version: 1.0\n");
180: buffer.append("Bundle-ManifestVersion: 2\n");
181: buffer
182: .append("Bundle-SymbolicName: com.example.xyz; singleton:=true\n");
183: buffer.append("Require-Bundle: com.example.abc\n");
184: fDocument.set(buffer.toString());
185: load(true);
186:
187: IManifestHeader header = fModel.getBundle().getManifestHeader(
188: Constants.BUNDLE_SYMBOLICNAME);
189: assertNotNull(header);
190: assertTrue(((BundleSymbolicNameHeader) header).isSingleton());
191: }
192:
193: public void testAddSingletonDirective() throws Exception {
194: StringBuffer buffer = new StringBuffer();
195: buffer.append("Manifest-Version: 1.0\n");
196: buffer.append("Bundle-ManifestVersion: 2\n");
197: buffer.append("Bundle-SymbolicName: com.example.xyz\n");
198: buffer.append("Require-Bundle: org.eclipse.osgi\n");
199: fDocument.set(buffer.toString());
200: load(true);
201: IManifestHeader header = fModel.getBundle().getManifestHeader(
202: Constants.BUNDLE_SYMBOLICNAME);
203: assertNotNull(header);
204: ((BundleSymbolicNameHeader) header).setSingleton(true);
205:
206: TextEdit[] ops = fListener.getTextOperations();
207: assertEquals(1, ops.length);
208:
209: ops[0].apply(fDocument);
210: assertEquals(5, fDocument.getNumberOfLines());
211: assertEquals(0, fDocument.getLineLength(4));
212:
213: int pos = fDocument.getLineOffset(2);
214: int length = fDocument.getLineLength(2);
215: assertEquals(
216: "Bundle-SymbolicName: com.example.xyz;singleton:=true\n",
217: fDocument.get(pos, length));
218: }
219:
220: public void testRemoveSingletonDirective() throws Exception {
221: StringBuffer buffer = new StringBuffer();
222: buffer.append("Manifest-Version: 1.0\n");
223: buffer.append("Bundle-ManifestVersion: 2\n");
224: buffer
225: .append("Bundle-SymbolicName: com.example.xyz; singleton:=true\n");
226: buffer
227: .append("Require-Bundle: org.eclipse.osgi;resolution:=optional\n");
228: fDocument.set(buffer.toString());
229: load(true);
230: IManifestHeader header = fModel.getBundle().getManifestHeader(
231: Constants.BUNDLE_SYMBOLICNAME);
232: assertNotNull(header);
233: ((BundleSymbolicNameHeader) header).setSingleton(false);
234:
235: TextEdit[] ops = fListener.getTextOperations();
236: assertEquals(1, ops.length);
237:
238: ops[0].apply(fDocument);
239: assertEquals(5, fDocument.getNumberOfLines());
240: assertEquals(0, fDocument.getLineLength(4));
241:
242: int pos = fDocument.getLineOffset(2);
243: int length = fDocument.getLineLength(2);
244: assertEquals("Bundle-SymbolicName: com.example.xyz\n",
245: fDocument.get(pos, length));
246: }
247:
248: }
|