From: <ce...@us...> - 2004-01-12 17:27:53
|
Update of /cvsroot/csseditor/net.sourceforge.csseditor.tests/src/net/sourceforge/csseditor/tests In directory sc8-pr-cvs1:/tmp/cvs-serv27502/src/net/sourceforge/csseditor/tests Added Files: TextEditorTestCase.java Log Message: Add a generic TextEditorTestCase for testing stuff that relies on an actual text editor being present --- NEW FILE: TextEditorTestCase.java --- /* * Copyright (c) 2003 Christopher Lenz and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * Christopher Lenz - initial API and implementation * * $Id: TextEditorTestCase.java,v 1.1 2004/01/12 17:27:50 cell Exp $ */ package net.sourceforge.csseditor.tests; import java.io.ByteArrayInputStream; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.text.IDocument; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.ui.texteditor.ITextEditor; import junit.framework.TestCase; /** * A base test case for tests that need the full text editor environment set up * before being able to run. */ public abstract class TextEditorTestCase extends TestCase { // Instance Variables ------------------------------------------------------ private String fileName; private IProject project; private IFile file; private ITextEditor editor; // Constructors ------------------------------------------------------------ /** * Constructor. * * @param fileName The name of the test file to create and edit */ public TextEditorTestCase(String fileName) { this.fileName = fileName; } // TestCase Implementation ------------------------------------------------- /* * @see junit.framework.TestCase#setUp() */ protected void setUp() throws CoreException { // Prepare a test project containing a single empty CSS style sheet IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); project = root.getProject("test"); if (!project.exists()) { project.create(null); } else { project.refreshLocal(IResource.DEPTH_INFINITE, null); } if (!project.isOpen()) { project.open(null); } file = project.getFile(fileName); } /* * @see junit.framework.TestCase#tearDown() */ protected void tearDown() throws CoreException { if (editor != null) { editor.close(false); } project.delete(true, true, null); } // Protected Methods ------------------------------------------------------- /** * Returns the document opened in the editor. * * @return the opened document * @throws IllegalStateException if called before {@link #setUpEditor()} */ protected final IDocument getDocument() { if (editor == null) { throw new IllegalStateException("You must call setUpEditor() " + "before accessing the document"); } return editor.getDocumentProvider().getDocument( editor.getEditorInput()); } /** * Initializes the editor and its input, and returns the initialized editor. * * @param text the contents of the file to create and edit * @return the initialized editor * @throws CoreException if the file couldn't be created, or the editor * failed to open */ protected final ITextEditor setUpEditor(String text) throws CoreException { // Stream the given content into the file ByteArrayInputStream in = new ByteArrayInputStream(text.getBytes()); if (!file.exists()) { file.create(in, true, null); } else { file.setContents(in, true, false, null); } // Open the editor IWorkbench workbench = PlatformUI.getWorkbench(); IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage(); editor = (ITextEditor) page.openEditor(new FileEditorInput(file), workbench.getEditorRegistry().getDefaultEditor(fileName).getId()); return editor; } } |