/*******************************************************************************
* Copyright (c) 2006-2010 eBay Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*******************************************************************************/
/**
*
*/
package org.ebayopensource.turmeric.eclipse.test.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
/**
* The Class SimpleTestUtil.
*
* @author rpallikonda
*/
public class SimpleTestUtil {
/**
* Sets the auto building.
*
* @param flag the new auto building
* @throws CoreException the core exception
*/
public static void setAutoBuilding(boolean flag ) throws CoreException {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceDescription description = workspace.getDescription();
description.setAutoBuilding(flag);
workspace.setDescription(description);
}
/*
* Read content from IFile into String
*/
/**
* Read contents from i file.
*
* @param file the file
* @return the string
* @throws CoreException the core exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public static String readContentsFromIFile(IFile file) throws CoreException, IOException {
InputStream is = file.getContents();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
reader.close();
return sb.toString();
}
/**
* Read file as string.
*
* @param filePath the file path
* @return the string
* @throws IOException Signals that an I/O exception has occurred.
*/
public static String readFileAsString(File filePath)
throws java.io.IOException{
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = new BufferedReader(
new FileReader(filePath));
char[] buf = new char[1024];
int numRead=0;
while((numRead=reader.read(buf)) != -1){
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
return fileData.toString();
}
}