MappedClassChecker.java ( File view )

  • By 2016-05-17
  • View(s):0
  • Download(s):0
  • Point(s): 1
			package net.chrisrichardson.ormunit.hibernate;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import junit.framework.Assert;
import net.chrisrichardson.ormunit.ClassMappingException;
import net.chrisrichardson.ormunit.NonPersistentPropertyException;
import net.chrisrichardson.ormunit.NonexistentPropertiesException;
import net.chrisrichardson.ormunit.UnmappedPropertiesException;

import org.hibernate.MappingException;
import org.hibernate.PropertyNotFoundException;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.List;
import org.hibernate.mapping.ManyToOne;
import org.hibernate.mapping.OneToMany;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Value;

public class MappedClassChecker {


	private final AccessStrategy accessStrategy;

	private final Class type;

	private final PersistentClass classMapping;
	private Class mappedSuper;
	
	public MappedClassChecker(Class type, Configuration configuration,
			AccessStrategy accessStrategy) {

		this.type = type;
		this.accessStrategy = accessStrategy;
		this.classMapping = configuration.getClassMapping(type.getName());
		this.mappedSuper = getMappedSuper(type, configuration);
	
}

	private Class getMappedSuper(Class type, Configuration configuration) {

		Class superType = type;
		while ((superType = superType.getSuperclass()) != Object.class) {

			if (configuration.getClassMapping(superType.getName()) != null)
				break;
		
}
		return superType;
	
}

	public void assertClassMapping(Class type, String tableName) {

		Assert.assertNotNull("No class mapping found for " + type.getName(),
				classMapping);
		if (!tableName.equalsIgnoreCase(classMapping.getTable().getName()))
			throw new ClassMappingException(type, tableName, classMapping
					.getTable().getName());
		assertHasConstructor();
	
}

	private void assertHasConstructor() {

		try {

			Constructor[] constructors = type.getDeclaredConstructors();
			for (Constructor constructor : constructors) {

				if (constructor.getParameterTypes().length == 0) {

					// have a default constructor
					if ((constructor.getModifiers() & Modifier.PRIVATE) != 0)
						Assert.fail("Constructor should not be private: "
								+ type.getName());
					return;
				
}
			
}
		
} catch (SecurityException e) {

			throw new RuntimeException(e);
		
}
		Assert.fail("No default constructor required by Hibernate: "
				+ type.getName());

	
}

	public static Set getPropertiesWithNonSubProps(Set propertyNames) {

		return PathUtil.getPropertiesWithNonSubProps(propertyNames);
	
}

	private void walkComponentProperties(Iterator propertyIterator,
			Set unmappedPropertyNames) {

		for (Iterator it = propertyIterator; it.hasNext();) {

			Property property = (Property) it.next();
			String name = property.getName();
			if (property.getPropertyAccessorName() != null)
				Assert.assertEquals("Property uses wrong access type: " + name, accessStrategy.getName(), property.getPropertyAccessorName());
			if (property.getValue() instanceof Component) {

				Component cv = (Component) property.getValue();
				assertPropertiesOfComponentPropertyMapped(name, cv,
						unmappedPropertyNames);
			
} else if (isListOfComponents(property)) {

				List value = (List) property.getValue();
				Component cv = (Component) value.getElement();
				assertPropertiesOfComponentPropertyMapped(name, cv,
						unmappedPropertyNames);
			
}
		
}
	
}

	private void assertPropertiesOfComponentPropertyMapped(String name,
			Component cv, Set unmappedPropertyNames) {


		Set unmappedPropertiesForThisComponent = PathUtil.getPathsStartingWith(
				name, unmappedPropertyNames);

		Set<String> unmappedDirectProperties = PathUtil
				.getRoots(unmappedPropertiesForThisComponent);
		assertAllDirectPropertiesOfComponentMapped(cv, unmappedDirectProperties);
		assertPropertiesExists(cv.getComponentClass(), unmappedDirectProperties);
		walkComponentProperties(cv.getPropertyIterator(),
				unmappedPropertiesForThisComponent);
	
}

	private void assertAllDirectPropertiesOfComponentMapped(Component cv,
			Set fieldsToIgnore) {

		Set unmappedProperties = new HashSet();
		Set mappedProperties = new HashSet();
		for (String propertyName : accessStrategy.getPersistableProperties(cv
				.getComponentClass(), Object.class)) {

			try {

				cv.getProperty(propertyName);
				mappedProperties.add(propertyName);
			
} catch (MappingException e) {

				unmappedProperties.add(propertyName);
			
}
		
}
		unmappedProperties.removeAll(fieldsToIgnore);
		if (!unmappedProperties.isEmpty())
			throw new UnmappedPropertiesException(cv.getComponentClass(),
					unmappedProperties);
		Set x = intersection(mappedProperties, fieldsToIgnore);
		if (!x.isEmpty()) {

			throw new NonPersistentPropertyException(cv.getComponentClass(), x);

		
}
	
}

	private Set intersection(Set mappedProperties, Set fieldsToIgnore) {

		Set result = new HashSet(mappedProperties);
		result.retainAll(fieldsToIgnore);
		return result;
	
}


	public ComponentPropertyMapping getComponentPropertyMapping(String propertyName)
			throws MappingException {

		Property property = classMapping.getProperty(propertyName);
		Component value = (Component) property.getValue();
		return new ComponentPropertyMapping(property, value, accessStrategy);
	
}

	public void assertAllPropertiesMapped()
			throws MappingException {

		assertAllPropertiesMappedExcept(Collections.EMPTY_SET);
	
}

	public static Set<String> getRoots(Set mungedPropertyNames) {

		return PathUtil.getRoots(mungedPropertyNames);
	
}

	private void assertPropertiesExists(Class type, Set propertyNames) {

		Set<String> persistableProperties = accessStrategy.getPersistableProperties(
				type, type == this.type ? mappedSuper : Object.class);
		if (!persistableProperties.containsAll(propertyNames)) {

			propertyNames.removeAll(persistableProperties);
			throw new NonexistentPropertiesException(type, propertyNames);
		
}
	
}

	private void assertNoOtherUnmappedProperties(Class type,
			Set nonPersistentProperties)
			throws MappingException {

		Property idProperty = classMapping.getIdentifierProperty();
		Set unmappedProperties = new HashSet();
		Set fieldsToIgnore = new HashSet(nonPersistentProperties);
		if (idProperty != null) {

			fieldsToIgnore.add(idProperty.getName());
		
}
		for (Iterator it = accessStrategy.getPersistableProperties(type,
				type == this.type ? mappedSuper : Object.class).iterator(); it.hasNext();) {

			String propertyName = (String) it.next();
			if (!fieldsToIgnore.contains(propertyName))
				try {

					classMapping.getProperty(propertyName);
				
} catch (MappingException e) {

					unmappedProperties.add(propertyName);
				
}
		
}
		if (!unmappedProperties.isEmpty())
			throw new UnmappedPropertiesException(type, unmappedProperties);
	
}

	private boolean isListOfComponents(Property property) {

		return property.getValue() instanceof List
				&& ((List) property.getValue()).getElement() instanceof Component;
	
}

	public void assertAllPropertiesMappedExcept(Set propertyNames) throws MappingException {

		assertHasConstructor();
		Iterator propertyIterator = classMapping.getPropertyIterator();
		walkComponentProperties(propertyIterator, propertyNames);

		assertNoMappingForProperties(type, PathUtil
				.getPropertiesWithNonSubProps(propertyNames));
		assertNoOtherUnmappedProperties(type, PathUtil.getRoots(propertyNames));
		assertPropertiesExists(type, PathUtil.getRoots(propertyNames));
	
}

	private void assertNoMappingForProperties(Class type, Set propertyNames) {

		for (Iterator iter = propertyNames.iterator(); iter.hasNext();) {

			String propertyName = (String) iter.next();
			try {

				Property r = classMapping.getProperty(propertyName);
				if (r.getName().equals(propertyName))
					throw new NonPersistentPropertyException(type, Collections
							.singleton(propertyName));
			
} catch (MappingException e) {


			
}
		
}
	
}

	public void assertCompositeListProperty(String propertyName)
			throws MappingException {

		Property property = classMapping.getProperty(propertyName);
		Value v = property.getValue();
		org.hibernate.mapping.List value = (org.hibernate.mapping.List) v;
	
}

	public ListPropertyMapping assertListProperty(String propertyName)
			throws MappingException {

		Property property = classMapping.getProperty(propertyName);
		Value v = property.getValue();
		Assert.assertTrue("Expected " + propertyName + "to be a <list> mapping",
				v instanceof org.hibernate.mapping.List);
		org.hibernate.mapping.List value = (org.hibernate.mapping.List) v;
		return new ListPropertyMapping(property, value);
	
}

	public CompositeListPropertyMapping getCompositeListPropertyMapping(
			String propertyName) throws MappingException {

		Property property = classMapping.getProperty(propertyName);
		Value v = property.getValue();
		org.hibernate.mapping.List value = (org.hibernate.mapping.List) v;
		return new CompositeListPropertyMapping(property, value, accessStrategy);
	
}

	public ListPropertyMapping getListPropertyMapping(String propertyName)
			throws MappingException {

		Property property = classMapping.getProperty(propertyName);
		Value v = property.getValue();
		org.hibernate.mapping.List value = (org.hibernate.mapping.List) v;
		return new ListPropertyMapping(property, value);
	
}

	public void assertComponentProperty(String propertyName)
			throws MappingException {

		Property property = classMapping.getProperty(propertyName);
		if (!(property.getValue() instanceof Component))
			Assert.fail("field " + propertyName
					+ " is not mapped as a component: " + property.getValue());
	
}

	public void assertSetProperty(String propertyName) throws MappingException {

		Property property = classMapping.getProperty(propertyName);
		org.hibernate.mapping.Set value = (org.hibernate.mapping.Set
...
...
(Not finished, please download and read the complete file)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
01.96 kB
pom.xml5.04 kB2007-04-27 13:43
dep.xml726.00 B2007-04-29 11:25
MANIFEST.MF39.00 B2007-03-25 15:17
ClassMappingException.java1.43 kB2007-03-25 15:17
AbstractORMappingTests.java1.54 kB2007-04-27 17:54
AccessStrategy.java789.00 B2007-04-27 17:54
ClassPropertyUtil.java2.01 kB2007-04-26 16:05
ComponentCollectionFieldMapping.java1.96 kB2007-04-27 13:43
ComponentPropertyMapping.java3.57 kB2007-04-27 13:43
CompositeListPropertyMapping.java1.67 kB2007-04-27 13:43
CompositeSetPropertyMapping.java1.05 kB2007-04-27 13:43
DatabaseInitializer.java134.00 B2007-03-25 15:17
DatabaseResetStrategy.java829.00 B2007-03-25 15:17
FieldMapping.java726.00 B2007-03-25 15:17
HibernateAssertUtil.java1.40 kB2007-04-27 13:43
HibernateMappingChecker.java1.34 kB2007-04-27 17:54
HibernateMappingTests.java5.27 kB2007-04-27 17:54
HibernateORMappingTests.java4.93 kB2007-04-27 17:54
HibernateORMTestCase.java1.80 kB2007-04-27 13:43
HibernatePersistenceTests.java4.28 kB2007-04-27 13:43
HibernatePersistenceTestsStrategy.java1.21 kB2007-04-27 13:43
HibernateSchemaChecker.java2.03 kB2007-04-27 13:43
HibernateSchemaTests.java1.36 kB2007-04-27 13:43
ListPropertyMapping.java2.37 kB2007-04-27 13:43
LocalSessionFactoryBeanUtil.java1.53 kB2007-03-25 15:17
MappedClassChecker.java13.11 kB2007-04-27 17:54
NoopDatabaseResetStrategy.java856.00 B2007-03-25 15:17
PathUtil.java1.49 kB2007-04-27 17:54
ResetDatabaseByRecreatingSchemaStrategy.java3.68 kB2007-03-25 15:17
RollbackTransactionHibernatePersistenceTestsStrategy.java2.42 kB2007-04-27 13:43
SimpleHibernatePersistenceTestsStrategy.java3.04 kB2007-04-27 13:43
ToOnePropertyMapping.java502.00 B2007-04-27 13:43
NonexistentPropertiesException.java1.25 kB2007-04-27 13:43
NonPersistentPropertyException.java1.20 kB2007-04-27 13:43
ORMUnitException.java927.00 B2007-03-25 15:17
ORMUnitMappingException.java962.00 B2007-03-25 15:17
PropertyNameSet.java1.33 kB2007-04-27 17:54
UnmappedPropertiesException.java1.31 kB2007-04-27 13:43
TxnCallback.java743.00 B2007-03-25 15:17
TxnCallbackWithResult.java755.00 B2007-03-25 15:17
Address.java1.47 kB2007-03-25 15:17
AddressWithProperties.java1.60 kB2007-04-26 13:33
BaseClass.java122.00 B2007-04-26 16:05
ClassWithUnmappedField.java785.00 B2007-03-25 15:17
ClassWithUnmappedProperty.java1,017.00 B2007-04-26 13:33
Customer.java1.21 kB2007-03-25 15:17
CustomerPersistenceTests.java1.80 kB2007-04-27 13:43
CustomerWithComponentAddress.java804.00 B2007-03-25 15:17
CustomerWithProperties.java574.00 B2007-04-26 13:33
CustomerWithPropertiesAndComponentAddress.java1.19 kB2007-04-26 13:33
ExampleMappingTests.java3.79 kB2007-04-27 13:43
ExampleWithPropertiesMappingTests.java4.25 kB2007-04-27 13:43
InheritanceTests.java1.94 kB2007-04-27 13:43
PointlessSchemaTests.java398.00 B2007-04-27 13:43
Subclass.java140.00 B2007-04-26 16:05
context.xml2.00 kB2007-03-25 15:17
contextWithProperties.xml2.02 kB2007-04-26 13:33
log4j.properties1.04 kB2007-03-25 15:17
example.hbm.xml1.41 kB2007-04-26 13:33
exampleWithProperties.hbm.xml1.52 kB2007-04-26 13:33
inheritanceOutsideOfHibernate.hbm.xml422.00 B2007-04-26 16:05
inheritanceUsingSubclass.hbm.xml559.00 B2007-04-26 16:05
01.96 kB
ormunit-hibernate-1.0-SNAPSHOT.jar49.43 kB2007-04-29 11:26
allclasses-frame.html7.33 kB2007-04-29 11:26
allclasses-noframe.html6.61 kB2007-04-29 11:26
constant-values.html5.24 kB2007-04-29 11:26
deprecated-list.html5.18 kB2007-04-29 11:26
help-doc.html9.77 kB2007-04-29 11:26
index-all.html105.10 kB2007-04-29 11:26
index.html1.42 kB2007-04-29 11:26
ClassMappingException.html6.04 kB2007-04-29 11:26
NonexistentPropertiesException.html6.13 kB2007-04-29 11:26
NonPersistentPropertyException.html6.13 kB2007-04-29 11:26
ORMUnitException.html9.69 kB2007-04-29 11:26
ORMUnitMappingException.html9.34 kB2007-04-29 11:26
PropertyNameSet.html19.14 kB2007-04-29 11:26
UnmappedPropertiesException.html6.10 kB2007-04-29 11:26
ClassMappingException.html13.09 kB2007-04-29 11:26
AbstractORMappingTests.html24.59 kB2007-04-29 11:26
AccessStrategy.html15.67 kB2007-04-29 11:26
AbstractORMappingTests.html8.78 kB2007-04-29 11:26
AccessStrategy.html14.86 kB2007-04-29 11:26
ClassPropertyUtil.html6.15 kB2007-04-29 11:26
ComponentCollectionFieldMapping.html8.77 kB2007-04-29 11:26
ComponentPropertyMapping.html9.76 kB2007-04-29 11:26
CompositeListPropertyMapping.html9.86 kB2007-04-29 11:26
CompositeSetPropertyMapping.html10.49 kB2007-04-29 11:26
DatabaseInitializer.html6.19 kB2007-04-29 11:26
DatabaseResetStrategy.html10.00 kB2007-04-29 11:26
FieldMapping.html8.46 kB2007-04-29 11:26
HibernateAssertUtil.html6.17 kB2007-04-29 11:26
HibernateMappingChecker.html8.38 kB2007-04-29 11:26
HibernateMappingTests.html6.19 kB2007-04-29 11:26
HibernateORMappingTests.html6.21 kB2007-04-29 11:26
HibernateORMTestCase.html10.33 kB2007-04-29 11:26
HibernatePersistenceTests.html6.23 kB2007-04-29 11:26
HibernatePersistenceTestsStrategy.html10.32 kB2007-04-29 11:26
HibernateSchemaChecker.html6.20 kB2007-04-29 11:26
HibernateSchemaTests.html6.18 kB2007-04-29 11:26
ListPropertyMapping.html14.53 kB2007-04-29 11:26
LocalSessionFactoryBeanUtil.html6.25 kB2007-04-29 11:26
MappedClassChecker.html9.40 kB2007-04-29 11:26
NoopDatabaseResetStrategy.html6.23 kB2007-04-29 11:26
PathUtil.html6.06 kB2007-04-29 11:26
ResetDatabaseByRecreatingSchemaStrategy.html6.37 kB2007-04-29 11:26
RollbackTransactionHibernatePersistenceTestsStrategy.html8.90 kB2007-04-29 11:26
SimpleHibernatePersistenceTestsStrategy.html6.37 kB2007-04-29 11:26
ToOnePropertyMapping.html10.53 kB2007-04-29 11:26
ClassPropertyUtil.html12.22 kB2007-04-29 11:26
ComponentCollectionFieldMapping.html15.50 kB2007-04-29 11:26
ComponentPropertyMapping.html16.16 kB2007-04-29 11:26
CompositeListPropertyMapping.html15.71 kB2007-04-29 11:26
CompositeSetPropertyMapping.html13.57 kB2007-04-29 11:26
DatabaseInitializer.html8.49 kB2007-04-29 11:26
DatabaseResetStrategy.html9.00 kB2007-04-29 11:26
FieldMapping.html12.08 kB2007-04-29 11:26
HibernateAssertUtil.html11.43 kB2007-04-29 11:26
HibernateMappingChecker.html13.26 kB2007-04-29 11:26
HibernateMappingTests.html45.05 kB2007-04-29 11:26
HibernateORMappingTests.html44.47 kB2007-04-29 11:26
HibernateORMTestCase.html19.56 kB2007-04-29 11:26
HibernatePersistenceTests.html31.88 kB2007-04-29 11:26
HibernatePersistenceTestsStrategy.html11.42 kB2007-04-29 11:26
HibernateSchemaChecker.html11.84 kB2007-04-29 11:26
HibernateSchemaTests.html19.65 kB2007-04-29 11:26
ListPropertyMapping.html17.05 kB2007-04-29 11:26
LocalSessionFactoryBeanUtil.html10.82 kB2007-04-29 11:26
MappedClassChecker.html31.44 kB2007-04-29 11:26
NoopDatabaseResetStrategy.html11.22 kB2007-04-29 11:26
package-frame.html5.50 kB2007-04-29 11:26
package-summary.html15.04 kB2007-04-29 11:26
package-tree.html14.34 kB2007-04-29 11:26
package-use.html11.65 kB2007-04-29 11:26
PathUtil.html12.76 kB2007-04-29 11:26
ResetDatabaseByRecreatingSchemaStrategy.html14.10 kB2007-04-29 11:26
RollbackTransactionHibernatePersistenceTestsStrategy.html16.54 kB2007-04-29 11:26
SimpleHibernatePersistenceTestsStrategy.html17.97 kB2007-04-29 11:26
ToOnePropertyMapping.html13.06 kB2007-04-29 11:26
NonexistentPropertiesException.html13.41 kB2007-04-29 11:26
NonPersistentPropertyException.html12.76 kB2007-04-29 11:26
ORMUnitException.html11.04 kB2007-04-29 11:26
ORMUnitMappingException.html12.00 kB2007-04-29 11:26
package-frame.html2.07 kB2007-04-29 11:26
package-summary.html8.28 kB2007-04-29 11:26
package-tree.html7.42 kB2007-04-29 11:26
package-use.html8.50 kB2007-04-29 11:26
PropertyNameSet.html17.07 kB2007-04-29 11:26
UnmappedPropertiesException.html13.12 kB2007-04-29 11:26
TxnCallback.html9.88 kB2007-04-29 11:26
TxnCallbackWithResult.html6.03 kB2007-04-29 11:26
package-frame.html1.18 kB2007-04-29 11:26
package-summary.html6.44 kB2007-04-29 11:26
package-tree.html6.13 kB2007-04-29 11:26
package-use.html6.80 kB2007-04-29 11:26
TxnCallback.html7.96 kB2007-04-29 11:26
TxnCallbackWithResult.html8.09 kB2007-04-29 11:26
options1.21 kB2007-04-29 11:26
overview-frame.html1.45 kB2007-04-29 11:26
overview-summary.html6.01 kB2007-04-29 11:26
overview-tree.html15.30 kB2007-04-29 11:26
package-list94.00 B2007-04-29 11:26
packages92.00 B2007-04-29 11:26
inherit.gif57.00 B2007-04-29 11:26
serialized-form.html11.13 kB2007-04-29 11:26
stylesheet.css1.20 kB2007-04-29 11:26
...
Sponsored links

MappedClassChecker.java (316.73 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D