<commit_before>/*  Copyright 2008 <NAME>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.qi4j.entity.ibatis;

import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.qi4j.bootstrap.AssemblyException;
import org.qi4j.bootstrap.ModuleAssembly;
import org.qi4j.composite.CompositeBuilder;
import org.qi4j.entity.UnitOfWork;
import org.qi4j.entity.UnitOfWorkCompletionException;
import org.qi4j.entity.ibatis.dbInitializer.DBInitializerConfiguration;
import org.qi4j.entity.ibatis.entity.PersonComposite;
import org.qi4j.entity.ibatis.entity.AccountComposite;
import org.qi4j.entity.ibatis.test.AbstractTestCase;
import org.qi4j.entity.ibatis.DerbyDatabaseHandler;
import org.qi4j.entity.memory.MemoryEntityStoreService;
import org.qi4j.property.Property;
import org.qi4j.spi.composite.CompositeBinding;
import org.qi4j.spi.entity.EntityState;
import org.qi4j.spi.entity.EntityStoreException;
import org.qi4j.spi.entity.QualifiedIdentity;
import org.qi4j.spi.entity.UuidIdentityGeneratorService;
import org.qi4j.spi.property.PropertyBinding;
import org.qi4j.spi.property.PropertyModel;
import org.qi4j.spi.structure.CompositeDescriptor;
import org.qi4j.structure.Visibility;

/**
 * {@code IBatisEntityStoreTest} tests {@code IBatisEntityStore}.
 *
 * @author <EMAIL>
 * @since 0.1.0
 */
public final class IBatisEntityStoreTest extends AbstractTestCase
{
    private static final String SCHEMA_FILE = "testDbSchema.sql";
    private static final String DATA_FILE = "testDbData.sql";
    private static final String SQL_MAP_CONFIG_XML = "SqlMapConfig.xml";

    private static final String NEW_TEST_ID = "111";
    private static final String JOHN_SMITH_ID = "1";

    private IBatisEntityStoreService entityStore;
    private static final String JOHNS_ACCOUNT = "Johns Account";

    @Test public void isThereDataInTheDatabaseAfterInitialization()
        throws Exception
    {
        entityStore.toString();
        derbyDatabaseHandler.checkDataInitialization();
    }

    @Test public final void createNewEntityStateWithoutPersisting()
        throws SQLException, UnitOfWorkCompletionException
    {
        final UnitOfWork uow = unitOfWorkFactory.newUnitOfWork();
        final CompositeDescriptor personCompositeDescriptor = getCompositeDescriptor( PersonComposite.class );

        final EntityState state = entityStore.newEntityState( personCompositeDescriptor, id( NEW_TEST_ID ) );
        assertNotNull( state );
        checkEntityStateProperties( getCompositeBinding( PersonComposite.class ), state, false );
        uow.complete();
    }

    @Test public final void newEntityStateIsPersistedToDatabase()
        throws SQLException, UnitOfWorkCompletionException
    {
        final Map<String, String> data = createTestData( "Edward", "Yakop" );

        final UnitOfWork uow = unitOfWorkFactory.newUnitOfWork();

        final CompositeBuilder<PersonComposite> builder = uow.newEntityBuilder( PersonComposite.class );
        final PersonComposite person = builder.newInstance();
        final String newId = person.identity().get();

        person.firstName().set( data.get( "FIRST_NAME" ) );
        person.lastName().set( data.get( "LAST_NAME" ) );

        uow.complete();
        assertPersonEqualsInDatabase( newId, data );
    }


    @Test public final void associationIsPersistedToDatabase()
        throws SQLException, UnitOfWorkCompletionException
    {
        UnitOfWork uow = unitOfWorkFactory.newUnitOfWork();
        final PersonComposite john = uow.find( JOHN_SMITH_ID, PersonComposite.class );
        assertNotNull( "john", john );
        final AccountComposite johnsAccount = uow.newEntity(AccountComposite.class );
        final String accountId = johnsAccount.identity().get();
        johnsAccount.name().set( JOHNS_ACCOUNT );
        johnsAccount.primaryContactPerson().set( john );
        uow.complete();

        uow = unitOfWorkFactory.newUnitOfWork();
        final AccountComposite account = uow.find( accountId, AccountComposite.class );
        assertEquals( "account name",JOHNS_ACCOUNT, account.name().get() );<commit_msg>tests for remove and update<commit_after>/*  Copyright 2008 <NAME>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.qi4j.entity.ibatis;

import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.qi4j.bootstrap.AssemblyException;
import org.qi4j.bootstrap.ModuleAssembly;
import org.qi4j.composite.CompositeBuilder;
import org.qi4j.entity.UnitOfWork;
import org.qi4j.entity.UnitOfWorkCompletionException;
import org.qi4j.entity.ibatis.dbInitializer.DBInitializerConfiguration;
import org.qi4j.entity.ibatis.entity.PersonComposite;
import org.qi4j.entity.ibatis.entity.AccountComposite;
import org.qi4j.entity.ibatis.test.AbstractTestCase;
import org.qi4j.entity.ibatis.DerbyDatabaseHandler;
import org.qi4j.entity.memory.MemoryEntityStoreService;
import org.qi4j.property.Property;
import org.qi4j.spi.composite.CompositeBinding;
import org.qi4j.spi.entity.EntityState;
import org.qi4j.spi.entity.EntityStoreException;
import org.qi4j.spi.entity.QualifiedIdentity;
import org.qi4j.spi.entity.UuidIdentityGeneratorService;
import org.qi4j.spi.property.PropertyBinding;
import org.qi4j.spi.property.PropertyModel;
import org.qi4j.spi.structure.CompositeDescriptor;
import org.qi4j.structure.Visibility;

/**
 * {@code IBatisEntityStoreTest} tests {@code IBatisEntityStore}.
 *
 * @author <EMAIL>
 * @since 0.1.0
 */
public final class IBatisEntityStoreTest extends AbstractTestCase
{
    private static final String SCHEMA_FILE = "testDbSchema.sql";
    private static final String DATA_FILE = "testDbData.sql";
    private static final String SQL_MAP_CONFIG_XML = "SqlMapConfig.xml";

    private static final String NEW_TEST_ID = "111";
    private static final String JOHN_SMITH_ID = "1";

    private IBatisEntityStoreService entityStore;
    private static final String JOHNS_ACCOUNT = "Johns Account";

    @Test public void isThereDataInTheDatabaseAfterInitialization()
        throws Exception
    {
        entityStore.toString();
        derbyDatabaseHandler.checkDataInitialization();
    }

    @Test public final void createNewEntityStateWithoutPersisting()
        throws SQLException, UnitOfWorkCompletionException
    {
        final UnitOfWork uow = unitOfWorkFactory.newUnitOfWork();
        final CompositeDescriptor personCompositeDescriptor = getCompositeDescriptor( PersonComposite.class );

        final EntityState state = entityStore.newEntityState( personCompositeDescriptor, id( NEW_TEST_ID ) );
        assertNotNull( state );
        checkEntityStateProperties( getCompositeBinding( PersonComposite.class ), state, false );
        uow.complete();
    }

    @Test public final void newEntityStateIsPersistedToDatabase()
        throws SQLException, UnitOfWorkCompletionException
    {
        final Map<String, String> data = createTestData( "Edward", "Yakop" );

        final UnitOfWork uow = unitOfWorkFactory.newUnitOfWork();

        final CompositeBuilder<PersonComposite> builder = uow.newEntityBuilder( PersonComposite.class );
        final PersonComposite person = builder.newInstance();
        final String newId = person.identity().get();

        person.firstName().set( data.get( "FIRST_NAME" ) );
        person.lastName().set( data.get( "LAST_NAME" ) );

        uow.complete();
        assertPersonEqualsInDatabase( newId, data );
    }

    @Test public final void existingEntityIsDeletedFromPersistentStore()
        throws SQLException, UnitOfWorkCompletionException
    {
        final UnitOfWork uow = unitOfWorkFactory.newUnitOfWork();
        final PersonComposite john = uow.find( JOHN_SMITH_ID, PersonComposite.class );
        uow.remove( john );
        uow.complete();
        derbyDatabaseHandler.executeStatement( "select count(*) CNT from person where ID= '"+JOHN_SMITH_ID+"'", new DerbyDatabaseHandler.ResultSetCallback() {
            public void row( final ResultSet rs ) throws SQLException
            {
                assertEquals( 0, rs.getInt( "CNT" ));
            }
        });
    }

    @Test public final void existingEntityIsUpdatedInPersistentStore()
        throws SQLException, UnitOfWorkCompletionException
    {
        final UnitOfWork uow = unitOfWorkFactory.newUnitOfWork();
        final PersonComposite john = uow.find( JOHN_SMITH_ID, PersonComposite.class );
        john.lastName().set( "Doe" );
        uow.complete();
        derbyDatabaseHandler.executeStatement( "select LAST_NAME from person where ID= '"+JOHN_SMITH_ID+"'", new DerbyDatabaseHandler.ResultSetCallback() {
            public void row( final ResultSet rs ) throws SQLException
            {
                assertEquals( "Doe", rs.getString( "LAST_NAME" ));
            }
        });
    }

    @Test public final void associationIsPersistedToDatabase()
        throws SQLException, UnitOfWorkCompletionException
    {
        UnitOfWork uow = unitOfWorkFactory.newUnitOfWork();
        final PersonComposite john = uow.find( JOHN_SMITH_ID, PersonComposite.class );
        assertNotNull( "john", john );
        final AccountComposite johnsAccount = uow.newEntity(AccountComposite.class );
        final String accountId = johnsAccount.identity().get();
        johnsAccount.name().set( JOHNS_ACCOUNT );
        johnsAccount.primaryContactPerson().set( john );
        uow.complete();

        uow = unitOfWorkFactory.newUnitOfWork();
        final AccountComposite account = uow.find( accountId, AccountComposite.class );
        assertEquals( "account name",JOHNS_ACCOUNT, account.name().get() );
{'fraction_non_alphanumeric': 0.09146397011934045, 'fraction_numerical': 0.006832467887400929, 'mean_word_length': 3.958446251129178, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '4630828', 'n_tokens_mistral': 3168, 'n_tokens_neox': 3066, 'n_words': 743}