Search in sources :

Example 6 with TestContext

use of cn.taketoday.test.context.TestContext in project today-framework by TAKETODAY.

the class ContextCacheTests method removeContextHierarchyCacheLevel2.

@Test
void removeContextHierarchyCacheLevel2() {
    // Load Level 3-A
    TestContext testContext3a = TestContextTestUtils.buildTestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
    testContext3a.getApplicationContext();
    assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
    assertParentContextCount(2);
    // Load Level 3-B
    TestContext testContext3b = TestContextTestUtils.buildTestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
    testContext3b.getApplicationContext();
    assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
    assertParentContextCount(2);
    // Remove Level 2
    // Should also remove Levels 3-A and 3-B, leaving only Level 1 as a context in the
    // cache but also removing the Level 1 hierarchy since all children have been
    // removed.
    contextCache.remove(getMergedContextConfiguration(testContext3a).getParent(), HierarchyMode.CURRENT_LEVEL);
    assertContextCacheStatistics(contextCache, "removed level 2", 1, 1, 4);
    assertParentContextCount(0);
}
Also used : TestContext(cn.taketoday.test.context.TestContext) Test(org.junit.jupiter.api.Test)

Example 7 with TestContext

use of cn.taketoday.test.context.TestContext in project today-framework by TAKETODAY.

the class ContextCacheTests method removeContextHierarchyCacheLevel1WithExhaustiveMode.

@Test
void removeContextHierarchyCacheLevel1WithExhaustiveMode() {
    // Load Level 3-A
    TestContext testContext3a = TestContextTestUtils.buildTestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
    testContext3a.getApplicationContext();
    assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
    assertParentContextCount(2);
    // Load Level 3-B
    TestContext testContext3b = TestContextTestUtils.buildTestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
    testContext3b.getApplicationContext();
    assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
    assertParentContextCount(2);
    // Remove Level 1
    // Should also remove Levels 2, 3-A, and 3-B, leaving nothing.
    contextCache.remove(getMergedContextConfiguration(testContext3a).getParent().getParent(), HierarchyMode.EXHAUSTIVE);
    assertContextCacheStatistics(contextCache, "removed level 1", 0, 1, 4);
    assertParentContextCount(0);
}
Also used : TestContext(cn.taketoday.test.context.TestContext) Test(org.junit.jupiter.api.Test)

Example 8 with TestContext

use of cn.taketoday.test.context.TestContext in project today-framework by TAKETODAY.

the class ApplicationTestContextBootstrapper method buildTestContext.

@Override
public TestContext buildTestContext() {
    TestContext context = super.buildTestContext();
    verifyConfiguration(context.getTestClass());
    WebEnvironment webEnvironment = getWebEnvironment(context.getTestClass());
    if (webEnvironment == WebEnvironment.MOCK && deduceWebApplicationType() == ApplicationType.SERVLET_WEB) {
        context.setAttribute(ACTIVATE_SERVLET_LISTENER, true);
    } else if (webEnvironment != null && webEnvironment.isEmbedded()) {
        context.setAttribute(ACTIVATE_SERVLET_LISTENER, false);
    }
    return context;
}
Also used : WebEnvironment(cn.taketoday.framework.test.context.ApplicationTest.WebEnvironment) TestContext(cn.taketoday.test.context.TestContext)

Example 9 with TestContext

use of cn.taketoday.test.context.TestContext in project today-framework by TAKETODAY.

the class ContextCacheTests method removeContextHierarchyCacheLevel2WithExhaustiveMode.

@Test
void removeContextHierarchyCacheLevel2WithExhaustiveMode() {
    // Load Level 3-A
    TestContext testContext3a = TestContextTestUtils.buildTestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
    testContext3a.getApplicationContext();
    assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
    assertParentContextCount(2);
    // Load Level 3-B
    TestContext testContext3b = TestContextTestUtils.buildTestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
    testContext3b.getApplicationContext();
    assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
    assertParentContextCount(2);
    // Remove Level 2
    // Should wipe the cache
    contextCache.remove(getMergedContextConfiguration(testContext3a).getParent(), HierarchyMode.EXHAUSTIVE);
    assertContextCacheStatistics(contextCache, "removed level 2", 0, 1, 4);
    assertParentContextCount(0);
}
Also used : TestContext(cn.taketoday.test.context.TestContext) Test(org.junit.jupiter.api.Test)

Example 10 with TestContext

use of cn.taketoday.test.context.TestContext in project today-framework by TAKETODAY.

the class ContextCacheTests method removeContextHierarchyCacheLevel3Then2WithExhaustiveMode.

@Test
void removeContextHierarchyCacheLevel3Then2WithExhaustiveMode() {
    // Load Level 3-A
    TestContext testContext3a = TestContextTestUtils.buildTestContext(ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
    testContext3a.getApplicationContext();
    assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
    assertParentContextCount(2);
    // Load Level 3-B
    TestContext testContext3b = TestContextTestUtils.buildTestContext(ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
    testContext3b.getApplicationContext();
    assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
    assertParentContextCount(2);
    // Remove Level 3-A
    // Should wipe the cache.
    contextCache.remove(getMergedContextConfiguration(testContext3a), HierarchyMode.EXHAUSTIVE);
    assertContextCacheStatistics(contextCache, "removed level 3-A", 0, 1, 4);
    assertParentContextCount(0);
    // Remove Level 2
    // Should not actually do anything since the cache was cleared in the
    // previous step. So the stats should remain the same.
    contextCache.remove(getMergedContextConfiguration(testContext3b).getParent(), HierarchyMode.EXHAUSTIVE);
    assertContextCacheStatistics(contextCache, "removed level 2", 0, 1, 4);
    assertParentContextCount(0);
}
Also used : TestContext(cn.taketoday.test.context.TestContext) Test(org.junit.jupiter.api.Test)

Aggregations

TestContext (cn.taketoday.test.context.TestContext)14 Test (org.junit.jupiter.api.Test)12 WebEnvironment (cn.taketoday.framework.test.context.ApplicationTest.WebEnvironment)2