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.ui.tests.performance;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.ByteArrayOutputStream;
014: import java.io.IOException;
015: import java.net.URL;
016: import java.util.zip.ZipEntry;
017: import java.util.zip.ZipInputStream;
018: import junit.framework.Assert;
019: import org.eclipse.core.resources.IFile;
020: import org.eclipse.core.resources.IProject;
021: import org.eclipse.core.resources.ResourcesPlugin;
022: import org.eclipse.core.runtime.CoreException;
023: import org.eclipse.core.runtime.NullProgressMonitor;
024: import org.eclipse.core.runtime.Platform;
025: import org.eclipse.test.performance.PerformanceTestCase;
026: import org.eclipse.ui.IViewPart;
027: import org.eclipse.ui.IWorkbenchPage;
028: import org.eclipse.ui.PartInitException;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.views.navigator.ResourceNavigator;
031: import org.osgi.framework.Bundle;
032:
033: /**
034: * This class/test was originally written for WTP bug 106158, and adapted to a
035: * generic "platform" test case, since some of the problem was due to bug
036: * 107121
037: *
038: * Thanks for Jeffrey Liu (jeffliu@ca.ibm.com) who wrote the test for WTP.
039: *
040: * And, thanks to Eric Glass <ericglass@maximus.com> for opening bug 106158
041: * (100% CPU for over 3 minutes opening a directory in the Navigator view with
042: * over 2000 HTML files) https://bugs.eclipse.org/bugs/show_bug.cgi?id=106158
043: * and for providing 2500 "generic" HTML files for the original test case in
044: * WTP.
045: *
046: * modified by David Williams for platform level test that does not depend on
047: * WTP or, for that matter, any meaningful content type. The content type
048: * assumed there is designed simply to "take a while" to complete, if asked
049: * for its properties.
050: *
051: * Note, since this test companion "ContentDescriberForTestsOnly", simply uses
052: * "sleep" to simulate computations, it only effects Elapsed Time (not CPU
053: * Time).
054: *
055: */
056: public class OpenNavigatorFolderTest extends PerformanceTestCase {
057: /*
058: * performance testcase for bug 106158
059: * https://bugs.eclipse.org/bugs/show_bug.cgi?id=106158
060: */
061: public void testOpenNavigatorFolder() {
062: IProject project = createProject("testViewAndContentTypeProject");
063: Bundle bundle = Platform.getBundle("org.eclipse.ui.tests");
064: URL url = bundle.getEntry("data/testContentType.zip");
065: ZipInputStream zis = null;
066: try {
067: zis = new ZipInputStream(url.openStream());
068: ZipEntry entry = zis.getNextEntry();
069: while (entry != null) {
070: ByteArrayOutputStream baos = new ByteArrayOutputStream();
071: try {
072: byte[] b = new byte[2048];
073: int read = zis.read(b);
074: while (read != -1) {
075: baos.write(b, 0, read);
076: read = zis.read(b);
077: }
078: } catch (IOException e) {
079: Assert.fail(e.getMessage());
080: } finally {
081: try {
082: baos.close();
083: } catch (IOException e) {
084: Assert.fail(e.getMessage());
085: }
086: }
087: IFile file = project.getFile(entry.getName());
088: ByteArrayInputStream bais = new ByteArrayInputStream(
089: baos.toByteArray());
090: try {
091: if (!file.exists())
092: file.create(bais, true,
093: new NullProgressMonitor());
094: else
095: file.setContents(bais, true, false,
096: new NullProgressMonitor());
097: } catch (CoreException e) {
098: Assert.fail(e.getMessage());
099: } finally {
100: try {
101: bais.close();
102: } catch (IOException e) {
103: Assert.fail(e.getMessage());
104: }
105: }
106: entry = zis.getNextEntry();
107: }
108: } catch (IOException e) {
109: Assert.fail(e.getMessage());
110: } finally {
111: try {
112: if (zis != null) {
113: zis.close();
114: }
115: } catch (IOException e) {
116: Assert.fail(e.getMessage());
117: }
118: }
119: startMeasuring();
120: IWorkbenchPage activePage = PlatformUI.getWorkbench()
121: .getActiveWorkbenchWindow().getActivePage();
122: IViewPart view = null;
123: try {
124: view = activePage
125: .showView("org.eclipse.ui.views.ResourceNavigator");
126: } catch (PartInitException e) {
127: Assert.fail(e.getMessage());
128: }
129: ResourceNavigator navigatorView = null;
130: try {
131: navigatorView = (ResourceNavigator) view;
132: } catch (ClassCastException e) {
133: Assert.fail(e.getMessage());
134: }
135: navigatorView.getTreeViewer().expandAll();
136: stopMeasuring();
137: commitMeasurements();
138: assertPerformance();
139: }
140:
141: private IProject createProject(String name) {
142: IProject project = ResourcesPlugin.getWorkspace().getRoot()
143: .getProject(name);
144: if (!project.exists()) {
145: try {
146: project.create(new NullProgressMonitor());
147: project.open(new NullProgressMonitor());
148: } catch (CoreException e) {
149: Assert.fail(e.getMessage());
150: }
151: }
152: return project;
153: }
154: }
|