jamesfredley commented on code in PR #16736: URL: https://github.com/apache/grails-core/pull/16736#discussion_r4427611322
########## grails-datasource/src/test/groovy/org/grails/plugins/datasource/DataSourceEnvironmentYamlSpec.groovy: ########## @@ -0,0 +1,215 @@ +/* + * 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.plugins.datasource + +import java.nio.file.Files +import java.nio.file.Path + +import javax.sql.DataSource + +import com.zaxxer.hikari.HikariDataSource + +import org.springframework.core.env.StandardEnvironment +import org.springframework.core.io.ByteArrayResource +import org.springframework.core.io.FileSystemResource +import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy +import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy + +import grails.util.Environment +import org.grails.config.yaml.YamlPropertySourceLoader +import org.grails.core.cfg.GroovyConfigPropertySourceLoader +import org.grails.datastore.gorm.jdbc.connections.DataSourceConnectionSourceFactory +import org.grails.datastore.mapping.core.connections.ConnectionSource + +import spock.lang.Specification + +class DataSourceEnvironmentYamlSpec extends Specification { + + private static final String APPLICATION_YML = '''\ +dataSource: + pooled: true + driverClassName: org.h2.Driver + username: sa + password: '' + type: com.zaxxer.hikari.HikariDataSource + properties: + maximumPoolSize: 4 + minimumIdle: 1 + connectionTimeout: 1000 +environments: + development: + dataSource: + dbCreate: create-drop + url: jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1 + properties: + maximumPoolSize: 16 + minimumIdle: 8 + connectionTimeout: 2000 + test: + dataSource: + dbCreate: update + url: jdbc:h2:mem:testDb;DB_CLOSE_DELAY=-1 + properties: + maximumPoolSize: 2 + minimumIdle: 1 + production: + dataSource: + dbCreate: none + url: jdbc:h2:mem:prodDb;DB_CLOSE_DELAY=-1 + properties: + maximumPoolSize: 32 + minimumIdle: 4 + connectionTimeout: 5000 +''' + + private String previousEnvironment + + def setup() { + previousEnvironment = System.getProperty(Environment.KEY) + } + + def cleanup() { + if (previousEnvironment == null) { + System.clearProperty(Environment.KEY) + } + else { + System.setProperty(Environment.KEY, previousEnvironment) + } + Environment.reset() + } + + void "application.yml #environment block supplies the dataSource properties"() { + given: + System.setProperty(Environment.KEY, environment) + Environment.reset() + def connectionSource = createFromApplicationYaml() + def dataSource = unwrap(connectionSource.source) + + expect: + connectionSource.settings.url == url + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == maximumPoolSize + dataSource.minimumIdle == minimumIdle + dataSource.connectionTimeout == connectionTimeout + + cleanup: + connectionSource?.close() + + where: + environment | url | maximumPoolSize | minimumIdle | connectionTimeout + 'development' | 'jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1' | 16 | 8 | 2000 + 'test' | 'jdbc:h2:mem:testDb;DB_CLOSE_DELAY=-1' | 2 | 1 | 1000 + 'production' | 'jdbc:h2:mem:prodDb;DB_CLOSE_DELAY=-1' | 32 | 4 | 5000 + } + + void "unset grails.env uses the development dataSource properties"() { + given: + System.clearProperty(Environment.KEY) + Environment.reset() + def connectionSource = createFromApplicationYaml() + def dataSource = unwrap(connectionSource.source) + + expect: + connectionSource.settings.url == 'jdbc:h2:mem:devDb;DB_CLOSE_DELAY=-1' + dataSource instanceof HikariDataSource + dataSource.maximumPoolSize == 16 + dataSource.minimumIdle == 8 + dataSource.connectionTimeout == 2000 + + cleanup: + connectionSource?.close() + } + + void "application.yml binds a list under dataSource properties"() { Review Comment: Leaving this as a follow-up, as requested. This PR does not change `DatastoreUtils.preparePropertyResolver`. The datasource YAML path still binds `connectionInitSqls` as a list. I added a sentence under "Not in this PR" so the datastore-path gap stays visible. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
