1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); you 5 * may not use this file except in compliance with the License. You may obtain a 6 * copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.ide.eclipse.tests; 17 18 import com.android.ide.eclipse.adt.AndroidConstants; 19 20 import org.eclipse.core.runtime.FileLocator; 21 import org.eclipse.core.runtime.Platform; 22 23 import java.io.File; 24 import java.io.IOException; 25 import java.net.URL; 26 import java.util.logging.Logger; 27 28 /** 29 * Helper class for retrieving test data 30 * <p/> 31 * All tests which need to retrieve paths to test data files should go through this class. 32 */ 33 public class AdtTestData { 34 35 /** singleton instance */ 36 private static AdtTestData sInstance = null; 37 private static final Logger sLogger = Logger.getLogger(AdtTestData.class.getName()); 38 39 /** The absolute file path to the plugin's contents. */ 40 private String mOsRootDataPath; 41 AdtTestData()42 private AdtTestData() { 43 // can set test_data env variable to override default behavior of 44 // finding data using class loader 45 // useful when running in plugin environment, where test data is inside 46 // bundled jar, and must be extracted to temp filesystem location to be 47 // accessed normally 48 mOsRootDataPath = System.getProperty("test_data"); 49 if (mOsRootDataPath == null) { 50 sLogger.info("Cannot find test_data environment variable, init to class loader"); 51 URL url = this.getClass().getClassLoader().getResource("."); //$NON-NLS-1$ 52 53 if (Platform.isRunning()) { 54 sLogger.info("Running as an Eclipse Plug-in JUnit test, using FileLocator"); 55 try { 56 mOsRootDataPath = FileLocator.resolve(url).getFile(); 57 } catch (IOException e) { 58 sLogger.warning("IOException while using FileLocator, reverting to url"); 59 mOsRootDataPath = url.getFile(); 60 } 61 } else { 62 sLogger.info("Running as an plain JUnit test, using url as-is"); 63 mOsRootDataPath = url.getFile(); 64 } 65 } 66 67 if (mOsRootDataPath.equals(AndroidConstants.WS_SEP)) { 68 sLogger.warning("Resource data not found using class loader!, Defaulting to no path"); 69 } 70 71 if (!mOsRootDataPath.endsWith(File.separator)) { 72 sLogger.info("Fixing test_data env variable (does not end with path separator)"); 73 mOsRootDataPath = mOsRootDataPath.concat(File.separator); 74 } 75 } 76 77 /** Get the singleton instance of AdtTestData */ getInstance()78 public static AdtTestData getInstance() { 79 if (sInstance == null) { 80 sInstance = new AdtTestData(); 81 } 82 return sInstance; 83 } 84 85 /** 86 * Returns the absolute file path to a file located in this plugin. 87 * 88 * @param osRelativePath {@link String} path to file contained in plugin. Must 89 * use path separators appropriate to host OS 90 * 91 * @return absolute OS path to test file 92 */ getTestFilePath(String osRelativePath)93 public String getTestFilePath(String osRelativePath) { 94 return mOsRootDataPath + osRelativePath; 95 } 96 } 97