examplewithpropertiesmappingtests.java

来自「一个hibernate的自动测试框架源码」· Java 代码 · 共 136 行

JAVA
136
字号
/*
 * Copyright (c) 2005 Chris Richardson
 * 
 * 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 net.chrisrichardson.ormunit.hibernate.tests;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import net.chrisrichardson.ormunit.ClassMappingException;
import net.chrisrichardson.ormunit.NonPersistentPropertyException;
import net.chrisrichardson.ormunit.NonexistentPropertiesException;
import net.chrisrichardson.ormunit.UnmappedPropertiesException;
import net.chrisrichardson.ormunit.hibernate.AccessStrategy;
import net.chrisrichardson.ormunit.hibernate.HibernateORMappingTests;

public class ExampleWithPropertiesMappingTests extends HibernateORMappingTests {

	@Override
	protected void onSetUp() throws Exception {
		super.onSetUp();
		setAccessStrategy(AccessStrategy.PROPERTY);
	}
	
	@Override
	protected String[] getConfigLocations() {
		return new String[] { "classpath:appctx/contextWithProperties.xml" };
	}

	public void testAllClassesMapped() {
		try {
			assertAllClassesMapped();
			fail();
		} catch (UnmappedPropertiesException e) {

		}
	}

	public void testAllClassesMappedExcept() {
		assertAllClassesMapped(except(CustomerWithPropertiesAndComponentAddress.class,
				"address.zip").and("address.id").and(
				ClassWithUnmappedProperty.class, "salary"));
	}			

	public void testCustomerMapping() throws Exception {
		assertClassMapping(CustomerWithProperties.class, "CustomerWithProperties");
		assertAllPropertiesMapped();
	}

	public void testCustomerMappingWrongTable() throws Exception {
		try {
			assertClassMapping(CustomerWithProperties.class, "CustomerX");
			fail();
		} catch (ClassMappingException e) {

		}
	}

	public void testClassWithUnmappedProperty() throws Exception {
		assertClassMapping(ClassWithUnmappedProperty.class,
				"ClassWithUnmappedProperty");
		assertAllPropertiesMappedExcept(Collections.singleton("salary"));
	}

	public void testClassWithUnmappedPropertyAssumingAllMapped() throws Exception {
		assertClassMapping(ClassWithUnmappedProperty.class,
				"ClassWithUnmappedProperty");
		try {
			assertAllPropertiesMapped();
			fail();
		} catch (UnmappedPropertiesException e) {
		}
	}

	public void testNoSuchNonPersistenceProperty() throws Exception {
		assertClassMapping(CustomerWithProperties.class, "CustomerWithProperties");
		try {
			assertAllPropertiesMappedExcept(Collections.singleton("salary"));
			fail();
		} catch (NonexistentPropertiesException e) {
		}
	}

	public void testNoNonPersistencePropertyReallyIs() throws Exception {
		assertClassMapping(CustomerWithProperties.class, "CustomerWithProperties");
		try {
			assertAllPropertiesMappedExcept(Collections.singleton("name"));
			fail();
		} catch (NonPersistentPropertyException e) {
		}
	}

	public void testClassWithUnmappedSubpropertyAssumeAllMapped() {
		assertClassMapping(CustomerWithPropertiesAndComponentAddress.class,
				"CustomerWithPropertiesAndComponentAddress");
		try {
			assertAllPropertiesMapped();
			fail();
		} catch (UnmappedPropertiesException e) {
		}
	}

	public void testClassWithUnmappedSubproperty() {
		assertClassMapping(CustomerWithPropertiesAndComponentAddress.class,
				"CustomerWithPropertiesAndComponentAddress");
		assertAllPropertiesMapped(except("address.zip").and("address.id"));
	}

	public void testAssertUnmappedSubpropThatReallyIsMapped() {
		assertClassMapping(CustomerWithPropertiesAndComponentAddress.class,
				"CustomerWithPropertiesAndComponentAddress");
		Set fields = new HashSet();
		fields.add("address.street1");
		fields.add("address.zip");
		fields.add("address.id");
		try {
			assertAllPropertiesMappedExcept(fields);
			fail();
		} catch (NonPersistentPropertyException e) {
		}
	}
}

⌨️ 快捷键说明

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