core/com.nokia.carbide.cpp.sysdoc.hover.tests/src/com/nokia/carbide/cpp/sysdoc/hover/TestHelper.java
author timkelly
Tue, 14 Apr 2009 10:18:48 -0500
changeset 91 2010205bb971
parent 2 d760517a8095
permissions -rw-r--r--
deprecate public API for new one that uses progress monitor (bug 7351)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
cawthron
parents:
diff changeset
     1
/*
cawthron
parents:
diff changeset
     2
 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
cawthron
parents:
diff changeset
     3
 * All rights reserved.
cawthron
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
cawthron
parents:
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
cawthron
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
cawthron
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
cawthron
parents:
diff changeset
     8
 *
cawthron
parents:
diff changeset
     9
 * Initial Contributors:
cawthron
parents:
diff changeset
    10
 * Nokia Corporation - initial contribution.
cawthron
parents:
diff changeset
    11
 *
cawthron
parents:
diff changeset
    12
 * Contributors:
cawthron
parents:
diff changeset
    13
 *				Deniz TURAN
cawthron
parents:
diff changeset
    14
 * Description: 
cawthron
parents:
diff changeset
    15
 * 				
cawthron
parents:
diff changeset
    16
 */
cawthron
parents:
diff changeset
    17
package com.nokia.carbide.cpp.sysdoc.hover;
cawthron
parents:
diff changeset
    18
cawthron
parents:
diff changeset
    19
import java.io.BufferedInputStream;
cawthron
parents:
diff changeset
    20
import java.io.File;
cawthron
parents:
diff changeset
    21
import java.io.FileOutputStream;
cawthron
parents:
diff changeset
    22
import java.io.InputStream;
cawthron
parents:
diff changeset
    23
import java.net.URL;
cawthron
parents:
diff changeset
    24
import java.util.Iterator;
cawthron
parents:
diff changeset
    25
import java.util.Set;
cawthron
parents:
diff changeset
    26
import java.util.concurrent.Callable;
cawthron
parents:
diff changeset
    27
import java.util.concurrent.FutureTask;
cawthron
parents:
diff changeset
    28
import java.util.zip.ZipEntry;
cawthron
parents:
diff changeset
    29
import java.util.zip.ZipInputStream;
cawthron
parents:
diff changeset
    30
cawthron
parents:
diff changeset
    31
import org.eclipse.cdt.core.model.CModelException;
cawthron
parents:
diff changeset
    32
import org.eclipse.cdt.core.model.CoreModel;
cawthron
parents:
diff changeset
    33
import org.eclipse.cdt.core.model.ICProject;
cawthron
parents:
diff changeset
    34
import org.eclipse.cdt.core.model.ITranslationUnit;
cawthron
parents:
diff changeset
    35
import org.eclipse.cdt.internal.core.model.CModelManager;
cawthron
parents:
diff changeset
    36
import org.eclipse.cdt.internal.ui.util.EditorUtility;
cawthron
parents:
diff changeset
    37
import org.eclipse.cdt.ui.CUIPlugin;
cawthron
parents:
diff changeset
    38
import org.eclipse.core.resources.IFile;
cawthron
parents:
diff changeset
    39
import org.eclipse.core.resources.IProject;
cawthron
parents:
diff changeset
    40
import org.eclipse.core.resources.IWorkspaceRoot;
cawthron
parents:
diff changeset
    41
import org.eclipse.core.runtime.FileLocator;
cawthron
parents:
diff changeset
    42
import org.eclipse.core.runtime.IPath;
cawthron
parents:
diff changeset
    43
import org.eclipse.core.runtime.Path;
cawthron
parents:
diff changeset
    44
import org.eclipse.swt.widgets.Display;
cawthron
parents:
diff changeset
    45
import org.eclipse.ui.IEditorPart;
cawthron
parents:
diff changeset
    46
import org.eclipse.ui.PartInitException;
cawthron
parents:
diff changeset
    47
import org.eclipse.ui.internal.WorkbenchPage;
cawthron
parents:
diff changeset
    48
import org.eclipse.ui.part.FileEditorInput;
cawthron
parents:
diff changeset
    49
cawthron
parents:
diff changeset
    50
import com.nokia.carbide.cpp.sysdoc.internal.hover.dal.AsynchronousLookup;
cawthron
parents:
diff changeset
    51
import com.nokia.carbide.cpp.sysdoc.internal.hover.dal.devlib.DevLibPluginController;
cawthron
parents:
diff changeset
    52
import com.nokia.carbide.cpp.sysdoc.internal.hover.dal.devlib.DevLibProperties;
cawthron
parents:
diff changeset
    53
import com.nokia.carbide.cpp.sysdoc.internal.hover.dal.interX.InterXIndexController;
cawthron
parents:
diff changeset
    54
import com.nokia.carbide.cpp.sysdoc.internal.hover.dal.interX.InterXProperties;
cawthron
parents:
diff changeset
    55
import com.nokia.carbide.cpp.sysdoc.internal.hover.dao.DevLibContent;
cawthron
parents:
diff changeset
    56
import com.nokia.carbide.cpp.sysdoc.internal.hover.uitlis.Logger;
cawthron
parents:
diff changeset
    57
cawthron
parents:
diff changeset
    58
public class TestHelper {
cawthron
parents:
diff changeset
    59
cawthron
parents:
diff changeset
    60
	private static String projectName="HW";
cawthron
parents:
diff changeset
    61
cawthron
parents:
diff changeset
    62
	public static Set<DevLibProperties> getAvailableDevLibProps()
cawthron
parents:
diff changeset
    63
			throws Exception {
cawthron
parents:
diff changeset
    64
cawthron
parents:
diff changeset
    65
		Set<DevLibProperties> devLibProps = DevLibPluginController
cawthron
parents:
diff changeset
    66
				.getInstance().getDevLibPropertiesSet();
cawthron
parents:
diff changeset
    67
cawthron
parents:
diff changeset
    68
		return devLibProps;
cawthron
parents:
diff changeset
    69
	}
cawthron
parents:
diff changeset
    70
cawthron
parents:
diff changeset
    71
	public static InterXProperties getAnInterXProps() {
cawthron
parents:
diff changeset
    72
		Set<DevLibProperties> devPropSet = null;
cawthron
parents:
diff changeset
    73
		try {
cawthron
parents:
diff changeset
    74
			devPropSet = getAvailableDevLibProps();
cawthron
parents:
diff changeset
    75
		} catch (Exception e) {
cawthron
parents:
diff changeset
    76
			return null;
cawthron
parents:
diff changeset
    77
		}
cawthron
parents:
diff changeset
    78
		if (devPropSet.isEmpty()) {
cawthron
parents:
diff changeset
    79
			return null;
cawthron
parents:
diff changeset
    80
		} else {
cawthron
parents:
diff changeset
    81
			Iterator<DevLibProperties> it = devPropSet.iterator();
cawthron
parents:
diff changeset
    82
			DevLibProperties devLib = it.next();
cawthron
parents:
diff changeset
    83
			return devLib.getInterXProperties();
cawthron
parents:
diff changeset
    84
		}
cawthron
parents:
diff changeset
    85
	}
cawthron
parents:
diff changeset
    86
cawthron
parents:
diff changeset
    87
	public static void waitIndexingComplete(long timeout) throws Exception {
cawthron
parents:
diff changeset
    88
		long sleep = 100;
cawthron
parents:
diff changeset
    89
		Display display = Activator.getDefault().getWorkbench().getDisplay();
cawthron
parents:
diff changeset
    90
		int totalSleep = 0;
cawthron
parents:
diff changeset
    91
		Thread.sleep(2000);
cawthron
parents:
diff changeset
    92
		Logger.logDebug("Waiting to timeout spend" + timeout);
cawthron
parents:
diff changeset
    93
		while (true) {
cawthron
parents:
diff changeset
    94
			if (totalSleep > timeout) {
cawthron
parents:
diff changeset
    95
				throw new Exception(
cawthron
parents:
diff changeset
    96
						"Timeout. Indexing has not finished in timout period:"
cawthron
parents:
diff changeset
    97
								+ timeout);
cawthron
parents:
diff changeset
    98
			}
cawthron
parents:
diff changeset
    99
			boolean indexingCompleted = InterXIndexController
cawthron
parents:
diff changeset
   100
					.isIndexingCompleted();
cawthron
parents:
diff changeset
   101
			if (indexingCompleted) {
cawthron
parents:
diff changeset
   102
				break;
cawthron
parents:
diff changeset
   103
			} else {
cawthron
parents:
diff changeset
   104
				display.readAndDispatch();
cawthron
parents:
diff changeset
   105
				Thread.sleep(sleep);
cawthron
parents:
diff changeset
   106
			}
cawthron
parents:
diff changeset
   107
			totalSleep += sleep;
cawthron
parents:
diff changeset
   108
		}
cawthron
parents:
diff changeset
   109
		Logger.logDebug("Waiting has finished " + timeout);
cawthron
parents:
diff changeset
   110
	}
cawthron
parents:
diff changeset
   111
cawthron
parents:
diff changeset
   112
	public static FutureTask<DevLibContent> createAsynchronousLookup(String fqn) {
cawthron
parents:
diff changeset
   113
		Callable<DevLibContent> callable = new AsynchronousLookup(fqn);
cawthron
parents:
diff changeset
   114
		FutureTask<DevLibContent> ft = new FutureTask<DevLibContent>(callable);
cawthron
parents:
diff changeset
   115
		new Thread(ft).start();
cawthron
parents:
diff changeset
   116
		return ft;
cawthron
parents:
diff changeset
   117
	}
cawthron
parents:
diff changeset
   118
cawthron
parents:
diff changeset
   119
	// this method creates a feature which simulates waiting
cawthron
parents:
diff changeset
   120
	public static FutureTask<DevLibContent> createAsynchronousLookupFuture(
cawthron
parents:
diff changeset
   121
			final String fqn) {
cawthron
parents:
diff changeset
   122
cawthron
parents:
diff changeset
   123
		Callable<DevLibContent> waitCallable = new Callable<DevLibContent>() {
cawthron
parents:
diff changeset
   124
			@Override
cawthron
parents:
diff changeset
   125
			public DevLibContent call() throws Exception {
cawthron
parents:
diff changeset
   126
				Thread.sleep(2000);
cawthron
parents:
diff changeset
   127
				Callable<DevLibContent> callable = new AsynchronousLookup(fqn);
cawthron
parents:
diff changeset
   128
				FutureTask<DevLibContent> ft1 = new FutureTask<DevLibContent>(
cawthron
parents:
diff changeset
   129
						callable);
cawthron
parents:
diff changeset
   130
				new Thread(ft1).start();
cawthron
parents:
diff changeset
   131
				Display display = Activator.getDefault().getWorkbench()
cawthron
parents:
diff changeset
   132
						.getDisplay();
cawthron
parents:
diff changeset
   133
				int sleep = 100;
cawthron
parents:
diff changeset
   134
				while (true) {
cawthron
parents:
diff changeset
   135
					if (ft1.isDone()) {
cawthron
parents:
diff changeset
   136
						return ft1.get();
cawthron
parents:
diff changeset
   137
					}
cawthron
parents:
diff changeset
   138
cawthron
parents:
diff changeset
   139
					if (ft1.isCancelled()) {
cawthron
parents:
diff changeset
   140
						return null;
cawthron
parents:
diff changeset
   141
					}
cawthron
parents:
diff changeset
   142
cawthron
parents:
diff changeset
   143
					display.readAndDispatch();
cawthron
parents:
diff changeset
   144
					Thread.sleep(sleep);
cawthron
parents:
diff changeset
   145
				}
cawthron
parents:
diff changeset
   146
			}
cawthron
parents:
diff changeset
   147
		};
cawthron
parents:
diff changeset
   148
cawthron
parents:
diff changeset
   149
		FutureTask<DevLibContent> ft = new FutureTask<DevLibContent>(
cawthron
parents:
diff changeset
   150
				waitCallable);
cawthron
parents:
diff changeset
   151
		new Thread(ft).start();
cawthron
parents:
diff changeset
   152
		return ft;
cawthron
parents:
diff changeset
   153
	}
cawthron
parents:
diff changeset
   154
cawthron
parents:
diff changeset
   155
	public static IEditorPart getIEditorPart() throws Exception{		
cawthron
parents:
diff changeset
   156
		IProject project=getProject();
cawthron
parents:
diff changeset
   157
		IFile file =  project.getFile("/src/HW.cpp");
cawthron
parents:
diff changeset
   158
		IEditorPart editorPart =EditorUtility.openInEditor(file);
cawthron
parents:
diff changeset
   159
		return editorPart;
cawthron
parents:
diff changeset
   160
//		IEditorPart findEditor = WorkbenchPage.this.getEditorManager().findEditor(new FileEditorInput(file));
cawthron
parents:
diff changeset
   161
		//WorkbenchHelper
cawthron
parents:
diff changeset
   162
	}
cawthron
parents:
diff changeset
   163
	
cawthron
parents:
diff changeset
   164
	
cawthron
parents:
diff changeset
   165
	private static IProject getProject() throws Exception{
cawthron
parents:
diff changeset
   166
		IProject project;
cawthron
parents:
diff changeset
   167
		IWorkspaceRoot root = CUIPlugin.getWorkspace().getRoot();
cawthron
parents:
diff changeset
   168
		IPath location = root.getLocation();
cawthron
parents:
diff changeset
   169
		URL src = FileLocator.find(ActivatorTest.getDefault().getBundle(),
cawthron
parents:
diff changeset
   170
				new Path("resource/HW.zip"), null);
cawthron
parents:
diff changeset
   171
		String dest = location.toOSString();
cawthron
parents:
diff changeset
   172
		uncompressedZip(src, dest);
cawthron
parents:
diff changeset
   173
		root.refreshLocal(0, null);
cawthron
parents:
diff changeset
   174
		Thread.sleep(2000);		
cawthron
parents:
diff changeset
   175
		project = CUIPlugin.getWorkspace().getRoot().getProject(projectName);
cawthron
parents:
diff changeset
   176
		if (!project.isOpen()){
cawthron
parents:
diff changeset
   177
			project.create(null);
cawthron
parents:
diff changeset
   178
			project.open(null);	
cawthron
parents:
diff changeset
   179
		}		
cawthron
parents:
diff changeset
   180
		return project;
cawthron
parents:
diff changeset
   181
	}
cawthron
parents:
diff changeset
   182
	
cawthron
parents:
diff changeset
   183
	public static ITranslationUnit getTranslationUnit()
cawthron
parents:
diff changeset
   184
			throws Exception {
cawthron
parents:
diff changeset
   185
	
cawthron
parents:
diff changeset
   186
		IProject project=getProject();
cawthron
parents:
diff changeset
   187
		IFile file =  project.getFile("/src/HW.cpp");		
cawthron
parents:
diff changeset
   188
		ITranslationUnit tUnit = (ITranslationUnit) CoreModel.getDefault()
cawthron
parents:
diff changeset
   189
				.create(file);
cawthron
parents:
diff changeset
   190
		return tUnit;
cawthron
parents:
diff changeset
   191
	}
cawthron
parents:
diff changeset
   192
cawthron
parents:
diff changeset
   193
	private static void uncompressedZip(URL jarSrc, String dest)
cawthron
parents:
diff changeset
   194
			throws Exception {
cawthron
parents:
diff changeset
   195
		InputStream os = jarSrc.openStream();
cawthron
parents:
diff changeset
   196
		BufferedInputStream bis = new BufferedInputStream(os);
cawthron
parents:
diff changeset
   197
		ZipInputStream zis = new ZipInputStream(bis);
cawthron
parents:
diff changeset
   198
		int i = 0;
cawthron
parents:
diff changeset
   199
		while (true) {
cawthron
parents:
diff changeset
   200
			ZipEntry zipEntry = zis.getNextEntry();
cawthron
parents:
diff changeset
   201
cawthron
parents:
diff changeset
   202
			if (zis.available() == 0 || zipEntry == null)
cawthron
parents:
diff changeset
   203
				break;
cawthron
parents:
diff changeset
   204
cawthron
parents:
diff changeset
   205
			uncompressZipEntry(zis, zipEntry, dest);
cawthron
parents:
diff changeset
   206
			i++;
cawthron
parents:
diff changeset
   207
		}
cawthron
parents:
diff changeset
   208
	}
cawthron
parents:
diff changeset
   209
cawthron
parents:
diff changeset
   210
	private static long uncompressZipEntry(ZipInputStream zis,
cawthron
parents:
diff changeset
   211
			ZipEntry zipEntry, String dest) throws Exception {
cawthron
parents:
diff changeset
   212
		byte[] buf = new byte[1024];
cawthron
parents:
diff changeset
   213
		String entryName = zipEntry.getName();
cawthron
parents:
diff changeset
   214
		int n;
cawthron
parents:
diff changeset
   215
		File rootDir = new File(dest);
cawthron
parents:
diff changeset
   216
		FileOutputStream fileoutputstream;
cawthron
parents:
diff changeset
   217
		File newFile = new File(rootDir, entryName);
cawthron
parents:
diff changeset
   218
		long totalLen = 0;
cawthron
parents:
diff changeset
   219
cawthron
parents:
diff changeset
   220
		if (zipEntry.isDirectory()) {
cawthron
parents:
diff changeset
   221
			if (!newFile.exists()) {
cawthron
parents:
diff changeset
   222
				newFile.mkdir();
cawthron
parents:
diff changeset
   223
			}
cawthron
parents:
diff changeset
   224
			return 0;
cawthron
parents:
diff changeset
   225
		}
cawthron
parents:
diff changeset
   226
		fileoutputstream = new FileOutputStream(newFile);
cawthron
parents:
diff changeset
   227
cawthron
parents:
diff changeset
   228
		while ((n = zis.read(buf, 0, 1024)) > -1) {
cawthron
parents:
diff changeset
   229
			fileoutputstream.write(buf, 0, n);
cawthron
parents:
diff changeset
   230
			totalLen += n;
cawthron
parents:
diff changeset
   231
		}
cawthron
parents:
diff changeset
   232
cawthron
parents:
diff changeset
   233
		fileoutputstream.close();
cawthron
parents:
diff changeset
   234
		zis.closeEntry();
cawthron
parents:
diff changeset
   235
		return totalLen;
cawthron
parents:
diff changeset
   236
	}
cawthron
parents:
diff changeset
   237
}