Skip to content

Commit

Permalink
Update tests to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
puneetbehl committed Jul 14, 2021
1 parent 6e52f17 commit 4686a41
Show file tree
Hide file tree
Showing 28 changed files with 355 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ class GlobalGrailsClassInjectorTransformation implements ASTTransformation, Comp
if(Modifier.isAbstract(classNode.getModifiers())) return false

def classNodeName = classNode.name
// generate META-INF/grails.factories
def factoriesFile = new File(compilationTargetDirectory, "META-INF/grails.factories")
factoriesFile.parentFile.mkdirs()

File sourceDirectory = findSourceDirectory(compilationTargetDirectory)
def sourceFactoriesFile = new File(sourceDirectory, "src/main/resources/META-INF/grails.factories")
def props = new Properties()
def superTypeName = superType.getName()
if (factoriesFile.exists()) {
if (sourceFactoriesFile.exists()) {
// update
factoriesFile.withInputStream { InputStream input ->
sourceFactoriesFile.withInputStream { InputStream input ->
props.load(input)
}

Expand All @@ -186,6 +186,9 @@ class GlobalGrailsClassInjectorTransformation implements ASTTransformation, Comp
} else {
props.put(superTypeName, classNodeName)
}
// generate META-INF/grails.factories
def factoriesFile = new File(compilationTargetDirectory, "META-INF/grails.factories")
factoriesFile.parentFile.mkdirs()
factoriesFile.withWriter { Writer writer ->
props.store(writer, "Grails Factories File")
}
Expand All @@ -194,6 +197,14 @@ class GlobalGrailsClassInjectorTransformation implements ASTTransformation, Comp
return false
}

private static File findSourceDirectory(File compilationTargetDirectory) {
File sourceDirectory = compilationTargetDirectory
while (sourceDirectory != null && !(sourceDirectory.name in ["build", "target"])) {
sourceDirectory = sourceDirectory.parentFile
}
sourceDirectory.parentFile
}

static Set<String> pendingPluginClasses = []
static Collection<String> pluginExcludes = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package org.grails.orm.support
import grails.spring.BeanBuilder
import grails.transaction.TransactionManagerAware
import org.grails.transaction.TransactionManagerPostProcessor
import org.junit.jupiter.api.Test
import org.springframework.jdbc.datasource.DataSourceTransactionManager
import org.springframework.jdbc.datasource.DriverManagerDataSource
import org.springframework.transaction.PlatformTransactionManager

import static org.junit.jupiter.api.Assertions.assertNotNull

/**
* @author Graeme Rocher
* @since 1.0
*/
class TransactionManagerPostProcessorTests extends GroovyTestCase{
class TransactionManagerPostProcessorTests {

@Test
void testTransactionManagerPostProccessor() {
def bb = new BeanBuilder()

Expand All @@ -35,8 +39,8 @@ class TransactionManagerPostProcessorTests extends GroovyTestCase{
def ctx = bb.createApplicationContext()

MyBean bean = ctx.getBean("myBean")
assert bean
assert bean.tm
assertNotNull bean
assertNotNull bean.tm
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package grails.util

import org.junit.jupiter.api.Test

import static org.junit.jupiter.api.Assertions.assertEquals

/**
* @author Graeme Rocher
* @since 1.1
*/
class ClosureToMapPopulatorTests extends GroovyTestCase {
class ClosureToMapPopulatorTests {

@Test
void testPopulate() {
def populator = new ClosureToMapPopulator()

Expand All @@ -17,6 +22,6 @@ class ClosureToMapPopulatorTests extends GroovyTestCase {

assertEquals "bar", result.foo
assertEquals "two", result.one
assertEquals "should have returned a list", ["four", "five"], result.three
assertEquals(["four", "five"], result.three, "should have returned a list")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
*/
package grails.util

class CollectionUtilsTests extends GroovyTestCase {
import org.junit.jupiter.api.Test

import static org.junit.jupiter.api.Assertions.*

class CollectionUtilsTests {

@Test
void testNewMapEvenArgs() {
def map = CollectionUtils.newMap("foo", 1, "bar", 2, "baz", 42)
assertNotNull map
Expand All @@ -26,24 +31,28 @@ class CollectionUtilsTests extends GroovyTestCase {
assertEquals 42, map.baz
}

@Test
void testNewMapOddArgs() {
shouldFail(IllegalArgumentException) {
assertThrows(IllegalArgumentException) {
CollectionUtils.newMap "foo", 1, "bar"
}
}

@Test
void testNewMapNull() {
def map = CollectionUtils.newMap(null)
assertTrue map instanceof Map
assertEquals 0, map.size()
}

@Test
void testNewSetNull() {
def set = CollectionUtils.newSet(null)
assertTrue set instanceof Set
assertEquals 0, set.size()
}

@Test
void testNewSet() {
def set = CollectionUtils.newSet(1, 2, 42)
assertTrue set instanceof Set
Expand All @@ -53,12 +62,14 @@ class CollectionUtilsTests extends GroovyTestCase {
assertTrue set.contains(42)
}

@Test
void testNewListNull() {
def list = CollectionUtils.newList(null)
assertTrue list instanceof List
assertEquals 0, list.size()
}

@Test
void testNewList() {
def list = CollectionUtils.newList(1, 2, 42)
assertTrue list instanceof List
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package grails.web

import org.grails.web.converters.configuration.ConvertersConfigurationInitializer
import grails.core.DefaultGrailsApplication
import org.grails.web.converters.configuration.ConvertersConfigurationInitializer
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

import static org.junit.jupiter.api.Assertions.assertEquals

/**
* @author Graeme Rocher
* @since 1.2
*/
class JSONBuilderTests extends GroovyTestCase {
class JSONBuilderTests {

@BeforeEach
void setUp() {
def initializer = new ConvertersConfigurationInitializer(grailsApplication: new DefaultGrailsApplication())
initializer.initialize()
}


@Test
void testSimple() {
def builder = new JSONBuilder()

Expand All @@ -24,6 +30,7 @@ class JSONBuilderTests extends GroovyTestCase {
assertEquals '{"rootprop":"something"}', result.toString()
}

@Test
void testArrays() {
def builder = new JSONBuilder()

Expand All @@ -35,6 +42,7 @@ class JSONBuilderTests extends GroovyTestCase {
assertEquals '{"categories":["a","b","c"],"rootprop":"something"}', result.toString()
}

@Test
void testSubObjects() {
def builder = new JSONBuilder()

Expand All @@ -49,6 +57,7 @@ class JSONBuilderTests extends GroovyTestCase {
assertEquals '{"categories":["a","b","c"],"rootprop":"something","test":{"subprop":10}}', result.toString()
}

@Test
void testAssignedObjects() {

def builder = new JSONBuilder()
Expand All @@ -64,6 +73,7 @@ class JSONBuilderTests extends GroovyTestCase {
assertEquals '{"categories":["a","b","c"],"rootprop":"something","test":{"subprop":10}}', result.toString()
}

@Test
void testNamedArgumentHandling() {
def builder = new JSONBuilder()

Expand All @@ -76,6 +86,7 @@ class JSONBuilderTests extends GroovyTestCase {
assertEquals '{"categories":["a","b","c"],"rootprop":"something","test":{"subprop":10,"three":[1,2,3]}}', result.toString()
}

@Test
void testArrayOfClosures() {
def builder = new JSONBuilder()

Expand All @@ -86,6 +97,7 @@ class JSONBuilderTests extends GroovyTestCase {
assertEquals '{"foo":[{"bar":"hello"}]}', result.toString()
}

@Test
void testRootElementList() {
def builder = new JSONBuilder()

Expand All @@ -106,6 +118,7 @@ class JSONBuilderTests extends GroovyTestCase {
assertEquals '["one","two","three"]', result.toString()
}

@Test
void testExampleFromReferenceGuide() {
def builder = new JSONBuilder()

Expand Down Expand Up @@ -138,6 +151,7 @@ class JSONBuilderTests extends GroovyTestCase {
assertEquals '{"books":[{"title":"one"},{"title":"two"},{"title":"three"}]}', result.toString()
}

@Test
void testAppendToArray() {
def builder = new JSONBuilder()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package org.grails.cli

import org.grails.build.parsing.ScriptNameResolver;
import org.grails.build.parsing.ScriptNameResolver
import org.junit.jupiter.api.Test

class ScriptNameResolverTests extends GroovyTestCase {
import static org.junit.jupiter.api.Assertions.assertTrue

class ScriptNameResolverTests {

@Test
void testFoo() {
assert ScriptNameResolver.resolvesTo('F', 'Foo')
assert ScriptNameResolver.resolvesTo('FB', 'FooBar')
assert ScriptNameResolver.resolvesTo('FoB', 'FooBar')
assert ScriptNameResolver.resolvesTo('FBa', 'FooBar')
assert ScriptNameResolver.resolvesTo('FoBa', 'FooBar')
assert ScriptNameResolver.resolvesTo('FooBar', 'FooBar')
assert !ScriptNameResolver.resolvesTo('FB', 'FooBarZoo')
assert !ScriptNameResolver.resolvesTo('FBaz', 'FooBar')
assert !ScriptNameResolver.resolvesTo('FBr', 'FooBar')
assert !ScriptNameResolver.resolvesTo('F', 'FooBar')
assert !ScriptNameResolver.resolvesTo('Fo', 'FooBar')
assert !ScriptNameResolver.resolvesTo('Foo', 'FooBar')
assertTrue ScriptNameResolver.resolvesTo('F', 'Foo')
assertTrue ScriptNameResolver.resolvesTo('FB', 'FooBar')
assertTrue ScriptNameResolver.resolvesTo('FoB', 'FooBar')
assertTrue ScriptNameResolver.resolvesTo('FBa', 'FooBar')
assertTrue ScriptNameResolver.resolvesTo('FoBa', 'FooBar')
assertTrue ScriptNameResolver.resolvesTo('FooBar', 'FooBar')
assertTrue !ScriptNameResolver.resolvesTo('FB', 'FooBarZoo')
assertTrue !ScriptNameResolver.resolvesTo('FBaz', 'FooBar')
assertTrue !ScriptNameResolver.resolvesTo('FBr', 'FooBar')
assertTrue !ScriptNameResolver.resolvesTo('F', 'FooBar')
assertTrue !ScriptNameResolver.resolvesTo('Fo', 'FooBar')
assertTrue !ScriptNameResolver.resolvesTo('Foo', 'FooBar')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package org.grails.commons

import grails.core.DefaultArtefactInfo
import org.grails.core.DefaultGrailsControllerClass
import org.junit.jupiter.api.Test

import static org.junit.jupiter.api.Assertions.assertEquals

/**
* @author Graeme Rocher
* @since 1.0
*/
class DefaultArtefactInfoTests extends GroovyTestCase {
class DefaultArtefactInfoTests {

@Test
void testAddGrailsClass() {
def info = new DefaultArtefactInfo()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
package org.grails.commons

import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

import static org.junit.jupiter.api.Assertions.assertEquals

/**
* @author Graeme Rocher
* @since 1.1
*/
class DefaultGrailsCodecClassTests extends GroovyTestCase {
class DefaultGrailsCodecClassTests {

@BeforeEach
protected void setUp() {
ExpandoMetaClass.enableGlobally()
}

@AfterEach
protected void tearDown() {
ExpandoMetaClass.disableGlobally()
}

@Test
void testCodecWithClosures() {
def codecClass = new DefaultGrailsCodecClass(CodecWithClosuresCodec)
codecClass.afterPropertiesSet();
assertEquals "encoded", codecClass.encoder.encode("stuff")
assertEquals "decoded", codecClass.decoder.decode("stuff")
}

@Test
void testCodecWithMethods() {
def codecClass = new DefaultGrailsCodecClass(CodecWithMethodsCodec)
codecClass.afterPropertiesSet();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package org.grails.commons

import grails.util.GrailsMetaClassUtils
import org.junit.jupiter.api.Test
import org.springframework.beans.BeanUtils

import static org.junit.jupiter.api.Assertions.assertEquals
import static org.junit.jupiter.api.Assertions.assertNotNull

/**
* Tests for the GrailsMetaClassUtils class.
*
* @author Graeme Rocher
*/
class GrailsMetaClassUtilsTests extends GroovyTestCase {
class GrailsMetaClassUtilsTests {

@Test
void testGetMetaRegistry() {
assertNotNull(GrailsMetaClassUtils.getRegistry())
}

@Test
void testCopyExpandoMetaClass() {
def metaClass = new ExpandoMetaClass(Dummy, true)

// add property
metaClass.getFoo = {-> "bar" }
// add instance method
metaClass.foo = { String txt -> "bar:$txt" }
metaClass.foo = { String txt -> "bar:$txt".toString() }
// add static method
metaClass.'static'.bar = {-> "foo" }
// add constructor
Expand Down
Loading

0 comments on commit 4686a41

Please sign in to comment.