matrei commented on code in PR #15453: URL: https://github.com/apache/grails-core/pull/15453#discussion_r2853495884
########## 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 { Review Comment: Updated ########## grails-testing-support-dbcleanup-core/src/main/groovy/org/apache/grails/testing/cleanup/core/DatabaseCleanupInterceptor.groovy: ########## @@ -119,41 +132,43 @@ class DatabaseCleanupInterceptor extends AbstractMethodInterceptor { return } - ApplicationContext appCtx = resolver.resolve(invocation) + def appCtx = resolver.resolve(invocation) if (appCtx) { context.applicationContext = appCtx } else { throw new IllegalStateException( - 'Could not resolve ApplicationContext from test instance. Ensure the spec is annotated with @Integration.') + 'Could not resolve ApplicationContext from test instance. ' + + 'Ensure the spec is annotated with @Integration.' + ) } } /** * Logs cleanup statistics and overall timing information for the cleanup operation. * - * @param statsList the list of cleanup statistics from individual datasources + * @param statsList the list of cleanup statistics from individual data sources * @param overallStartTime the overall start time of the cleanup operation */ private static void logStats(List<DatabaseCleanupStats> statsList, long overallStartTime) { if (DatabaseCleanupStats.debugEnabled) { long overallEndTime = System.currentTimeMillis() long overallDuration = overallEndTime - overallStartTime - String separator = '==========================================================' - String startTimeFormatted = DatabaseCleanupStats.formatTime(overallStartTime) - String endTimeFormatted = DatabaseCleanupStats.formatTime(overallEndTime) + def separator = '==========================================================' + def startTimeFormatted = DatabaseCleanupStats.formatTime(overallStartTime) + def endTimeFormatted = DatabaseCleanupStats.formatTime(overallEndTime) - System.out.println(separator) - System.out.println('Overall Cleanup Timing') - System.out.println("Start Time: ${startTimeFormatted}") - System.out.println("End Time: ${endTimeFormatted}") - System.out.println("Duration: ${overallDuration} ms") - System.out.println(separator) + println(separator) + println('Overall Cleanup Timing') + println("Start Time: $startTimeFormatted") + println("End Time: $endTimeFormatted") + println("Duration: $overallDuration ms") + println(separator) - for (DatabaseCleanupStats stats : statsList) { - if (stats.tableRowCounts) { - System.out.println(stats.toFormattedReport()) + statsList.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]
