001: /*******************************************************************************
002: * Copyright (c) 2005, 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.wizards;
011:
012: import java.io.ByteArrayInputStream;
013: import java.lang.reflect.InvocationTargetException;
014:
015: import junit.framework.Test;
016: import junit.framework.TestCase;
017: import junit.framework.TestSuite;
018:
019: import org.eclipse.core.resources.IFile;
020: import org.eclipse.core.resources.IFolder;
021: import org.eclipse.core.resources.IProject;
022: import org.eclipse.core.resources.IWorkspaceRoot;
023: import org.eclipse.core.resources.ResourcesPlugin;
024: import org.eclipse.core.runtime.CoreException;
025: import org.eclipse.core.runtime.IPath;
026: import org.eclipse.core.runtime.NullProgressMonitor;
027: import org.eclipse.core.runtime.Path;
028: import org.eclipse.core.runtime.Platform;
029: import org.eclipse.pde.internal.core.natures.PDE;
030: import org.eclipse.pde.internal.core.site.WorkspaceSiteModel;
031: import org.eclipse.pde.internal.ui.wizards.site.NewSiteProjectCreationOperation;
032: import org.eclipse.swt.widgets.Display;
033: import org.eclipse.ui.PlatformUI;
034: import org.eclipse.ui.progress.IProgressService;
035:
036: public class NewSiteProjectTestCase extends TestCase {
037: private static final String EXISTING_PROJECT_NAME = "ExistingSiteProject"; //$NON-NLS-1$
038:
039: public static Test suite() {
040: return new TestSuite(NewSiteProjectTestCase.class);
041: }
042:
043: /*
044: * (non-Javadoc)
045: *
046: * @see junit.framework.TestCase#setUp()
047: */
048: protected void setUp() throws Exception {
049: super .setUp();
050: if ("testExistingSiteProject".equalsIgnoreCase(getName())) { //$NON-NLS-1$
051: IProject project = ResourcesPlugin.getWorkspace().getRoot()
052: .getProject(EXISTING_PROJECT_NAME);
053: project.create(new NullProgressMonitor());
054: project.open(new NullProgressMonitor());
055: IFile file = project.getFile(new Path("site.xml")); //$NON-NLS-1$
056: String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //$NON-NLS-1$
057: + "<site>" //$NON-NLS-1$
058: + "<category-def name=\"new_category_1\" label=\"New Category 1\"/>" //$NON-NLS-1$
059: + "</site>"; //$NON-NLS-1$
060: ByteArrayInputStream source = new ByteArrayInputStream(
061: content.getBytes("ASCII")); //$NON-NLS-1$
062: if (file.exists())
063: file.setContents(source, true, false,
064: new NullProgressMonitor());
065: else
066: file.create(source, true, new NullProgressMonitor());
067: project.delete(false, true, new NullProgressMonitor());
068: }
069: }
070:
071: /*
072: * (non-Javadoc)
073: *
074: * @see junit.framework.TestCase#tearDown()
075: */
076: protected void tearDown() throws Exception {
077: IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace()
078: .getRoot();
079: IProject[] projects = workspaceRoot.getProjects();
080: try {
081: for (int i = 0; i < projects.length; i++) {
082: projects[i].delete(true, new NullProgressMonitor());
083: }
084: } catch (CoreException e) {
085: // do nothing if deletion fails. No need to fail the test.
086: }
087: super .tearDown();
088: }
089:
090: private void createSite(IProject project, IPath path,
091: String webLocation) throws InvocationTargetException,
092: InterruptedException {
093: NewSiteProjectCreationOperation createOperation = new NewSiteProjectCreationOperation(
094: Display.getDefault(), project, path, webLocation);
095: IProgressService progressService = PlatformUI.getWorkbench()
096: .getProgressService();
097: progressService.runInUI(progressService, createOperation, null);
098: }
099:
100: /**
101: * @param project
102: */
103: private void ensureCreated(IProject project) {
104: assertTrue("Project not created.", project.exists()); //$NON-NLS-1$
105: assertTrue("Project not open.", project.isOpen()); //$NON-NLS-1$
106: try {
107: assertTrue("Site nature not added.", project //$NON-NLS-1$
108: .hasNature(PDE.SITE_NATURE));
109: } catch (Exception e) {
110: }
111: assertTrue("site.xml not created.", project //$NON-NLS-1$
112: .exists(new Path("site.xml"))); //$NON-NLS-1$
113: WorkspaceSiteModel model = new WorkspaceSiteModel(project
114: .getFile(new Path("site.xml"))); //$NON-NLS-1$
115: model.load();
116: assertTrue("Model cannot be loaded.", model.isLoaded()); //$NON-NLS-1$
117: assertTrue("Model is not valid.", model.isValid()); //$NON-NLS-1$
118: assertFalse("ISite is null.", model.getSite() == null); //$NON-NLS-1$
119: model.dispose();
120: }
121:
122: public void testExistingSiteProject() {
123: IProject project = ResourcesPlugin.getWorkspace().getRoot()
124: .getProject(EXISTING_PROJECT_NAME);
125: IPath path = Platform.getLocation();
126: try {
127: createSite(project, path, null);
128: } catch (Exception e) {
129: e.printStackTrace();
130: fail("testExistingSiteProject: " + e); //$NON-NLS-1$
131: }
132: ensureCreated(project);
133: WorkspaceSiteModel model = new WorkspaceSiteModel(project
134: .getFile(new Path("site.xml"))); //$NON-NLS-1$
135: model.load();
136: assertTrue("Existig site overwritten.", model.getSite() //$NON-NLS-1$
137: .getCategoryDefinitions().length > 0);
138: model.dispose();
139:
140: }
141:
142: public void testSiteProject() {
143: String projectName = "SiteProject"; //$NON-NLS-1$
144: IProject project = ResourcesPlugin.getWorkspace().getRoot()
145: .getProject(projectName);
146: IPath path = Platform.getLocation();
147: try {
148: createSite(project, path, null);
149: } catch (Exception e) {
150: e.printStackTrace();
151: fail("testSiteProject: " + e); //$NON-NLS-1$
152: }
153: ensureCreated(project);
154: assertFalse(
155: "index.html should have not been generated.", project //$NON-NLS-1$
156: .exists(new Path("index.html"))); //$NON-NLS-1$
157: }
158:
159: public void testSiteProjectWithWeb() {
160: String projectName = "SiteProjectWithWeb"; //$NON-NLS-1$
161: IProject project = ResourcesPlugin.getWorkspace().getRoot()
162: .getProject(projectName);
163: IPath path = Platform.getLocation();
164: try {
165: createSite(project, path, "testWeb"); //$NON-NLS-1$
166: } catch (Exception e) {
167: e.printStackTrace();
168: fail("testSiteProjectWithWeb: " + e); //$NON-NLS-1$
169: }
170: ensureCreated(project);
171: assertTrue(
172: "index.html not generated.", project.exists(new Path( //$NON-NLS-1$
173: "index.html"))); //$NON-NLS-1$
174: IFolder webFolder = project.getFolder(new Path("testWeb")); //$NON-NLS-1$
175: assertTrue("Web folder not generated.", webFolder.exists()); //$NON-NLS-1$
176: assertTrue(
177: "site.xsl not generated.", webFolder.exists(new Path( //$NON-NLS-1$
178: "site.xsl"))); //$NON-NLS-1$
179: assertTrue(
180: "site.css not generated.", webFolder.exists(new Path( //$NON-NLS-1$
181: "site.css"))); //$NON-NLS-1$
182: }
183: }
|