Skip to content

Commit

Permalink
HHH-8558 - Bytecode enhancer: testsuite reorganization with added laz…
Browse files Browse the repository at this point in the history
…y loading tests
  • Loading branch information
barreiro authored and sebersole committed Jul 6, 2015
1 parent c6fa2b1 commit 44a02e5
Show file tree
Hide file tree
Showing 36 changed files with 1,653 additions and 1,567 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
*/ */
package org.hibernate.bytecode.enhance.internal; package org.hibernate.bytecode.enhance.internal;


import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.Embedded;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import javassist.CannotCompileException; import javassist.CannotCompileException;
import javassist.CtClass; import javassist.CtClass;
import javassist.CtField; import javassist.CtField;
Expand All @@ -29,15 +38,6 @@
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;


import javax.persistence.Embedded;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.List;

/** /**
* enhancer for persistent attributes of any type of entity * enhancer for persistent attributes of any type of entity
* *
Expand Down Expand Up @@ -225,7 +225,7 @@ private void handleBiDirectionalAssociation(CtClass managedCtClass, CtField pers
} }
final String mappedBy = getMappedBy( persistentField, targetEntity ); final String mappedBy = getMappedBy( persistentField, targetEntity );
if ( mappedBy.isEmpty() ) { if ( mappedBy.isEmpty() ) {
log.debugf( log.warnf(
"Could not find bi-directional association for field [%s#%s]", "Could not find bi-directional association for field [%s#%s]",
managedCtClass.getName(), managedCtClass.getName(),
persistentField.getName() persistentField.getName()
Expand Down Expand Up @@ -258,17 +258,18 @@ private void handleBiDirectionalAssociation(CtClass managedCtClass, CtField pers
} }
if ( persistentField.hasAnnotation( OneToMany.class ) ) { if ( persistentField.hasAnnotation( OneToMany.class ) ) {
// only remove elements not in the new collection or else we would loose those elements // only remove elements not in the new collection or else we would loose those elements
// don't use iterator to avoid ConcurrentModException
fieldWriter.insertBefore( fieldWriter.insertBefore(
String.format( String.format(
"if ($0.%s != null) for (java.util.Iterator itr = $0.%<s.iterator(); itr.hasNext(); ) { %s target = (%<s) itr.next(); if ($1 == null || !$1.contains(target)) target.%s(null); }%n", "if ($0.%s != null) { Object[] array = $0.%<s.toArray(); for (int i = 0; i < array.length; i++) { %s target = (%<s) array[i]; if ($1 == null || !$1.contains(target)) target.%s(null); } }%n",
persistentField.getName(), persistentField.getName(),
targetEntity.getName(), targetEntity.getName(),
mappedBySetterName mappedBySetterName
) )
); );
fieldWriter.insertAfter( fieldWriter.insertAfter(
String.format( String.format(
"if ($1 != null) for (java.util.Iterator itr = $1.iterator(); itr.hasNext(); ) { %s target = (%<s) itr.next(); if (target.%s() != $0) target.%s((%s)$0); }%n", "if ($1 != null) { Object[] array = $1.toArray(); for (int i = 0; i < array.length; i++) { %s target = (%<s) array[i]; if (target.%s() != $0) target.%s((%s)$0); } }%n",
targetEntity.getName(), targetEntity.getName(),
mappedByGetterName, mappedByGetterName,
mappedBySetterName, mappedBySetterName,
Expand All @@ -287,23 +288,23 @@ private void handleBiDirectionalAssociation(CtClass managedCtClass, CtField pers
// check .contains($0) to avoid double inserts (but preventing duplicates) // check .contains($0) to avoid double inserts (but preventing duplicates)
fieldWriter.insertAfter( fieldWriter.insertAfter(
String.format( String.format(
"if ($1 != null && $1.%s() != null && !$1.%<s().contains($0) ) $1.%<s().add($0);%n", "if ($1 != null) { java.util.Collection c = $1.%s(); if (c != null && !c.contains($0)) c.add($0); }%n",
mappedByGetterName mappedByGetterName
) )
); );
} }
if ( persistentField.hasAnnotation( ManyToMany.class ) ) { if ( persistentField.hasAnnotation( ManyToMany.class ) ) {
fieldWriter.insertBefore( fieldWriter.insertBefore(
String.format( String.format(
"if ($0.%s != null) for (java.util.Iterator itr = $0.%<s.iterator(); itr.hasNext(); ) { %s target = (%<s) itr.next(); if ($1 == null || !$1.contains(target)) target.%s().remove($0); }%n", "if ($0.%s != null) { Object[] array = $0.%<s.toArray(); for (int i = 0; i < array.length; i++) { %s target = (%<s) array[i]; if ($1 == null || !$1.contains(target)) target.%s().remove($0); } }%n",
persistentField.getName(), persistentField.getName(),
targetEntity.getName(), targetEntity.getName(),
mappedByGetterName mappedByGetterName
) )
); );
fieldWriter.insertAfter( fieldWriter.insertAfter(
String.format( String.format(
"if ($1 != null) for (java.util.Iterator itr = $1.iterator(); itr.hasNext(); ) { %s target = (%<s) itr.next(); if (target.%s() != $0 && target.%<s() != null) target.%<s().add($0); }%n", "if ($1 != null) { Object[] array = $1.toArray(); for (int i = 0; i < array.length; i++) { %s target = (%<s) array[i]; java.util.Collection c = target.%s(); if ( c != $0 && c != null) c.add($0); } }%n",
targetEntity.getName(), targetEntity.getName(),
mappedByGetterName mappedByGetterName
) )
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.bytecode.enhancement;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;

import org.hibernate.testing.ServiceRegistryBuilder;

/**
* @author Luis Barreiro
*/
public abstract class AbstractEnhancerTestTask implements EnhancerTestTask {

private ServiceRegistry serviceRegistry;
private SessionFactory factory;

public final void prepare(Configuration user) {
Configuration cfg = new Configuration();
cfg.setProperties( user.getProperties() );
cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );

Class<?>[] resources = getAnnotatedClasses();
for ( Class<?> resource : resources ) {
cfg.addAnnotatedClass( resource );
}

serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( cfg.getProperties() );
factory = cfg.buildSessionFactory( serviceRegistry );
}

public final void complete() {
try {
cleanup();
}
finally {
factory.close();
factory = null;
if ( serviceRegistry != null ) {
ServiceRegistryBuilder.destroy( serviceRegistry );
serviceRegistry = null;
}
}
}

protected SessionFactory getFactory() {
return factory;
}

protected abstract void cleanup();

}

This file was deleted.

Loading

0 comments on commit 44a02e5

Please sign in to comment.