Skip to content

Commit

Permalink
Convert guice/guice7 modules to junit5
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 18, 2025
1 parent 1b7a759 commit dbb5df4
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected OptimizedValueInstantiator createSubclass(Constructor<?> ctor, Method
impl = loader.loadAndResolve(baseName, bytecode);
}
try {
return (OptimizedValueInstantiator) impl.newInstance();
return (OptimizedValueInstantiator) impl.getConstructor().newInstance();
} catch (Exception e) {
throw new IllegalStateException("Failed to generate accessor class '"+baseName+"': "+e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public BeanPropertyMutator buildMutator(MyClassLoader classLoader)
final ClassName baseName = ClassName.constructFor(beanClass, "$Access4JacksonDeserializer");
Class<?> accessorClass = generateMutatorClass(classLoader, baseName);
try {
return (BeanPropertyMutator) accessorClass.newInstance();
return (BeanPropertyMutator) accessorClass.getConstructor().newInstance();
} catch (Exception e) {
throw new IllegalStateException("Failed to generate accessor class '"+accessorClass.getName()+"': "+e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public BeanPropertyAccessor findAccessor(MyClassLoader classLoader)
final ClassName baseName = ClassName.constructFor(beanClass, "$Access4JacksonSerializer");
Class<?> accessorClass = generateAccessorClass(classLoader, baseName);
try {
return (BeanPropertyAccessor) accessorClass.newInstance();
return (BeanPropertyAccessor) accessorClass.getConstructor().newInstance();
} catch (Exception e) {
throw new IllegalStateException("Failed to generate accessor class '"+accessorClass.getName()+"': "+e.getMessage(), e);
}
Expand Down
1 change: 0 additions & 1 deletion afterburner/src/test/java/perftest/TestJvmDeserPerf.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

/**
* Micro-benchmark for comparing performance of bean deserialization
Expand Down
2 changes: 1 addition & 1 deletion android-record/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<artifactId>jackson-databind</artifactId>
</dependency>

<!-- Test dependencies: -->
<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
10 changes: 8 additions & 2 deletions guice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@
<version>${version.guice}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.fasterxml.jackson.module.guice;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.inject.Inject;

import org.junit.jupiter.api.Test;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.name.Names;
import org.junit.Assert;
import org.junit.Test;

import javax.inject.Inject;
import com.fasterxml.jackson.annotation.*;

import com.fasterxml.jackson.databind.ObjectMapper;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;

/**
*/
Expand Down Expand Up @@ -62,30 +65,30 @@ public void configure(Binder binder)

final ReadableBean bean = mapper.readValue("{\"constructor_value\":\"myConstructor\",\"field_value\":\"myField\",\"method_value\":\"myMethod\"}", ReadableBean.class);

Assert.assertEquals("myConstructor", bean.constructorValue);
Assert.assertEquals("myMethod", bean.methodValue);
Assert.assertEquals("myField", bean.fieldValue);
assertEquals("myConstructor", bean.constructorValue);
assertEquals("myMethod", bean.methodValue);
assertEquals("myField", bean.fieldValue);

verifyInjections("From Jackson's ObjectMapper", bean);

}

private void verifyInjections(String message, InjectableBean injected)
{
Assert.assertSame(message, constructorInjected, injected.constructorInjected);
Assert.assertSame(message, constructorInjectedWithJavaAnnotation, injected.constructorInjectedWithJavaAnnotation);
Assert.assertSame(message, constructorInjectedWithGuiceAnnotation, injected.constructorInjectedWithGuiceAnnotation);
Assert.assertSame(message, constructorInjectedWithCustomAnnotation, injected.constructorInjectedWithCustomAnnotation);

Assert.assertSame(message, methodInjected, injected.methodInjected);
Assert.assertSame(message, methodInjectedWithJavaAnnotation, injected.methodInjectedWithJavaAnnotation);
Assert.assertSame(message, methodInjectedWithGuiceAnnotation, injected.methodInjectedWithGuiceAnnotation);
Assert.assertSame(message, methodInjectedWithCustomAnnotation, injected.methodInjectedWithCustomAnnotation);

Assert.assertSame(message, fieldInjected, injected.fieldInjected);
Assert.assertSame(message, fieldInjectedWithJavaAnnotation, injected.fieldInjectedWithJavaAnnotation);
Assert.assertSame(message, fieldInjectedWithGuiceAnnotation, injected.fieldInjectedWithGuiceAnnotation);
Assert.assertSame(message, fieldInjectedWithCustomAnnotation, injected.fieldInjectedWithCustomAnnotation);
assertSame(constructorInjected, injected.constructorInjected);
assertSame(constructorInjectedWithJavaAnnotation, injected.constructorInjectedWithJavaAnnotation);
assertSame(constructorInjectedWithGuiceAnnotation, injected.constructorInjectedWithGuiceAnnotation);
assertSame(constructorInjectedWithCustomAnnotation, injected.constructorInjectedWithCustomAnnotation);

assertSame(methodInjected, injected.methodInjected);
assertSame(methodInjectedWithJavaAnnotation, injected.methodInjectedWithJavaAnnotation);
assertSame(methodInjectedWithGuiceAnnotation, injected.methodInjectedWithGuiceAnnotation);
assertSame(methodInjectedWithCustomAnnotation, injected.methodInjectedWithCustomAnnotation);

assertSame(fieldInjected, injected.fieldInjected);
assertSame(fieldInjectedWithJavaAnnotation, injected.fieldInjectedWithJavaAnnotation);
assertSame(fieldInjectedWithGuiceAnnotation, injected.fieldInjectedWithGuiceAnnotation);
assertSame(fieldInjectedWithCustomAnnotation, injected.fieldInjectedWithCustomAnnotation);
}

/* ===================================================================== */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
import java.io.IOException;
import java.math.BigInteger;

import org.junit.jupiter.api.Test;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.name.Names;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonProperty;

Expand All @@ -14,15 +23,7 @@
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.name.Names;

import org.junit.Assert;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ObjectMapperModuleTest
{
Expand Down Expand Up @@ -67,7 +68,7 @@ public void testModulesRegisteredThroughNormalInstantiation() throws Exception

final ObjectMapper mapper = injector.getInstance(ObjectMapper.class);

Assert.assertEquals(mapper.writeValueAsString(new Integer(10)), "\"A\"");
assertEquals(mapper.writeValueAsString(10), "\"A\"");
}

@Test
Expand All @@ -79,7 +80,7 @@ public void testModulesRegisteredThroughInjection() throws Exception

final ObjectMapper mapper = injector.getInstance(ObjectMapper.class);

Assert.assertEquals(mapper.writeValueAsString(new Integer(10)), "\"A\"");
assertEquals(mapper.writeValueAsString(10), "\"A\"");
}

@Test
Expand All @@ -99,7 +100,7 @@ public void configure(Binder binder)

final ObjectMapper mapper = injector.getInstance(ObjectMapper.class);

Assert.assertEquals(mapper.writeValueAsString(new Integer(10)), "\"A\"");
assertEquals(mapper.writeValueAsString(10), "\"A\"");
}

@Test
Expand All @@ -121,7 +122,7 @@ public void configure(Binder binder)

final ObjectMapper mapper = injector.getInstance(ObjectMapper.class);

Assert.assertEquals(mapper.writeValueAsString(new Integer(10)), "\"A\"");
assertEquals(mapper.writeValueAsString(10), "\"A\"");
}

@Test
Expand All @@ -133,7 +134,7 @@ public void testModulesRegisteredThroughInjectionWithKey() throws Exception

final ObjectMapper mapper = injector.getInstance(ObjectMapper.class);

Assert.assertEquals(mapper.writeValueAsString(new Integer(10)), "\"A\"");
assertEquals(mapper.writeValueAsString(10), "\"A\"");
}

private static class SomeBean
Expand Down Expand Up @@ -188,15 +189,15 @@ private void injectNine(long n)

public boolean verify()
{
Assert.assertEquals(1, one);
Assert.assertEquals(2, two);
Assert.assertEquals(3, three);
Assert.assertEquals(4, four);
Assert.assertEquals(5, five);
Assert.assertEquals(6, six);
Assert.assertEquals(7, seven);
Assert.assertEquals(8, eight);
Assert.assertEquals(9, nine);
assertEquals(1, one);
assertEquals(2, two);
assertEquals(3, three);
assertEquals(4, four);
assertEquals(5, five);
assertEquals(6, six);
assertEquals(7, seven);
assertEquals(8, eight);
assertEquals(9, nine);
return true;
}

Expand Down
10 changes: 8 additions & 2 deletions guice7/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@
<version>${version.guice}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.fasterxml.jackson.module.guice7;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.inject.Inject;

import org.junit.jupiter.api.Test;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.name.Names;
import org.junit.Assert;
import org.junit.Test;

import jakarta.inject.Inject;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;

/**
*/
Expand Down Expand Up @@ -62,30 +64,30 @@ public void configure(Binder binder)

final ReadableBean bean = mapper.readValue("{\"constructor_value\":\"myConstructor\",\"field_value\":\"myField\",\"method_value\":\"myMethod\"}", ReadableBean.class);

Assert.assertEquals("myConstructor", bean.constructorValue);
Assert.assertEquals("myMethod", bean.methodValue);
Assert.assertEquals("myField", bean.fieldValue);
assertEquals("myConstructor", bean.constructorValue);
assertEquals("myMethod", bean.methodValue);
assertEquals("myField", bean.fieldValue);

verifyInjections("From Jackson's ObjectMapper", bean);

}

private void verifyInjections(String message, InjectableBean injected)
{
Assert.assertSame(message, constructorInjected, injected.constructorInjected);
Assert.assertSame(message, constructorInjectedWithJavaAnnotation, injected.constructorInjectedWithJavaAnnotation);
Assert.assertSame(message, constructorInjectedWithGuiceAnnotation, injected.constructorInjectedWithGuiceAnnotation);
Assert.assertSame(message, constructorInjectedWithCustomAnnotation, injected.constructorInjectedWithCustomAnnotation);

Assert.assertSame(message, methodInjected, injected.methodInjected);
Assert.assertSame(message, methodInjectedWithJavaAnnotation, injected.methodInjectedWithJavaAnnotation);
Assert.assertSame(message, methodInjectedWithGuiceAnnotation, injected.methodInjectedWithGuiceAnnotation);
Assert.assertSame(message, methodInjectedWithCustomAnnotation, injected.methodInjectedWithCustomAnnotation);

Assert.assertSame(message, fieldInjected, injected.fieldInjected);
Assert.assertSame(message, fieldInjectedWithJavaAnnotation, injected.fieldInjectedWithJavaAnnotation);
Assert.assertSame(message, fieldInjectedWithGuiceAnnotation, injected.fieldInjectedWithGuiceAnnotation);
Assert.assertSame(message, fieldInjectedWithCustomAnnotation, injected.fieldInjectedWithCustomAnnotation);
assertSame(constructorInjected, injected.constructorInjected);
assertSame(constructorInjectedWithJavaAnnotation, injected.constructorInjectedWithJavaAnnotation);
assertSame(constructorInjectedWithGuiceAnnotation, injected.constructorInjectedWithGuiceAnnotation);
assertSame(constructorInjectedWithCustomAnnotation, injected.constructorInjectedWithCustomAnnotation);

assertSame(methodInjected, injected.methodInjected);
assertSame(methodInjectedWithJavaAnnotation, injected.methodInjectedWithJavaAnnotation);
assertSame(methodInjectedWithGuiceAnnotation, injected.methodInjectedWithGuiceAnnotation);
assertSame(methodInjectedWithCustomAnnotation, injected.methodInjectedWithCustomAnnotation);

assertSame(fieldInjected, injected.fieldInjected);
assertSame(fieldInjectedWithJavaAnnotation, injected.fieldInjectedWithJavaAnnotation);
assertSame(fieldInjectedWithGuiceAnnotation, injected.fieldInjectedWithGuiceAnnotation);
assertSame(fieldInjectedWithCustomAnnotation, injected.fieldInjectedWithCustomAnnotation);
}

/* ===================================================================== */
Expand Down
Loading

0 comments on commit dbb5df4

Please sign in to comment.