nonreflectivebindertest.java

来自「好东西,hibernate-3.2.0,他是一开元的树杖hibernate-3.」· Java 代码 · 共 207 行

JAVA
207
字号
//$Id: NonReflectiveBinderTest.java 10191 2006-08-02 15:05:40Z max.andersen@jboss.com $
package org.hibernate.test.legacy;

import java.util.Iterator;
import java.util.Map;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.hibernate.MappingException;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.Bag;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.MetaAttribute;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;


public class NonReflectiveBinderTest extends TestCase {

	Configuration cfg;
	private Class lastTestClass;

	public static Test suite() {
		return new TestSuite(NonReflectiveBinderTest.class);
	}

	Configuration getCfg() {
		return cfg;
	}

	public String[] getMappings() {
		return new String[] { "legacy/Wicked.hbm.xml"};
	}

	void buildConfiguration(String[] files) throws MappingException {

			try {
				setCfg( new Configuration() );
								
				for (int i=0; i<files.length; i++) {
					if ( !files[i].startsWith("net/") ) files[i] = "org/hibernate/test/" + files[i];
					getCfg().addResource( files[i], TestCase.class.getClassLoader() );
				}
			} catch (MappingException e) {
				throw e;
			}

	}

	protected void setUp() throws Exception {
		if ( getCfg()==null || lastTestClass!=getClass() ) {
			buildConfiguration( getMappings() );
			lastTestClass = getClass();
		}
	}

	/**
	 * @param configuration
	 */
	private void setCfg(Configuration configuration) {
		cfg = configuration;
	}

	public void testMetaInheritance() {
		Configuration cfg = getCfg();
		cfg.buildMappings();
		PersistentClass cm = cfg.getClassMapping("org.hibernate.test.legacy.Wicked");
		Map m = cm.getMetaAttributes();
		assertNotNull(m);
		assertNotNull(cm.getMetaAttribute("global"));
		assertNull(cm.getMetaAttribute("globalnoinherit"));
		
		MetaAttribute metaAttribute = cm.getMetaAttribute("implements");
		assertNotNull(metaAttribute);
		assertEquals("implements", metaAttribute.getName());
		assertTrue(metaAttribute.isMultiValued());
		assertEquals(3, metaAttribute.getValues().size());
		assertEquals("java.lang.Observer",metaAttribute.getValues().get(0));
		assertEquals("java.lang.Observer",metaAttribute.getValues().get(1));
		assertEquals("org.foo.BogusVisitor",metaAttribute.getValues().get(2));
				
		/*Property property = cm.getIdentifierProperty();
		property.getMetaAttribute(null);*/
		
		Iterator propertyIterator = cm.getPropertyIterator();
		while (propertyIterator.hasNext()) {
			Property element = (Property) propertyIterator.next();
			System.out.println(element);
			Map ma = element.getMetaAttributes();
			assertNotNull(ma);
			assertNotNull(element.getMetaAttribute("global"));
			MetaAttribute metaAttribute2 = element.getMetaAttribute("implements");
			assertNotNull(metaAttribute2);
			assertNull(element.getMetaAttribute("globalnoinherit"));
						
		}
		
		Property element = cm.getProperty("component");
		Map ma = element.getMetaAttributes();
		assertNotNull(ma);
		assertNotNull(element.getMetaAttribute("global"));
		assertNotNull(element.getMetaAttribute("componentonly"));
		assertNotNull(element.getMetaAttribute("allcomponent"));
		assertNotNull(element.getMetaAttribute("implements"));
		assertNull(element.getMetaAttribute("globalnoinherit"));							
		
	}

	// HBX-718
	public void testNonMutatedInheritance() {
		Configuration cfg = getCfg();
		cfg.buildMappings();
		
		PersistentClass cm = cfg.getClassMapping("org.hibernate.test.legacy.Wicked");
		MetaAttribute metaAttribute = cm.getMetaAttribute( "globalmutated" );
		
		assertNotNull(metaAttribute);
		assertEquals( metaAttribute.getValues().size(), 2 );		
		assertEquals( "top level", metaAttribute.getValues().get(0) );
		assertEquals( "wicked level", metaAttribute.getValues().get(1) );
		
		Property property = cm.getProperty( "component" );
		MetaAttribute propertyAttribute = property.getMetaAttribute( "globalmutated" );
		
		assertNotNull(propertyAttribute);
		assertEquals( propertyAttribute.getValues().size(), 3 );
		assertEquals( "top level", propertyAttribute.getValues().get(0) );
		assertEquals( "wicked level", propertyAttribute.getValues().get(1) );
		assertEquals( "monetaryamount level", propertyAttribute.getValues().get(2) );
		
		org.hibernate.mapping.Component component = (Component)property.getValue();
		property = component.getProperty( "x" );
		propertyAttribute = property.getMetaAttribute( "globalmutated" );
		
		assertNotNull(propertyAttribute);
		assertEquals( propertyAttribute.getValues().size(), 4 );
		assertEquals( "top level", propertyAttribute.getValues().get(0) );
		assertEquals( "wicked level", propertyAttribute.getValues().get(1) );
		assertEquals( "monetaryamount level", propertyAttribute.getValues().get(2) );
		assertEquals( "monetaryamount x level", propertyAttribute.getValues().get(3) );
		
		property = cm.getProperty( "sortedEmployee" );
		propertyAttribute = property.getMetaAttribute( "globalmutated" );
		
		assertNotNull(propertyAttribute);
		assertEquals( propertyAttribute.getValues().size(), 3 );
		assertEquals( "top level", propertyAttribute.getValues().get(0) );
		assertEquals( "wicked level", propertyAttribute.getValues().get(1) );
		assertEquals( "sortedemployee level", propertyAttribute.getValues().get(2) );
		
		property = cm.getProperty( "anotherSet" );
		propertyAttribute = property.getMetaAttribute( "globalmutated" );
		
		assertNotNull(propertyAttribute);
		assertEquals( propertyAttribute.getValues().size(), 2 );
		assertEquals( "top level", propertyAttribute.getValues().get(0) );
		assertEquals( "wicked level", propertyAttribute.getValues().get(1) );
				
		Bag bag = (Bag) property.getValue();
		component = (Component)bag.getElement(); 
		
		assertEquals(4,component.getMetaAttributes().size());
		
		metaAttribute = component.getMetaAttribute( "globalmutated" );
		assertEquals( metaAttribute.getValues().size(), 3 );
		assertEquals( "top level", metaAttribute.getValues().get(0) );
		assertEquals( "wicked level", metaAttribute.getValues().get(1) );
		assertEquals( "monetaryamount anotherSet composite level", metaAttribute.getValues().get(2) );		
		
		property = component.getProperty( "emp" );
		propertyAttribute = property.getMetaAttribute( "globalmutated" );
		
		assertNotNull(propertyAttribute);
		assertEquals( propertyAttribute.getValues().size(), 4 );
		assertEquals( "top level", propertyAttribute.getValues().get(0) );
		assertEquals( "wicked level", propertyAttribute.getValues().get(1) );
		assertEquals( "monetaryamount anotherSet composite level", propertyAttribute.getValues().get(2) );
		assertEquals( "monetaryamount anotherSet composite property emp level", propertyAttribute.getValues().get(3) );
		
		
		property = component.getProperty( "empinone" );
		propertyAttribute = property.getMetaAttribute( "globalmutated" );
		
		assertNotNull(propertyAttribute);
		assertEquals( propertyAttribute.getValues().size(), 4 );
		assertEquals( "top level", propertyAttribute.getValues().get(0) );
		assertEquals( "wicked level", propertyAttribute.getValues().get(1) );
		assertEquals( "monetaryamount anotherSet composite level", propertyAttribute.getValues().get(2) );
		assertEquals( "monetaryamount anotherSet composite property empinone level", propertyAttribute.getValues().get(3) );
		
		
	}
	
	public void testComparator() {
		Configuration cfg = getCfg();
		cfg.buildMappings();
		PersistentClass cm = cfg.getClassMapping("org.hibernate.test.legacy.Wicked");
		
		Property property = cm.getProperty("sortedEmployee");
		Collection col = (Collection) property.getValue();
		assertEquals(col.getComparatorClassName(),"org.hibernate.test.legacy.NonExistingComparator");
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?