<commit_before>/**
 * Copyright 2005-2013 The Kuali Foundation
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 *
 * 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.kuali.rice.krad.data.jpa;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.kuali.rice.core.api.data.DataType;
import org.kuali.rice.krad.data.jpa.eclipselink.EclipseLinkJpaMetadataProviderImpl;
import org.kuali.rice.krad.data.jpa.testbo.CollectionDataObject;
import org.kuali.rice.krad.data.jpa.testbo.TestDataObject;
import org.kuali.rice.krad.data.jpa.testbo.TestDataObjectTwoPkFields;
import org.kuali.rice.krad.data.jpa.testbo.TestNonPersistableObject;
import org.kuali.rice.krad.data.metadata.DataObjectAttribute;
import org.kuali.rice.krad.data.metadata.DataObjectAttributeRelationship;
import org.kuali.rice.krad.data.metadata.DataObjectCollection;
import org.kuali.rice.krad.data.metadata.DataObjectMetadata;
import org.kuali.rice.krad.data.metadata.DataObjectRelationship;

public class JpaMetadataProviderTest {
	static EclipseLinkJpaMetadataProviderImpl metadataProvider;


	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		metadataProvider = new EclipseLinkJpaMetadataProviderImpl();
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("krad-data-unit-test");
		metadataProvider.setEntityManager(entityManagerFactory.createEntityManager());
	}

	@Before
	public void setUp() throws Exception {
		// metadataProvider = new JpaMetadataProviderImpl();
	}

	@Test
	public void testGetMetadataForType() throws Exception {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		Assert.assertNotNull("Returned metadata should not be null", metadata);
		metadata = metadataProvider.getMetadataForType(Class.forName("org.kuali.rice.krad.data.jpa.testbo.TestDataObject"));
		Assert.assertNotNull("Returned metadata should not be null", metadata);
	}

	@Test
	public void testGetAllMetadata() throws Exception {
		Map<Class<?>, DataObjectMetadata> metadata = metadataProvider.provideMetadata();
		Assert.assertNotNull("Returned metadata should not be null", metadata);
		Assert.assertFalse("metadata map should not have been empty", metadata.isEmpty());
		Assert.assertTrue("Should have had an entry for TestDataObject",
				metadata.containsKey(TestDataObject.class));
		Assert.assertTrue("Should have had an entry for TestDataObject (when class name specified)", metadata
.containsKey(Class.forName("org.kuali.rice.krad.data.jpa.testbo.TestDataObject")));
	}

	@Test
	public void testGetMetadataForClass_ClassData() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		System.err.println(metadata);
		Assert.assertEquals("Incorrect Data Object Type", TestDataObject.class,
				metadata.getType());
		Assert.assertEquals("Incorrect Type Label", "Test Data Object", metadata.getLabel());
		Assert.assertEquals("Table name not set as the backing object name", "KRTST_TEST_TABLE_T",
				metadata.getBackingObjectName());
	}

	@Test
	public void testGetMetadataForClass_VersionAttribute() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		Assert.assertEquals("Incorrect Version Setting on TestDataObject", Boolean.FALSE,
				metadata.isSupportsOptimisticLocking());

		metadata = metadataProvider.getMetadataForType(TestDataObjectTwoPkFields.class);
		Assert.assertEquals("Incorrect Version Setting on TestDataObjectTwoPkFields", Boolean.TRUE,
				metadata.isSupportsOptimisticLocking());
	}

	@Test
	public void testGetMetadataForClass_Attributes() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		List<DataObjectAttribute> attributes = metadata.getAttributes();
		System.err.println(attributes);
		Assert.assertNotNull("Returned attributes should not have been null", attributes);
		Assert.assertFalse("Returned attributes should not have been empty", attributes.isEmpty());
		DataObjectAttribute firstAttribute = metadata.getAttribute("primaryKeyProperty");
		Assert.assertEquals("property name incorrect", "primaryKeyProperty", firstAttribute.getName());
		Assert.assertEquals("Property label incorrect", "Primary Key Property", firstAttribute.getLabel());
		Assert.assertEquals("Column name not set as the backing object name", "PK_PROP",
				firstAttribute.getBackingObjectName());
	}

	@Test
	public void testGetMetadataForClass_Attribute_Types() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		DataObjectAttribute attribute = metadata.getAttribute("stringProperty");

		Assert.assertEquals("property DataType incorrect", DataType.STRING, attribute.getDataType());

		attribute = metadata.getAttribute("dateProperty");

		Assert.assertEquals("property DataType incorrect", DataType.DATE, attribute.getDataType());

		attribute = metadata.getAttribute("currencyProperty");

		Assert.assertEquals("property DataType incorrect", DataType.CURRENCY, attribute.getDataType());
	}

	@Test
	public void testGetMetadataForClass_Attribute_Type_Unknown() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		DataObjectAttribute attribute = metadata.getAttribute("nonStandardDataType");

		Assert.assertEquals("nonStandardDataType property DataType incorrect", DataType.STRING, attribute.getDataType());
	}

	@Test
	public void testGetMetadataForClass_Attribute_Non_OJB_Property() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		DataObjectAttribute attribute = metadata.getAttribute("nonPersistedProperty");

		Assert.assertNull("nonPersistedProperty should not exist", attribute);
	}

	@Test
	public void testGetMetadataForCollection() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		List<DataObjectCollection> collections = metadata.getCollections();

        // TestDataObject has 4 collections
		Assert.assertNotNull("Collections object should not be null", collections);
		Assert.assertEquals("Collections size incorrect", 4, collections.size());

		DataObjectCollection collection = collections.get(0);
		Assert.assertEquals("property name incorrect", "collectionProperty", collection.getName());
		Assert.assertEquals("collection backing object incorrect", "KRTST_TEST_COLL_T",
				collection.getBackingObjectName());
		Assert.assertEquals("collection label incorrect", "Collection Property", collection.getLabel());
		Assert.assertEquals("collection item label incorrect", "Collection Data Object", collection.getElementLabel());
		
		collection = metadata.getCollection("collectionPropertyTwo");
		Assert.assertNotNull("Collection object for collectionPropertyTwo should not be null", collection);
		Assert.assertEquals("property name incorrect", "collectionPropertyTwo", collection.getName());

		Assert.assertNotNull("attribute relationships must not be null", collection.getAttributeRelationships());
		Assert.assertEquals("attribute relationships size incorrect", 1, collection.getAttributeRelationships().size());
		DataObjectAttributeRelationship relationship = collection.getAttributeRelationships().get(0);
		Assert.assertEquals("parent attribute name mismatch", "primaryKeyProperty",
				relationship.getParentAttributeName());
		Assert.assertEquals("child attribute name mismatch", "primaryKeyPropertyUsingDifferentName",
				relationship.getChildAttributeName());

		Assert.assertNotNull("collection default sort list must not be null", collection.getDefaultOrdering());
		Assert.assertEquals("collection default sort size incorrect", 1, collection.getDefaultOrdering()
				.size());
	}<commit_msg>KULRICE-10326 : Test was wrong to begin with - should have been failing before<commit_after>/**
 * Copyright 2005-2013 The Kuali Foundation
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 *
 * 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.kuali.rice.krad.data.jpa;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.kuali.rice.core.api.data.DataType;
import org.kuali.rice.krad.data.jpa.eclipselink.EclipseLinkJpaMetadataProviderImpl;
import org.kuali.rice.krad.data.jpa.testbo.CollectionDataObject;
import org.kuali.rice.krad.data.jpa.testbo.TestDataObject;
import org.kuali.rice.krad.data.jpa.testbo.TestDataObjectTwoPkFields;
import org.kuali.rice.krad.data.jpa.testbo.TestNonPersistableObject;
import org.kuali.rice.krad.data.metadata.DataObjectAttribute;
import org.kuali.rice.krad.data.metadata.DataObjectAttributeRelationship;
import org.kuali.rice.krad.data.metadata.DataObjectCollection;
import org.kuali.rice.krad.data.metadata.DataObjectMetadata;
import org.kuali.rice.krad.data.metadata.DataObjectRelationship;

public class JpaMetadataProviderTest {
	static EclipseLinkJpaMetadataProviderImpl metadataProvider;


	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		metadataProvider = new EclipseLinkJpaMetadataProviderImpl();
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("krad-data-unit-test");
		metadataProvider.setEntityManager(entityManagerFactory.createEntityManager());
	}

	@Before
	public void setUp() throws Exception {
		// metadataProvider = new JpaMetadataProviderImpl();
	}

	@Test
	public void testGetMetadataForType() throws Exception {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		Assert.assertNotNull("Returned metadata should not be null", metadata);
		metadata = metadataProvider.getMetadataForType(Class.forName("org.kuali.rice.krad.data.jpa.testbo.TestDataObject"));
		Assert.assertNotNull("Returned metadata should not be null", metadata);
	}

	@Test
	public void testGetAllMetadata() throws Exception {
		Map<Class<?>, DataObjectMetadata> metadata = metadataProvider.provideMetadata();
		Assert.assertNotNull("Returned metadata should not be null", metadata);
		Assert.assertFalse("metadata map should not have been empty", metadata.isEmpty());
		Assert.assertTrue("Should have had an entry for TestDataObject",
				metadata.containsKey(TestDataObject.class));
		Assert.assertTrue("Should have had an entry for TestDataObject (when class name specified)", metadata
.containsKey(Class.forName("org.kuali.rice.krad.data.jpa.testbo.TestDataObject")));
	}

	@Test
	public void testGetMetadataForClass_ClassData() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		System.err.println(metadata);
		Assert.assertEquals("Incorrect Data Object Type", TestDataObject.class,
				metadata.getType());
		Assert.assertEquals("Incorrect Type Label", "Test Data Object", metadata.getLabel());
		Assert.assertEquals("Table name not set as the backing object name", "KRTST_TEST_TABLE_T",
				metadata.getBackingObjectName());
	}

	@Test
	public void testGetMetadataForClass_VersionAttribute() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		Assert.assertEquals("Incorrect Version Setting on TestDataObject", Boolean.FALSE,
				metadata.isSupportsOptimisticLocking());

		metadata = metadataProvider.getMetadataForType(TestDataObjectTwoPkFields.class);
		Assert.assertEquals("Incorrect Version Setting on TestDataObjectTwoPkFields", Boolean.TRUE,
				metadata.isSupportsOptimisticLocking());
	}

	@Test
	public void testGetMetadataForClass_Attributes() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		List<DataObjectAttribute> attributes = metadata.getAttributes();
		System.err.println(attributes);
		Assert.assertNotNull("Returned attributes should not have been null", attributes);
		Assert.assertFalse("Returned attributes should not have been empty", attributes.isEmpty());
		DataObjectAttribute firstAttribute = metadata.getAttribute("primaryKeyProperty");
		Assert.assertEquals("property name incorrect", "primaryKeyProperty", firstAttribute.getName());
		Assert.assertEquals("Property label incorrect", "Primary Key Property", firstAttribute.getLabel());
		Assert.assertEquals("Column name not set as the backing object name", "PK_PROP",
				firstAttribute.getBackingObjectName());
	}

	@Test
	public void testGetMetadataForClass_Attribute_Types() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		DataObjectAttribute attribute = metadata.getAttribute("stringProperty");

		Assert.assertEquals("property DataType incorrect", DataType.STRING, attribute.getDataType());

		attribute = metadata.getAttribute("dateProperty");

		Assert.assertEquals("property DataType incorrect", DataType.DATE, attribute.getDataType());

		attribute = metadata.getAttribute("currencyProperty");

		Assert.assertEquals("property DataType incorrect", DataType.CURRENCY, attribute.getDataType());
	}

	@Test
	public void testGetMetadataForClass_Attribute_Type_Unknown() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		DataObjectAttribute attribute = metadata.getAttribute("nonStandardDataType");

		Assert.assertEquals("nonStandardDataType property DataType incorrect", DataType.STRING, attribute.getDataType());
	}

	@Test
	public void testGetMetadataForClass_Attribute_Non_OJB_Property() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		DataObjectAttribute attribute = metadata.getAttribute("nonPersistedProperty");

		Assert.assertNull("nonPersistedProperty should not exist", attribute);
	}

	@Test
	public void testGetMetadataForCollection() {
		DataObjectMetadata metadata = metadataProvider
				.getMetadataForType(TestDataObject.class);
		List<DataObjectCollection> collections = metadata.getCollections();

        // TestDataObject has 4 collections
		Assert.assertNotNull("Collections object should not be null", collections);
		Assert.assertEquals("Collections size incorrect", 4, collections.size());

		DataObjectCollection collection = collections.get(0);
		Assert.assertEquals("property name incorrect", "collectionProperty", collection.getName());
		Assert.assertEquals("collection backing object incorrect", "KRTST_TEST_COLL_T",
				collection.getBackingObjectName());
		Assert.assertEquals("collection label incorrect", "Collection Property", collection.getLabel());
		Assert.assertEquals("collection item label incorrect", "Collection Data Object", collection.getElementLabel());
		
		collection = metadata.getCollection("collectionPropertyTwo");
		Assert.assertNotNull("Collection object for collectionPropertyTwo should not be null", collection);
		Assert.assertEquals("property name incorrect", "collectionPropertyTwo", collection.getName());

		Assert.assertNotNull("attribute relationships must not be null", collection.getAttributeRelationships());
		Assert.assertEquals("attribute relationships size incorrect", 1, collection.getAttributeRelationships().size());
		DataObjectAttributeRelationship relationship = collection.getAttributeRelationships().get(0);
		Assert.assertEquals("parent attribute name mismatch", "stringProperty",
				relationship.getParentAttributeName());
		Assert.assertEquals("child attribute name mismatch", "primaryKeyPropertyUsingDifferentName",
				relationship.getChildAttributeName());

		Assert.assertNotNull("collection default sort list must not be null", collection.getDefaultOrdering());
		Assert.assertEquals("collection default sort size incorrect", 1, collection.getDefaultOrdering()
				.size());
	}
{'fraction_non_alphanumeric': 0.09662894721251258, 'fraction_numerical': 0.002310563422003673, 'mean_word_length': 6.583108715184187, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3321580', 'n_tokens_mistral': 4576, 'n_tokens_neox': 4279, 'n_words': 1043}