experiments/JavaCompilerTests.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sat, 15 Dec 2012 17:59:35 +0100
branchdevelopment
changeset 1880 27b932afa4a7
parent 1869 0ae14ac1c9af
child 1882 0178ded13292
permissions -rw-r--r--
Refactored JavaCompiler (part 1) Created concrete implementation as a private class of JavaCompiler - allows for different compilers to hook in (Javac, javac external, ecj, jikes...). Source code analyzer refactored.

"
 Copyright (c) 2010-2011 Jan Vrany, Jan Kurs & Marcel Hlopko,
                         SWING Research Group, Czech Technical University 
                         in Prague

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the 'Software'), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
"
"{ Package: 'stx:libjava/experiments' }"

TestCase subclass:#JavaCompilerTests
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Java-Tests-Compiling'
!

!JavaCompilerTests class methodsFor:'documentation'!

copyright
"
 Copyright (c) 2010-2011 Jan Vrany, Jan Kurs & Marcel Hlopko,
                         SWING Research Group, Czech Technical University 
                         in Prague

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the 'Software'), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.

"
!

history

    "Created: #testCompilingFromStringWithInCodeDependencies / 09-12-2012 / 09:29:55 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: #registerFooBarSuperclass / 09-12-2012 / 20:35:03 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: #testCompilingFromStringWithInCodeDependencies / 09-12-2012 / 20:36:29 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaCompilerTests methodsFor:'analyzer - class name tests'!

testGettingClassName
    | source  className |
    source := '
package stx.libjava.tests;
public class Foo {}
'.
    className := (JavaCompiler newAnalyzer analyze: source) className.
    self assert: className = 'Foo' message: 'className should be Foo'.

    "Created: / 08-12-2012 / 18:52:39 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaCompilerTests methodsFor:'analyzer - imports tests'!

testGettingImportWhenManyPresent
    | source  imports  expectedImports |
    source := '
package stx.libjava.tests;
import java.util.List;
import java.util.Collection;
import  stxLibjavaTests;
public class Foo {}
'.
    expectedImports := Array 
            with: 'java.util.List'
            with: 'java.util.Collection'
            with: 'stxLibjavaTests'.
    imports := (JavaCompiler newAnalyzer analyze: source) imports.
    self 
        assert: imports = expectedImports
        message: 'analyzer should return an array containing "java.util.List"'.

    "Created: / 08-12-2012 / 20:12:23 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

testGettingImportWhenNonePresent
    | source  imports |
    source := '
package stx.libjava.tests;
public class Foo {}
'.
    imports := (JavaCompiler newAnalyzer analyze: source) imports.
    self 
        assert: imports size = 0
        message: 'analyzer should return an empty array'.

    "Created: / 08-12-2012 / 18:56:37 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

testGettingImportWhenOnePresent
    | source  imports |
    source := '
package stx.libjava.tests;
import java.util.List;
public class Foo {}
'.
    imports := (JavaCompiler newAnalyzer analyze: source) imports.
    self assert: (imports size = 1 and: [(imports at: 1) = 'java.util.List'])
        message: 'analyzer should return an array containing "java.util.List"'.

    "Created: / 08-12-2012 / 20:10:36 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

testGettingImportWhenStaticPresent
    | source  imports  expectedImports |
    source := '
package stx.libjava.tests;
import static org.junit.Assert.*;
import java.util.Collection;
import  stxLibjavaTests;
public class Foo {}
'.
    expectedImports := Array 
            with: 'org.junit.Assert'
            with: 'java.util.Collection'
            with: 'stxLibjavaTests'.
    imports := (JavaCompiler newAnalyzer analyze: source) imports.
    self assert: imports = expectedImports
        message: 'analyzer should return an array containing "java.util.List"'.

    "Created: / 08-12-2012 / 20:13:15 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaCompilerTests methodsFor:'analyzer - package tests'!

testGettingPackageName
    | source  package |
    source := '
package stx.libjava.tests;
public class Foo {}
'.
    package := (JavaCompiler newAnalyzer analyze: source) packageName.
    self assert: package = 'stx.libjava.tests'
        message: 'package should be stx.libjava.tests'.

    "Created: / 08-12-2012 / 18:45:46 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

testGettingPackageNameWhenNoPackagePresent
    | source  package |
    source := '
public class Foo {}
'.
    package := (JavaCompiler newAnalyzer analyze: source) packageName.
    self assert: package isNil
        message: 'package should be nil'.

    "Created: / 08-12-2012 / 18:52:11 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaCompilerTests methodsFor:'helpers'!

registerFooBarSuperclass
    | sourceCode  compiledClass |
    sourceCode := '
package stx.libjava;
class FooBar {    

}
'.
    compiledClass := JavaCompiler compile: sourceCode.
    compiledClass isNil ifTrue: [
        self 
            error: 'these tests need PackageVisibleClass and aparently it could not be compiled'
    ].
    JavaVM registry registerClass: compiledClass.

    "Created: / 09-12-2012 / 09:28:10 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 09-12-2012 / 20:35:03 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

registerPackageVisibleClass
    | sourceCode  compiledClass |
    sourceCode := '
package stx.libjava.tests;
class PackageVisibleClass {

    public int foo() {
        return 42;
    }

}
'.
    compiledClass := JavaCompiler compile: sourceCode.
    compiledClass isNil ifTrue: [
        self 
            error: 'these tests need PackageVisibleClass and aparently it could not be compiled'
    ].
    JavaVM registry registerClass: compiledClass.

    "Created: / 08-12-2012 / 20:31:13 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaCompilerTests methodsFor:'tests'!

testCompilingFromString
    | sourceCode  compiledClass |
    sourceCode := '
public class Foo {

    public int foo() {
        return 42;
    }

}
'.
    compiledClass := JavaCompiler compile: sourceCode.
    self assert: compiledClass notNil
        message: 'compiler should return compiled class'.
    self assert: compiledClass isJavaClass
        message: 'compiler should return java class'.

    "Created: / 06-12-2012 / 22:59:42 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testCompilingFromStringWithImports
    | sourceCode  compiledClass |
    sourceCode := '
package stx.libjava.tests;
import org.junit.Test;
public class Wohoo {

    public int foo() {
        return 42;
    }
}
'.
    compiledClass := JavaCompiler compile: sourceCode.
    self assert: compiledClass notNil
        message: 'compiler should return compiled class'.
    self assert: compiledClass isJavaClass
        message: 'compiler should return java class'.

    "Created: / 08-12-2012 / 20:29:05 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
!

testCompilingFromStringWithPackage
    | sourceCode  compiledClass |
    sourceCode := '
package stx.libjava.tests;
public class Wohoo {

    public int foo() {
        return 42;
    }
}
'.
    compiledClass := JavaCompiler compile: sourceCode.
    self assert: compiledClass notNil
        message: 'compiler should return compiled class'.
    self assert: compiledClass isJavaClass
        message: 'compiler should return java class'.

    "Created: / 06-12-2012 / 23:30:54 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
!

testCompilingFromStringWithPackageDependencies
    | sourceCode  compiledClass |
    self registerPackageVisibleClass.
    sourceCode := '
package stx.libjava.tests;
public class Wohoo {

    public int foo() {
        return new PackageVisibleClass().foo();
    }
}
'.
    compiledClass := JavaCompiler compile: sourceCode.
    self assert: compiledClass notNil
        message: 'compiler should return compiled class'.
    self assert: compiledClass isJavaClass
        message: 'compiler should return java class'.

    "Created: / 08-12-2012 / 20:30:05 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !

!JavaCompilerTests class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
!

version_SVN
    ^ '§Id::                                                                                                                        §'
! !