From b42ded9451552bffb35fd7581a05218b37dfd1e2 Mon Sep 17 00:00:00 2001 From: Shicong Huang Date: Wed, 3 Jun 2020 14:55:37 -0400 Subject: [PATCH] Add test to verify the behavior of @DualDatabaseTest (#606) --- ...baseTestInvocationContextProviderTest.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 core/src/test/java/google/registry/testing/DualDatabaseTestInvocationContextProviderTest.java diff --git a/core/src/test/java/google/registry/testing/DualDatabaseTestInvocationContextProviderTest.java b/core/src/test/java/google/registry/testing/DualDatabaseTestInvocationContextProviderTest.java new file mode 100644 index 000000000..40cdd8fe0 --- /dev/null +++ b/core/src/test/java/google/registry/testing/DualDatabaseTestInvocationContextProviderTest.java @@ -0,0 +1,54 @@ +// Copyright 2020 The Nomulus Authors. All Rights Reserved. +// +// Licensed 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 +// +// http://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 google.registry.testing; + +import static com.google.common.truth.Truth.assertThat; +import static google.registry.persistence.transaction.TransactionManagerFactory.tm; + +import google.registry.model.ofy.DatastoreTransactionManager; +import google.registry.persistence.transaction.JpaTransactionManager; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.RegisterExtension; + +/** + * Test to verify that {@link DualDatabaseTestInvocationContextProvider} extension executes {@link + * TestTemplate} test twice with different databases. + */ +@DualDatabaseTest +public class DualDatabaseTestInvocationContextProviderTest { + + private static int datastoreTestCounter = 0; + private static int postgresqlTestCounter = 0; + + @RegisterExtension + public final AppEngineRule appEngine = AppEngineRule.builder().withDatastoreAndCloudSql().build(); + + @TestTemplate + void testToUseTransactionManager() { + if (tm() instanceof DatastoreTransactionManager) { + datastoreTestCounter++; + } + if (tm() instanceof JpaTransactionManager) { + postgresqlTestCounter++; + } + } + + @AfterAll + static void assertEachTransactionManagerIsUsed() { + assertThat(datastoreTestCounter).isEqualTo(1); + assertThat(postgresqlTestCounter).isEqualTo(1); + } +}