experiments/JavaCompilerTests.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 26 Feb 2013 11:32:43 +0000
branchdevelopment
changeset 2429 ebece4dcaab9
parent 2380 9195eccdcbd9
parent 2396 fadc6d7a2f5b
child 2475 27f0940ec023
permissions -rw-r--r--
Merged 009a0df3afce and 3c9b3181ded (branch development - CVS HEAD)

"
 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: #resources / 21-12-2012 / 18:24:51 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
! !


!JavaCompilerTests class methodsFor:'accessing'!

resources
    ^ Array with: JavaInitializedResource

    "Created: / 02-01-2013 / 15:16:55 / Jan Vrany <jan.vrany@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 asArray = expectedImports asArray   
        message: 'analyzer should return an array containing "java.util.List"'.

    "Created: / 08-12-2012 / 20:12:23 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 15-12-2012 / 22:42:09 / Jan Vrany <jan.vrany@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 asArray = expectedImports asArray
        message: 'analyzer should return an array containing "java.util.List"'.

    "Created: / 08-12-2012 / 20:13:15 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 15-12-2012 / 22:43:22 / Jan Vrany <jan.vrany@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  compiledClasses |
    sourceCode := '
package stx.libjava;
class FooBar {    

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

    "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>"
    "Modified: / 02-01-2013 / 17:02:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

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

    public int foo() {
        return 42;
    }

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

    "Created: / 08-12-2012 / 20:31:13 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 02-01-2013 / 17:02:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !


!JavaCompilerTests methodsFor:'tests'!

testCompilingFromComplexStringWithComplexAssertions
    | sourceCode  compiledClass |
    self registerPackageVisibleClass.
    sourceCode := '
package stx.libjava.tests;
import org.junit.Test;
import static java.lang.String.*;
public class Wohoo {

    public int foo() {
        return new PackageVisibleClass().foo();
    }
}
'.
    compiledClass := (JavaCompiler compile: sourceCode) anElement.
    self assert: compiledClass notNil message: 'compiler should return compiled class'.
    self assert: compiledClass isJavaClass message: 'compiler should return java class'.
    self assert: compiledClass javaName = 'stx.libjava.tests.Wohoo' message: 'class name is not correct'.
    self assert: compiledClass javaPackage = 'stx.libjava.tests' message: 'java package is not correct'.
    self assert: (compiledClass lookupMethodFor: #'foo()I') notNil message: 'compiled class should have the foo method'.

    "Created: / 16-12-2012 / 15:38:56 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 02-01-2013 / 16:33:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

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

    public int foo() {
        return 42;
    }

}
'.
    compiledClasses := (JavaCompiler compile: sourceCode) anElement.
    self assert: compiledClasses notNil
        message: 'compiler should return compiled class'.
    self assert: compiledClasses isJavaClass
        message: 'compiler should return java class'.

    "Created: / 06-12-2012 / 22:59:42 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 02-01-2013 / 17:04:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

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

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

    "Created: / 08-12-2012 / 20:29:05 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 02-01-2013 / 17:05:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

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

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

    "Created: / 06-12-2012 / 23:30:54 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
    "Modified: / 02-01-2013 / 17:05:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

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

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

    "Created: / 08-12-2012 / 20:30:05 / Marcel Hlopko <marcel.hlopko@fit.cvut.cz>"
    "Modified: / 02-01-2013 / 17:05:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !


!JavaCompilerTests class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libjava/experiments/JavaCompilerTests.st,v 1.2 2013-02-25 11:15:34 vrany Exp $'
!

version_HG

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

version_SVN
    ^ '§Id::                                                                                                                        §'
! !