Skip to content

[JSTEP-10] Migrate test to junit 5 #181

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;

public abstract class BaseTest extends junit.framework.TestCase
import static org.junit.jupiter.api.Assertions.fail;

public abstract class BaseTest
{
protected BaseTest() { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import com.fasterxml.jackson.datatype.hibernate4.data.Customer;
import com.fasterxml.jackson.datatype.hibernate4.data.Payment;
import org.hibernate.Hibernate;
import org.junit.Test;

import org.junit.jupiter.api.Test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.Map;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

public class ForceLazyLoadingTest extends BaseTest
{
// [Issue#15]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate4.data.Customer;
import com.fasterxml.jackson.datatype.hibernate4.data.Employee;

import static org.junit.jupiter.api.Assertions.*;

public class HibernateTest extends BaseTest
{
protected EntityManagerFactory emf;

@Override
@BeforeEach
public void setUp() {
emf = Persistence.createEntityManagerFactory("persistenceUnit");
}
@Override

@AfterEach
public void tearDown() {
if (emf!=null) {
emf.close();
Expand All @@ -37,7 +38,7 @@ public void tearDown() {
@Test
public void testGetEntityManager() {
EntityManager em = emf.createEntityManager();
Assert.assertNotNull(em);
assertNotNull(em);
}

@Test
Expand All @@ -58,7 +59,7 @@ public void testGetCustomerJson() throws Exception {
@Test
public void testAllCustomersJson() throws Exception {
EntityManager em = emf.createEntityManager();
Assert.assertNotNull(em);
assertNotNull(em);

Query query = em.createQuery("select c from Customer c");
// false -> no forcing of lazy loading
Expand All @@ -85,8 +86,8 @@ public void testCyclesJson() throws Exception {
EntityManager em = emf.createEntityManager();

Employee salesEmployee = em.find(Employee.class, 1370);
Assert.assertNotNull(salesEmployee);
Assert.assertTrue(salesEmployee.getCustomers().size()>0);
assertNotNull(salesEmployee);
assertTrue(salesEmployee.getCustomers().size()>0);

// false -> no forcing of lazy loading
ObjectMapper mapper = mapperWithModule(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import java.util.*;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;

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

public class InclusionTest extends BaseTest
{
static class Mock
Expand All @@ -14,6 +18,7 @@ static class Mock
}

// [hibernate#65]
@Test
public void testInclusion() throws Exception
{
final ObjectMapper mapper = mapperWithModule(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import javax.persistence.Persistence;

import org.hibernate.Hibernate;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate4.data.Contrato;

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

public class InfiniteRecursion70Test extends BaseTest
{
// [datatype-hibernate#70]
@Test
public void testInfinite() throws Exception
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import com.fasterxml.jackson.datatype.hibernate4.data.Payment;

import org.hibernate.Hibernate;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class LazyLoadingTest extends BaseTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@

import org.hibernate.Hibernate;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate4.data.Customer;

import static org.junit.jupiter.api.Assertions.*;

// [Issue#125]
public class MissingEntitiesAsNullTest extends BaseTest {
@Test
public void testMissingProductWhenMissing() throws Exception {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");

Expand All @@ -39,6 +44,7 @@ public void testMissingProductWhenMissing() throws Exception {
}
}

@Test
public void testProductWithValidForeignKey() throws Exception {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");

Expand Down Expand Up @@ -66,6 +72,7 @@ public void testProductWithValidForeignKey() throws Exception {

// caused by javax.persistence.EntityNotFoundException: Unable to find
// com.fasterxml.jackson.datatype.hibernate4.data.Product with id X10_1678
@Test
public void testExceptionWithInvalidForeignKey() throws Exception {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");

Expand All @@ -90,6 +97,7 @@ public void testExceptionWithInvalidForeignKey() throws Exception {
}
}

@Test
public void testWriteAsNullWithInvalidForeignKey() throws Exception {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

import javax.persistence.OneToMany;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

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

public class OneToManyTest extends BaseTest
{
static final String EXPECTED_JSON = "{\"m\":{\"A\":\"A\"}}";
Expand All @@ -18,15 +22,17 @@ static final class X {
static final class Y {
public final Map<String, String> m = new LinkedHashMap<String, String>();
}


@Test
public void testMap() throws Exception {
Y object = new Y();
object.m.put("A", "A");

assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
}


@Test
public void testMapWithOneToMany() throws Exception {
X object = new X();
object.m.put("A", "A");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import java.util.*;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class Polymorphic81Test extends BaseTest
{
static class TestLayout {
Expand Down Expand Up @@ -59,6 +63,7 @@ public static Expression simpleFieldExpression(String field) {
}
}

@Test
public void testPolymorphic81() throws Exception
{
final ObjectMapper mapper = mapperWithModule(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
import com.fasterxml.jackson.datatype.hibernate4.data.Customer;
import com.fasterxml.jackson.datatype.hibernate4.data.Payment;
import org.hibernate.Hibernate;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import org.junit.jupiter.api.*;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.Map;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

public class ReplacePersistentCollectionTest {

private EntityManagerFactory emf;

private EntityManager em;

@Before
@BeforeEach
public void setUp() throws Exception {
emf = Persistence.createEntityManagerFactory("persistenceUnit");
em = emf.createEntityManager();
}

@After
@AfterEach
public void tearDown() throws Exception {
em.close();
emf.close();
Expand All @@ -43,9 +43,9 @@ public void testNoReplacePersistentCollection() throws Exception {
).enableDefaultTyping();

Customer customer = em.find(Customer.class, 103);
Assert.assertFalse(Hibernate.isInitialized(customer.getPayments()));
assertFalse(Hibernate.isInitialized(customer.getPayments()));
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer);
Assert.assertTrue(json.contains("org.hibernate.collection"));
assertTrue(json.contains("org.hibernate.collection"));
// should force loading...
Set<Payment> payments = customer.getPayments();
/*
Expand All @@ -54,17 +54,17 @@ public void testNoReplacePersistentCollection() throws Exception {
System.out.println("--- /JSON ---");
*/

Assert.assertTrue(Hibernate.isInitialized(payments));
assertTrue(Hibernate.isInitialized(payments));
// TODO: verify
Assert.assertNotNull(json);
assertNotNull(json);

boolean exceptionThrown = false;
try {
Map<?, ?> stuff = mapper.readValue(json, Map.class);
} catch (JsonMappingException e) {
exceptionThrown = true;
}
Assert.assertTrue(exceptionThrown);
assertTrue(exceptionThrown);
}

// [Issue#93], backwards compatible case
Expand All @@ -77,9 +77,9 @@ public void testReplacePersistentCollection() throws Exception {
).enableDefaultTyping();

Customer customer = em.find(Customer.class, 103);
Assert.assertFalse(Hibernate.isInitialized(customer.getPayments()));
assertFalse(Hibernate.isInitialized(customer.getPayments()));
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer);
Assert.assertFalse(json.contains("org.hibernate.collection"));
assertFalse(json.contains("org.hibernate.collection"));
// should force loading...
Set<Payment> payments = customer.getPayments();
/*
Expand All @@ -88,9 +88,9 @@ public void testReplacePersistentCollection() throws Exception {
System.out.println("--- /JSON ---");
*/

Assert.assertTrue(Hibernate.isInitialized(payments));
assertTrue(Hibernate.isInitialized(payments));
// TODO: verify
Assert.assertNotNull(json);
assertNotNull(json);

/*
* Currently this cannot be verified due to Issue#94 default typing fails on 2.7.0 - 2.8.2-SNAPSHOT,
Expand All @@ -104,12 +104,12 @@ public void testReplacePersistentCollection() throws Exception {
issue94failed = true;
}

Assert.assertTrue("If this fails, means #94 is fixed. Replace to the below commented lines", issue94failed);
assertTrue(issue94failed, "If this fails, means #94 is fixed. Replace to the below commented lines");

// Map<?, ?> stuff = mapper.readValue(json, Map.class);
//
// Assert.assertTrue(stuff.containsKey("payments"));
// Assert.assertTrue(stuff.containsKey("orders"));
// Assert.assertNull(stuff.get("orderes"));
// assertTrue(stuff.containsKey("payments"));
// assertTrue(stuff.containsKey("orders"));
// assertNull(stuff.get("orderes"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import java.util.*;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

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

public class TestMaps extends BaseTest
{
@Test
public void testSimpleMap() throws Exception
{
ObjectMapper mapper = mapperWithModule(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.fasterxml.jackson.datatype.hibernate4;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.Versioned;

import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
import static org.junit.jupiter.api.Assertions.assertFalse;

public class TestVersions extends BaseTest
{
@Test
public void testMapperVersions()
{
assertVersion(new Hibernate4Module());
Expand All @@ -15,7 +18,7 @@ public void testMapperVersions()
private void assertVersion(Versioned vers)
{
Version v = vers.version();
assertFalse("Should find version information (got "+v+")", v.isUnknownVersion());
assertFalse(v.isUnknownVersion(), "Should find version information (got "+v+")");
// Version exp = PackageVersion.VERSION;
// assertEquals(exp.toFullString(), v.toFullString());
// assertEquals(exp, v);
Expand Down
Loading