From 80b8fb8b83aa63c149a731e8d0274f52e19906b5 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 23 Feb 2010 17:43:00 +0000 Subject: [PATCH] [SPR-6879] @DirtiesContext is now an @Inherited annotation. --- .../test/annotation/DirtiesContext.java | 2 + .../ClassLevelDirtiesContextTests.java | 69 ++++++++++++++----- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/org.springframework.test/src/main/java/org/springframework/test/annotation/DirtiesContext.java b/org.springframework.test/src/main/java/org/springframework/test/annotation/DirtiesContext.java index 6d0b6e6e77..a21e998343 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/annotation/DirtiesContext.java +++ b/org.springframework.test/src/main/java/org/springframework/test/annotation/DirtiesContext.java @@ -18,6 +18,7 @@ package org.springframework.test.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @@ -54,6 +55,7 @@ import java.lang.annotation.Target; * @since 2.0 */ @Documented +@Inherited @Retention(RetentionPolicy.RUNTIME) @Target( { ElementType.TYPE, ElementType.METHOD }) public @interface DirtiesContext { diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTests.java index 48b3f43660..59d5da748b 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/ClassLevelDirtiesContextTests.java @@ -103,50 +103,71 @@ public class ClassLevelDirtiesContextTests { @Test public void verifyDirtiesContextBehavior() throws Exception { - runTestClassAndAssertStats(CleanTestCase.class, 1); - assertCacheStats("after clean test class", 1, cacheHits.get(), cacheMisses.incrementAndGet()); + assertBehaviorForCleanTestCase(); runTestClassAndAssertStats(ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase.class, 1); assertCacheStats("after class-level @DirtiesContext with clean test method and default class mode", 0, cacheHits.incrementAndGet(), cacheMisses.get()); + assertBehaviorForCleanTestCase(); - runTestClassAndAssertStats(CleanTestCase.class, 1); - assertCacheStats("after clean test class", 1, cacheHits.get(), cacheMisses.incrementAndGet()); + runTestClassAndAssertStats(InheritedClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase.class, 1); + assertCacheStats("after inherited class-level @DirtiesContext with clean test method and default class mode", + 0, cacheHits.incrementAndGet(), cacheMisses.get()); + assertBehaviorForCleanTestCase(); runTestClassAndAssertStats(ClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase.class, 1); assertCacheStats("after class-level @DirtiesContext with clean test method and AFTER_CLASS mode", 0, cacheHits.incrementAndGet(), cacheMisses.get()); + assertBehaviorForCleanTestCase(); - runTestClassAndAssertStats(CleanTestCase.class, 1); - assertCacheStats("after clean test class", 1, cacheHits.get(), cacheMisses.incrementAndGet()); + runTestClassAndAssertStats(InheritedClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase.class, 1); + assertCacheStats("after inherited class-level @DirtiesContext with clean test method and AFTER_CLASS mode", 0, + cacheHits.incrementAndGet(), cacheMisses.get()); + assertBehaviorForCleanTestCase(); runTestClassAndAssertStats(ClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase.class, 3); assertCacheStats("after class-level @DirtiesContext with clean test method and AFTER_EACH_TEST_METHOD mode", 0, cacheHits.incrementAndGet(), cacheMisses.addAndGet(2)); + assertBehaviorForCleanTestCase(); - runTestClassAndAssertStats(CleanTestCase.class, 1); - assertCacheStats("after clean test class", 1, cacheHits.get(), cacheMisses.incrementAndGet()); + runTestClassAndAssertStats(InheritedClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase.class, 3); + assertCacheStats( + "after inherited class-level @DirtiesContext with clean test method and AFTER_EACH_TEST_METHOD mode", 0, + cacheHits.incrementAndGet(), cacheMisses.addAndGet(2)); + assertBehaviorForCleanTestCase(); runTestClassAndAssertStats(ClassLevelDirtiesContextWithDirtyMethodsTestCase.class, 1); assertCacheStats("after class-level @DirtiesContext with dirty test method", 0, cacheHits.incrementAndGet(), cacheMisses.get()); - runTestClassAndAssertStats(ClassLevelDirtiesContextWithDirtyMethodsTestCase.class, 1); assertCacheStats("after class-level @DirtiesContext with dirty test method", 0, cacheHits.get(), cacheMisses.incrementAndGet()); - runTestClassAndAssertStats(ClassLevelDirtiesContextWithDirtyMethodsTestCase.class, 1); assertCacheStats("after class-level @DirtiesContext with dirty test method", 0, cacheHits.get(), cacheMisses.incrementAndGet()); + assertBehaviorForCleanTestCase(); - runTestClassAndAssertStats(CleanTestCase.class, 1); - assertCacheStats("after clean test class", 1, cacheHits.get(), cacheMisses.incrementAndGet()); + runTestClassAndAssertStats(InheritedClassLevelDirtiesContextWithDirtyMethodsTestCase.class, 1); + assertCacheStats("after inherited class-level @DirtiesContext with dirty test method", 0, + cacheHits.incrementAndGet(), cacheMisses.get()); + runTestClassAndAssertStats(InheritedClassLevelDirtiesContextWithDirtyMethodsTestCase.class, 1); + assertCacheStats("after inherited class-level @DirtiesContext with dirty test method", 0, cacheHits.get(), + cacheMisses.incrementAndGet()); + runTestClassAndAssertStats(InheritedClassLevelDirtiesContextWithDirtyMethodsTestCase.class, 1); + assertCacheStats("after inherited class-level @DirtiesContext with dirty test method", 0, cacheHits.get(), + cacheMisses.incrementAndGet()); + assertBehaviorForCleanTestCase(); runTestClassAndAssertStats(ClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase.class, 1); assertCacheStats("after class-level @DirtiesContext with clean test method and AFTER_CLASS mode", 0, cacheHits.incrementAndGet(), cacheMisses.get()); } + private void assertBehaviorForCleanTestCase() { + runTestClassAndAssertStats(CleanTestCase.class, 1); + assertCacheStats("after clean test class", 1, cacheHits.get(), cacheMisses.incrementAndGet()); + } + @AfterClass public static void verifyFinalCacheState() { assertCacheStats("AfterClass", 0, cacheHits.get(), cacheMisses.get()); @@ -180,7 +201,7 @@ public class ClassLevelDirtiesContextTests { } @DirtiesContext - public static final class ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase extends BaseTestCase { + public static class ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase extends BaseTestCase { @Test public void verifyContextWasAutowired() { @@ -188,8 +209,12 @@ public class ClassLevelDirtiesContextTests { } } + public static class InheritedClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase extends + ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase { + } + @DirtiesContext(classMode = ClassMode.AFTER_CLASS) - public static final class ClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase extends BaseTestCase { + public static class ClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase extends BaseTestCase { @Test public void verifyContextWasAutowired() { @@ -197,8 +222,12 @@ public class ClassLevelDirtiesContextTests { } } + public static class InheritedClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase extends + ClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase { + } + @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) - public static final class ClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase extends BaseTestCase { + public static class ClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase extends BaseTestCase { @Test public void verifyContextWasAutowired1() { @@ -216,8 +245,12 @@ public class ClassLevelDirtiesContextTests { } } + public static class InheritedClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase extends + ClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase { + } + @DirtiesContext - public static final class ClassLevelDirtiesContextWithDirtyMethodsTestCase extends BaseTestCase { + public static class ClassLevelDirtiesContextWithDirtyMethodsTestCase extends BaseTestCase { @Test @DirtiesContext @@ -226,4 +259,8 @@ public class ClassLevelDirtiesContextTests { } } + public static class InheritedClassLevelDirtiesContextWithDirtyMethodsTestCase extends + ClassLevelDirtiesContextWithDirtyMethodsTestCase { + } + }