<commit_before>/*******************************************************************************
 * * Copyright 2011 Impetus Infotech.
 *  *
 *  * 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 com.impetus.client.persistence;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import javax.persistence.spi.PersistenceUnitTransactionType;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.impetus.kundera.Constants;
import com.impetus.kundera.PersistenceProperties;
import com.impetus.kundera.metadata.model.ApplicationMetadata;
import com.impetus.kundera.metadata.model.EntityMetadata;
import com.impetus.kundera.metadata.model.KunderaMetadata;
import com.impetus.kundera.metadata.model.MetamodelImpl;
import com.impetus.kundera.metadata.model.PersistenceUnitMetadata;
import com.impetus.kundera.metadata.processor.TableProcessor;
import com.impetus.kundera.persistence.EntityManagerFactoryImpl;
import com.impetus.kundera.persistence.EntityManagerImpl;
import com.impetus.kundera.query.QueryImpl;

/**
 * Junit test case for NativeQuery support.
 * 
 * @author vivek.mishra
 * 
 */
public class NativeQueryTest
{

    /** The schema. */
    private final String schema = "test";

    /**
     * Sets the up.
     * 
     * @throws Exception
     *             the exception
     */
    @Before
    public void setUp() throws Exception
    {
        CassandraCli.cassandraSetUp();
        CassandraCli.createKeySpace("KunderaExamples");
    }

    /**
     * Test create native query.
     */
    @Test
    public void testCreateNativeQuery()
    {
        EntityManagerFactoryImpl emf = getEntityManagerFactory();
        EntityManager em = new EntityManagerImpl(emf, PersistenceUnitTransactionType.RESOURCE_LOCAL,
                PersistenceContextType.EXTENDED);
        String nativeSql = "Select * from Cassandra c";
        QueryImpl q = (QueryImpl) em.createNativeQuery(nativeSql, CassandraEntitySample.class);
        Assert.assertEquals(nativeSql, q.getJPAQuery());
        Assert.assertEquals(true, KunderaMetadata.INSTANCE.getApplicationMetadata().isNative(nativeSql));
    }

    /**
     * Test execute native create keyspace query.
     */
    @Test
    public void testExecutNativeQuery()
    {
        EntityManagerFactoryImpl emf = getEntityManagerFactory();
        String nativeSql = "CREATE KEYSPACE " + schema
                + " with strategy_class = 'SimpleStrategy' and strategy_options:replication_factor=1";
        String useNativeSql = "USE test";

        EntityManager em = new EntityManagerImpl(emf, PersistenceUnitTransactionType.RESOURCE_LOCAL,
                PersistenceContextType.EXTENDED);
        Query q = em.createNativeQuery(nativeSql, CassandraEntitySample.class);
        // q.getResultList();
        q.executeUpdate();
        q = em.createNativeQuery(useNativeSql, CassandraEntitySample.class);
        // q.getResultList();
        q.executeUpdate();
        Assert.assertTrue(CassandraCli.keyspaceExist(schema));
        Assert.assertFalse(CassandraCli.keyspaceExist("invalidSchema"));
    }

    /**
     * Test create insert column family query.
     */
    @Test
    public void testCreateInsertColumnFamilyQuery()
    {
        EntityManagerFactoryImpl emf = getEntityManagerFactory();
        EntityManager em = new EntityManagerImpl(emf, PersistenceUnitTransactionType.RESOURCE_LOCAL,
                PersistenceContextType.EXTENDED);

        // create column family
        String colFamilySql = "CREATE COLUMNFAMILY users (key varchar PRIMARY KEY,full_name varchar, birth_date int,state varchar)";
        Query q = em.createNativeQuery(colFamilySql, CassandraEntitySample.class);
        // q.getResultList();
        q.executeUpdate();
        Assert.assertTrue(CassandraCli.columnFamilyExist("users", "KunderaExamples"));

        // Add indexes
        String idxSql = "CREATE INDEX ON users (birth_date)";
        q = em.createNativeQuery(idxSql, CassandraEntitySample.class);
        // q.getResultList();
        q.executeUpdate();
        idxSql = "CREATE INDEX ON users (state)";
        q = em.createNativeQuery(idxSql, CassandraEntitySample.class);<commit_msg>Add a testcase for the native query connection leak.<commit_after>/*******************************************************************************
 * * Copyright 2011 Impetus Infotech.
 *  *
 *  * 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 com.impetus.client.persistence;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import javax.persistence.spi.PersistenceUnitTransactionType;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.impetus.kundera.Constants;
import com.impetus.kundera.PersistenceProperties;
import com.impetus.kundera.metadata.model.ApplicationMetadata;
import com.impetus.kundera.metadata.model.EntityMetadata;
import com.impetus.kundera.metadata.model.KunderaMetadata;
import com.impetus.kundera.metadata.model.MetamodelImpl;
import com.impetus.kundera.metadata.model.PersistenceUnitMetadata;
import com.impetus.kundera.metadata.processor.TableProcessor;
import com.impetus.kundera.persistence.EntityManagerFactoryImpl;
import com.impetus.kundera.persistence.EntityManagerImpl;
import com.impetus.kundera.query.QueryImpl;

/**
 * Junit test case for NativeQuery support.
 * 
 * @author vivek.mishra
 * 
 */
public class NativeQueryTest
{

    /** The schema. */
    private final String schema = "test";

    /**
     * Sets the up.
     * 
     * @throws Exception
     *             the exception
     */
    @Before
    public void setUp() throws Exception
    {
        CassandraCli.cassandraSetUp();
        CassandraCli.createKeySpace("KunderaExamples");
    }

    /**
     * Test create native query.
     */
    @Test
    public void testCreateNativeQuery()
    {
        EntityManagerFactoryImpl emf = getEntityManagerFactory();
        EntityManager em = new EntityManagerImpl(emf, PersistenceUnitTransactionType.RESOURCE_LOCAL,
                PersistenceContextType.EXTENDED);
        String nativeSql = "Select * from Cassandra c";
        QueryImpl q = (QueryImpl) em.createNativeQuery(nativeSql, CassandraEntitySample.class);
        Assert.assertEquals(nativeSql, q.getJPAQuery());
        Assert.assertEquals(true, KunderaMetadata.INSTANCE.getApplicationMetadata().isNative(nativeSql));
    }

    /**
     * Test execute native create keyspace query.
     */
    @Test
    public void testExecutNativeQuery()
    {
        EntityManagerFactoryImpl emf = getEntityManagerFactory();
        String nativeSql = "CREATE KEYSPACE " + schema
                + " with strategy_class = 'SimpleStrategy' and strategy_options:replication_factor=1";
        String useNativeSql = "USE test";

        EntityManager em = new EntityManagerImpl(emf, PersistenceUnitTransactionType.RESOURCE_LOCAL,
                PersistenceContextType.EXTENDED);
        Query q = em.createNativeQuery(nativeSql, CassandraEntitySample.class);
        // q.getResultList();
        q.executeUpdate();
        q = em.createNativeQuery(useNativeSql, CassandraEntitySample.class);
        // q.getResultList();
        q.executeUpdate();
        Assert.assertTrue(CassandraCli.keyspaceExist(schema));
        Assert.assertFalse(CassandraCli.keyspaceExist("invalidSchema"));
    }

	/**
	 * Native queries should not leak connections. Pelops pool fails providing a
	 * connection if we don't call {@link IPooledConnection#release()}
	 */
	@Test
	public void testReleasesNativeQueryConnection() {
		EntityManagerFactoryImpl emf = getEntityManagerFactory();
		String nativeSql = "CREATE KEYSPACE "
				+ schema
				+ " with strategy_class = 'SimpleStrategy' and strategy_options:replication_factor=1";
		String useNativeSql = "USE test";

		EntityManager em = new EntityManagerImpl(emf,
				PersistenceUnitTransactionType.RESOURCE_LOCAL,
				PersistenceContextType.EXTENDED);
		Query q = em.createNativeQuery(nativeSql, CassandraEntitySample.class);
		// q.getResultList();
		q.executeUpdate();

		// won't be able to loop if connections are leaked
		for (int i = 0; i < 30; i++) {
			q = em.createNativeQuery(useNativeSql, CassandraEntitySample.class);
			// q.getResultList();
			q.executeUpdate();
		}
	}
	
    /**
     * Test create insert column family query.
     */
    @Test
    public void testCreateInsertColumnFamilyQuery()
    {
        EntityManagerFactoryImpl emf = getEntityManagerFactory();
        EntityManager em = new EntityManagerImpl(emf, PersistenceUnitTransactionType.RESOURCE_LOCAL,
                PersistenceContextType.EXTENDED);

        // create column family
        String colFamilySql = "CREATE COLUMNFAMILY users (key varchar PRIMARY KEY,full_name varchar, birth_date int,state varchar)";
        Query q = em.createNativeQuery(colFamilySql, CassandraEntitySample.class);
        // q.getResultList();
        q.executeUpdate();
        Assert.assertTrue(CassandraCli.columnFamilyExist("users", "KunderaExamples"));

        // Add indexes
        String idxSql = "CREATE INDEX ON users (birth_date)";
        q = em.createNativeQuery(idxSql, CassandraEntitySample.class);
        // q.getResultList();
        q.executeUpdate();
        idxSql = "CREATE INDEX ON users (state)";
        q = em.createNativeQuery(idxSql, CassandraEntitySample.class);
{'fraction_non_alphanumeric': 0.12058155383916401, 'fraction_numerical': 0.0019990913221263062, 'mean_word_length': 3.5782029950083194, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2147688', 'n_tokens_mistral': 3082, 'n_tokens_neox': 2974, 'n_words': 699}