This is an automated email from the ASF dual-hosted git repository. jamesfredley pushed a commit to branch test/core-config-coverage-7.0.x in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 0c78d805f52a4140db4f6c0aa9761532665cfabe Author: James Fredley <[email protected]> AuthorDate: Tue Oct 13 17:34:07 2026 -0400 Add unit tests for composite config and small core utilities. Cover CompositeConfig, PrefixedConfig, StopWatch, LazyMetaPropertyMap, and previously untested GrailsClassUtils behavior. Assisted-by: sisyphus-grok:grok-4.7 --- .../beans/util/LazyMetaPropertyMapSpec.groovy | 138 +++++++++++++++ .../groovy/grails/util/GrailsClassUtilsSpec.groovy | 196 +++++++++++++++++++++ .../org/grails/config/CompositeConfigSpec.groovy | 184 +++++++++++++++++++ .../org/grails/config/PrefixedConfigSpec.groovy | 140 +++++++++++++++ .../org/grails/core/util/StopWatchSpec.groovy | 150 ++++++++++++++++ 5 files changed, 808 insertions(+) diff --git a/grails-core/src/test/groovy/grails/beans/util/LazyMetaPropertyMapSpec.groovy b/grails-core/src/test/groovy/grails/beans/util/LazyMetaPropertyMapSpec.groovy new file mode 100644 index 0000000000..9a7f7c3a2f --- /dev/null +++ b/grails-core/src/test/groovy/grails/beans/util/LazyMetaPropertyMapSpec.groovy @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package grails.beans.util + +import groovy.transform.CompileStatic +import spock.lang.Specification + +class LazyMetaPropertyMapSpec extends Specification { + + void 'get reads bean properties lazily and supports CharSequence keys and subsets'() { + given: + def bean = new TestBean(name: 'Grails', count: 7) + def map = new LazyMetaPropertyMap(bean) + + expect: + map.get('name') == 'Grails' + map.get(new StringBuilder('count')) == 7 + map.get('missing') == null + map.get('constraints') == null + map.get(['name', 'count', 'missing', null]) == [name: 'Grails', count: 7] + + when: + bean.name = 'changed' + + then: + map.get('name') == 'changed' + map.getInstance().is(bean) + } + + void 'put returns the old value and updates the bean'() { + given: + def bean = new TestBean(name: 'original', count: 1) + def map = new LazyMetaPropertyMap(bean) + + when: + def oldName = map.put(new StringBuilder('name'), 'changed') + def oldCount = map.put('count', [count: 2]) + map.putAll([name: 'final', count: 3]) + + then: + oldName == 'original' + oldCount == 1 + bean.name == 'final' + bean.count == 3 + map.get('name') == 'final' + map.put('missing', 'ignored') == null + map.put('id', 99L) == null + bean.id == 1L + } + + void 'keys and map views exclude static and configurational properties'() { + given: + def map = new LazyMetaPropertyMap(new TestBean(name: 'Grails', count: 7)) + + expect: + map.containsKey('name') + map.containsKey(new StringBuilder('count')) + !map.containsKey('missing') + !map.containsKey('constraints') + map.keySet() == ['name', 'count'] as Set + map.size() == 2 + !map.isEmpty() + map.values().toSet() == ['Grails', 7] as Set + map.containsValue('Grails') + !map.containsValue('missing') + map.entrySet().collectEntries { [it.key, it.value] } == [name: 'Grails', count: 7] + } + + void 'maps compare and hash using the wrapped bean'() { + given: + def bean = new TestBean() + def map = new LazyMetaPropertyMap(bean) + + expect: + mapsEqual(map, new LazyMetaPropertyMap(bean)) + map.hashCode() == bean.hashCode() + !mapsEqual(map, new LazyMetaPropertyMap(new TestBean())) + !mapsEqual(map, bean) + !mapsEqual(map, null) + } + + void 'null beans and non string containsKey arguments are rejected'() { + when: + operation() + + then: + thrown(IllegalArgumentException) + + where: + operation << [ + { new LazyMetaPropertyMap(null) }, + { new LazyMetaPropertyMap(new TestBean()).containsKey(1) } + ] + } + + void 'remove and clear are unsupported'() { + given: + def map = new LazyMetaPropertyMap(new TestBean(name: 'Grails')) + + when: + operation(map) + + then: + thrown(UnsupportedOperationException) + map.get('name') == 'Grails' + + where: + operation << [{ it.remove('name') }, { it.clear() }] + } + + @CompileStatic + private static boolean mapsEqual(LazyMetaPropertyMap map, Object other) { + map.equals(other) + } + + static class TestBean { + String name + int count + Long id = 1L + static String constraints = 'configuration' + } +} diff --git a/grails-core/src/test/groovy/grails/util/GrailsClassUtilsSpec.groovy b/grails-core/src/test/groovy/grails/util/GrailsClassUtilsSpec.groovy new file mode 100644 index 0000000000..4785340000 --- /dev/null +++ b/grails-core/src/test/groovy/grails/util/GrailsClassUtilsSpec.groovy @@ -0,0 +1,196 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package grails.util + +import java.io.ByteArrayInputStream +import java.io.Closeable + +import grails.artefact.Enhanced +import groovy.util.ConfigObject +import spock.lang.Specification + +class GrailsClassUtilsSpec extends Specification { + + void 'interface discovery includes superclass and superinterface declarations'() { + given: + def instance = new ByteArrayInputStream(new byte[0]) + def expected = [Closeable, AutoCloseable] as Set + + expect: + (GrailsClassUtils.getAllInterfaces(instance) as Set) == expected + GrailsClassUtils.getAllInterfacesAsSet(instance) == expected + (GrailsClassUtils.getAllInterfacesForClass(ByteArrayInputStream) as Set) == expected + (GrailsClassUtils.getAllInterfacesForClass(ByteArrayInputStream, getClass().classLoader) as Set) == expected + GrailsClassUtils.getAllInterfacesForClassAsSet(ByteArrayInputStream) == expected + GrailsClassUtils.getAllInterfacesForClassAsSet(ByteArrayInputStream, getClass().classLoader) == expected + GrailsClassUtils.getAllInterfacesForClass(Object).length == 0 + } + + void 'interface discovery rejects null arguments'() { + when: + operation() + + then: + thrown(IllegalArgumentException) + + where: + operation << [ + { GrailsClassUtils.getAllInterfaces(null) }, + { GrailsClassUtils.getAllInterfacesAsSet(null) }, + { GrailsClassUtils.getAllInterfacesForClassAsSet(null) } + ] + } + + void 'visibility checks accept visible classes and reject unavailable classes'() { + given: + def bootstrapOnly = new ClassLoader(null) { } + + expect: + GrailsClassUtils.isVisible(TestBean, null) + GrailsClassUtils.isVisible(TestBean, getClass().classLoader) + GrailsClassUtils.isVisible(String, bootstrapOnly) + !GrailsClassUtils.isVisible(TestBean, bootstrapOnly) + } + + void 'property descriptors expose property types and exact type matching'() { + expect: + GrailsClassUtils.getPropertyType(TestBean, 'name') == String + GrailsClassUtils.getPropertyType(TestBean, 'missing') == null + GrailsClassUtils.getPropertyType(null, 'name') == null + GrailsClassUtils.getPropertyType(TestBean, ' ') == null + GrailsClassUtils.isPropertyOfType(TestBean, 'name', String) + !GrailsClassUtils.isPropertyOfType(TestBean, 'name', CharSequence) + !GrailsClassUtils.isPropertyOfType(TestBean, 'missing', String) + GrailsClassUtils.getProperty(TestBean, 'name').propertyType == String + GrailsClassUtils.getProperty(TestBean, 'name', String).name == 'name' + GrailsClassUtils.getProperty(TestBean, 'name', Integer) == null + GrailsClassUtils.getProperty(TestBean, 'missing') == null + GrailsClassUtils.getProperty(null, 'name') == null + GrailsClassUtils.getProperty(TestBean, null) == null + GrailsClassUtils.getProperty(TestBean, 'name', null) == null + } + + void 'property type selection distinguishes assignable properties from accepting properties'() { + expect: + GrailsClassUtils.getPropertiesOfType(TestBean, String)*.name.toSet() == ['name', 'text'] as Set + GrailsClassUtils.getPropertiesAssignableToType(TestBean, String)*.name == ['name'] + GrailsClassUtils.getPropertiesAssignableToType(TestBean, CharSequence)*.name.toSet() == ['name', 'text'] as Set + GrailsClassUtils.getPropertiesOfType(null, String).length == 0 + GrailsClassUtils.getPropertiesOfType(TestBean, null).length == 0 + GrailsClassUtils.getPropertiesAssignableToType(null, String).length == 0 + GrailsClassUtils.getPropertiesAssignableToType(TestBean, null).length == 0 + } + + void 'new instance property lookup handles valid invalid and uninstantiable classes'() { + expect: + GrailsClassUtils.getPropertyValueOfNewInstance(TestBean, 'name') == 'Grails' + GrailsClassUtils.getPropertyValueOfNewInstance(TestBean, 'name', String) == 'Grails' + GrailsClassUtils.getPropertyValueOfNewInstance(TestBean, 'missing') == null + GrailsClassUtils.getPropertyValueOfNewInstance(null, 'name') == null + GrailsClassUtils.getPropertyValueOfNewInstance(TestBean, ' ') == null + GrailsClassUtils.getPropertyValueOfNewInstance(Runnable, 'name') == null + GrailsClassUtils.getPropertyValueOfNewInstance(Runnable, 'name', String) == null + } + + void 'property descriptor lookup finds matching values or returns null'() { + given: + def bean = new TestBean() + + expect: + GrailsClassUtils.getPropertyDescriptorForValue(bean, 'Grails').name == 'name' + GrailsClassUtils.getPropertyDescriptorForValue(bean, 'missing') == null + GrailsClassUtils.getPropertyDescriptorForValue(null, 'Grails') == null + GrailsClassUtils.getPropertyDescriptorForValue(bean, null) == null + } + + void 'collection conversion preserves order and accepts null collections'() { + expect: + GrailsClassUtils.collectionToObjectArray(['one', 2, null]).toList() == ['one', 2, null] + GrailsClassUtils.collectionToObjectArray([]).length == 0 + GrailsClassUtils.collectionToObjectArray(null).length == 0 + } + + void 'boolean map lookup converts values and respects defaults'() { + expect: + GrailsClassUtils.getBooleanFromMap('enabled', values, defaultValue) == expected + GrailsClassUtils.getBooleanFromMap('enabled', values) == withoutDefault + + where: + values | defaultValue || expected | withoutDefault + null | true || true | false + [:] | true || true | false + [enabled: null] | true || true | false + [enabled: true] | false || true | true + [enabled: false] | true || false | false + [enabled: 'true'] | false || true | true + [enabled: 'TRUE'] | false || true | true + [enabled: 'false'] | true || false | false + [enabled: 'anything'] | true || false | false + } + + void 'package matching accepts containing packages and skips null entries'() { + expect: + GrailsClassUtils.isClassBelowPackage(GrailsClassUtils, ['grails.util']) + GrailsClassUtils.isClassBelowPackage(GrailsClassUtils, [null, 'grails']) + !GrailsClassUtils.isClassBelowPackage(GrailsClassUtils, ['org.grails']) + !GrailsClassUtils.isClassBelowPackage(GrailsClassUtils, []) + } + + void 'flat configuration selects an implementation or uses the default'() { + expect: + GrailsClassUtils.instantiateFromFlatConfig([:], 'implementation', ArrayList.name) instanceof ArrayList + GrailsClassUtils.instantiateFromFlatConfig([implementation: LinkedHashSet.name], 'implementation', ArrayList.name) instanceof LinkedHashSet + GrailsClassUtils.instantiateFromFlatConfig([implementation: new StringBuilder(LinkedHashSet.name)], 'implementation', ArrayList.name) instanceof LinkedHashSet + GrailsClassUtils.instantiateFromFlatConfig([implementation: 42], 'implementation', ArrayList.name) instanceof ArrayList + } + + void 'nested configuration is flattened before instantiation'() { + given: + def config = new ConfigObject() + config.service.implementation = LinkedHashSet.name + + expect: + GrailsClassUtils.instantiateFromConfig(config, 'service.implementation', ArrayList.name) instanceof LinkedHashSet + } + + void 'unknown implementation classes propagate class loading failures'() { + when: + GrailsClassUtils.instantiateFromFlatConfig([implementation: 'grails.util.MissingImplementation'], 'implementation', ArrayList.name) + + then: + thrown(ClassNotFoundException) + } + + void 'feature enhancement requires an annotation containing the feature'() { + expect: + GrailsClassUtils.hasBeenEnhancedForFeature(EnhancedBean, 'controllers') + !GrailsClassUtils.hasBeenEnhancedForFeature(EnhancedBean, 'services') + !GrailsClassUtils.hasBeenEnhancedForFeature(TestBean, 'controllers') + } + + static class TestBean { + String name = 'Grails' + CharSequence text = 'text' + Object value + } + + @Enhanced(version = '7.0', enhancedFor = ['controllers']) + static class EnhancedBean { + } +} diff --git a/grails-core/src/test/groovy/org/grails/config/CompositeConfigSpec.groovy b/grails-core/src/test/groovy/org/grails/config/CompositeConfigSpec.groovy new file mode 100644 index 0000000000..30c2e940ed --- /dev/null +++ b/grails-core/src/test/groovy/org/grails/config/CompositeConfigSpec.groovy @@ -0,0 +1,184 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.config + +import org.grails.core.exceptions.GrailsConfigurationException +import spock.lang.Specification + +class CompositeConfigSpec extends Specification { + + void 'first config takes precedence and last config supplies missing values'() { + given: + def config = new CompositeConfig() + config.addLast(new PrefixedConfig('scope', new PropertySourcesConfig([scope: [shared: 'middle', fallback: 42]]))) + config.addLast(new PrefixedConfig('scope', new PropertySourcesConfig([scope: [shared: 'last', lastOnly: 'last']]))) + config.addFirst(new PrefixedConfig('scope', new PropertySourcesConfig([scope: [shared: 'first', firstOnly: 'first']]))) + + expect: + config.getAt('shared') == 'first' + config.get('shared') == 'first' + config.getProperty('shared') == 'first' + config.getProperty('fallback', Integer) == 42 + config.getAt('lastOnly') == 'last' + config.getAt('firstOnly') == 'first' + config.getAt('missing') == null + config.get('missing') == null + config.getProperty('missing', Integer) == null + config.getProperty('missing', Integer, 7) == 7 + config.getProperty('missing', 'default') == 'default' + } + + void 'navigate searches nested paths across configs'() { + given: + def config = new CompositeConfig() + config.addFirst(new PropertySourcesConfig([service: [host: 'first']])) + config.addLast(new PropertySourcesConfig([service: [host: 'last', port: 8080]])) + + expect: + config.navigate('service', 'host') == 'first' + config.navigate('service', 'port') == 8080 + config.navigate('service', 'missing') == null + } + + void 'flatten and properties combine configs in insertion order'() { + given: + def config = new CompositeConfig() + config.addFirst(new PropertySourcesConfig([shared: 'first', firstOnly: 'one'])) + config.addLast(new PropertySourcesConfig([shared: 'last', lastOnly: 'two'])) + + expect: + config.flatten() == [shared: 'last', firstOnly: 'one', lastOnly: 'two'] + config.toProperties() == [shared: 'last', firstOnly: 'one', lastOnly: 'two'] as Properties + config.getProperty('shared') == 'first' + } + + void 'map views include every config and deduplicate keys'() { + given: + def first = new PropertySourcesConfig([shared: 'first', firstOnly: 'one']) + def last = new PropertySourcesConfig([shared: 'last', lastOnly: 'two']) + def config = new CompositeConfig() + config.addFirst(first) + config.addLast(last) + + expect: + config.size() == first.size() + last.size() + !config.isEmpty() + config.keySet() == ['shared', 'firstOnly', 'lastOnly'] as Set + config.containsKey('firstOnly') + config.containsKey('lastOnly') + !config.containsKey('missing') + config.containsProperty('shared') + !config.containsProperty('missing') + config.containsValue('first') + config.containsValue('last') + !config.containsValue('missing') + config.values().toSet() == ['first', 'last', 'one', 'two'] as Set + config.entrySet() == (first.entrySet() + last.entrySet()) as Set + config.iterator().toList().toSet() == config.entrySet() + } + + void 'empty composites and empty delegates have empty views'() { + given: + def config = new CompositeConfig() + config.addLast(new PropertySourcesConfig()) + + expect: + config.isEmpty() + config.size() == 0 + config.keySet().isEmpty() + !config.iterator().hasNext() + config.flatten().isEmpty() + config.toProperties().isEmpty() + } + + void 'allowed values accept configured and default values'() { + given: + def config = new CompositeConfig() + config.addLast(new PropertySourcesConfig([mode: 'enabled'])) + + expect: + config.getProperty('mode', String, 'disabled', ['enabled', 'disabled']) == 'enabled' + config.getProperty('missing', String, 'disabled', ['enabled', 'disabled']) == 'disabled' + } + + void 'invalid allowed values throw a configuration exception'() { + given: + def config = new CompositeConfig() + config.addLast(new PropertySourcesConfig([mode: 'invalid'])) + + when: + config.getProperty('mode', String, 'disabled', ['enabled', 'disabled']) + + then: + thrown(GrailsConfigurationException) + } + + void 'required properties return values and blank strings use defaults'() { + given: + def config = new CompositeConfig() + config.addFirst(new PropertySourcesConfig([name: 'Grails', count: 2, blank: ' '])) + + expect: + config.getRequiredProperty('name') == 'Grails' + config.getRequiredProperty('count', Integer) == 2 + config.getProperty('blank', 'default') == 'default' + } + + void 'missing required properties are rejected'() { + given: + def config = new CompositeConfig() + + when: + operation(config) + + then: + thrown(IllegalStateException) + + where: + operation << [ + { it.getRequiredProperty('missing') }, + { it.getRequiredProperty('missing', Integer) } + ] + } + + void 'modification and placeholder resolution are unsupported'() { + given: + def config = new CompositeConfig() + config.addLast(new PropertySourcesConfig([name: 'original'])) + + when: + operation(config) + + then: + thrown(UnsupportedOperationException) + config.getProperty('name') == 'original' + + where: + operation << [ + { it.merge([name: 'changed']) }, + { it.setAt('name', 'changed') }, + { it.put('name', 'changed') }, + { it.remove('name') }, + { it.putAll([name: 'changed']) }, + { it.clear() }, + { it.resolvePlaceholders('${name}') }, + { it.resolveRequiredPlaceholders('${name}') } + ] + } +} diff --git a/grails-core/src/test/groovy/org/grails/config/PrefixedConfigSpec.groovy b/grails-core/src/test/groovy/org/grails/config/PrefixedConfigSpec.groovy new file mode 100644 index 0000000000..cfd3cbe9fe --- /dev/null +++ b/grails-core/src/test/groovy/org/grails/config/PrefixedConfigSpec.groovy @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.config + +import groovy.transform.CompileStatic +import spock.lang.Specification + +class PrefixedConfigSpec extends Specification { + + void 'relative keys omit the prefix when reading the delegate'() { + given: + def delegate = new PropertySourcesConfig([ + 'app.service.host': 'localhost', + 'app.service.port': 8080, + host: 'outside' + ]) + def config = new PrefixedConfig('app.service', delegate) + + expect: + config.getAt('host') == 'localhost' + config.get('port') == 8080 + config.getProperty('host') == 'localhost' + config.getProperty('host', 'default') == 'localhost' + config.getProperty('port', Integer) == 8080 + config.getProperty('port', Integer, 80) == 8080 + config.getRequiredProperty('host') == 'localhost' + config.getRequiredProperty('port', Integer) == 8080 + config.containsKey('host') + config.containsProperty('port') + config.navigate('host') == 'localhost' + config.size() == delegate.size() + config.isEmpty() == delegate.isEmpty() + config.values() == delegate.values() + config.containsValue('localhost') + !config.containsValue('absent') + } + + void 'missing prefixed keys return null or the supplied default'() { + given: + def config = new PrefixedConfig('app.service', new PropertySourcesConfig([host: 'outside'])) + + expect: + config.getAt('host') == null + config.getProperty('host') == null + config.getProperty('host', 'default') == 'default' + config.getProperty('port', Integer) == null + config.getProperty('port', Integer, 80) == 80 + !config.containsKey('host') + !config.containsProperty('port') + config.navigate('missing') == null + } + + void 'navigation prepends every token of a dotted prefix'() { + given: + def delegate = new PropertySourcesConfig([app: [service: [connection: [timeout: 30]]]]) + def config = new PrefixedConfig('app.service', delegate) + + expect: + config.navigate('connection', 'timeout') == 30 + config.getProperty('connection.timeout', Integer) == 30 + } + + void 'missing required properties propagate delegate exceptions'() { + given: + def config = new PrefixedConfig('app', new PropertySourcesConfig()) + + when: + operation(config) + + then: + thrown(IllegalStateException) + + where: + operation << [ + { it.getRequiredProperty('missing') }, + { it.getRequiredProperty('missing', Integer) } + ] + } + + void 'equality and hash codes depend on the prefix and delegate'() { + given: + def delegate = new PropertySourcesConfig([name: 'Grails']) + def config = new PrefixedConfig('app', delegate) + def equivalent = new PrefixedConfig('app', delegate) + + expect: + configsEqual(config, config) + configsEqual(config, equivalent) + config.hashCode() == equivalent.hashCode() + !configsEqual(config, null) + !configsEqual(config, delegate) + !configsEqual(config, new PrefixedConfig('other', delegate)) + !configsEqual(config, new PrefixedConfig('app', new PropertySourcesConfig([name: 'other']))) + } + + void 'modification and placeholder resolution are unsupported'() { + given: + def delegate = new PropertySourcesConfig(['app.name': 'original']) + def config = new PrefixedConfig('app', delegate) + + when: + operation(config) + + then: + thrown(UnsupportedOperationException) + delegate.getProperty('app.name') == 'original' + + where: + operation << [ + { it.merge([name: 'changed']) }, + { it.setAt('name', 'changed') }, + { it.put('name', 'changed') }, + { it.remove('name') }, + { it.putAll([name: 'changed']) }, + { it.clear() }, + { it.resolvePlaceholders('${name}') }, + { it.resolveRequiredPlaceholders('${name}') } + ] + } + @CompileStatic + private static boolean configsEqual(PrefixedConfig config, Object other) { + config.equals(other) + } +} diff --git a/grails-core/src/test/groovy/org/grails/core/util/StopWatchSpec.groovy b/grails-core/src/test/groovy/org/grails/core/util/StopWatchSpec.groovy new file mode 100644 index 0000000000..637ca0b9d6 --- /dev/null +++ b/grails-core/src/test/groovy/org/grails/core/util/StopWatchSpec.groovy @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.core.util + +import spock.lang.Specification + +class StopWatchSpec extends Specification { + + void 'new watches have no tasks or elapsed time'() { + given: + def watch = new StopWatch() + + expect: + !watch.isRunning() + watch.getTaskCount() == 0 + watch.getTaskInfo().length == 0 + watch.getTotalTimeMillis() == 0 + watch.getTotalTimeSeconds() == 0.0 + watch.shortSummary() == "StopWatch '': running time (millis) = 0" + watch.prettyPrint().contains('Task name') + watch.toString() == watch.shortSummary() + } + + void 'start and stop record an unnamed task and complete ends the watch'() { + given: + def watch = new StopWatch('startup') + + when: + watch.start() + + then: + watch.isRunning() + watch.getTaskCount() == 1 + watch.getLastTaskName() == '' + + when: + watch.stop() + watch.complete() + + then: + !watch.isRunning() + watch.getTaskInfo().length == 1 + watch.getLastTaskTimeMillis() >= 0 + watch.getLastTaskInfo().is(watch.getTaskInfo()[0]) + watch.getTotalTimeMillis() == watch.getLastTaskTimeMillis() + watch.getTotalTimeSeconds() == watch.getTotalTimeMillis() / 1000.0 + watch.getLastTaskInfo().getTimeSeconds() == watch.getLastTaskTimeMillis() / 1000.0 + } + + void 'multiple named tasks contribute to totals and reports'() { + given: + def watch = new StopWatch('startup') + + when: + watch.start('configuration') + watch.stop() + watch.start('beans') + watch.stop() + watch.complete() + + then: + watch.getTaskCount() == 2 + watch.getTaskInfo()*.taskName == ['configuration', 'beans'] + watch.getLastTaskName() == 'beans' + watch.getTotalTimeMillis() == watch.getTaskInfo()*.timeMillis.sum() + watch.getTaskInfo().every { it.timeMillis >= 0 } + watch.shortSummary() == "StopWatch 'startup': running time (millis) = ${watch.getTotalTimeMillis()}" + watch.prettyPrint().startsWith(watch.shortSummary()) + watch.prettyPrint().contains('ms % Task name') + watch.prettyPrint().contains('configuration\n') + watch.prettyPrint().contains('beans\n') + watch.toString().contains('[configuration] took ') + watch.toString().contains('[beans] took ') + } + + void 'starting twice supports nested tasks stopped in reverse order'() { + given: + def watch = new StopWatch('nested') + + when: + watch.start('outer') + watch.start('inner') + watch.stop() + watch.stop() + watch.complete() + + then: + watch.getTaskCount() == 2 + watch.getTaskInfo()*.taskName == ['inner', 'outer'] + watch.getTaskInfo()[1].timeMillis >= watch.getTaskInfo()[0].timeMillis + watch.getTotalTimeMillis() == watch.getTaskInfo()*.timeMillis.sum() + !watch.isRunning() + } + + void 'stopping without starting throws'() { + when: + new StopWatch().stop() + + then: + thrown(IllegalStateException) + } + + void 'last task accessors throw before any task is started'() { + given: + def watch = new StopWatch() + + when: + operation(watch) + + then: + thrown(IllegalStateException) + + where: + operation << [ + { it.getLastTaskName() }, + { it.getLastTaskTimeMillis() }, + { it.getLastTaskInfo() } + ] + } + + void 'completed watches reject stop'() { + given: + def watch = new StopWatch() + watch.start('task') + watch.stop() + watch.complete() + + when: + watch.stop() + + then: + thrown(IllegalStateException) + } +}
