01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.ui.tests.model.xml;
11:
12: import junit.framework.TestCase;
13:
14: import org.eclipse.core.runtime.CoreException;
15: import org.eclipse.jface.text.BadLocationException;
16: import org.eclipse.jface.text.Document;
17: import org.eclipse.pde.internal.core.text.IModelTextChangeListener;
18: import org.eclipse.pde.internal.core.text.plugin.PluginModel;
19: import org.eclipse.pde.internal.core.text.plugin.XMLTextChangeListener;
20: import org.eclipse.text.edits.MalformedTreeException;
21: import org.eclipse.text.edits.MultiTextEdit;
22: import org.eclipse.text.edits.TextEdit;
23:
24: public abstract class XMLModelTestCase extends TestCase {
25:
26: protected static final String LF = "\n";
27: protected static final String CR = "\r";
28: protected static final String CRLF = CR + LF;
29:
30: protected Document fDocument;
31: protected PluginModel fModel;
32: protected IModelTextChangeListener fListener;
33:
34: protected void setUp() throws Exception {
35: fDocument = new Document();
36: }
37:
38: protected void load() {
39: load(false);
40: }
41:
42: protected void load(boolean addListener) {
43: try {
44: fModel = new PluginModel(fDocument, true);
45: fModel.load();
46: if (!fModel.isLoaded() || !fModel.isValid())
47: fail("model cannot be loaded");
48: if (addListener) {
49: fListener = new XMLTextChangeListener(fModel
50: .getDocument());
51: fModel.addModelChangedListener(fListener);
52: }
53: } catch (CoreException e) {
54: fail("model cannot be loaded");
55: }
56: }
57:
58: protected void setXMLContents(StringBuffer body, String newline) {
59: StringBuffer sb = new StringBuffer();
60: sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
61: sb.append(newline);
62: sb.append("<?eclipse version=\"3.2\"?>");
63: sb.append(newline);
64: sb.append("<plugin>");
65: sb.append(newline);
66: if (body != null)
67: sb.append(body.toString());
68: sb.append(newline);
69: sb.append("</plugin>");
70: sb.append(newline);
71: fDocument.set(sb.toString());
72: }
73:
74: protected void reload() {
75: TextEdit[] ops = fListener.getTextOperations();
76: if (ops.length == 0)
77: return;
78: MultiTextEdit multi = new MultiTextEdit();
79: multi.addChildren(ops);
80: try {
81: multi.apply(fDocument);
82: } catch (MalformedTreeException e) {
83: fail(e.getMessage());
84: } catch (BadLocationException e) {
85: fail(e.getMessage());
86: }
87: load();
88: }
89:
90: }
|