// Path: core/src/main/java/eu/drus/jpa/unit/spi/Constants.java
// public final class Constants {
// 
//     private Constants() {}
// 
//     public static final String KEY_ENTITY_MANAGER_FACTORY = "eu.drus.jpa.unit.core.EntityManagerFactory";
//     public static final String KEY_ENTITY_MANAGER = "eu.drus.jpa.unit.core.EntityManager";
// }
// 
// Path: core/src/main/java/eu/drus/jpa/unit/spi/ExecutionContext.java
// public interface ExecutionContext {
// 
//     Field getPersistenceField();
// 
//     PersistenceUnitDescriptor getDescriptor();
// 
//     void storeData(String key, Object value);
// 
//     Object getData(String key);
// }
// 
// Path: core/src/main/java/eu/drus/jpa/unit/spi/TestMethodDecorator.java
// public interface TestMethodDecorator extends TestDecorator {
// 
//     boolean isConfigurationSupported(ExecutionContext ctx);
// 
//     void beforeTest(TestInvocation invocation) throws Exception;
// 
//     void afterTest(TestInvocation invocation) throws Exception;
// }
// 
// Path: core/src/main/java/eu/drus/jpa/unit/spi/TestInvocation.java
// public interface TestInvocation {
// 
//     Class<?> getTestClass();
// 
//     ExecutionContext getContext();
// 
//     Optional<Method> getTestMethod();
// 
//     Optional<Object> getTestInstance();
// 
//     Optional<Throwable> getException();
// 
//     FeatureResolver getFeatureResolver();
// }


import javax.persistence.EntityManagerFactory;
import eu.drus.jpa.unit.spi.Constants;
import eu.drus.jpa.unit.spi.ExecutionContext;
import eu.drus.jpa.unit.spi.TestMethodDecorator;
import eu.drus.jpa.unit.spi.TestInvocation;
package eu.drus.jpa.unit.decorator.jpa;



public class SecondLevelCacheDecorator implements TestMethodDecorator {

    @Override
    public int getPriority() {
        return 0;
    }

    private void evictCache(final boolean doEvict, final EntityManagerFactory emf) {
        if (doEvict) {
            emf.getCache().evictAll();
        }
    }

    @Override
    public void beforeTest(final TestInvocation invocation) throws Exception {
        final EntityManagerFactory emf = (EntityManagerFactory) invocation.getContext().getData(Constants.KEY_ENTITY_MANAGER_FACTORY);

        evictCache(invocation.getFeatureResolver().shouldEvictCacheBefore(), emf);
    }

    @Override
    public void afterTest(final TestInvocation invocation) throws Exception {
// Path: core/src/main/java/eu/drus/jpa/unit/spi/Constants.java
// public final class Constants {
// 
//     private Constants() {}
// 
//     public static final String KEY_ENTITY_MANAGER_FACTORY = "eu.drus.jpa.unit.core.EntityManagerFactory";
//     public static final String KEY_ENTITY_MANAGER = "eu.drus.jpa.unit.core.EntityManager";
// }
// 
// Path: core/src/main/java/eu/drus/jpa/unit/spi/ExecutionContext.java
// public interface ExecutionContext {
// 
//     Field getPersistenceField();
// 
//     PersistenceUnitDescriptor getDescriptor();
// 
//     void storeData(String key, Object value);
// 
//     Object getData(String key);
// }
// 
// Path: core/src/main/java/eu/drus/jpa/unit/spi/TestMethodDecorator.java
// public interface TestMethodDecorator extends TestDecorator {
// 
//     boolean isConfigurationSupported(ExecutionContext ctx);
// 
//     void beforeTest(TestInvocation invocation) throws Exception;
// 
//     void afterTest(TestInvocation invocation) throws Exception;
// }
// 
// Path: core/src/main/java/eu/drus/jpa/unit/spi/TestInvocation.java
// public interface TestInvocation {
// 
//     Class<?> getTestClass();
// 
//     ExecutionContext getContext();
// 
//     Optional<Method> getTestMethod();
// 
//     Optional<Object> getTestInstance();
// 
//     Optional<Throwable> getException();
// 
//     FeatureResolver getFeatureResolver();
// }


// Path: core/src/main/java/eu/drus/jpa/unit/decorator/jpa/SecondLevelCacheDecorator.java
import javax.persistence.EntityManagerFactory;
import eu.drus.jpa.unit.spi.Constants;
import eu.drus.jpa.unit.spi.ExecutionContext;
import eu.drus.jpa.unit.spi.TestMethodDecorator;
import eu.drus.jpa.unit.spi.TestInvocation;

package eu.drus.jpa.unit.decorator.jpa;



public class SecondLevelCacheDecorator implements TestMethodDecorator {

    @Override
    public int getPriority() {
        return 0;
    }

    private void evictCache(final boolean doEvict, final EntityManagerFactory emf) {
        if (doEvict) {
            emf.getCache().evictAll();
        }
    }

    @Override
    public void beforeTest(final TestInvocation invocation) throws Exception {
        final EntityManagerFactory emf = (EntityManagerFactory) invocation.getContext().getData(Constants.KEY_ENTITY_MANAGER_FACTORY);

        evictCache(invocation.getFeatureResolver().shouldEvictCacheBefore(), emf);
    }

    @Override
    public void afterTest(final TestInvocation invocation) throws Exception {
        final EntityManagerFactory emf = (EntityManagerFactory) invocation.getContext().getData(Constants.KEY_ENTITY_MANAGER_FACTORY);