alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (BaseAnnotationBindingTestCase.java)

This example Hibernate source code file (BaseAnnotationBindingTestCase.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

class, entitybinding, frameworkmethod, frameworkmethod, keepsetupfailurestatement, keepsetupfailurestatement, metadataimpl, metadatasources, methodrule, override, statement, statement, throwable, throwable

The Hibernate BaseAnnotationBindingTestCase.java source code

/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.hibernate.metamodel.source.annotations.entity;

import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;

/**
 * @author Hardy Ferentschik
 */
public abstract class BaseAnnotationBindingTestCase extends BaseUnitTestCase {
	protected MetadataSources sources;
	protected MetadataImpl meta;

	@Rule
	public MethodRule buildMetaData = new MethodRule() {
		@Override
		public Statement apply(final Statement statement, FrameworkMethod frameworkMethod, Object o) {
			return new KeepSetupFailureStatement( statement, frameworkMethod );
		}
	};

	@After
	public void tearDown() {
		sources = null;
		meta = null;
	}

	public EntityBinding getEntityBinding(Class<?> clazz) {
		return meta.getEntityBinding( clazz.getName() );
	}

	public EntityBinding getRootEntityBinding(Class<?> clazz) {
		return meta.getRootEntityBinding( clazz.getName() );
	}

	class KeepSetupFailureStatement extends Statement {
		private final Statement origStatement;
		private final FrameworkMethod origFrameworkMethod;
		private Throwable setupError;
		private boolean expectedException;

		KeepSetupFailureStatement(Statement statement, FrameworkMethod frameworkMethod) {
			this.origStatement = statement;
			this.origFrameworkMethod = frameworkMethod;
		}

		@Override
		public void evaluate() throws Throwable {
			try {
				createBindings();
				origStatement.evaluate();
				if ( setupError != null ) {
					throw setupError;
				}
			}
			catch ( Throwable t ) {
				if ( setupError == null ) {
					throw t;
				}
				else {
					if ( !expectedException ) {
						throw setupError;
					}
				}
			}
		}

		private void createBindings() {
			try {
				sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() );
				Resources resourcesAnnotation = origFrameworkMethod.getAnnotation( Resources.class );
				if ( resourcesAnnotation != null ) {
					sources.getMetadataBuilder().with( resourcesAnnotation.cacheMode() );

					for ( Class<?> annotatedClass : resourcesAnnotation.annotatedClasses() ) {
						sources.addAnnotatedClass( annotatedClass );
					}
					if ( !resourcesAnnotation.ormXmlPath().isEmpty() ) {
						sources.addResource( resourcesAnnotation.ormXmlPath() );
					}
				}
				meta = (MetadataImpl) sources.buildMetadata();
			}
			catch ( final Throwable t ) {
				setupError = t;
				Test testAnnotation = origFrameworkMethod.getAnnotation( Test.class );
				Class<?> expected = testAnnotation.expected();
				if ( t.getClass().equals( expected ) ) {
					expectedException = true;
				}
			}
		}
	}
}


Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate BaseAnnotationBindingTestCase.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.