matrei commented on code in PR #15453: URL: https://github.com/apache/grails-core/pull/15453#discussion_r2853499101
########## grails-testing-support-dbcleanup-core/src/main/groovy/org/apache/grails/testing/cleanup/core/DatabaseCleanupContext.groovy: ########## @@ -0,0 +1,220 @@ +/* + * 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.apache.grails.testing.cleanup.core + +import javax.sql.DataSource + +import groovy.transform.CompileStatic +import groovy.util.logging.Slf4j + +import org.springframework.context.ApplicationContext + +/** + * Context that holds the discovered {@link DatabaseCleaner} implementations and + * provides access to the application's data sources for cleanup operations. + * + * <p>Multiple cleaners can be registered (one per database type). When performing cleanup, + * the context matches each datasource to an appropriate cleaner using either an explicit + * database type from the {@link DatasourceCleanupMapping} or auto-discovery via + * {@link DatabaseCleaner#supports(DataSource)}. If a datasource is marked for cleanup + * but no matching cleaner is found, an error is thrown.</p> + */ +@Slf4j +@CompileStatic +class DatabaseCleanupContext { + + private ApplicationContext applicationContext + private final Map<String, DatabaseCleaner> cleanersByType + + DatabaseCleanupContext(List<DatabaseCleaner> cleaners) { + cleanersByType = createCleanersMap(cleaners) + } + + private static Map<String, DatabaseCleaner> createCleanersMap(List<DatabaseCleaner> cleaners) { + def typeMap = [:] as Map<String, DatabaseCleaner> + cleaners.each { + def type = it.databaseType()?.trim() + if (!type) { + throw new IllegalStateException( + "DatabaseCleaner implementation $it.class.name " + + 'returned a null or empty databaseType()' + ) + } + def existing = typeMap[type] + if (existing) { + throw new IllegalStateException( + "Duplicate databaseType '$type' declared by both " + + "$existing.class.name and $it.class.name. " + + 'Each DatabaseCleaner must declare a unique databaseType.' + ) + } + + typeMap[type] = it + log.debug( + 'Discovered DatabaseCleaner implementation: {} (type: {})', + it.class.name, + type + ) + } + typeMap.asImmutable() + } + + /** + * @return the Spring {@link ApplicationContext}, or null if not yet resolved + */ + ApplicationContext getApplicationContext() { + applicationContext + } + + /** + * Sets the application context. Called by the interceptor when the test's + * Spring context becomes available. + */ + void setApplicationContext(ApplicationContext applicationContext) { + this.applicationContext = applicationContext + } + + /** + * Performs cleanup on data sources found in the application context using the + * provided mapping from the {@link DatabaseCleanup} annotation. + * + * <p>If the mapping specifies explicit database types for data sources, those types + * are used to look up the cleaner directly. Otherwise, auto-discovery via + * {@link DatabaseCleaner#supports(DataSource)} is used.</p> + * + * @param mapping the parsed annotation value describing which data sources to clean + * and optionally which cleaner types to use + * @return a list of {@link DatabaseCleanupStats}, one per datasource cleaned + * @throws IllegalStateException if a datasource marked for cleanup has no + * matching {@link DatabaseCleaner} on the classpath, if an explicitly + * specified database type has no matching cleaner, or if a specified + * datasource bean does not exist in the application context + */ + List<DatabaseCleanupStats> performCleanup(DatasourceCleanupMapping mapping) { + if (!applicationContext) { + throw new IllegalStateException( + 'Cannot perform database cleanup: ApplicationContext is not available' + ) + } + if (!cleanersByType) { + throw new IllegalStateException( + 'Cannot perform database cleanup: no DatabaseCleaner implementations found' + ) + } + + def allDataSources = applicationContext.getBeansOfType(DataSource) + def allStats = [] as List<DatabaseCleanupStats> + + if (mapping.cleanAll) { + // Clean all data sources using auto-discovery + allDataSources.each { beanName, dataSource -> + def cleaner = findCleanerFor(dataSource) + if (!cleaner) { + throw new IllegalStateException( + 'No DatabaseCleaner implementation found that supports ' + + "datasource '$beanName'. Ensure that a database-specific " + + 'cleanup library (e.g., grails-testing-support-dbcleanup-h2) ' + + 'is on the classpath for each database type used in your tests. ' + + "Available cleaners: ${cleanersByType.values()*.databaseType()}" + ) + } + + log.debug( + 'Cleaning up datasource: {} (using {} cleaner)', + beanName, + cleaner.databaseType() + ) + def stats = cleaner.cleanup(applicationContext, dataSource) + stats.datasourceName = beanName + if (stats.tableRowCounts) { + log.debug( + 'Cleaned {} tables from datasource {}', + stats.tableRowCounts.size(), + beanName + ) + } + allStats << stats + } + } else { + // Clean specific data sources per the mapping entries + mapping.entries.each { Review Comment: Updated -- 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]
