<commit_before>package net.jonp.sorm.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;

import net.jonp.sorm.CacheMode;
import net.jonp.sorm.SormContext;
import net.jonp.sorm.SormSession;
import net.jonp.sorm.example.Person;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;

/**
 * Tests ORM accesses on the Person test object.
 */
public class PersonTest
{
    private SormContext context;

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp()
        throws Exception
    {
        context = DBInit.dbinit();
    }

    @AfterClass
    public static void tearDownClass()
    {
        DBInit.delete();
    }

    @Test
    public void testSingleInsert()
        throws SQLException
    {
        final SormSession session = context.getTransientSession(CacheMode.None);

        try {
            testSingleInsertImpl(session);
        }
        finally {
            session.close();
        }
    }

    @Test
    public void testCachedSingleInsert()
        throws SQLException
    {
        final SormSession session = context.getTransientSession(CacheMode.Immediate);

        try {
            final Person person = testSingleInsertImpl(session);

            // XXX: By holding onto a strong reference to the Person in the
            // cache, this should prevent the garbage collector from clearing it
            // out
            // However, it is still possible, since the key in the weak hash map
            // is a boxed Integer, and may be cleared anyway
            assertNotNull(session.cacheGet(Person.class, person.getId()));
        }
        finally {
            session.close();
        }
    }

    /**
     * Insert a person, then test that reading the person with that ID matches.
     * 
     * @param session The session to use.
     * @return The original person that was inserted.
     * @throws SQLException If there was a problem.
     */
    private Person testSingleInsertImpl(final SormSession session)
        throws SQLException
    {
        final Person person = buildSimpleObjects(1)[0];
        Person.Orm.create(session, person);

        final Person test = Person.Orm.read(session, person.getId());

        assertPersonEquals(person, test);

        return person;
    }

    @Test
    public void testMultipleInsert()
        throws SQLException
    {
        final SormSession session = context.getSession(CacheMode.None);
        final Person[] people = buildSimpleObjects(100);

        Person.Orm.create(session, people);

        final Integer keys[] = new Integer[people.length];
        for (int i = 0; i < keys.length; i++) {
            keys[i] = people[i].getId();
        }

        final Collection<Person> testCollection = Person.Orm.read(session, keys);
        final Person test[] = testCollection.toArray(new Person[testCollection.size()]);

        for (int i = 0; i < people.length; i++) {
            assertPersonEquals(people[i], test[i]);
        }
    }

    @Test
    public void testSingleUpdate()
        throws SQLException
    {
        final SormSession session = context.getTransientSession(CacheMode.None);

        try {
            final Person person = populate(1)[0];

            final java.sql.Date today = new java.sql.Date(new java.util.Date().getTime());

            person.setName("newname");
            person.setGender("trans");
            person.setDob(today);
            Person.Orm.update(session, person);

            final Person test = Person.Orm.read(session, person.getId());
            assertEquals("newname", test.getName());
            assertEquals("trans", test.getGender());
            assertEquals(today, test.getDob());
        }
        finally {
            session.close();
        }
    }

    @Test
    public void testMultipleUpdate()
    {
        fail("Not implemented");
    }

    @Test
    public void testSingleDelete()
        throws SQLException
    {
        final SormSession session = context.getTransientSession(CacheMode.None);

        try {
            final Person person = populate(1)[0];

            Person.Orm.delete(session, person);

            final Person test = Person.Orm.read(session, person.getId());
            assertNull(test);
        }
        finally {
            session.close();
        }
    }

    @Test
    public void testMultipleDelete()
        throws SQLException
    {
        final SormSession session = context.getTransientSession(CacheMode.Immediate);

        try {
            final Person[] people = populate(100);
            Person.Orm.delete(session, people);

            for (final Person person : people) {
                final Person test = Person.Orm.read(session, person.getId());
                assertNull(test);
            }
        }
        finally {
            session.close();
        }
    }

    /**
     * Build objects with no inter-object relationships. Does not insert them
     * into the database.
     * 
     * @param count The number of objects to build.
     * @return The objects that were built.
     */
    private Person[] buildSimpleObjects(final int count)
    {
        final Calendar dobcal = Calendar.getInstance();
        dobcal.add(Calendar.YEAR, -count);
        dobcal.add(Calendar.MONTH, -count);

        final Person[] objs = new Person[count];
        for (int i = 0; i < count; i++) {
            objs[i] = new Person();
            objs[i].setName("Person " + i);
            objs[i].setDob(dobcal.getTime());
<commit_msg>Testing: Build the database once, and keep reusing the same context.<commit_after>package net.jonp.sorm.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;

import net.jonp.sorm.CacheMode;
import net.jonp.sorm.SormContext;
import net.jonp.sorm.SormSession;
import net.jonp.sorm.example.Person;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Tests ORM accesses on the Person test object.
 */
public class PersonTest
{
    private static SormContext context;

    @BeforeClass
    public static void setUpClass()
        throws Exception
    {
        context = DBInit.dbinit();
    }

    @AfterClass
    public static void tearDownClass()
    {
        context.close();
        DBInit.delete();
    }

    @Test
    public void testSingleInsert()
        throws SQLException
    {
        final SormSession session = context.getSession(CacheMode.None);
        testSingleInsertImpl(session);
    }

    @Test
    public void testCachedSingleInsert()
        throws SQLException
    {
        final SormSession session = context.getSession(CacheMode.Immediate);
        final Person person = testSingleInsertImpl(session);

        // XXX: By holding onto a strong reference to the Person in the
        // cache, this should prevent the garbage collector from clearing it
        // out
        // However, it is still possible, since the key in the weak hash map
        // is a boxed Integer, and may be cleared anyway
        assertNotNull(session.cacheGet(Person.class, person.getId()));
    }

    /**
     * Insert a person, then test that reading the person with that ID matches.
     * 
     * @param session The session to use.
     * @return The original person that was inserted.
     * @throws SQLException If there was a problem.
     */
    private Person testSingleInsertImpl(final SormSession session)
        throws SQLException
    {
        final Person person = buildSimpleObjects(1)[0];
        Person.Orm.create(session, person);

        final Person test = Person.Orm.read(session, person.getId());

        assertPersonEquals(person, test);

        return person;
    }

    @Test
    public void testMultipleInsert()
        throws SQLException
    {
        final SormSession session = context.getSession(CacheMode.None);
        final Person[] people = buildSimpleObjects(100);

        Person.Orm.create(session, people);

        final Integer keys[] = new Integer[people.length];
        for (int i = 0; i < keys.length; i++) {
            keys[i] = people[i].getId();
        }

        final Collection<Person> testCollection = Person.Orm.read(session, keys);
        final Person test[] = testCollection.toArray(new Person[testCollection.size()]);

        for (int i = 0; i < people.length; i++) {
            assertPersonEquals(people[i], test[i]);
        }
    }

    @Test
    public void testSingleUpdate()
        throws SQLException
    {
        final SormSession session = context.getSession(CacheMode.None);
        final Person person = populate(1)[0];

        final java.sql.Date today = new java.sql.Date(new java.util.Date().getTime());

        person.setName("newname");
        person.setGender("trans");
        person.setDob(today);
        Person.Orm.update(session, person);

        final Person test = Person.Orm.read(session, person.getId());
        assertEquals("newname", test.getName());
        assertEquals("trans", test.getGender());
        assertEquals(today, test.getDob());
    }

    @Test
    public void testMultipleUpdate()
    {
        fail("Not implemented");
    }

    @Test
    public void testSingleDelete()
        throws SQLException
    {
        final SormSession session = context.getSession(CacheMode.None);
        final Person person = populate(1)[0];

        Person.Orm.delete(session, person);

        final Person test = Person.Orm.read(session, person.getId());
        assertNull(test);
    }

    @Test
    public void testMultipleDelete()
        throws SQLException
    {
        final SormSession session = context.getSession(CacheMode.Immediate);
        final Person[] people = populate(100);
        Person.Orm.delete(session, people);

        for (final Person person : people) {
            final Person test = Person.Orm.read(session, person.getId());
            assertNull(test);
        }
    }

    /**
     * Build objects with no inter-object relationships. Does not insert them
     * into the database.
     * 
     * @param count The number of objects to build.
     * @return The objects that were built.
     */
    private Person[] buildSimpleObjects(final int count)
    {
        final Calendar dobcal = Calendar.getInstance();
        dobcal.add(Calendar.YEAR, -count);
        dobcal.add(Calendar.MONTH, -count);

        final Person[] objs = new Person[count];
        for (int i = 0; i < count; i++) {
            objs[i] = new Person();
            objs[i].setName("Person " + i);
            objs[i].setDob(dobcal.getTime());

{'fraction_non_alphanumeric': 0.10321288795849641, 'fraction_numerical': 0.002730499681441704, 'mean_word_length': 2.28, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '6611904', 'n_tokens_mistral': 3040, 'n_tokens_neox': 2963, 'n_words': 808}