Skip to content

Commit

Permalink
Added first structure builder test.
Browse files Browse the repository at this point in the history
  • Loading branch information
dragos committed Jul 6, 2011
1 parent 3ce779b commit 7ac50e5
Show file tree
Hide file tree
Showing 5 changed files with 423 additions and 0 deletions.
@@ -0,0 +1,62 @@
package scala.tools.eclipse.testsetup

import java.io._

object FileUtils {

def read(file: File): Array[Byte] = {
val len = file.length.toInt
val bytes = new Array[Byte](len)
val stream = new java.io.FileInputStream(file)
var bytesRead = 0
var lastReadSize = 0

try {
while ((lastReadSize != -1) && (bytesRead != len)) {
lastReadSize = stream.read(bytes, bytesRead, len - bytesRead)
bytesRead += lastReadSize
}
} finally {
stream.close()
}
bytes
}

/**
* Copy file from src (path to the original file) to dest (path to the destination file).
*/
def copy(src: File, dest: File) {
val srcBytes = read(src)

val out = new FileOutputStream(dest)
try {
out.write(srcBytes)
} finally {
out.close()
}
}

/**
* Copy the given source directory (and all its contents) to the given target directory.
*/
def copyDirectory(source: File, target: File) {
def shouldSkip(name: String) = name match {
case "CVS" | ".svn" | ".git" => true
case _ => false
}

if (!target.exists) target.mkdirs()

val files = source.listFiles();
if (files == null) return;

for (src <- files; val name = src.getName; if !shouldSkip(src.getName)) {
val targetChild = new File(target, name)
if (src.isDirectory)
copyDirectory(src, targetChild)
else
copy(src, targetChild)
}
}

}
@@ -0,0 +1,137 @@
package scala.tools.eclipse
package testsetup

import java.io.{ ByteArrayInputStream, File, IOException, InputStream }
import java.net.URL

import org.eclipse.core.resources.{ IFile, IFolder, IProject, IProjectDescription, IWorkspaceRoot, ResourcesPlugin }
import org.eclipse.core.runtime.{ CoreException, IPath, Path, Platform }
import org.eclipse.jdt.core.{ IClasspathEntry, ICompilationUnit, IJavaProject, IPackageFragment, IPackageFragmentRoot, IType, JavaCore, JavaModelException }
import org.eclipse.jdt.launching.JavaRuntime
import org.osgi.framework.Bundle

/** A test project, created from scratch.
*
* @author Miles Sabin
*/
class SDTTestProject(project : IProject) {
val location = project.getLocation.toOSString

val javaProject = JavaCore.create(project)
addJavaNature
addScalaNature
javaProject.setRawClasspath(new Array[IClasspathEntry](0), null)
addJavaSystemLibraries
addScalaSystemLibraries
val sourceFolder = createSourceFolder
val binFolder = createBinFolder
createOutputFolder(binFolder)

def this(remove : Boolean = false, projectName : String = "Project-1") {
this({
val root = ResourcesPlugin.getWorkspace.getRoot
val project = root.getProject(projectName)
if (remove)
project.delete(true, null)
project.create(null)
project.open(null)
project
})
}

def addJar(plugin : String, jar : String) {
val result = findFileInPlugin(plugin, jar)
addToClasspath(JavaCore.newLibraryEntry(result, null, null))
}

def createPackage(name : String) : IPackageFragment =
sourceFolder.createPackageFragment(name, false, null)

def createType(pack : IPackageFragment, cuName : String, source : String) : IType = {
val buf = new StringBuffer
buf.append("package " + pack.getElementName() + ";\n")
buf.append("\n")
buf.append(source)
val cu = pack.createCompilationUnit(cuName, buf.toString(), false, null)
cu.getTypes()(0)
}

def createFile(name : String, content : Array[Byte]) : IFile = {
val file = project.getFile(name)
val inputStream = new ByteArrayInputStream(content)
file.create(inputStream, true, null)
file
}

def createFolder(name : String) : IFolder = {
val folder = project.getFolder(name)
folder.create(true, true, null)
val keep = project.getFile(name + "/keep")
keep.create(new ByteArrayInputStream(Array(0)), true, null)
folder
}

def dispose {
if (project.exists)
project.delete(true, true, null)
else
SDTTestUtils.deleteRecursive(new File(location))
}

def createBinFolder() : IFolder = {
val binFolder = project.getFolder("bin")
binFolder.create(false, true, null)
binFolder
}

def addJavaNature() {
addNature(JavaCore.NATURE_ID)
}

def addScalaNature() {
addNature(ScalaPlugin.plugin.natureId)
}

def addNature(natureId : String) {
val description = project.getDescription
description.setNatureIds(natureId +: description.getNatureIds)
project.setDescription(description, null)
}

def addToClasspath(entry : IClasspathEntry) {
javaProject.setRawClasspath(entry +: javaProject.getRawClasspath, null)
}

def createOutputFolder(binFolder : IFolder) {
val outputLocation = binFolder.getFullPath
javaProject.setOutputLocation(outputLocation, null)
}

def createSourceFolder() : IPackageFragmentRoot = {
val folder = project.getFolder("src")
folder.create(false, true, null)
val root = javaProject.getPackageFragmentRoot(folder)
addToClasspath(JavaCore.newSourceEntry(root.getPath))
root
}

def addJavaSystemLibraries() {
addToClasspath(JavaRuntime.getDefaultJREContainerEntry)
}

def addScalaSystemLibraries() {
addToClasspath(JavaCore.newContainerEntry(Path.fromPortableString(ScalaPlugin.plugin.scalaLibId)))
}

def findFileInPlugin(plugin : String, file : String) : Path = {
val bundle = Platform.getBundle(plugin)
val resource = bundle.getResource(file)
new Path(resource.getPath)
}

def getFileContent(filepath : String) : String = {
val file = project.getFile(filepath)
val stream = file.getContents
SDTTestUtils.slurpAndClose(stream)
}
}
@@ -0,0 +1,116 @@
package scala.tools.eclipse
package testsetup

import org.eclipse.jdt.core.IJavaProject
import org.eclipse.jdt.core.JavaCore
import org.eclipse.core.runtime.FileLocator
import org.eclipse.core.runtime.Platform
import java.io.{ ByteArrayInputStream, File, IOException, InputStream }
import org.eclipse.core.resources.{ IContainer, IFile, IFolder, IProject, IProjectDescription, ResourcesPlugin }
import org.eclipse.core.runtime.{ IPath, Path }
import scala.tools.eclipse.util.OSGiUtils

/** Utility functions for setting up test projects.
*
* @author Miles Sabin
*/
object SDTTestUtils {

lazy val sourceWorkspaceLoc = {
val bundle= Platform.getBundle("org.scala-ide.sdt.core.tests")
OSGiUtils.pathInBundle(bundle, File.separatorChar + "test-workspace").get
}

lazy val workspace = ResourcesPlugin.getWorkspace

/** Setup the project in the target workspace. The 'name' project should
* exist in the source workspace.
*/
def setupProject(name: String): ScalaProject = {
val wspaceLoc = workspace.getRoot.getLocation
val src = new File(sourceWorkspaceLoc.toFile().getAbsolutePath + File.separatorChar + name)
val dst = new File(wspaceLoc.toFile().getAbsolutePath + File.separatorChar + name)
println("copying %s to %s".format(src, dst))
FileUtils.copyDirectory(src, dst)
val project = workspace.getRoot.getProject(name)
project.create(null)
project.open(null)
JavaCore.create(project)
ScalaPlugin.plugin.getScalaProject(project)
}

def deleteRecursive(d : File) {
if (d.exists) {
val filesOpt = Option(d.listFiles)
for (files <- filesOpt ; file <- files)
if (file.isDirectory)
deleteRecursive(file)
else
file.delete
d.delete
}
}

def createTempDir(name : String) : File = {
val userHome = new File(System.getProperty("user.home")).getAbsolutePath
val rootDir = new File(userHome, "SDTCoreTestTempDir")
val result = new File(rootDir, name)
if (result.exists)
deleteRecursive(result)
result
}

def deleteTempDirs() {
val userHome = new File(System.getProperty("user.home")).getAbsolutePath
val rootDir = new File(userHome, "SDTCoreTestTempDir")
if (rootDir.exists)
deleteRecursive(rootDir)
}

def addFileToProject(project : IProject, path : String, content : String) : IFile = {
val filePath = new Path(path)
val segments = filePath.segments
segments.foldLeft(project : IContainer) { (container, segment) =>
val folder = container.getFolder(new Path(segment))
if (!folder.exists())
folder.create(false, true, null)
folder
}
val file = project.getFile(filePath);
file.create(new ByteArrayInputStream(content.getBytes(project.getDefaultCharset())), true, null);
file
}

def changeContentOfFile(project : IProject, file : IFile, newContent : String) : IFile = {
file.setContents(new ByteArrayInputStream(newContent.getBytes(project.getDefaultCharset())), 0, null)
file
}

def createProjectInLocalFileSystem(parentFile : File, projectName : String) : IProject = {
val project = ResourcesPlugin.getWorkspace.getRoot.getProject(projectName)
if (project.exists)
project.delete(true, null)
val testFile = new File(parentFile, projectName)
if (testFile.exists)
deleteRecursive(testFile)

val desc = ResourcesPlugin.getWorkspace.newProjectDescription(projectName)
desc.setLocation(new Path(new File(parentFile, projectName).getPath))
project.create(desc, null)
project.open(null)
project
}

def slurpAndClose(inputStream : InputStream) : String = {
val stringBuilder = new StringBuilder
try {
var ch : Int = 0
while ({ ch = inputStream.read; ch } != -1) {
stringBuilder.append(ch.toChar)
}
} finally {
inputStream.close
}
stringBuilder.toString
}
}

0 comments on commit 7ac50e5

Please sign in to comment.