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.TestCase;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.jface.text.Document;
016: import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
017: import org.eclipse.pde.internal.core.text.bundle.BundleModel;
018: import org.eclipse.pde.internal.core.text.bundle.BundleTextChangeListener;
019:
020: public abstract class BundleModelTestCase extends TestCase {
021:
022: protected Document fDocument;
023: protected BundleModel fModel;
024: protected BundleTextChangeListener fListener;
025: protected String fHeaderName;
026:
027: public BundleModelTestCase(String headerName) {
028: fHeaderName = headerName;
029: }
030:
031: protected void setUp() throws Exception {
032: fDocument = new Document();
033: }
034:
035: protected void load() {
036: load(false);
037: }
038:
039: protected void load(boolean addListener) {
040: try {
041: fModel = new BundleModel(fDocument, false);
042: fModel.load();
043: if (!fModel.isLoaded() || !fModel.isValid())
044: fail("model cannot be loaded");
045: if (addListener) {
046: fListener = new BundleTextChangeListener(fModel
047: .getDocument());
048: fModel.addModelChangedListener(fListener);
049: }
050: } catch (CoreException e) {
051: fail("model cannot be loaded");
052: }
053: }
054:
055: public void testAbsentHeader() {
056: StringBuffer buffer = new StringBuffer();
057: buffer.append("Manifest-Version: 1.0\n");
058: buffer.append("Bundle-ManifestVersion: 2\n");
059: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
060: fDocument.set(buffer.toString());
061: load();
062:
063: assertNull(fModel.getBundle().getManifestHeader(fHeaderName));
064: }
065:
066: public void testPresentHeader() {
067: StringBuffer buffer = new StringBuffer();
068: buffer.append("Manifest-Version: 1.0\n");
069: buffer.append("Bundle-ManifestVersion: 2\n");
070: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
071: buffer.append(fHeaderName);
072: buffer.append(": com.example.abc\n");
073: fDocument.set(buffer.toString());
074: load();
075:
076: assertNotNull(fModel.getBundle().getManifestHeader(fHeaderName));
077: }
078:
079: public void testHeaderOffset1() throws Exception {
080: StringBuffer buffer = new StringBuffer();
081: buffer.append("Manifest-Version: 1.0\n");
082: buffer.append("Bundle-ManifestVersion: 2\n");
083: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
084: buffer.append(fHeaderName);
085: buffer.append(": com.example.abc\n");
086: fDocument.set(buffer.toString());
087: load();
088:
089: IManifestHeader header = fModel.getBundle().getManifestHeader(
090: fHeaderName);
091: assertEquals(fDocument.getLineOffset(3), header.getOffset());
092: }
093:
094: public void testHeaderOffset2() throws Exception {
095: StringBuffer buffer = new StringBuffer();
096: buffer.append("Manifest-Version: 1.0\n");
097: buffer.append("Bundle-ManifestVersion: 2\n");
098: buffer.append(fHeaderName);
099: buffer.append(": com.example.abc\n");
100: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
101: fDocument.set(buffer.toString());
102: load();
103:
104: IManifestHeader header = fModel.getBundle().getManifestHeader(
105: fHeaderName);
106: assertEquals(fDocument.getLineOffset(2), header.getOffset());
107: }
108:
109: public void testHeaderLength() throws Exception {
110: StringBuffer buffer = new StringBuffer();
111: buffer.append("Manifest-Version: 1.0\n");
112: buffer.append("Bundle-ManifestVersion: 2\n");
113: buffer.append("Bundle-SymoblicName: com.example.xyz\n");
114: buffer.append(fHeaderName);
115: buffer.append(": com.example.abc\n");
116: fDocument.set(buffer.toString());
117: load();
118:
119: IManifestHeader header = fModel.getBundle().getManifestHeader(
120: fHeaderName);
121: assertEquals(fDocument.getLineLength(3), header.getLength());
122: }
123:
124: public void testHeaderLengthWithWindowsDelimiter() throws Exception {
125: StringBuffer buffer = new StringBuffer();
126: buffer.append("Manifest-Version: 1.0\r\n");
127: buffer.append("Bundle-ManifestVersion: 2\r\n");
128: buffer.append("Bundle-SymoblicName: com.example.xyz\r\n");
129: buffer.append(fHeaderName);
130: buffer.append(": com.example.abc\r\n");
131: fDocument.set(buffer.toString());
132: load();
133:
134: IManifestHeader header = fModel.getBundle().getManifestHeader(
135: fHeaderName);
136: assertEquals(fDocument.getLineLength(3), header.getLength());
137: }
138: }
|