<commit_before>package eu.drus.test.persistence;

import static eu.drus.test.persistence.core.metadata.TestCodeUtils.buildModel;
import static eu.drus.test.persistence.core.metadata.TestCodeUtils.compileModel;
import static eu.drus.test.persistence.core.metadata.TestCodeUtils.loadClass;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.PersistenceUnit;

import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.mockito.ArgumentCaptor;

import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JPackage;

public class JpaTestRunnerTest {

    @ClassRule
    public static TemporaryFolder testFolder = new TemporaryFolder();

    @Test
    public void testClassWithoutPersistenceContextField() throws Exception {
        // GIVEN
        final JCodeModel jCodeModel = new JCodeModel();
        final JPackage jp = jCodeModel.rootPackage();
        final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
        final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class);
        jAnnotationUse.param("value", JpaTestRunner.class);
        final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
        jMethod.annotate(Test.class);

        buildModel(testFolder.getRoot(), jCodeModel);
        compileModel(testFolder.getRoot());

        final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
        final JpaTestRunner runner = new JpaTestRunner(cut);

        try {
            // WHEN
            runner.run(new RunNotifier());
            fail("IllegalArgumentException expected");
        } catch (final IllegalArgumentException e) {

            // THEN
            assertThat(e.getMessage(), containsString("EntityManagerFactory or EntityManager field annotated"));
        }
    }

    @Test
    public void testClassWithMultiplePersistenceContextFields() throws Exception {
        // GIVEN
        final JCodeModel jCodeModel = new JCodeModel();
        final JPackage jp = jCodeModel.rootPackage();
        final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
        final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class);
        jAnnotationUse.param("value", JpaTestRunner.class);
        final JFieldVar em1Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em1");
        em1Field.annotate(PersistenceContext.class);
        final JFieldVar em2Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em2");
        em2Field.annotate(PersistenceContext.class);
        final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
        jMethod.annotate(Test.class);

        buildModel(testFolder.getRoot(), jCodeModel);
        compileModel(testFolder.getRoot());

        final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
        final JpaTestRunner runner = new JpaTestRunner(cut);

        try {
            // WHEN
            runner.run(new RunNotifier());
            fail("IllegalArgumentException expected");
        } catch (final IllegalArgumentException e) {

            // THEN
            assertThat(e.getMessage(), containsString("Only single field is allowed"));
        }
    }

    @Test
    public void testClassWithMultiplePersistenceUnitFields() throws Exception {
        // GIVEN
        final JCodeModel jCodeModel = new JCodeModel();
        final JPackage jp = jCodeModel.rootPackage();
        final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
        final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class);
        jAnnotationUse.param("value", JpaTestRunner.class);
        final JFieldVar emf1Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf1");
        emf1Field.annotate(PersistenceUnit.class);
        final JFieldVar emf2Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf2");
        emf2Field.annotate(PersistenceUnit.class);
        final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
        jMethod.annotate(Test.class);

        buildModel(testFolder.getRoot(), jCodeModel);
        compileModel(testFolder.getRoot());

        final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
        final JpaTestRunner runner = new JpaTestRunner(cut);

        try {
            // WHEN
            runner.run(new RunNotifier());
            fail("IllegalArgumentException expected");
        } catch (final IllegalArgumentException e) {

            // THEN
            assertThat(e.getMessage(), containsString("Only single field is allowed"));
        }
    }

    @Test
    public void testClassWithPersistenceContextFieldOfWrongType() throws Exception {
        // GIVEN
        final JCodeModel jCodeModel = new JCodeModel();
        final JPackage jp = jCodeModel.rootPackage();
        final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
        final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class);
        jAnnotationUse.param("value", JpaTestRunner.class);
        final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "em");
        emField.annotate(PersistenceContext.class);
        final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
        jMethod.annotate(Test.class);

        buildModel(testFolder.getRoot(), jCodeModel);
        compileModel(testFolder.getRoot());

        final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
        final JpaTestRunner runner = new JpaTestRunner(cut);

        try {
            // WHEN
            runner.run(new RunNotifier());
            fail("IllegalArgumentException expected");<commit_msg>new tests for JpaTestRunner<commit_after>package eu.drus.test.persistence;

import static eu.drus.test.persistence.core.metadata.TestCodeUtils.buildModel;
import static eu.drus.test.persistence.core.metadata.TestCodeUtils.compileModel;
import static eu.drus.test.persistence.core.metadata.TestCodeUtils.loadClass;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.PersistenceUnit;

import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.mockito.ArgumentCaptor;

import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JPackage;

public class JpaTestRunnerTest {

    @ClassRule
    public static TemporaryFolder testFolder = new TemporaryFolder();

    @Test
    public void testClassWithoutPersistenceContextField() throws Exception {
        // GIVEN
        final JCodeModel jCodeModel = new JCodeModel();
        final JPackage jp = jCodeModel.rootPackage();
        final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
        final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class);
        jAnnotationUse.param("value", JpaTestRunner.class);
        final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
        jMethod.annotate(Test.class);

        buildModel(testFolder.getRoot(), jCodeModel);
        compileModel(testFolder.getRoot());

        final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
        final JpaTestRunner runner = new JpaTestRunner(cut);

        try {
            // WHEN
            runner.run(new RunNotifier());
            fail("IllegalArgumentException expected");
        } catch (final IllegalArgumentException e) {

            // THEN
            assertThat(e.getMessage(), containsString("EntityManagerFactory or EntityManager field annotated"));
        }
    }

    @Test
    public void testClassWithMultiplePersistenceContextFields() throws Exception {
        // GIVEN
        final JCodeModel jCodeModel = new JCodeModel();
        final JPackage jp = jCodeModel.rootPackage();
        final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
        final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class);
        jAnnotationUse.param("value", JpaTestRunner.class);
        final JFieldVar em1Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em1");
        em1Field.annotate(PersistenceContext.class);
        final JFieldVar em2Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em2");
        em2Field.annotate(PersistenceContext.class);
        final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
        jMethod.annotate(Test.class);

        buildModel(testFolder.getRoot(), jCodeModel);
        compileModel(testFolder.getRoot());

        final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
        final JpaTestRunner runner = new JpaTestRunner(cut);

        try {
            // WHEN
            runner.run(new RunNotifier());
            fail("IllegalArgumentException expected");
        } catch (final IllegalArgumentException e) {

            // THEN
            assertThat(e.getMessage(), containsString("Only single field is allowed"));
        }
    }

    @Test
    public void testClassWithMultiplePersistenceUnitFields() throws Exception {
        // GIVEN
        final JCodeModel jCodeModel = new JCodeModel();
        final JPackage jp = jCodeModel.rootPackage();
        final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
        final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class);
        jAnnotationUse.param("value", JpaTestRunner.class);
        final JFieldVar emf1Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf1");
        emf1Field.annotate(PersistenceUnit.class);
        final JFieldVar emf2Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf2");
        emf2Field.annotate(PersistenceUnit.class);
        final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
        jMethod.annotate(Test.class);

        buildModel(testFolder.getRoot(), jCodeModel);
        compileModel(testFolder.getRoot());

        final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
        final JpaTestRunner runner = new JpaTestRunner(cut);

        try {
            // WHEN
            runner.run(new RunNotifier());
            fail("IllegalArgumentException expected");
        } catch (final IllegalArgumentException e) {

            // THEN
            assertThat(e.getMessage(), containsString("Only single field is allowed"));
        }
    }

    @Test
    public void testClassWithPersistenceContextAndPersistenceUnitFields() throws Exception {
        // GIVEN
        final JCodeModel jCodeModel = new JCodeModel();
        final JPackage jp = jCodeModel.rootPackage();
        final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
        final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class);
        jAnnotationUse.param("value", JpaTestRunner.class);
        final JFieldVar emf1Field = jClass.field(JMod.PRIVATE, EntityManager.class, "em");
        emf1Field.annotate(PersistenceContext.class);
        final JFieldVar emf2Field = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "emf");
        emf2Field.annotate(PersistenceUnit.class);
        final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
        jMethod.annotate(Test.class);

        buildModel(testFolder.getRoot(), jCodeModel);
        compileModel(testFolder.getRoot());

        final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
        final JpaTestRunner runner = new JpaTestRunner(cut);

        try {
            // WHEN
            runner.run(new RunNotifier());
            fail("IllegalArgumentException expected");
        } catch (final IllegalArgumentException e) {

            // THEN
            assertThat(e.getMessage(), containsString("either @PersistenceUnit or @PersistenceContext"));
        }
    }

    @Test
    public void testClassWithPersistenceContextFieldOfWrongType() throws Exception {
        // GIVEN
        final JCodeModel jCodeModel = new JCodeModel();
        final JPackage jp = jCodeModel.rootPackage();
        final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest");
        final JAnnotationUse jAnnotationUse = jClass.annotate(RunWith.class);
        jAnnotationUse.param("value", JpaTestRunner.class);
        final JFieldVar emField = jClass.field(JMod.PRIVATE, EntityManagerFactory.class, "em");
        emField.annotate(PersistenceContext.class);
        final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod");
        jMethod.annotate(Test.class);

        buildModel(testFolder.getRoot(), jCodeModel);
        compileModel(testFolder.getRoot());

        final Class<?> cut = loadClass(testFolder.getRoot(), jClass.name());
        final JpaTestRunner runner = new JpaTestRunner(cut);

        try {
            // WHEN
            runner.run(new RunNotifier());
            fail("IllegalArgumentException expected");
{'fraction_non_alphanumeric': 0.10262742676819647, 'fraction_numerical': 0.001920834190848597, 'mean_word_length': 3.3856799037304453, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4941536', 'n_tokens_mistral': 4144, 'n_tokens_neox': 4077, 'n_words': 803}