Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

37 jpa unit rdbms extension requires the usage of #40

Merged
merged 23 commits into from
Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
491854f
Artifact names in bom fixed. Old names were referenced
dadrus Feb 24, 2018
b47fef8
made use of property
dadrus Mar 26, 2018
ed079a4
#37 fixed
dadrus Jun 17, 2018
4a2535e
new tests added
dadrus Jun 17, 2018
e23640d
test fixed according to new requirements from #37
dadrus Jun 17, 2018
e2d7f9b
cglib replaced by ByteBuddy in concordion plugin
dadrus Jun 27, 2018
f537e11
.gitignore updated
dadrus Jun 27, 2018
c3a45cb
maven-flatten-plugin introduced
dadrus Jun 27, 2018
be18246
cglib replaced by ByteBuddy in cucumber plugin
dadrus Jun 27, 2018
a5cbccc
byte-buddy version is managed by parent pom
dadrus Jun 27, 2018
806284c
moved usage of flatten-maven-plugin to the release profile
dadrus Jun 27, 2018
e6c2065
removed usage of shade plugin in cucumber extension
dadrus Jun 27, 2018
6625c38
integration test fixed after updates
dadrus Jun 27, 2018
ccbfe76
explicit javassist test dependency added to overcome issues in the fo…
dadrus Jun 27, 2018
19bbf42
updated used powermock and hibernate 5 versions. cglib references rem…
dadrus Jun 27, 2018
fe9bd93
maven shade plugin is not used any more
dadrus Jun 28, 2018
45f1a83
moved configuration of flatten plugin into pluginManagement section
dadrus Jun 28, 2018
fbb6636
further integration tests fixed after ByteBuddy introduction
dadrus Jun 28, 2018
3a44bba
moved neo4j repository to neo4j extension project
dadrus Jun 28, 2018
db2c1ed
ReflectionUtils enhanced
dadrus Jun 28, 2018
a7f00b3
resolved sonar issues and adjustments to the new ReflectionUtil API
dadrus Jun 28, 2018
f8c9326
ReflectionUtils enhanced to cope with inheritance while looking for f…
dadrus Jun 28, 2018
514464f
Merge branch 'master' into 37-jpa-unit_rdbms_extension_requires_the_u…
dadrus Jun 28, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
.project
target
*~
dependency-reduced-pom.xml
.flattened-pom.xml



.idea/
Expand Down
29 changes: 25 additions & 4 deletions bom/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.github.dadrus.jpa-unit</groupId>
<artifactId>jpa-unit</artifactId>
<version>0.5.0-SNAPSHOT</version>
<version>${revision}</version>
</parent>

<artifactId>jpa-unit-bom</artifactId>
Expand All @@ -16,12 +18,12 @@
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jpa-unit-junit4</artifactId>
<artifactId>jpa-unit4</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jpa-unit-junit5</artifactId>
<artifactId>jpa-unit5</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand Down Expand Up @@ -56,4 +58,23 @@
</dependency>
</dependencies>
</dependencyManagement>


<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<configuration>
<updatePomFile>true</updatePomFile>
<flattenMode>ossrh</flattenMode>
<pomElements>
<dependencyManagement>resolve</dependencyManagement>
</pomElements>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
5 changes: 3 additions & 2 deletions cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

<parent>
<groupId>com.github.dadrus.jpa-unit</groupId>
<artifactId>jpa-unit</artifactId>
<version>0.5.0-SNAPSHOT</version>
<artifactId>jpa-unit-parent</artifactId>
<version>${revision}</version>
<relativePath>../parent</relativePath>
</parent>

<artifactId>jpa-unit-cdi</artifactId>
Expand Down
69 changes: 12 additions & 57 deletions concordion/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

<parent>
<groupId>com.github.dadrus.jpa-unit</groupId>
<artifactId>jpa-unit</artifactId>
<version>0.5.0-SNAPSHOT</version>
<artifactId>jpa-unit-parent</artifactId>
<version>${revision}</version>
<relativePath>../parent</relativePath>
</parent>

<artifactId>jpa-unit-concordion</artifactId>
Expand All @@ -18,8 +19,8 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand All @@ -30,8 +31,6 @@
<artifactId>junit</artifactId>
</dependency>



<!-- TEST DEPENDENCIES -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
Expand Down Expand Up @@ -67,6 +66,12 @@
</dependency>

<!-- put here because of different javassist versions used by powermock and concordion -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.22.0-GA</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.concordion</groupId>
<artifactId>concordion</artifactId>
Expand Down Expand Up @@ -137,56 +142,6 @@
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>cglib:cglib</include>
<include>org.ow2.asm:asm</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/maven/**</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer">
<addHeader>false</addHeader>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
</transformers>
<relocations>
<relocation>
<pattern>net.sf.cglib</pattern>
<shadedPattern>eu.drus.jpa.unit.concordion.cglib</shadedPattern>
</relocation>
<relocation>
<pattern>org.objectweb.asm</pattern>
<shadedPattern>eu.drus.jpa.unit.concordion.asm</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CdiEnabledNewDepositorFixture extends AbstractConcordionFixture {
@Override
public Resource locateSpecification(final Object fixtureObject, final String typeSuffix) {
String slashedClassName = fixtureObject.getClass().getName().replaceAll("\\.", "/");
slashedClassName = slashedClassName.substring(0, slashedClassName.indexOf("$$"));
slashedClassName = slashedClassName.substring(0, slashedClassName.indexOf('$'));
String fixturePath = slashedClassName.replace(CdiEnabledNewDepositorFixture.class.getSimpleName(),
NewDepositorFixture.class.getSimpleName());
fixturePath = fixturePath.replaceAll("(Fixture|Test)$", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static eu.drus.jpa.unit.util.ReflectionUtils.getValue;
import static eu.drus.jpa.unit.util.ReflectionUtils.injectValue;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.concordion.api.Fixture;
Expand All @@ -17,14 +16,11 @@
import org.slf4j.LoggerFactory;

import eu.drus.jpa.unit.api.JpaUnitException;
import eu.drus.jpa.unit.concordion.ConcordionInterceptor;
import eu.drus.jpa.unit.concordion.EqualsInterceptor;
import eu.drus.jpa.unit.concordion.EnhancedProxy;
import eu.drus.jpa.unit.concordion.JpaUnitFixture;
import eu.drus.jpa.unit.spi.DecoratorExecutor;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackHelper;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.Factory;
import eu.drus.jpa.unit.util.ReflectionUtils;


public class JpaUnitConcordionRunner extends ConcordionRunner {

Expand All @@ -37,27 +33,22 @@ public JpaUnitConcordionRunner(final Class<?> fixtureClass) throws Initializatio
}

private static Object getDelegate(final Object fixtureObject) {
final Callback[] callbacks = ((Factory) fixtureObject).getCallbacks();
for (final Callback callback : callbacks) {
if (callback instanceof ConcordionInterceptor) {
return ((ConcordionInterceptor) callback).getDelegate();
}
}

throw new JpaUnitException("Internal Error. No ConcordionInterceptor registered. Please submit a bug report!");
try {
return ReflectionUtils.getValue(fixtureObject, "bean");
} catch (Exception e) {
throw new JpaUnitException("Internal Error. No ConcordionInterceptor registered. Please submit a bug report!");
}
}

@Override
protected Object createTest() throws Exception {
Object enhancedFixture;
final Field firstTestSuperField = getClass().getSuperclass().getDeclaredField("firstTest");
final Field setupFixtureField = getClass().getSuperclass().getDeclaredField("setupFixture");

final Fixture setupFixture = (Fixture) getValue(setupFixtureField, this);
final boolean isFirstTest = (boolean) getValue(firstTestSuperField, this);
final Fixture setupFixture = (Fixture) getValue(this, "setupFixture");
final boolean isFirstTest = (boolean) getValue(this, "firstTest");

if (isFirstTest) {
injectValue(firstTestSuperField, this, false);
injectValue(this, "firstTest", false);
// we've already created a test object above, so reuse it to make sure we don't
// initialize the fixture object multiple times
enhancedFixture = setupFixture.getFixtureObject();
Expand All @@ -74,7 +65,7 @@ protected Object createTest() throws Exception {
// the setup of concordion scoped objects is done in this call
final Object fixtureObject = super.createTest();
injectFields(fixtureObject);
enhancedFixture = createProxy(fixtureObject);
enhancedFixture = EnhancedProxy.create(fixtureObject, executor);
}

return enhancedFixture;
Expand All @@ -100,29 +91,14 @@ private void injectFields(final Object fixtureObject) {

@Override
protected Fixture createFixture(final Object fixtureObject) {
if (fixtureObject instanceof Factory) {

if (fixtureObject instanceof EnhancedProxy) {
return new JpaUnitFixture(executor, fixtureObject);
} else {
return new JpaUnitFixture(executor, createProxy(fixtureObject));
return new JpaUnitFixture(executor, EnhancedProxy.create(fixtureObject, executor));
}
}

private Object createProxy(final Object bean) {
final CallbackHelper helper = new CallbackHelper(bean.getClass(), new Class[0]) {

@Override
protected Object getCallback(final Method method) {
if (method.getName().equals("equals")) {
return new EqualsInterceptor(bean);
} else {
return new ConcordionInterceptor(executor, bean);
}
}
};

return Enhancer.create(bean.getClass(), new Class[0], helper, helper.getCallbacks());
}

@Override
protected SpecificationLocator getSpecificationLocator() {
return new ClassNameAndTypeBasedSpecificationLocator() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,72 +1,38 @@
package eu.drus.jpa.unit.concordion;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import eu.drus.jpa.unit.api.CleanupPhase;
import eu.drus.jpa.unit.spi.DecoratorExecutor;
import eu.drus.jpa.unit.spi.FeatureResolver;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class ConcordionInterceptor implements MethodInterceptor {

private final DecoratorExecutor executor;
private Object delegate;

public ConcordionInterceptor(final DecoratorExecutor executor, final Object delegate) {
this.executor = executor;
this.delegate = delegate;
}

private static boolean isObjectMethod(final Method method) {
switch (method.getName()) {
case "hashCode":
case "toString":
case "clone":
return true;
default:
return false;
}
}

private static boolean hasConcordionAnnotations(final Method method) {
final Annotation[] annotations = method.getDeclaredAnnotations();
for (final Annotation annotation : annotations) {
final Class<? extends Annotation> annotationType = annotation.annotationType();
if (annotationType.getName().startsWith("org.concordion.api")) {
return true;
}
}
return false;
}

@Override
public Object intercept(final Object proxy, final Method method, final Object[] args, final MethodProxy methodProxy) throws Throwable {
if (isObjectMethod(method) || hasConcordionAnnotations(method)) {
return methodProxy.invoke(delegate, args);
}

final FeatureResolver resolver = FeatureResolver.newFeatureResolver(delegate.getClass()).withTestMethod(method)
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.FieldValue;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;

public final class ConcordionInterceptor {

private ConcordionInterceptor() {}

@RuntimeType
public static Object intercept(@FieldValue("executor") DecoratorExecutor executor, @FieldValue("bean") Object bean, @Origin Method method, @AllArguments Object[] args) throws Exception {

final FeatureResolver resolver = FeatureResolver.newFeatureResolver(bean.getClass()).withTestMethod(method)
.withDefaultCleanupPhase(CleanupPhase.NONE).build();

Object result = null;
final TestInvocationImpl invocation = new TestInvocationImpl(delegate, method, resolver);
final TestInvocationImpl invocation = new TestInvocationImpl(bean, method, resolver);
executor.processBefore(invocation);
try {
result = methodProxy.invoke(delegate, args);
result = method.invoke(bean, args);
} catch (final Exception e) {
invocation.setTestException(e);
Exception cause = (Exception)e.getCause();
invocation.setTestException(cause);
executor.processAfter(invocation);
throw e;
throw cause;
}
executor.processAfter(invocation);

return result;
}

public Object getDelegate() {
return delegate;
}

}
Loading