-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
3,579 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
coverage-report/src/test/java/org/jooby/hbm/HbmEventListenerFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.jooby.hbm; | ||
|
||
import java.net.URISyntaxException; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.inject.Inject; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.TypedQuery; | ||
|
||
import org.hibernate.event.spi.EventType; | ||
import org.hibernate.event.spi.PostLoadEvent; | ||
import org.hibernate.event.spi.PostLoadEventListener; | ||
import org.jooby.Results; | ||
import org.jooby.hbm.data.Member; | ||
import org.jooby.test.ServerFeature; | ||
import org.junit.Test; | ||
|
||
import com.typesafe.config.ConfigFactory; | ||
import com.typesafe.config.ConfigValueFactory; | ||
|
||
public class HbmEventListenerFeature extends ServerFeature { | ||
|
||
public static class Service { | ||
public String alias() { | ||
return "post-load"; | ||
} | ||
} | ||
|
||
@SuppressWarnings("serial") | ||
public static class MemberPostLoad implements PostLoadEventListener { | ||
|
||
private Service service; | ||
|
||
@Inject | ||
public MemberPostLoad(final Service service) { | ||
this.service = service; | ||
} | ||
|
||
@Override | ||
public void onPostLoad(final PostLoadEvent event) { | ||
Member member = (Member) event.getEntity(); | ||
member.setAlias(service.alias()); | ||
} | ||
|
||
} | ||
|
||
{ | ||
use(ConfigFactory.empty() | ||
.withValue("db", ConfigValueFactory.fromAnyRef("mem"))); | ||
|
||
use(new Hbm() | ||
.onEvent(EventType.POST_LOAD, MemberPostLoad.class) | ||
.classes(Member.class)); | ||
|
||
use("*", Hbm.openSessionInView()); | ||
|
||
get("/members", req -> { | ||
EntityManager em = req.require(EntityManager.class); | ||
TypedQuery<Member> query = em.createQuery("from Member", Member.class); | ||
return query.getResultList().stream().map(m -> m.getAlias()).collect(Collectors.toList()); | ||
}); | ||
|
||
post("/members", (req, rsp, chain) -> { | ||
Member member = req.params().to(Member.class); | ||
EntityManager em = req.require(EntityManager.class); | ||
em.persist(member); | ||
if (req.param("err").toOptional(Boolean.class).orElse(false)) { | ||
throw new IllegalArgumentException("Rollback on err"); | ||
} | ||
// we do this way just to make sure the correct delegate got executed | ||
rsp.send(Results.with(member)); | ||
chain.next(req, rsp); | ||
}); | ||
} | ||
|
||
@Test | ||
public void hbm() throws URISyntaxException, Exception { | ||
request() | ||
.post("/members") | ||
.form() | ||
.add("id", 1) | ||
.add("name", "pedro") | ||
.expect("pedro(1)"); | ||
|
||
request() | ||
.get("/members") | ||
.expect("[post-load]"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
coverage-report/src/test/java/org/jooby/hbm/HbmJpaListenerFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.jooby.hbm; | ||
|
||
import java.net.URISyntaxException; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.TypedQuery; | ||
|
||
import org.jooby.Results; | ||
import org.jooby.hbm.data.Member; | ||
import org.jooby.test.ServerFeature; | ||
import org.junit.Test; | ||
|
||
import com.typesafe.config.ConfigFactory; | ||
import com.typesafe.config.ConfigValueFactory; | ||
|
||
public class HbmJpaListenerFeature extends ServerFeature { | ||
|
||
{ | ||
use(ConfigFactory.empty() | ||
.withValue("db", ConfigValueFactory.fromAnyRef("mem"))); | ||
|
||
use(new Hbm() | ||
.classes(Member.class)); | ||
|
||
use("*", Hbm.openSessionInView()); | ||
|
||
get("/members", req -> { | ||
EntityManager em = req.require(EntityManager.class); | ||
TypedQuery<Member> query = em.createQuery("from Member", Member.class); | ||
return query.getResultList().stream().map(m -> m.getAlias()).collect(Collectors.toList()); | ||
}); | ||
|
||
post("/members", (req, rsp, chain) -> { | ||
Member member = req.params().to(Member.class); | ||
EntityManager em = req.require(EntityManager.class); | ||
em.persist(member); | ||
if (req.param("err").toOptional(Boolean.class).orElse(false)) { | ||
throw new IllegalArgumentException("Rollback on err"); | ||
} | ||
// we do this way just to make sure the correct delegate got executed | ||
rsp.send(Results.with(member)); | ||
chain.next(req, rsp); | ||
}); | ||
} | ||
|
||
@Test | ||
public void hbm() throws URISyntaxException, Exception { | ||
request() | ||
.post("/members") | ||
.form() | ||
.add("id", 1) | ||
.add("name", "pedro") | ||
.expect("pedro(1)"); | ||
|
||
request() | ||
.get("/members") | ||
.expect("[post-load]"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
coverage-report/src/test/java/org/jooby/hbm/data/MemberListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.jooby.hbm.data; | ||
|
||
import javax.inject.Inject; | ||
import javax.persistence.PostLoad; | ||
|
||
public class MemberListener { | ||
public static class Service { | ||
public String alias() { | ||
return "post-load"; | ||
} | ||
} | ||
|
||
private Service service; | ||
|
||
@Inject | ||
public MemberListener(final Service service) { | ||
this.service = service; | ||
} | ||
|
||
@PostLoad | ||
public void onPostLoad(final Member member) { | ||
member.setAlias(service.alias()); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
coverage-report/src/test/java/org/jooby/issues/Issue439.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.jooby.issues; | ||
|
||
import java.net.URISyntaxException; | ||
|
||
import org.jooby.hbm.Hbm; | ||
import org.jooby.hbm.UnitOfWork; | ||
import org.jooby.hbm.data.Member; | ||
import org.jooby.test.ServerFeature; | ||
import org.junit.Test; | ||
|
||
import com.typesafe.config.ConfigFactory; | ||
import com.typesafe.config.ConfigValueFactory; | ||
|
||
public class Issue439 extends ServerFeature { | ||
|
||
{ | ||
use(ConfigFactory.empty() | ||
.withValue("db", ConfigValueFactory.fromAnyRef("mem"))); | ||
|
||
use(new Hbm().classes(Member.class)); | ||
|
||
use("*", Hbm.openSessionInView()); | ||
|
||
get("/members", req -> { | ||
return req.require(UnitOfWork.class).apply(em -> { | ||
return em.createQuery("from Member").getResultList(); | ||
}); | ||
|
||
}); | ||
|
||
post("/members", (req, rsp, chain) -> { | ||
req.require(UnitOfWork.class).accept(em -> { | ||
Member member = req.params().to(Member.class); | ||
em.persist(member); | ||
rsp.send(member); | ||
chain.next(req, rsp); | ||
}); | ||
}); | ||
} | ||
|
||
@Test | ||
public void hbm() throws URISyntaxException, Exception { | ||
request() | ||
.get("/members") | ||
.expect("[]"); | ||
|
||
request() | ||
.post("/members") | ||
.form() | ||
.add("id", 1) | ||
.add("name", "pedro") | ||
.expect("pedro(1)"); | ||
|
||
request() | ||
.get("/members") | ||
.expect("[pedro(1)]"); | ||
|
||
request() | ||
.post("/members") | ||
.form() | ||
.add("id", 2) | ||
.add("name", "pablo") | ||
.expect("pablo(2)"); | ||
|
||
request() | ||
.get("/members") | ||
.expect("[pedro(1), pablo(2)]"); | ||
} | ||
} |
Oops, something went wrong.