debuggercdi/com.nokia.cdt.debug.cw.symbian.tests/src/com/nokia/cdt/debug/cw/symbian/tests/TestsPlugin.java
author wpaul
Wed, 25 Mar 2009 12:24:24 -0500
changeset 9 6ef327765a4e
parent 2 d760517a8095
child 127 c937102a5510
permissions -rw-r--r--
added copyright
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     1
/*
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     3
* All rights reserved.
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     4
* This component and the accompanying materials are made available
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     6
* which accompanies this distribution, and is available
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     8
*
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
     9
* Initial Contributors:
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    10
* Nokia Corporation - initial contribution.
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    11
*
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    12
* Contributors:
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    13
*
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    14
* Description: 
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    15
*
6ef327765a4e added copyright
wpaul
parents: 2
diff changeset
    16
*/
2
cawthron
parents:
diff changeset
    17
package com.nokia.cdt.debug.cw.symbian.tests;
cawthron
parents:
diff changeset
    18
cawthron
parents:
diff changeset
    19
import java.io.File;
cawthron
parents:
diff changeset
    20
import java.io.FilenameFilter;
cawthron
parents:
diff changeset
    21
import java.io.IOException;
cawthron
parents:
diff changeset
    22
import java.net.URL;
cawthron
parents:
diff changeset
    23
cawthron
parents:
diff changeset
    24
import junit.framework.TestCase;
cawthron
parents:
diff changeset
    25
cawthron
parents:
diff changeset
    26
import org.eclipse.core.runtime.FileLocator;
cawthron
parents:
diff changeset
    27
import org.eclipse.core.runtime.Path;
cawthron
parents:
diff changeset
    28
import org.eclipse.core.runtime.Platform;
cawthron
parents:
diff changeset
    29
import org.eclipse.core.runtime.Plugin;
cawthron
parents:
diff changeset
    30
import org.osgi.framework.Bundle;
cawthron
parents:
diff changeset
    31
import org.osgi.framework.BundleContext;
cawthron
parents:
diff changeset
    32
cawthron
parents:
diff changeset
    33
/**
cawthron
parents:
diff changeset
    34
 * The activator class controls the plug-in life cycle
cawthron
parents:
diff changeset
    35
 */
cawthron
parents:
diff changeset
    36
public class TestsPlugin extends Plugin {
cawthron
parents:
diff changeset
    37
cawthron
parents:
diff changeset
    38
	// The plug-in ID
cawthron
parents:
diff changeset
    39
	public static final String PLUGIN_ID = "com.nokia.cdt.debug.cw.symbian.tests";
cawthron
parents:
diff changeset
    40
cawthron
parents:
diff changeset
    41
	// The shared instance
cawthron
parents:
diff changeset
    42
	private static TestsPlugin plugin;
cawthron
parents:
diff changeset
    43
	
cawthron
parents:
diff changeset
    44
	/**
cawthron
parents:
diff changeset
    45
	 * The constructor
cawthron
parents:
diff changeset
    46
	 */
cawthron
parents:
diff changeset
    47
	public TestsPlugin() {
cawthron
parents:
diff changeset
    48
	}
cawthron
parents:
diff changeset
    49
cawthron
parents:
diff changeset
    50
	/*
cawthron
parents:
diff changeset
    51
	 * (non-Javadoc)
cawthron
parents:
diff changeset
    52
	 * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
cawthron
parents:
diff changeset
    53
	 */
cawthron
parents:
diff changeset
    54
	public void start(BundleContext context) throws Exception {
cawthron
parents:
diff changeset
    55
		super.start(context);
cawthron
parents:
diff changeset
    56
		plugin = this;
cawthron
parents:
diff changeset
    57
	}
cawthron
parents:
diff changeset
    58
cawthron
parents:
diff changeset
    59
	/*
cawthron
parents:
diff changeset
    60
	 * (non-Javadoc)
cawthron
parents:
diff changeset
    61
	 * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
cawthron
parents:
diff changeset
    62
	 */
cawthron
parents:
diff changeset
    63
	public void stop(BundleContext context) throws Exception {
cawthron
parents:
diff changeset
    64
		plugin = null;
cawthron
parents:
diff changeset
    65
		super.stop(context);
cawthron
parents:
diff changeset
    66
	}
cawthron
parents:
diff changeset
    67
cawthron
parents:
diff changeset
    68
	/**
cawthron
parents:
diff changeset
    69
	 * Returns the shared instance
cawthron
parents:
diff changeset
    70
	 *
cawthron
parents:
diff changeset
    71
	 * @return the shared instance
cawthron
parents:
diff changeset
    72
	 */
cawthron
parents:
diff changeset
    73
	public static TestsPlugin getDefault() {
cawthron
parents:
diff changeset
    74
		return plugin;
cawthron
parents:
diff changeset
    75
	}
cawthron
parents:
diff changeset
    76
cawthron
parents:
diff changeset
    77
	/************* Utility methods **********************/
cawthron
parents:
diff changeset
    78
	
cawthron
parents:
diff changeset
    79
	public static String[] getFileNamesInFolder(final String folderName, final String fileExtention) {
cawthron
parents:
diff changeset
    80
		File folder = new File(folderName);
cawthron
parents:
diff changeset
    81
		
cawthron
parents:
diff changeset
    82
		String[] files = folder.list(new FilenameFilter() {
cawthron
parents:
diff changeset
    83
cawthron
parents:
diff changeset
    84
			public boolean accept(File dir, String name) {
cawthron
parents:
diff changeset
    85
				return name.endsWith(fileExtention);
cawthron
parents:
diff changeset
    86
			}
cawthron
parents:
diff changeset
    87
			
cawthron
parents:
diff changeset
    88
		});
cawthron
parents:
diff changeset
    89
	
cawthron
parents:
diff changeset
    90
		return files;
cawthron
parents:
diff changeset
    91
	}
cawthron
parents:
diff changeset
    92
	
cawthron
parents:
diff changeset
    93
	public static String[] getFileFullNamesInFolder(final String folderName, final String fileExtention) {
cawthron
parents:
diff changeset
    94
		String[] files = getFileNamesInFolder(folderName, fileExtention);
cawthron
parents:
diff changeset
    95
cawthron
parents:
diff changeset
    96
		for (int i=0; i < files.length; i++) {
cawthron
parents:
diff changeset
    97
			files[i] = folderName + "\\" + files[i];
cawthron
parents:
diff changeset
    98
		}
cawthron
parents:
diff changeset
    99
		
cawthron
parents:
diff changeset
   100
		return files;
cawthron
parents:
diff changeset
   101
	}
cawthron
parents:
diff changeset
   102
	
cawthron
parents:
diff changeset
   103
	/**
cawthron
parents:
diff changeset
   104
	 * Get location of this plugin.
cawthron
parents:
diff changeset
   105
	 * 
cawthron
parents:
diff changeset
   106
	 * @return
cawthron
parents:
diff changeset
   107
	 * @throws IOException
cawthron
parents:
diff changeset
   108
	 */
cawthron
parents:
diff changeset
   109
    public static String getPluginPath() throws IOException {
cawthron
parents:
diff changeset
   110
        if (!Platform.isRunning()) {
cawthron
parents:
diff changeset
   111
            // get file relative to CWD (i.e. this project)
cawthron
parents:
diff changeset
   112
            File f = new File(".");
cawthron
parents:
diff changeset
   113
            f = f.getCanonicalFile();
cawthron
parents:
diff changeset
   114
            return f.getAbsolutePath();
cawthron
parents:
diff changeset
   115
        } else {
cawthron
parents:
diff changeset
   116
cawthron
parents:
diff changeset
   117
        	if (getDefault() == null)
cawthron
parents:
diff changeset
   118
				return null;
cawthron
parents:
diff changeset
   119
			Bundle bundle = getDefault().getBundle();
cawthron
parents:
diff changeset
   120
			if (bundle == null)
cawthron
parents:
diff changeset
   121
				return null;
cawthron
parents:
diff changeset
   122
			URL url = FileLocator.find(bundle, new Path("."), null);
cawthron
parents:
diff changeset
   123
			if (url == null)
cawthron
parents:
diff changeset
   124
				TestCase.fail("could not find URL for bundle: " + bundle);
cawthron
parents:
diff changeset
   125
			url = FileLocator.resolve(url);
cawthron
parents:
diff changeset
   126
			TestCase.assertEquals("file", url.getProtocol());
cawthron
parents:
diff changeset
   127
cawthron
parents:
diff changeset
   128
			return url.getPath();
cawthron
parents:
diff changeset
   129
        }
cawthron
parents:
diff changeset
   130
    }
cawthron
parents:
diff changeset
   131
cawthron
parents:
diff changeset
   132
	 /**
cawthron
parents:
diff changeset
   133
		 * Load a file relative to this plugin in the running workbench
cawthron
parents:
diff changeset
   134
		 * 
cawthron
parents:
diff changeset
   135
		 * @param file
cawthron
parents:
diff changeset
   136
		 * @return File
cawthron
parents:
diff changeset
   137
		 * @throws IOException
cawthron
parents:
diff changeset
   138
		 */
cawthron
parents:
diff changeset
   139
    public static File pluginRelativeFile(String fileName) throws IOException {
cawthron
parents:
diff changeset
   140
        String bundlePath = getPluginPath();
cawthron
parents:
diff changeset
   141
cawthron
parents:
diff changeset
   142
        if (bundlePath == null)
cawthron
parents:
diff changeset
   143
        	return null;
cawthron
parents:
diff changeset
   144
        
cawthron
parents:
diff changeset
   145
        return new File(bundlePath, fileName);
cawthron
parents:
diff changeset
   146
    }
cawthron
parents:
diff changeset
   147
cawthron
parents:
diff changeset
   148
    /**
cawthron
parents:
diff changeset
   149
     *  Find a file relative to the project.  Works if running
cawthron
parents:
diff changeset
   150
     *  in the workbench or standalone.
cawthron
parents:
diff changeset
   151
     *  @param file the relative path (from the project) to the file
cawthron
parents:
diff changeset
   152
     *  @return File
cawthron
parents:
diff changeset
   153
     */
cawthron
parents:
diff changeset
   154
    public static File projectRelativeFile(String file) throws Exception {
cawthron
parents:
diff changeset
   155
        File f;
cawthron
parents:
diff changeset
   156
        if (!Platform.isRunning()) {
cawthron
parents:
diff changeset
   157
            // get file relative to CWD (i.e. this project)
cawthron
parents:
diff changeset
   158
            f = new File(file);
cawthron
parents:
diff changeset
   159
            f = f.getCanonicalFile();
cawthron
parents:
diff changeset
   160
        } else {
cawthron
parents:
diff changeset
   161
            // get file relative to running plugin (still this project)
cawthron
parents:
diff changeset
   162
            f = pluginRelativeFile(file);
cawthron
parents:
diff changeset
   163
        }
cawthron
parents:
diff changeset
   164
        if (f == null)
cawthron
parents:
diff changeset
   165
            TestCase.fail("Cannot find file " + file + " relative to project");
cawthron
parents:
diff changeset
   166
        return f;
cawthron
parents:
diff changeset
   167
    }
cawthron
parents:
diff changeset
   168
}