http://code.google.com/p/tapestry5-cayenne/source/detail?r=238
Added:
/trunk/tapestry5-cayenne-server/src/test/java/com/googlecode/tapestry5cayenne/integration/TapestryCayenneIntegrationTest.java
Deleted:
/trunk/tapestry5-cayenne-server/src/test/java/com/googlecode/tapestry5cayenne/integration/TapestryCayenneIntegrationTests.java
Modified:
/trunk/tapestry5-cayenne-core/src/main/java/com/googlecode/tapestry5cayenne/annotations/CommitAfter.java
/trunk/tapestry5-cayenne-core/src/main/java/com/googlecode/tapestry5cayenne/services/CayenneCommitAfterWorker.java
/trunk/tapestry5-cayenne-server/src/test/java/com/googlecode/tapestry5cayenne/integration/app0/pages/CommitAfterTestPage.java
/trunk/tapestry5-cayenne-server/src/test/java/com/googlecode/tapestry5cayenne/integration/app0/services/AppModule.java
/trunk/tapestry5-cayenne-server/src/test/resources/com/googlecode/tapestry5cayenne/integration/app0/pages/CommitAfterTestPage.tml
=======================================
--- /dev/null
+++
/trunk/tapestry5-cayenne-server/src/test/java/com/googlecode/tapestry5cayenne/integration/TapestryCayenneIntegrationTest.java
Thu Mar 29 05:33:48 2012
@@ -0,0 +1,47 @@
+package com.googlecode.tapestry5cayenne.integration;
+
+import org.apache.tapestry5.test.AbstractIntegrationTestSuite;
+import org.apache.tapestry5.test.SeleniumTestCase;
+import org.testng.annotations.Test;
+
+@Test(sequential=true,groups="integration")
+public class TapestryCayenneIntegrationTest extends
AbstractIntegrationTestSuite {
+
+ public TapestryCayenneIntegrationTest() {
+ super("src/test/app0");
+ }
+
+ public void test_commit_after() {
+ open("/commitaftertestpage");
+ waitForPageToLoad();
+ assertTextPresent("Dali");
+ clickAndWait("link=Commit Ok");
+ assertTextPresent("Commitokname");
+ clickAndWait("link=Runtime Exception");
+ assertTextPresent("Commitokname");
+ clickAndWait("link=Checked Exception");
+ assertTextPresent("savesokwithcheckedexceptionname");
+ }
+
+ public void test_inject_objectcontext() {
+ open("/injectobjectcontexttestpage");
+ waitForPageToLoad();
+ assertTextPresent("Injecting the current context really /does/
give you the current context.");
+ assertTextPresent("Injecting with no annotation is the same as
injecting with the octype current.");
+ assertTextPresent("Injecting with octype child yields child of
current context.");
+ assertTextPresent("Injecting with octype new yields a new context,
not child of current context.");
+
+ String newContextHash = getText("id=newContextProp").trim();
+
+ clickAndWait("link=Invalidate Session");
+
+ assertTextPresent("Injecting the current context really /does/
give you the current context.");
+ assertTextPresent("Injecting with no annotation is the same as
injecting with the octype current.");
+ assertTextPresent("Injecting with octype child yields child of
current context.");
+ assertTextPresent("Injecting with octype new yields a new context,
not child of current context.");
+
+ String newContextHash2 = getText("id=newContextProp").trim();
+ assertFalse(newContextHash.equals(newContextHash2));
+
+ }
+}
=======================================
---
/trunk/tapestry5-cayenne-server/src/test/java/com/googlecode/tapestry5cayenne/integration/TapestryCayenneIntegrationTests.java
Sat Mar 12 12:33:24 2011
+++ /dev/null
@@ -1,47 +0,0 @@
-package com.googlecode.tapestry5cayenne.integration;
-
-import org.apache.tapestry5.test.AbstractIntegrationTestSuite;
-import org.apache.tapestry5.test.SeleniumTestCase;
-import org.testng.annotations.Test;
-
-@Test(sequential=true,groups="integration")
-public class TapestryCayenneIntegrationTests extends
AbstractIntegrationTestSuite {
-
- public TapestryCayenneIntegrationTests() {
- super("src/test/app0");
- }
-
- public void test_commit_after() {
- open("/commitaftertestpage");
- waitForPageToLoad();
- assertTextPresent("Dali");
- clickAndWait("link=Commit Ok");
- assertTextPresent("commitokname");
- clickAndWait("link=Runtime Exception");
- assertTextPresent("commitokname");
- clickAndWait("link=Checked Exception");
- assertTextPresent("savesokwithcheckedexceptionname");
- }
-
- public void test_inject_objectcontext() {
- open("/injectobjectcontexttestpage");
- waitForPageToLoad();
- assertTextPresent("Injecting the current context really /does/
give you the current context.");
- assertTextPresent("Injecting with no annotation is the same as
injecting with the octype current.");
- assertTextPresent("Injecting with octype child yields child of
current context.");
- assertTextPresent("Injecting with octype new yields a new context,
not child of current context.");
-
- String newContextHash = getText("id=newContextProp").trim();
-
- clickAndWait("link=Invalidate Session");
-
- assertTextPresent("Injecting the current context really /does/
give you the current context.");
- assertTextPresent("Injecting with no annotation is the same as
injecting with the octype current.");
- assertTextPresent("Injecting with octype child yields child of
current context.");
- assertTextPresent("Injecting with octype new yields a new context,
not child of current context.");
-
- String newContextHash2 = getText("id=newContextProp").trim();
- assertFalse(newContextHash.equals(newContextHash2));
-
- }
-}
=======================================
---
/trunk/tapestry5-cayenne-core/src/main/java/com/googlecode/tapestry5cayenne/annotations/CommitAfter.java
Thu Mar 5 23:53:23 2009
+++
/trunk/tapestry5-cayenne-core/src/main/java/com/googlecode/tapestry5cayenne/annotations/CommitAfter.java
Thu Mar 29 05:33:48 2012
@@ -1,5 +1,11 @@
package com.googlecode.tapestry5cayenne.annotations;
+import java.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.METHOD;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
/**
* Perfectly analogous to the tapestry-hibernate "CommitAfter" annotation.
* Apply this to a method where you want the current object context to be
committed.
@@ -8,6 +14,10 @@
* Generally, you should handle committing yourself to ensure that you
handle errors in a meaningful way.
* However, this annotation is useful for rapid prototyping.
*/
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target({METHOD})
+@Documented
public @interface CommitAfter {
}
=======================================
---
/trunk/tapestry5-cayenne-core/src/main/java/com/googlecode/tapestry5cayenne/services/CayenneCommitAfterWorker.java
Sat Mar 12 12:33:24 2011
+++
/trunk/tapestry5-cayenne-core/src/main/java/com/googlecode/tapestry5cayenne/services/CayenneCommitAfterWorker.java
Thu Mar 29 05:33:48 2012
@@ -20,12 +20,10 @@
public void advise(ComponentMethodInvocation invocation) {
try {
-
invocation.proceed();
provider.currentContext().commitChanges();
} catch (RuntimeException e) {
-
provider.currentContext().rollbackChanges();
throw e;
@@ -37,7 +35,9 @@
public void transform(ClassTransformation transformation,
MutableComponentModel model) {
+ System.out.println("Attempting to add advice for
CommitAfter-annotated methods for model " + model + "; transformation: " +
transformation);
for(TransformMethod method:
transformation.matchMethodsWithAnnotation(CommitAfter.class)) {
+ System.out.println("Adding commit after advice to method: " +
method);
method.addAdvice(advice);
}
}
=======================================
---
/trunk/tapestry5-cayenne-server/src/test/java/com/googlecode/tapestry5cayenne/integration/app0/pages/CommitAfterTestPage.java
Fri Mar 6 01:00:07 2009
+++
/trunk/tapestry5-cayenne-server/src/test/java/com/googlecode/tapestry5cayenne/integration/app0/pages/CommitAfterTestPage.java
Thu Mar 29 05:33:48 2012
@@ -3,7 +3,10 @@
import java.sql.SQLException;
import java.util.List;
+import org.apache.cayenne.DataObjectUtils;
import org.apache.cayenne.ObjectContext;
+import org.apache.cayenne.query.Ordering;
+import org.apache.cayenne.query.SortOrder;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
@@ -21,6 +24,9 @@
@Inject
private PersistentManager manager;
+//note that you have to @Persist the artist through requests to ensure
that you are using the correct artist each time.
+//Otherwise, having multiple artists can throw things off. But, to fully
test CommitAfter, you also have to refresh
+//the artist from the DB each time.
@Property
@Persist
private Artist artist;
@@ -28,45 +34,48 @@
@SetupRender
public void setupRender() {
if (artist == null) {
- List<Artist> artists = manager.listAll(Artist.class);
- if (artists.isEmpty()) {
- artist = context.newObject(Artist.class);
- artist.setName("Dali");
- context.commitChanges();
- } else {
- artist = artists.get(0);
- }
+ List<Artist> artists = manager.listAll(Artist.class, new
Ordering("name", SortOrder.ASCENDING));
+ if (artists.isEmpty()) {
+ artist = context.newObject(Artist.class);
+ artist.setName("Dali");
+ context.commitChanges();
+ } else {
+ artist = artists.get(0);
+ }
+ } else {
+ //force a DB refresh...
+ artist = (Artist) DataObjectUtils.objectForPK(context,
artist.getObjectId());
}
}
@CommitAfter
- void onActionFromCommitOk() {
- artist.setName("commitokname");
+ void onActionFromCommitOk(Artist artist) {
+ artist.setName("Commitokname");
}
@CommitAfter
- void doActionFromRuntimeException() {
- artist.setName("savefailsname");
+ void doActionFromRuntimeException(Artist artist) {
+ artist.setName("Failedsavename");
throw new RuntimeException("ignore");
}
- void onActionFromRuntimeException() {
+ void onActionFromRuntimeException(Artist artist) {
try {
- doActionFromRuntimeException();
+ doActionFromRuntimeException(artist);
} catch (RuntimeException e) {
//Ignore
}
}
@CommitAfter
- void doActionFromCheckedException() throws SQLException {
+ void doActionFromCheckedException(Artist artist) throws SQLException {
artist.setName("savesokwithcheckedexceptionname");
throw new SQLException("blah");
}
- void onActionFromCheckedException() {
+ void onActionFromCheckedException(Artist artist) {
try {
- doActionFromCheckedException();
+ doActionFromCheckedException(artist);
} catch (SQLException e) {
//Ignore
}
=======================================
---
/trunk/tapestry5-cayenne-server/src/test/java/com/googlecode/tapestry5cayenne/integration/app0/services/AppModule.java
Wed May 5 12:56:11 2010
+++
/trunk/tapestry5-cayenne-server/src/test/java/com/googlecode/tapestry5cayenne/integration/app0/services/AppModule.java
Thu Mar 29 05:33:48 2012
@@ -5,6 +5,8 @@
*/
package com.googlecode.tapestry5cayenne.integration.app0.services;
+import static
com.googlecode.tapestry5cayenne.T5CayenneConstants.PROJECT_FILE;
+
import java.util.List;
import org.apache.cayenne.BaseContext;
@@ -30,6 +32,7 @@
{
configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en");
configuration.add(SymbolConstants.PRODUCTION_MODE,"false");
+ configuration.add(PROJECT_FILE, "cayenne-App0.xml");
}
public static void
contributeApplicationInitializer(OrderedConfiguration<ApplicationInitializerFilter>
conf) {
=======================================
---
/trunk/tapestry5-cayenne-server/src/test/resources/com/googlecode/tapestry5cayenne/integration/app0/pages/CommitAfterTestPage.tml
Thu Mar 5 23:53:23 2009
+++
/trunk/tapestry5-cayenne-server/src/test/resources/com/googlecode/tapestry5cayenne/integration/app0/pages/CommitAfterTestPage.tml
Thu Mar 29 05:33:48 2012
@@ -1,6 +1,6 @@
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
${artist.name}
- <t:actionlink t:id="commitok">Commit Ok</t:actionlink>
- <t:actionlink t:id="runtimeexception">Runtime Exception</t:actionlink>
- <t:actionlink t:id="checkedexception">Checked Exception</t:actionlink>
+ <t:actionlink t:id="commitok" context="artist">Commit Ok</t:actionlink>
+ <t:actionlink t:id="runtimeexception" context="artist">Runtime
Exception</t:actionlink>
+ <t:actionlink t:id="checkedexception" context="artist">Checked
Exception</t:actionlink>
</html>