001: /*******************************************************************************
002: * Copyright (c) 2000, 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.IOException;
013: import java.io.InputStream;
014: import java.io.InputStreamReader;
015: import java.io.Reader;
016: import java.io.StringReader;
017: import java.util.ArrayList;
018: import java.util.Iterator;
019: import java.util.List;
020:
021: import org.eclipse.core.resources.IFile;
022: import org.eclipse.core.resources.IWorkspaceDescription;
023: import org.eclipse.core.resources.IWorkspaceRoot;
024: import org.eclipse.core.resources.IncrementalProjectBuilder;
025: import org.eclipse.core.resources.ResourcesPlugin;
026: import org.eclipse.core.runtime.CoreException;
027: import org.eclipse.core.runtime.Path;
028: import org.eclipse.ui.tests.harness.util.FileTool;
029:
030: public class ResourceTestHelper {
031:
032: public static void replicate(String src, String destPrefix,
033: String destSuffix, int n) throws CoreException {
034: for (int i = 0; i < n; i++)
035: copy(src, destPrefix + i + destSuffix);
036: }
037:
038: public static void copy(String src, String dest)
039: throws CoreException {
040: IFile file = getRoot().getFile(new Path(src));
041: file.copy(new Path(dest), true, null);
042: }
043:
044: public static void delete(String file) throws CoreException {
045: getRoot().getFile(new Path(file)).delete(true, null);
046: }
047:
048: public static IFile findFile(String path) {
049: return getRoot().getFile(new Path(path));
050: }
051:
052: public static IFile[] findFiles(String prefix, String suffix,
053: int i, int n) {
054: IWorkspaceRoot root = getRoot();
055: List files = new ArrayList(n - i);
056: for (int j = i; j < i + n; j++) {
057: String path = root.getLocation().toString() + "/" + prefix
058: + j + suffix;
059: files.add(findFile(path));
060: }
061: return (IFile[]) files.toArray(new IFile[files.size()]);
062: }
063:
064: public static StringBuffer read(String src) throws IOException,
065: CoreException {
066: return FileTool.read(new InputStreamReader(getRoot().getFile(
067: new Path(src)).getContents()));
068: }
069:
070: public static void write(String dest, final String content)
071: throws IOException, CoreException {
072: InputStream stream = new InputStream() {
073: private Reader fReader = new StringReader(content);
074:
075: public int read() throws IOException {
076: return fReader.read();
077: }
078: };
079: getRoot().getFile(new Path(dest)).create(stream, true, null);
080: }
081:
082: public static void replicate(String src, String destPrefix,
083: String destSuffix, int n, String srcName,
084: String destNamePrefix) throws IOException, CoreException {
085:
086: StringBuffer s = read(src);
087:
088: List positions = identifierPositions(s, srcName);
089:
090: for (int j = 0; j < n; j++) {
091: StringBuffer c = new StringBuffer(s.toString());
092: replacePositions(c, srcName.length(), destNamePrefix + j,
093: positions);
094: write(destPrefix + j + destSuffix, c.toString());
095: }
096: }
097:
098: public static void copy(String src, String dest, String srcName,
099: String destName) throws IOException, CoreException {
100: StringBuffer buf = read(src);
101: List positions = identifierPositions(buf, srcName);
102: replacePositions(buf, srcName.length(), destName, positions);
103: write(dest, buf.toString());
104: }
105:
106: private static void replacePositions(StringBuffer c,
107: int origLength, String string, List positions) {
108: int offset = 0;
109: for (Iterator iter = positions.iterator(); iter.hasNext();) {
110: int position = ((Integer) iter.next()).intValue();
111: c.replace(offset + position,
112: offset + position + origLength, string);
113: offset += string.length() - origLength;
114: }
115: }
116:
117: private static List identifierPositions(StringBuffer buffer,
118: String identifier) {
119: List positions = new ArrayList();
120: int i = -1;
121: while (true) {
122: i = buffer.indexOf(identifier, i + 1);
123: if (i == -1)
124: break;
125: if (i > 0
126: && Character.isJavaIdentifierPart(buffer
127: .charAt(i - 1)))
128: continue;
129: if (i < buffer.length() - 1
130: && Character.isJavaIdentifierPart(buffer.charAt(i
131: + identifier.length())))
132: continue;
133: positions.add(new Integer(i));
134: }
135: return positions;
136: }
137:
138: private static IWorkspaceRoot getRoot() {
139: return ResourcesPlugin.getWorkspace().getRoot();
140: }
141:
142: public static boolean enableAutoBuilding() {
143: IWorkspaceDescription description = ResourcesPlugin
144: .getWorkspace().getDescription();
145: boolean wasOff = !description.isAutoBuilding();
146: if (wasOff)
147: description.setAutoBuilding(true);
148: return wasOff;
149: }
150:
151: public static void incrementalBuild() throws CoreException {
152: ResourcesPlugin.getWorkspace().build(
153: IncrementalProjectBuilder.INCREMENTAL_BUILD, null);
154: }
155:
156: public static void fullBuild() throws CoreException {
157: ResourcesPlugin.getWorkspace().build(
158: IncrementalProjectBuilder.FULL_BUILD, null);
159: }
160:
161: public static boolean disableAutoBuilding() {
162: IWorkspaceDescription description = ResourcesPlugin
163: .getWorkspace().getDescription();
164: boolean wasOn = description.isAutoBuilding();
165: if (wasOn)
166: description.setAutoBuilding(false);
167: return wasOn;
168: }
169: }
|