test added

This commit is contained in:
nik
2010-02-27 19:19:26 +03:00
parent 4d7dd78542
commit 68528e7ae3
18 changed files with 478 additions and 7 deletions
+2
View File
@@ -4,6 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/testSrc" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
@@ -12,6 +13,7 @@
<orderEntry type="library" name="Javac2" level="project" />
<orderEntry type="module" module-name="antlayout" />
<orderEntry type="library" name="Gant" level="application" />
<orderEntry type="library" scope="TEST" name="JUnit" level="project" />
</component>
</module>
+1
View File
@@ -3,6 +3,7 @@ def ctx = context(scope: scriptScope(), filetypes : ["gant"])
contributor ([ctx], {
property name:"project", type:"org.jetbrains.jps.Project"
method name:"jdk", type:"org.jetbrains.jps.JavaSdk", params:[name:"String", jdkPath:"String"]
method name:"jdk", type:"org.jetbrains.jps.JavaSdk", params:[name:"String", jdkPath:"String", initializer:{}]
method name:"globalLibrary", type:"org.jetbrains.jps.Library", params: [name:"String", initializer:{}]
method name:"library", type:"org.jetbrains.jps.Library", params: [name:"String", initializer:{}]
})
+6 -3
View File
@@ -22,9 +22,12 @@ final class Jps {
return project.createGlobalLibrary(name, initializer)
})
binding.setVariable("jdk", {String name, String path ->
//todo[nik] support initializer parameter
return project.createJavaSdk(name, path, {})
binding.setVariable("jdk", {Object[] args ->
if (!(args.length in [2,3])) {
project.error("expected 2 to 3 parameters for jdk() but ${args.length} found")
}
Closure initializer = args.length > 2 ? args[2] : {}
return project.createJavaSdk((String)args[0], (String)args[1], initializer)
})
binding.setVariable("moduleTests", {String name ->
@@ -80,7 +80,7 @@ class ProjectBuilder {
if (currentOutput != null) return currentOutput
project.stage("Making module ${chunk.name}")
def dst = folderForChunkOutput(chunk, classesDir(binding.project), false)
def dst = folderForChunkOutput(chunk, classesDir(project), false)
outputs[chunk] = dst
compile(chunk, dst, false)
@@ -59,8 +59,15 @@ class ArchiveElement extends CompositeLayoutElement {
}
def build(Project project) {
project.binding.jar.call([name, {
buildChildren(project)
}].toArray())
if (name.endsWith(".jar")) {
project.binding.ant.jar(name: name, filesetmanifest: "mergewithoutmain", duplicate: "preserve", {
buildChildren(project)
})
}
else {
project.binding.ant.zip(name: name, duplicate: "preserve", {
buildChildren(project)
})
}
}
}
+1
View File
@@ -0,0 +1 @@
f
+27
View File
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="web" name="Web">
<configuration>
<descriptors>
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
</descriptors>
<webroots>
<root url="file://$MODULE_DIR$/web" relative="/" />
</webroots>
<context-artifact name="explodedWar" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="junit" level="project" />
<orderEntry type="library" name="jdom" level="application" />
</component>
</module>
+59
View File
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ArtifactManager">
<artifact type="exploded-war" name="explodedWar">
<output-path>$PROJECT_DIR$/out/artifacts/explodedWar</output-path>
<root id="root">
<element id="javaee-facet-resources" facet="iprProject/web/Web" />
<element id="directory" name="WEB-INF">
<element id="directory" name="classes">
<element id="module-output" name="iprProject" />
</element>
<element id="directory" name="lib">
<element id="library" level="project" name="junit" />
<element id="library" level="application" name="jdom" />
</element>
</element>
</root>
</artifact>
<artifact type="jar" name="archive">
<output-path>$PROJECT_DIR$/out/artifacts/archive</output-path>
<root id="archive" name="archive.jar">
<element id="directory" name="files">
<element id="artifact" artifact-name="files" />
</element>
<element id="archive" name="sources.zip">
<element id="dir-copy" path="$PROJECT_DIR$/src" />
</element>
</root>
</artifact>
<artifact name="files">
<output-path>$PROJECT_DIR$/out/artifacts/files</output-path>
<root id="root">
<element id="file-copy" path="$PROJECT_DIR$/data/f.txt" output-file-name="g.txt" />
<element id="file-copy" path="$PROJECT_DIR$/data/f.txt" />
<element id="directory" name="dir">
<element id="dir-copy" path="$PROJECT_DIR$/data" />
</element>
</root>
</artifact>
</component>
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/iprProject.iml" filepath="$PROJECT_DIR$/iprProject.iml" />
</modules>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_5" assert-keyword="true" jdk-15="true" project-jdk-name="1.6" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="libraryTable">
<library name="junit">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/junit.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
</project>
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,4 @@
package xxx;
public class MyClass {
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
</web-app>
+5
View File
@@ -0,0 +1,5 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Simple jsp page</title></head>
<body>Place your content here</body>
</html>
@@ -0,0 +1,66 @@
package org.jetbrains.jps
/**
* @author nik
*/
class BuildFromIprTest extends JpsBuildTestCase {
public void testBuild() {
def globalLib = {Project project ->
project.createGlobalLibrary("jdom") {
classpath "testData/iprProject/lib/jdom.jar"
}
}
doTest("testData/iprProject/iprProject.ipr", globalLib) {
dir("artifacts") {
dir("archive") {
archive("archive.jar") {
dir("files") {
dir("dir") {
file("f.txt", "f")
}
file("f.txt", "f")
file("g.txt", "f")
}
archive("sources.zip") {
dir("xxx") {
file("MyClass.java")
}
}
dir("META-INF") { file("MANIFEST.MF") }
}
}
dir("files") {
dir("dir") {
file("f.txt", "f")
}
file("f.txt", "f")
file("g.txt", "f")
}
dir("explodedWar") {
dir("WEB-INF") {
dir("classes") {
dir("xxx") {
file("MyClass.class")
}
}
dir("lib") {
file("jdom.jar")
file("junit.jar")
}
file("web.xml")
}
file("index.jsp")
}
}
dir("production") {
dir("iprProject") {
dir("xxx") {
file("MyClass.class")
}
}
}
}
}
}
@@ -0,0 +1,49 @@
package org.jetbrains.jps
import junit.framework.Assert
/**
* @author nik
*/
class FileSystemItem {
boolean directory = false
boolean archive = false
String name
String content = null
private final Map<String, FileSystemItem> children = [:]
FileSystemItem leftShift(FileSystemItem item) {
assert !children.containsKey(item.getName()) : "${item.name} already added"
children[item.name] = item
return this
}
def assertDirectoryEqual(File file, String relativePath) throws IOException {
Set<String> notFound = new HashSet<String>(children.keySet());
file.listFiles()?.each {File child ->
final def name = child.name
final def item = children[name]
Assert.assertNotNull("unexpected file: $relativePath$name", item)
item.assertFileEqual(child, relativePath + name + "/")
notFound.remove(name)
}
Assert.assertTrue("files $notFound not found in $relativePath", notFound.isEmpty());
}
def assertFileEqual(File file, String relativePath) throws IOException {
Assert.assertEquals("in $relativePath", name, file.name);
if (archive) {
final File dirForExtracted = FileUtil.createTempDirectory("extracted_archive");
ZipUtil.extract(file, dirForExtracted, null);
assertDirectoryEqual(dirForExtracted, relativePath);
}
else if (directory) {
Assert.assertTrue("$relativePath${file.name} is not a directory", file.isDirectory());
assertDirectoryEqual(file, relativePath);
}
else if (content != null) {
final String content = new String(FileUtil.loadFileText(file));
Assert.assertEquals("content mismatch for " + relativePath, content, content);
}
}
}
@@ -0,0 +1,132 @@
package org.jetbrains.jps
/**
* @author nik
*/
class FileUtil {
static String loadFileText(File file) throws IOException{
InputStream stream = new FileInputStream(file);
Reader reader = new InputStreamReader(stream);
try{
return new String(loadText(reader, (int)file.length()));
}
finally{
reader.close();
}
}
static char[] loadText(Reader reader, int length) throws IOException {
char[] chars = new char[length];
int count = 0;
while (count < chars.length) {
int n = reader.read(chars, count, chars.length - count);
if (n <= 0) break;
count += n;
}
if (count == chars.length){
return chars;
}
else{
char[] newChars = new char[count];
System.arraycopy(chars, 0, newChars, 0, count);
return newChars;
}
}
static boolean createParentDirs(File file) {
if (!file.exists()) {
String parentDirPath = file.getParent()
if (parentDirPath != null) {
final File parentFile = new File(parentDirPath)
return parentFile.exists() && parentFile.isDirectory() || parentFile.mkdirs()
}
}
return false
}
static boolean delete(File file){
File[] files = file.listFiles();
if (files != null) {
for (File file1 : files) {
if (!delete(file1)) return false;
}
}
for (int i = 0; i < 10; i++){
if (file.delete() || !file.exists()) return true;
try {
Thread.sleep(10);
}
catch (InterruptedException ignored) {
}
}
return false;
}
static def compareFiles(File file1, File file2, String relativePath) {
def name = file1.name
if (name.endsWith(".jar") || name.endsWith(".war") || name.endsWith(".zip")) {
def dir1 = ZipUtil.extractToTempDir(file1)
def dir2 = ZipUtil.extractToTempDir(file1)
compareDirectories(dir1, dir2, relativePath)
delete(dir1)
delete(dir2)
}
else {
def len1 = file1.length()
def len2 = file2.length()
if (len1 != len2) {
System.out.println("file length mismatch for $relativePath: #1.len=$len1, #2.len=$len2");
}
}
}
static def compareDirectories(File dir1, File dir2) {
if (!dir1.exists()) {
System.out.println("${dir1.absolutePath} doesn't exist");
return
}
if (!dir2.exists()) {
System.out.println("${dir2.absolutePath} doesn't exist");
return
}
System.out.println("Comparing ${dir1.absolutePath}(#1) and ${dir2.absolutePath}(#2)");
compareDirectories(dir1, dir2, "")
}
private static def compareDirectories(File dir1, File dir2, String relativePath) {
// System.out.print(".");
Set<String> dir2Files = dir2.listFiles()*.name as Set
dir1.listFiles().each {File child1 ->
File child2 = new File(dir2, child1.name)
if (!child2.exists()) {
System.out.println("#1: $relativePath/${child1.name}");
}
else {
if (child1.isFile() && child2.isFile()) {
compareFiles(child1, child2, relativePath + "/" + child1.name)
}
else if (child1.isDirectory() && child2.isDirectory()) {
compareDirectories(child1, child2, relativePath + "/" + child1.name)
}
else {
System.out.println("type mismatch for $relativePath: #1 is ${child1.isDirectory() ? "dir" : "file"}, #2 is ${child2.isDirectory() ? "dir" : "file"}");
}
}
dir2Files.remove(child1.name)
}
dir2Files.each {
System.out.println("#2: ${dir2.absolutePath}/$it");
}
}
static File createTempDirectory(String prefix) {
def output = File.createTempFile(prefix, "tmp")
output.delete()
output.mkdirs()
return output
}
}
@@ -0,0 +1,50 @@
package org.jetbrains.jps
import junit.framework.TestCase
import org.codehaus.gant.GantBinding
import org.jetbrains.jps.idea.IdeaProjectLoader;
/**
* @author nik
*/
class JpsBuildTestCase extends TestCase {
def doTest(String projectPath, Closure initProject, Closure expectedOutput) {
def binding = new GantBinding()
binding.includeTool << Jps
def project = new Project(binding)
new IdeaProjectLoader().loadFromPath(project, projectPath)
initProject(project)
def target = FileUtil.createTempDirectory("targetDir")
project.targetFolder = target.absolutePath
project.clean()
project.makeAll()
project.buildArtifacts()
def root = new FileSystemItem(name: "<root>")
initFileSystemItem(root, expectedOutput)
root.assertDirectoryEqual(target, "");
}
def initFileSystemItem(FileSystemItem item, Closure initializer) {
def meta = new InitializingExpando()
meta.dir = {String name, Closure content ->
def dir = new FileSystemItem(name: name, directory: true)
initFileSystemItem(dir, content)
item << dir
}
meta.archive = {String name, Closure content ->
def archive = new FileSystemItem(name: name, archive: true)
initFileSystemItem(archive, content)
item << archive
}
meta.file = {Object[] args ->
item << new FileSystemItem(name: args[0], content: args.length > 1 ? args[1] : null)
}
initializer.delegate = meta
initializer.setResolveStrategy Closure.DELEGATE_FIRST
initializer()
}
}
@@ -0,0 +1,57 @@
package org.jetbrains.jps
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
/**
* @author nik
*/
class ZipUtil {
static File extractToTempDir(File file) throws IOException {
File output = FileUtil.createTempDirectory("extracted")
extract(file, output, null)
return output
}
static def extract(final File file, File outputDir, FilenameFilter filenameFilter) throws IOException {
ZipFile zipFile = new ZipFile(file)
try {
final Enumeration entries = zipFile.entries()
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement()
final File entryFile = new File(outputDir, entry.getName())
if (filenameFilter == null || filenameFilter.accept(entryFile.getParentFile(), entryFile.getName())) {
extractEntry(entry, zipFile.getInputStream(entry), outputDir)
}
}
}
finally {
zipFile.close();
}
}
static def extractEntry(ZipEntry entry, final InputStream inputStream, File outputDir) throws IOException {
final boolean isDirectory = entry.isDirectory()
final String relativeName = entry.getName()
final File file = new File(outputDir, relativeName)
FileUtil.createParentDirs(file)
if (isDirectory) {
file.mkdir()
}
else {
final BufferedInputStream input = new BufferedInputStream(inputStream)
final BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file))
try {
final byte[] buffer = new byte[1024*20]
int len
while ((len = input.read(buffer)) >= 0) {
output.write(buffer, 0, len)
}
}
finally {
output.close()
input.close()
}
}
}
}