diff --git a/grails-core/src/main/groovy/org/grails/compiler/injection/GlobalGrailsClassInjectorTransformation.groovy b/grails-core/src/main/groovy/org/grails/compiler/injection/GlobalGrailsClassInjectorTransformation.groovy index b1de05aee3e..cbba99b1308 100644 --- a/grails-core/src/main/groovy/org/grails/compiler/injection/GlobalGrailsClassInjectorTransformation.groovy +++ b/grails-core/src/main/groovy/org/grails/compiler/injection/GlobalGrailsClassInjectorTransformation.groovy @@ -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) } @@ -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") } @@ -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 pendingPluginClasses = [] static Collection pluginExcludes = [] diff --git a/grails-test-suite-persistence/src/test/groovy/org/grails/orm/support/TransactionManagerPostProcessorTests.groovy b/grails-test-suite-persistence/src/test/groovy/org/grails/orm/support/TransactionManagerPostProcessorTests.groovy index a42ecdf143a..c1d4d6de8c4 100644 --- a/grails-test-suite-persistence/src/test/groovy/org/grails/orm/support/TransactionManagerPostProcessorTests.groovy +++ b/grails-test-suite-persistence/src/test/groovy/org/grails/orm/support/TransactionManagerPostProcessorTests.groovy @@ -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() @@ -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 } } diff --git a/grails-test-suite-uber/src/test/groovy/grails/util/ClosureToMapPopulatorTests.groovy b/grails-test-suite-uber/src/test/groovy/grails/util/ClosureToMapPopulatorTests.groovy index 488683d5a51..6bedfd2abec 100644 --- a/grails-test-suite-uber/src/test/groovy/grails/util/ClosureToMapPopulatorTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/grails/util/ClosureToMapPopulatorTests.groovy @@ -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() @@ -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") } } diff --git a/grails-test-suite-uber/src/test/groovy/grails/util/CollectionUtilsTests.groovy b/grails-test-suite-uber/src/test/groovy/grails/util/CollectionUtilsTests.groovy index e0ef959e3ef..ea06812f4e5 100644 --- a/grails-test-suite-uber/src/test/groovy/grails/util/CollectionUtilsTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/grails/util/CollectionUtilsTests.groovy @@ -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 @@ -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 @@ -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 diff --git a/grails-test-suite-uber/src/test/groovy/grails/web/JSONBuilderTests.groovy b/grails-test-suite-uber/src/test/groovy/grails/web/JSONBuilderTests.groovy index cc49bf4c024..f7dde788ff5 100644 --- a/grails-test-suite-uber/src/test/groovy/grails/web/JSONBuilderTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/grails/web/JSONBuilderTests.groovy @@ -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() @@ -24,6 +30,7 @@ class JSONBuilderTests extends GroovyTestCase { assertEquals '{"rootprop":"something"}', result.toString() } + @Test void testArrays() { def builder = new JSONBuilder() @@ -35,6 +42,7 @@ class JSONBuilderTests extends GroovyTestCase { assertEquals '{"categories":["a","b","c"],"rootprop":"something"}', result.toString() } + @Test void testSubObjects() { def builder = new JSONBuilder() @@ -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() @@ -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() @@ -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() @@ -86,6 +97,7 @@ class JSONBuilderTests extends GroovyTestCase { assertEquals '{"foo":[{"bar":"hello"}]}', result.toString() } + @Test void testRootElementList() { def builder = new JSONBuilder() @@ -106,6 +118,7 @@ class JSONBuilderTests extends GroovyTestCase { assertEquals '["one","two","three"]', result.toString() } + @Test void testExampleFromReferenceGuide() { def builder = new JSONBuilder() @@ -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() diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/cli/ScriptNameResolverTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/cli/ScriptNameResolverTests.groovy index 225ec390ce9..08e595fdf98 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/cli/ScriptNameResolverTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/cli/ScriptNameResolverTests.groovy @@ -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') } } diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/commons/DefaultArtefactInfoTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/commons/DefaultArtefactInfoTests.groovy index 388c978f952..c1b9563ffe7 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/commons/DefaultArtefactInfoTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/commons/DefaultArtefactInfoTests.groovy @@ -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() diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/commons/DefaultGrailsCodecClassTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/commons/DefaultGrailsCodecClassTests.groovy index 70389c1cc8c..676bc49e7ea 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/commons/DefaultGrailsCodecClassTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/commons/DefaultGrailsCodecClassTests.groovy @@ -1,18 +1,28 @@ 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(); @@ -20,6 +30,7 @@ class DefaultGrailsCodecClassTests extends GroovyTestCase { assertEquals "decoded", codecClass.decoder.decode("stuff") } + @Test void testCodecWithMethods() { def codecClass = new DefaultGrailsCodecClass(CodecWithMethodsCodec) codecClass.afterPropertiesSet(); diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/commons/GrailsMetaClassUtilsTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/commons/GrailsMetaClassUtilsTests.groovy index a4a50714d59..91b976d39ee 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/commons/GrailsMetaClassUtilsTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/commons/GrailsMetaClassUtilsTests.groovy @@ -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 diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/commons/MultipleClassesPerFileTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/commons/MultipleClassesPerFileTests.groovy index 9110d62c7f0..75cbddc59d2 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/commons/MultipleClassesPerFileTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/commons/MultipleClassesPerFileTests.groovy @@ -15,18 +15,24 @@ */ package org.grails.commons +import org.junit.jupiter.api.Test + +import static org.junit.jupiter.api.Assertions.assertNotNull +import static org.junit.jupiter.api.Assertions.assertThrows + /** * @author Steven Devijver */ -class MultipleClassesPerFileTests extends GroovyTestCase { +class MultipleClassesPerFileTests { + @Test void testMultipleClassesPerFile() { GroovyClassLoader cl = new GroovyClassLoader() cl.parseClass(getClass().classLoader.getResourceAsStream('org/grails/commons/classes.groovy'), 'classes.groovy') assertNotNull cl.loadClass('TestClass1') assertNotNull cl.loadClass('TestClass2') - shouldFail(ClassNotFoundException) { + assertThrows(ClassNotFoundException) { cl.loadClass('TestClass3') } } diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/commons/UrlMappingsArtefactHandlerTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/commons/UrlMappingsArtefactHandlerTests.groovy index 48f31f64aee..555df600f1f 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/commons/UrlMappingsArtefactHandlerTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/commons/UrlMappingsArtefactHandlerTests.groovy @@ -1,15 +1,19 @@ package org.grails.commons import org.grails.core.artefact.UrlMappingsArtefactHandler +import org.junit.jupiter.api.Test import org.springframework.core.io.ByteArrayResource +import static org.junit.jupiter.api.Assertions.assertNotNull +import static org.junit.jupiter.api.Assertions.assertTrue + /** * Tests for UrlMappingsArtefactHandler. * * @author Graeme Rocher * @since 0.5 */ -class UrlMappingsArtefactHandlerTests extends GroovyTestCase { +class UrlMappingsArtefactHandlerTests { def mappingScript = ''' mappings { @@ -29,12 +33,13 @@ mappings { } ''' + @Test void testUrlMappingsArtefactHandler() { def gcl = new GroovyClassLoader() Class mappings = gcl.parseClass(new ByteArrayResource(mappingScript.bytes).inputStream, "MyUrlMappings") def handler = new UrlMappingsArtefactHandler() - assert handler.isArtefactClass(mappings) - assert handler.newArtefactClass(mappings) + assertTrue handler.isArtefactClass(mappings) + assertNotNull handler.newArtefactClass(mappings) } } diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/commons/metaclass/LazyMetaPropertyMapTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/commons/metaclass/LazyMetaPropertyMapTests.groovy index 7bf9f606647..b226745f2fc 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/commons/metaclass/LazyMetaPropertyMapTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/commons/metaclass/LazyMetaPropertyMapTests.groovy @@ -1,12 +1,16 @@ package org.grails.commons.metaclass import grails.beans.util.LazyMetaPropertyMap +import org.junit.jupiter.api.Test + +import static org.junit.jupiter.api.Assertions.* /** * @author Graeme Rocher */ -class LazyMetaPropertyMapTests extends GroovyTestCase { +class LazyMetaPropertyMapTests { + @Test void testOverridePropertiesRecursionBug() { PropertyMapTest.metaClass.getProperties = {-> new LazyMetaPropertyMap(delegate) } @@ -16,26 +20,30 @@ class LazyMetaPropertyMapTests extends GroovyTestCase { assertEquals 3, obj.properties.size() } + @Test void testSelectSubMap() { def map = new LazyMetaPropertyMap(new PropertyMapTest(name:"Bart", age:11, other:"stuff")) def submap = map['name', 'age'] - assertEquals 2, submap.size() + assertEquals 2, (int) submap.size() assertEquals "Bart", submap.name - assertEquals 11, submap.age + assertEquals 11, (int) submap.age } + @Test void testSize() { def map = new LazyMetaPropertyMap(new PropertyMapTest()) assertEquals 3, map.size() } + @Test void testIsEmpty() { def map = new LazyMetaPropertyMap(new PropertyMapTest()) assertFalse map.isEmpty() } + @Test void testContainsKey() { def map = new LazyMetaPropertyMap(new PropertyMapTest()) @@ -44,6 +52,7 @@ class LazyMetaPropertyMapTests extends GroovyTestCase { assertFalse map.containsKey("fo") } + @Test void testContainsValue() { def map = new LazyMetaPropertyMap(new PropertyMapTest(name:"Homer", age:45)) @@ -52,6 +61,7 @@ class LazyMetaPropertyMapTests extends GroovyTestCase { assertFalse map.containsValue("fo") } + @Test void testGet() { def map = new LazyMetaPropertyMap(new PropertyMapTest(name:"Homer", age:45)) @@ -60,7 +70,7 @@ class LazyMetaPropertyMapTests extends GroovyTestCase { assertEquals "Homer", map['name'] assertEquals 45, map.get("age") - assertEquals 45, map.age + assertEquals 45, (int) map.age assertEquals 45, map['age'] assertNull map.foo @@ -68,6 +78,7 @@ class LazyMetaPropertyMapTests extends GroovyTestCase { assertNull map.get('foo') } + @Test void testPut() { def map = new LazyMetaPropertyMap(new PropertyMapTest(name:"Bart", age:11)) @@ -83,6 +94,7 @@ class LazyMetaPropertyMapTests extends GroovyTestCase { assertEquals "lisa", map.name } + @Test void testKeySet() { def map = new LazyMetaPropertyMap(new PropertyMapTest(name:"Bart", age:11)) @@ -92,6 +104,7 @@ class LazyMetaPropertyMapTests extends GroovyTestCase { assertTrue keys.contains("age") } + @Test void testValues() { def map = new LazyMetaPropertyMap(new PropertyMapTest(name:"Bart", age:11)) diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/plugins/PluginLoadOrderTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/plugins/PluginLoadOrderTests.groovy index 6a2b82ed4d2..ca2e8d7453a 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/plugins/PluginLoadOrderTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/plugins/PluginLoadOrderTests.groovy @@ -2,13 +2,17 @@ package org.grails.plugins import grails.core.DefaultGrailsApplication import grails.plugins.DefaultGrailsPluginManager +import org.junit.jupiter.api.Test + +import static org.junit.jupiter.api.Assertions.assertEquals /** * @author Graeme Rocher * @since 1.1 */ -class PluginLoadOrderTests extends GroovyTestCase { +class PluginLoadOrderTests { + @Test void testPluginLoadBeforeAfter() { def gcl = new GroovyClassLoader() diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/plugins/metadata/GrailsPluginMetadataTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/plugins/metadata/GrailsPluginMetadataTests.groovy index 47f8209d792..0d8737f218e 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/plugins/metadata/GrailsPluginMetadataTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/plugins/metadata/GrailsPluginMetadataTests.groovy @@ -1,16 +1,21 @@ package org.grails.plugins.metadata -import grails.plugins.DefaultGrailsPluginManager import grails.core.DefaultGrailsApplication +import grails.plugins.DefaultGrailsPluginManager import grails.plugins.metadata.GrailsPlugin import grails.util.GrailsUtil +import org.junit.Test + +import static org.junit.jupiter.api.Assertions.assertEquals +import static org.junit.jupiter.api.Assertions.assertNull /** * @author Graeme Rocher * @since 1.2 */ -class GrailsPluginMetadataTests extends GroovyTestCase { +class GrailsPluginMetadataTests { + @Test void testAnnotatedMetadata() { def app = new DefaultGrailsApplication([Test1, Test2, Test3] as Class[], getClass().classLoader) def pluginManager = new DefaultGrailsPluginManager([] as Class[], app) diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletRequestTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletRequestTests.groovy index 3464cfba028..f33d788bb9d 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletRequestTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletRequestTests.groovy @@ -15,11 +15,14 @@ package org.grails.plugins.testing import org.grails.plugins.testing.GrailsMockHttpServletRequest +import org.junit.jupiter.api.Test + +import static org.junit.jupiter.api.Assertions.* /** * Test case for {@link org.grails.plugins.testing.GrailsMockHttpServletRequest}. */ -class GrailsMockHttpServletRequestTests extends GroovyTestCase { +class GrailsMockHttpServletRequestTests { static final String TEST_XML_CONTENT = """\ @@ -43,6 +46,7 @@ class GrailsMockHttpServletRequestTests extends GroovyTestCase { { qty: 2, name: "Chair", type: "Furniture" } ] """ + @Test void testGetXML() { // Set up the test data. def request = new GrailsMockHttpServletRequest() @@ -54,6 +58,7 @@ class GrailsMockHttpServletRequestTests extends GroovyTestCase { verifyXmlResult request.XML } + @Test void testGetXMLMultipleCalls() { def request = new GrailsMockHttpServletRequest() request.method = 'POST' @@ -70,14 +75,16 @@ class GrailsMockHttpServletRequestTests extends GroovyTestCase { verifyXmlResult request.XML } + @Test void testGetXMLNoContent() { def request = new GrailsMockHttpServletRequest() request.method = 'POST' - shouldFail { + assertThrows(Exception) { request.XML } } + @Test void testGetXMLContentNotXml() { // Set up the test data. def content = """\ @@ -90,11 +97,12 @@ Second line request.content = content.getBytes("UTF-8") // Test the method. - shouldFail { + assertThrows(Exception) { request.XML } } + @Test void testGetJSON() { // Set up the test data. def request = new GrailsMockHttpServletRequest() @@ -105,6 +113,7 @@ Second line verifyJsonResult request.JSON } + @Test void testGetJSONMultipleCalls() { // Set up the test data. def request = new GrailsMockHttpServletRequest() @@ -121,11 +130,13 @@ Second line verifyJsonResult request.JSON } + @Test void testGetJSONNoContent() { def request = new GrailsMockHttpServletRequest() - assert request.JSON.size() == 0 + assertEquals 0, (Integer) request.JSON.size() } + @Test void testGetJSONContentNotJson() { // Set up the test data. def content = """\ @@ -137,18 +148,18 @@ Second line request.content = content.getBytes("UTF-8") // Test the method. - assert !request.JSON : "should not contain JSON" + assertTrue request.JSON.isEmpty(), "should not contain JSON" } private void verifyXmlResult(xml) { - assertEquals 3, xml.item.size() - assertEquals "Apple", xml.item[1].name.text() - assertEquals "2", xml.item[2].@qty.text() + assertEquals 3, (int) xml.item.size() + assertEquals "Apple", (String) xml.item[1].name.text() + assertEquals "2", (String) xml.item[2].@qty.text() } private void verifyJsonResult(json) { - assertEquals 3, json.size() - assertEquals "Apple", json[1].name - assertEquals 2, json[2].qty + assertEquals 3, (int) json.size() + assertEquals "Apple", (String) json[1].name + assertEquals 2, (int) json[2].qty } } diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletResponseTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletResponseTests.groovy index 1ee0df75cf2..3b6d69aa27d 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletResponseTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpServletResponseTests.groovy @@ -15,15 +15,19 @@ package org.grails.plugins.testing import org.grails.plugins.testing.GrailsMockHttpServletResponse +import org.junit.jupiter.api.Test + +import static org.junit.jupiter.api.Assertions.assertEquals /** * Test case for {@link org.grails.plugins.testing.GrailsMockHttpServletResponse}. */ -class GrailsMockHttpServletResponseTests extends GroovyTestCase { +class GrailsMockHttpServletResponseTests { /** * Tests that the left-shift operator appends the given text to the * response output. */ + @Test void testLeftShift() { def testResponse = new GrailsMockHttpServletResponse() assertEquals "", testResponse.contentAsString diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpSessionTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpSessionTests.groovy index 623bd0f5729..286ae60f999 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpSessionTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/plugins/testing/GrailsMockHttpSessionTests.groovy @@ -15,11 +15,15 @@ package org.grails.plugins.testing import org.grails.plugins.testing.GrailsMockHttpSession +import org.junit.jupiter.api.Test + +import static org.junit.jupiter.api.Assertions.assertEquals +import static org.junit.jupiter.api.Assertions.assertNull /** * Test case for {@link org.grails.plugins.testing.GrailsMockHttpSession}. */ -class GrailsMockHttpSessionTests extends GroovyTestCase { +class GrailsMockHttpSessionTests { /** * Tests that property access on the session retrieves the value * for the attribute with the same name as the property unless @@ -27,6 +31,7 @@ class GrailsMockHttpSessionTests extends GroovyTestCase { * the session should only retrieve an attribute if the property * is missing. */ + @Test void testGetProperty() { // Set up the test session with some attributes. def testSession = new GrailsMockHttpSession() @@ -52,6 +57,7 @@ class GrailsMockHttpSessionTests extends GroovyTestCase { * notation, while real properties on the session can still be * modified. */ + @Test void testSetProperty() { // Set up the test session. def testSession = new GrailsMockHttpSession() @@ -75,6 +81,7 @@ class GrailsMockHttpSessionTests extends GroovyTestCase { * the session should only retrieve an attribute if the property * is missing. */ + @Test void testGetAt() { // Set up the test session with some attributes. def testSession = new GrailsMockHttpSession() @@ -96,6 +103,7 @@ class GrailsMockHttpSessionTests extends GroovyTestCase { * notation, while real properties on the session can still be * modified. */ + @Test void testPutAt() { // Set up the test session. def testSession = new GrailsMockHttpSession() diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/reload/SpringProxiedBeanReloadTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/reload/SpringProxiedBeanReloadTests.groovy index f439f18e6d5..a1bb23647fa 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/reload/SpringProxiedBeanReloadTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/reload/SpringProxiedBeanReloadTests.groovy @@ -1,18 +1,22 @@ package org.grails.reload import grails.spring.BeanBuilder -import org.junit.Ignore -import org.springframework.aop.framework.ProxyFactoryBean import org.aopalliance.intercept.MethodInterceptor import org.aopalliance.intercept.MethodInvocation +import org.junit.Ignore +import org.junit.jupiter.api.Test +import org.springframework.aop.framework.ProxyFactoryBean + +import static org.junit.jupiter.api.Assertions.assertEquals /** * @author Graeme Rocher * @since 1.0 */ @Ignore //TODO Ignore for JDK 11 -class SpringProxiedBeanReloadTests extends GroovyTestCase { +class SpringProxiedBeanReloadTests { + @Test void testReloadCGLibProxiedBean() { def gcl = new GroovyClassLoader() def cls = gcl.parseClass("class Book { String title = 'The Stand'; String author }") diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/test/support/ControllerNameExtractorTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/test/support/ControllerNameExtractorTests.groovy index 18de588626f..c087b4ec375 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/test/support/ControllerNameExtractorTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/test/support/ControllerNameExtractorTests.groovy @@ -1,9 +1,13 @@ package org.grails.test.support import org.grails.test.support.ControllerNameExtractor +import org.junit.jupiter.api.Test -class ControllerNameExtractorTests extends GroovyTestCase { +import static org.junit.jupiter.api.Assertions.assertEquals +class ControllerNameExtractorTests { + + @Test void testExtractControllerNameFromTestClassName() { String[] testClassSuffixes = ['Test', 'Tests'] diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/web/errors/GrailsExceptionResolverTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/web/errors/GrailsExceptionResolverTests.groovy index bbc970c4a8d..aa73015c878 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/web/errors/GrailsExceptionResolverTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/web/errors/GrailsExceptionResolverTests.groovy @@ -14,6 +14,9 @@ import org.grails.support.MockApplicationContext import org.grails.web.mapping.DefaultUrlMappingEvaluator import org.grails.web.mapping.DefaultUrlMappingsHolder import org.grails.web.servlet.view.CompositeViewResolver +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import org.springframework.mock.web.MockHttpServletRequest import org.springframework.mock.web.MockHttpServletResponse import org.springframework.mock.web.MockServletContext @@ -23,30 +26,33 @@ import org.springframework.web.multipart.support.StandardServletMultipartResolve import org.springframework.web.servlet.View import org.springframework.web.servlet.ViewResolver import org.springframework.web.servlet.view.InternalResourceView + +import static org.junit.jupiter.api.Assertions.* + /** * Test case for {@link org.grails.web.errors.GrailsExceptionResolver}. */ -class GrailsExceptionResolverTests extends GroovyTestCase { +class GrailsExceptionResolverTests { private application = new DefaultGrailsApplication() private resolver = new GrailsExceptionResolver() private mockContext = new MockServletContext() private mockCtx = new MockApplicationContext() - @Override - protected void tearDown() { + @AfterEach + void tearDown() { RequestContextHolder.resetRequestAttributes() } - @Override - protected void setUp() throws Exception { + @BeforeEach + void setUp() throws Exception { mockCtx.registerMockBean(GrailsApplication.APPLICATION_ID, new DefaultGrailsApplication()) - super.setUp(); def mainContext = new MockApplicationContext(); mainContext.registerMockBean(UrlConverter.BEAN_NAME, new CamelCaseUrlConverter()); application.mainContext = mainContext } + @Test void testGetRootCause() { def ex = new Exception() assertEquals ex, GrailsExceptionResolver.getRootCause(ex) @@ -58,11 +64,12 @@ class GrailsExceptionResolverTests extends GroovyTestCase { ex = new IllegalStateException(ex) assertEquals root, GrailsExceptionResolver.getRootCause(ex) - shouldFail(NullPointerException) { + assertThrows(NullPointerException) { GrailsExceptionResolver.getRootCause(null) } } + @Test void testResolveExceptionToView() { def mappings = new DefaultUrlMappingEvaluator(mockCtx).evaluateMappings { "500"(view:"myView") @@ -89,10 +96,11 @@ class GrailsExceptionResolverTests extends GroovyTestCase { def handler = new Object() def modelAndView = resolver.resolveException(request, response, handler, ex) - assertNotNull "should have returned a ModelAndView", modelAndView + assertNotNull modelAndView, "should have returned a ModelAndView" assertEquals "/myView", modelAndView.view.url } + @Test void testResolveExceptionToController() { def mappings = new DefaultUrlMappingEvaluator(mockCtx).evaluateMappings { "500"(controller:"foo", action:"bar") @@ -117,12 +125,13 @@ class GrailsExceptionResolverTests extends GroovyTestCase { def handler = new Object() def modelAndView = resolver.resolveException(request, response, handler, ex) - assertNotNull "should have returned a ModelAndView", modelAndView + assertNotNull modelAndView, "should have returned a ModelAndView" assertTrue modelAndView.empty assertEquals "/foo/bar",response.getForwardedUrl() } + @Test void testResolveExceptionToControllerWhenResponseCommitted() { def mappings = new DefaultUrlMappingEvaluator(mockCtx).evaluateMappings { "500"(controller:"foo", action:"bar") @@ -148,10 +157,11 @@ class GrailsExceptionResolverTests extends GroovyTestCase { response.setCommitted(true) def modelAndView = resolver.resolveException(request, response, handler, ex) - assertNotNull "should have returned a ModelAndView", modelAndView + assertNotNull modelAndView, "should have returned a ModelAndView" assertFalse modelAndView.empty } + @Test void testLogRequestWithException() { def config = new ConfigSlurper().parse(''' grails.exceptionresolver.params.exclude = ['jennysPhoneNumber'] @@ -175,6 +185,7 @@ bad things happened. Stacktrace follows:'''.replaceAll('[\n\r]', ''), msg.replac } + @Test void testLogRequest() { def config = new ConfigSlurper().parse(''' grails.exceptionresolver.params.exclude = ['jennysPhoneNumber'] @@ -197,6 +208,7 @@ jennysPhoneNumber: *** Stacktrace follows:'''.replaceAll('[\n\r]', ''), msg.replaceAll('[\n\r]', '') } + @Test void testDisablingRequestParameterLogging() { def oldEnvName = Environment.current.name diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/web/filters/HiddenHttpMethodFilterTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/web/filters/HiddenHttpMethodFilterTests.groovy index accbf0126a5..cbb5b13d572 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/web/filters/HiddenHttpMethodFilterTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/web/filters/HiddenHttpMethodFilterTests.groovy @@ -1,16 +1,21 @@ package org.grails.web.filters import org.grails.web.filters.HiddenHttpMethodFilter +import org.junit.jupiter.api.Test import org.springframework.mock.web.MockHttpServletRequest import org.springframework.mock.web.MockHttpServletResponse + import javax.servlet.FilterChain +import static org.junit.jupiter.api.Assertions.assertEquals + /** * @author Graeme Rocher * @since 1.1 */ -class HiddenHttpMethodFilterTests extends GroovyTestCase { +class HiddenHttpMethodFilterTests { + @Test void testDefaultCase() { def filter = new HiddenHttpMethodFilter() def req = new MockHttpServletRequest() @@ -22,6 +27,7 @@ class HiddenHttpMethodFilterTests extends GroovyTestCase { assertEquals "POST", method } + @Test void testWithParameter() { def filter = new HiddenHttpMethodFilter() def req = new MockHttpServletRequest() @@ -34,6 +40,7 @@ class HiddenHttpMethodFilterTests extends GroovyTestCase { assertEquals "DELETE", method } + @Test void testWithHeader() { def filter = new HiddenHttpMethodFilter() def req = new MockHttpServletRequest() diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/web/i18n/ParamsAwareLocaleChangeInterceptorTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/web/i18n/ParamsAwareLocaleChangeInterceptorTests.groovy index 249f9491b4c..6d2df3e3092 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/web/i18n/ParamsAwareLocaleChangeInterceptorTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/web/i18n/ParamsAwareLocaleChangeInterceptorTests.groovy @@ -1,22 +1,28 @@ package org.grails.web.i18n import grails.util.GrailsWebMockUtil - +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Test import org.springframework.mock.web.MockHttpServletRequest import org.springframework.web.context.request.RequestContextHolder import org.springframework.web.servlet.DispatcherServlet import org.springframework.web.servlet.i18n.SessionLocaleResolver +import static org.junit.jupiter.api.Assertions.assertEquals +import static org.junit.jupiter.api.Assertions.assertNotEquals + /** * @author Graeme Rocher * @since 1.0 */ -class ParamsAwareLocaleChangeInterceptorTests extends GroovyTestCase { +class ParamsAwareLocaleChangeInterceptorTests { + @AfterEach protected void tearDown() { RequestContextHolder.resetRequestAttributes() } + @Test void testSwitchLocaleWithStringArrayParamsObject() { def webRequest = GrailsWebMockUtil.bindMockWebRequest() @@ -40,7 +46,7 @@ class ParamsAwareLocaleChangeInterceptorTests extends GroovyTestCase { assert localeChangeInterceptor.preHandle(request, response, null) - assertNotSame locale, localeResolver.resolveLocale(request) + assertNotEquals locale, localeResolver.resolveLocale(request) locale = localeResolver.resolveLocale(request) @@ -48,6 +54,7 @@ class ParamsAwareLocaleChangeInterceptorTests extends GroovyTestCase { assertEquals "DE", locale.getCountry() } + @Test void testSwitchLocaleWithParamsObject() { def webRequest = GrailsWebMockUtil.bindMockWebRequest() @@ -71,7 +78,7 @@ class ParamsAwareLocaleChangeInterceptorTests extends GroovyTestCase { assert localeChangeInterceptor.preHandle(request, response, null) - assertNotSame locale, localeResolver.resolveLocale(request) + assertNotEquals locale, localeResolver.resolveLocale(request) locale = localeResolver.resolveLocale(request) @@ -79,7 +86,8 @@ class ParamsAwareLocaleChangeInterceptorTests extends GroovyTestCase { assertEquals "DE", locale.getCountry() } - void testSwithLocaleWithRequestParameter() { + @Test + void testSwitchLocaleWithRequestParameter() { def webRequest = GrailsWebMockUtil.bindMockWebRequest() @@ -102,7 +110,7 @@ class ParamsAwareLocaleChangeInterceptorTests extends GroovyTestCase { assert localeChangeInterceptor.preHandle(request, response, null) - assertNotSame locale, localeResolver.resolveLocale(request) + assertNotEquals locale, localeResolver.resolveLocale(request) locale = localeResolver.resolveLocale(request) diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/web/json/JSONObjectTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/web/json/JSONObjectTests.groovy index 93bd552b0aa..25a3f910740 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/web/json/JSONObjectTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/web/json/JSONObjectTests.groovy @@ -1,13 +1,15 @@ package org.grails.web.json +import org.junit.jupiter.api.Test import spock.lang.Issue /** * @author Graeme Rocher * @since 1.0 */ -class JSONObjectTests extends GroovyTestCase { +class JSONObjectTests { + @Test void testContainsKey() { JSONObject j = new JSONObject() j.put('test', 1) diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/web/metaclass/WithFormMethodTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/web/metaclass/WithFormMethodTests.groovy index 7158b4b40be..c8d72255a97 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/web/metaclass/WithFormMethodTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/web/metaclass/WithFormMethodTests.groovy @@ -17,34 +17,38 @@ package org.grails.web.metaclass import grails.artefact.Controller import grails.util.GrailsWebMockUtil - import org.grails.core.exceptions.GrailsRuntimeException import org.grails.web.servlet.mvc.SynchronizerTokensHolder +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import org.springframework.web.context.request.RequestContextHolder +import static org.junit.jupiter.api.Assertions.* + /** * @author Graeme Rocher * @since 1.1 */ -class WithFormMethodTests extends GroovyTestCase { +class WithFormMethodTests { private withForm = new WithFormMethod() private request - @Override - protected void setUp() { - super.setUp() + @BeforeEach + void setUp() { request = GrailsWebMockUtil.bindMockWebRequest() } - @Override - protected void tearDown() { + @AfterEach + void tearDown() { RequestContextHolder.resetRequestAttributes() } + @Test void testMissingToken() { - shouldFail(GrailsRuntimeException) { + assertThrows(GrailsRuntimeException) { withForm.withForm(request) { // should not get here }.invalidToken { @@ -53,11 +57,12 @@ class WithFormMethodTests extends GroovyTestCase { } } + @Test void testTokenHolderEmpty() { request.session.setAttribute(SynchronizerTokensHolder.HOLDER,new SynchronizerTokensHolder()) - shouldFail(GrailsRuntimeException) { + assertThrows(GrailsRuntimeException) { withForm.withForm(request) { // should not get here }.invalidToken { @@ -66,6 +71,7 @@ class WithFormMethodTests extends GroovyTestCase { } } + @Test void testTokenInvalidWithEmptyTokenHolder() { def url = "http://grails.org/submit" @@ -74,7 +80,7 @@ class WithFormMethodTests extends GroovyTestCase { request.currentRequest.addParameter(SynchronizerTokensHolder.TOKEN_URI,url) request.currentRequest.addParameter(SynchronizerTokensHolder.TOKEN_KEY,UUID.randomUUID().toString()) - shouldFail(GrailsRuntimeException) { + assertThrows(GrailsRuntimeException) { withForm.withForm(request) { // should not get here }.invalidToken { @@ -83,8 +89,9 @@ class WithFormMethodTests extends GroovyTestCase { } } + @Test void testTokenInvalid() { - def url = "http://grails.org/submit" + def url = "https://grails.org/submit" SynchronizerTokensHolder tokensHolder = new SynchronizerTokensHolder() tokensHolder.generateToken(url) @@ -93,7 +100,7 @@ class WithFormMethodTests extends GroovyTestCase { request.currentRequest.addParameter(SynchronizerTokensHolder.TOKEN_URI,url) request.currentRequest.addParameter(SynchronizerTokensHolder.TOKEN_KEY,UUID.randomUUID().toString()) - shouldFail(GrailsRuntimeException) { + assertThrows(GrailsRuntimeException) { withForm.withForm(request) { // should not get here }.invalidToken { @@ -102,6 +109,7 @@ class WithFormMethodTests extends GroovyTestCase { } } + @Test void testTokenValid() { def url = "http://grails.org/submit" @@ -121,6 +129,7 @@ class WithFormMethodTests extends GroovyTestCase { assertEquals "bar", result.foo } + @Test void testNonEmptyHolderStays() { def url1 = "http://grails.org/submit1" def url2 = "http://grails.org/submit2" @@ -145,6 +154,7 @@ class WithFormMethodTests extends GroovyTestCase { assertTrue tokensHolder.isValid(url2, token2) } + @Test void testEmptyHolderIsDeleted() { def url = "http://grails.org/submit" @@ -164,8 +174,9 @@ class WithFormMethodTests extends GroovyTestCase { assertNull request.session.getAttribute(SynchronizerTokensHolder.HOLDER) } + @Test void testHandleDoubleSubmit() { - def url = "http://grails.org/submit" + def url = "https://grails.org/submit" SynchronizerTokensHolder tokensHolder = new SynchronizerTokensHolder() def token = tokensHolder.generateToken(url) @@ -182,7 +193,7 @@ class WithFormMethodTests extends GroovyTestCase { assertEquals "bar", result.foo - shouldFail(GrailsRuntimeException) { + assertThrows(GrailsRuntimeException) { withForm.withForm(request) { // should not get here }.invalidToken { @@ -191,9 +202,10 @@ class WithFormMethodTests extends GroovyTestCase { } } + @Test void testHandleSubmitOfTwoForms() { - def url1 = "http://grails.org/submit1" - def url2 = "http://grails.org/submit2" + def url1 = "https://grails.org/submit1" + def url2 = "https://grails.org/submit2" SynchronizerTokensHolder tokensHolder = new SynchronizerTokensHolder() def token1 = tokensHolder.generateToken(url1) @@ -225,9 +237,10 @@ class WithFormMethodTests extends GroovyTestCase { assertEquals "bar", result2.foo } + @Test void testHandleSubmitOfTwoFormsWithSameURL() { - def url1 = "http://grails.org/submit" - def url2 = "http://grails.org/submit" + def url1 = "https://grails.org/submit" + def url2 = "https://grails.org/submit" SynchronizerTokensHolder tokensHolder = new SynchronizerTokensHolder() def token1 = tokensHolder.generateToken(url1) @@ -258,7 +271,7 @@ class WithFormMethodTests extends GroovyTestCase { assertEquals "bar", result2.foo - shouldFail(GrailsRuntimeException) { + assertThrows(GrailsRuntimeException) { withForm.withForm(request2) { return [foo:"bar"] }.invalidToken { @@ -268,4 +281,4 @@ class WithFormMethodTests extends GroovyTestCase { } } -class WithFormMethod implements Controller {} \ No newline at end of file +class WithFormMethod implements Controller {} diff --git a/grails-test-suite-uber/src/test/groovy/org/grails/web/util/WebUtilsTests.groovy b/grails-test-suite-uber/src/test/groovy/org/grails/web/util/WebUtilsTests.groovy index 01543747b9c..66695be4d40 100644 --- a/grails-test-suite-uber/src/test/groovy/org/grails/web/util/WebUtilsTests.groovy +++ b/grails-test-suite-uber/src/test/groovy/org/grails/web/util/WebUtilsTests.groovy @@ -2,24 +2,31 @@ package org.grails.web.util import grails.core.DefaultGrailsApplication import grails.core.GrailsApplication +import grails.web.mime.MimeType import org.grails.config.PropertySourcesConfig import org.grails.plugins.web.mime.MimeTypesFactoryBean import org.grails.support.MockApplicationContext -import grails.web.mime.MimeType import org.grails.web.servlet.mvc.GrailsWebRequest +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import org.springframework.mock.web.MockHttpServletRequest import org.springframework.mock.web.MockHttpServletResponse import org.springframework.mock.web.MockServletContext import org.springframework.web.context.request.RequestContextHolder +import static org.junit.jupiter.api.Assertions.* + /** * @author Graeme Rocher * @since 1.0 */ -class WebUtilsTests extends GroovyTestCase { +class WebUtilsTests { def config - protected void setUp() { + + @BeforeEach + void setUp() { RequestContextHolder.resetRequestAttributes() config = new ConfigSlurper().parse(""" grails.mime.file.extensions=false @@ -38,15 +45,17 @@ grails.mime.types = [ html: ['text/html','application/xhtml+xml'], ] """) } - protected void tearDown() { + @AfterEach + void tearDown() { RequestContextHolder.resetRequestAttributes() } + @Test void testAreFileExtensionsEnabled() { def ga = new DefaultGrailsApplication(config:new PropertySourcesConfig().merge(config)) bindMockRequest(ga) - assert !WebUtils.areFileExtensionsEnabled() + assertFalse WebUtils.areFileExtensionsEnabled() config = new ConfigSlurper().parse(""" grails.mime.file.extensions=true @@ -54,7 +63,7 @@ grails.mime.file.extensions=true ga.config = new PropertySourcesConfig().merge(config) - assert WebUtils.areFileExtensionsEnabled() + assertTrue WebUtils.areFileExtensionsEnabled() } private def bindMockRequest(DefaultGrailsApplication ga) { @@ -70,6 +79,7 @@ grails.mime.file.extensions=true RequestContextHolder.setRequestAttributes(webRequest) } + @Test void testGetFormatFromURI() { def ga = new DefaultGrailsApplication(config:new PropertySourcesConfig().merge(config)) bindMockRequest(ga) @@ -84,6 +94,7 @@ grails.mime.file.extensions=true assertEquals "xml", WebUtils.getFormatFromURI("/foo/bar.suff/bar.xml") } + @Test void testGetRequestURIForGrailsDispatchURI() { def request = new MockHttpServletRequest() request.contextPath = "/root" @@ -96,13 +107,14 @@ grails.mime.file.extensions=true assertEquals "/example/index",WebUtils.getRequestURIForGrailsDispatchURI(request) } + @Test void testRetrieveGrailsWebRequest() { // Validate the initial conditions. assertNull RequestContextHolder.getRequestAttributes() // An exception should be thrown if no web request is attached // to the thread. - shouldFail(IllegalStateException) { + assertThrows(IllegalStateException) { WebUtils.retrieveGrailsWebRequest() } @@ -116,6 +128,7 @@ grails.mime.file.extensions=true assertEquals mockWebRequest, WebUtils.retrieveGrailsWebRequest() } + @Test void testStoreGrailsWebRequest() { // Validate the initial conditions. assertNull RequestContextHolder.getRequestAttributes() @@ -136,6 +149,7 @@ grails.mime.file.extensions=true assertEquals mockWebRequest, mockHttpRequest.getAttribute(GrailsApplicationAttributes.WEB_REQUEST) } + @Test void clearGrailsWebRequest() { // Create a mock web request and store it on the thread. def mockHttpRequest = new MockHttpServletRequest() diff --git a/grails-test-suite-web/src/test/groovy/org/grails/web/servlet/mvc/SynchronizerTokensHolderTests.groovy b/grails-test-suite-web/src/test/groovy/org/grails/web/servlet/mvc/SynchronizerTokensHolderTests.groovy index 31be5bcd380..610e5a0f628 100644 --- a/grails-test-suite-web/src/test/groovy/org/grails/web/servlet/mvc/SynchronizerTokensHolderTests.groovy +++ b/grails-test-suite-web/src/test/groovy/org/grails/web/servlet/mvc/SynchronizerTokensHolderTests.groovy @@ -1,8 +1,12 @@ package org.grails.web.servlet.mvc +import org.junit.jupiter.api.Test -class SynchronizerTokensHolderTests extends GroovyTestCase { +import static org.junit.jupiter.api.Assertions.* +class SynchronizerTokensHolderTests { + + @Test // GRAILS-9923 void testSerializable() { SynchronizerTokensHolder holder = new SynchronizerTokensHolder() @@ -17,68 +21,72 @@ class SynchronizerTokensHolderTests extends GroovyTestCase { ObjectInputStream ios = new ObjectInputStream(new ByteArrayInputStream(data)) def deserialized = ios.readObject() - assert deserialized instanceof SynchronizerTokensHolder + assertTrue deserialized instanceof SynchronizerTokensHolder SynchronizerTokensHolder holder2 = deserialized - assert holder2.currentTokens == holder.currentTokens - assert 2 == holder2.currentTokens.size() + assertEquals holder2.currentTokens, holder.currentTokens + assertEquals 2, holder2.currentTokens.size() holder.generateToken 'url3' - assert 2 == holder2.currentTokens.size() + assertEquals 2, holder2.currentTokens.size() holder2.generateToken 'url3' - assert 3 == holder2.currentTokens.size() + assertEquals 3, holder2.currentTokens.size() } + @Test void testGenerate() { SynchronizerTokensHolder holder = new SynchronizerTokensHolder() assert holder.empty String url1 = 'url1' - assert holder.generateToken(url1) - assert 1 == holder.currentTokens.size() - assert 1 == holder.currentTokens[url1].size() + assertNotNull holder.generateToken(url1) + assertEquals 1, holder.currentTokens.size() + assertEquals 1, holder.currentTokens[url1].size() - assert holder.generateToken(url1) - assert 1 == holder.currentTokens.size() - assert 2 == holder.currentTokens[url1].size() + assertNotNull holder.generateToken(url1) + assertEquals 1, holder.currentTokens.size() + assertEquals 2, holder.currentTokens[url1].size() String url2 = 'url2' - assert holder.generateToken(url2) - assert 2 == holder.currentTokens.size() - assert 2 == holder.currentTokens[url1].size() - assert 1 == holder.currentTokens[url2].size() + assertNotNull holder.generateToken(url2) + assertEquals 2, holder.currentTokens.size() + assertEquals 2, holder.currentTokens[url1].size() + assertEquals 1, holder.currentTokens[url2].size() } + @Test void testIsValid() { SynchronizerTokensHolder holder = new SynchronizerTokensHolder() - assert holder.empty + assertTrue holder.empty String url = 'url1' String token = holder.generateToken(url) - assert holder.isValid(url, token) - assert !holder.isValid(url, token + '!') + assertTrue holder.isValid(url, token) + assertFalse holder.isValid(url, token + '!') } + @Test void testResetTokens() { SynchronizerTokensHolder holder = new SynchronizerTokensHolder() - assert holder.empty + assertTrue holder.empty String url1 = 'url1' String url2 = 'url2' - assert holder.generateToken(url1) - assert holder.generateToken(url2) - assert 2 == holder.currentTokens.size() + assertNotNull holder.generateToken(url1) + assertNotNull holder.generateToken(url2) + assertEquals 2, holder.currentTokens.size() holder.resetToken url1 - assert 1 == holder.currentTokens.size() + assertEquals 1, holder.currentTokens.size() holder.resetToken url2 - assert 0 == holder.currentTokens.size() + assertEquals 0, holder.currentTokens.size() } + @Test void testResetToken() { SynchronizerTokensHolder holder = new SynchronizerTokensHolder() @@ -89,24 +97,24 @@ class SynchronizerTokensHolderTests extends GroovyTestCase { String token2 = holder.generateToken(url1) String token3 = holder.generateToken(url1) String token4 = holder.generateToken(url2) - assert 2 == holder.currentTokens.size() + assertEquals 2, holder.currentTokens.size() holder.resetToken url1, token1 - assert 2 == holder.currentTokens.size() + assertEquals 2, holder.currentTokens.size() holder.resetToken url1, token2 - assert 2 == holder.currentTokens.size() + assertEquals 2, holder.currentTokens.size() holder.resetToken url1, token3 - assert 1 == holder.currentTokens.size() + assertEquals 1, holder.currentTokens.size() holder.resetToken url1, token4 - assert 1 == holder.currentTokens.size() + assertEquals 1, holder.currentTokens.size() holder.resetToken url1, token4 + '!' - assert 1 == holder.currentTokens.size() + assertEquals 1, holder.currentTokens.size() holder.resetToken url2, token4 - assert 0 == holder.currentTokens.size() + assertEquals 0, holder.currentTokens.size() } } diff --git a/grails-test-suite-web/src/test/groovy/org/grails/web/servlet/view/GroovyPageViewTests.groovy b/grails-test-suite-web/src/test/groovy/org/grails/web/servlet/view/GroovyPageViewTests.groovy index efb118da15f..51175c8d20d 100644 --- a/grails-test-suite-web/src/test/groovy/org/grails/web/servlet/view/GroovyPageViewTests.groovy +++ b/grails-test-suite-web/src/test/groovy/org/grails/web/servlet/view/GroovyPageViewTests.groovy @@ -1,13 +1,19 @@ package org.grails.web.servlet.view + import grails.util.GrailsWebMockUtil import org.grails.core.io.MockStringResourceLoader import org.grails.gsp.GroovyPagesTemplateEngine import org.grails.support.MockApplicationContext +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Test import org.springframework.web.context.request.RequestContextHolder +import static org.junit.jupiter.api.Assertions.assertEquals + @SuppressWarnings("unused") -class GroovyPageViewTests extends GroovyTestCase { +class GroovyPageViewTests { + @Test void testGroovyPageView() { def webRequest = GrailsWebMockUtil.bindMockWebRequest() @@ -37,6 +43,7 @@ class GroovyPageViewTests extends GroovyTestCase { assertEquals "successbar", webRequest.currentResponse.contentAsString } + @AfterEach void tearDown() { RequestContextHolder.resetRequestAttributes() } diff --git a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/DefaultUrlCreatorTests.groovy b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/DefaultUrlCreatorTests.groovy index 2f11fbf408c..661d6128c00 100644 --- a/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/DefaultUrlCreatorTests.groovy +++ b/grails-web-url-mappings/src/test/groovy/org/grails/web/mapping/DefaultUrlCreatorTests.groovy @@ -1,12 +1,16 @@ package org.grails.web.mapping import grails.util.GrailsWebMockUtil - import org.grails.web.mapping.DefaultUrlCreator -import org.springframework.web.context.request.* +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Test +import org.springframework.web.context.request.RequestContextHolder + +import static org.junit.jupiter.api.Assertions.assertEquals -class DefaultUrlCreatorTests extends GroovyTestCase { +class DefaultUrlCreatorTests { + @Test void testCreateUrl() { def webRequest = GrailsWebMockUtil.bindMockWebRequest() @@ -22,6 +26,7 @@ class DefaultUrlCreatorTests extends GroovyTestCase { assertEquals "/foo/index?hello=world&fred=flintstone", creator.createURL(hello:"world", fred:"flintstone", "utf-8") } + @Test void testCreateUrlNoCharacterEncoding() { def webRequest = GrailsWebMockUtil.bindMockWebRequest() webRequest.currentRequest.characterEncoding = null @@ -31,6 +36,7 @@ class DefaultUrlCreatorTests extends GroovyTestCase { assertEquals "/foo/index", creator.createURL(null, "utf-8") } + @AfterEach void tearDown() { RequestContextHolder.resetRequestAttributes() }