PageRenderTime 2777ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/persist/test/com/google/inject/persist/jpa/ManualLocalTransactionsConfidenceTest.java

https://gitlab.com/metamorphiccode/guice
Java | 95 lines | 59 code | 17 blank | 19 comment | 0 complexity | 17732441dfed0e96306c8c47338cac55 MD5 | raw file
  1. /**
  2. * Copyright (C) 2010 Google, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.inject.persist.jpa;
  17. import com.google.inject.Guice;
  18. import com.google.inject.Inject;
  19. import com.google.inject.Injector;
  20. import com.google.inject.persist.PersistService;
  21. import com.google.inject.persist.Transactional;
  22. import junit.framework.TestCase;
  23. import java.util.Date;
  24. import javax.persistence.EntityManager;
  25. import javax.persistence.PersistenceException;
  26. /**
  27. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  28. */
  29. public class ManualLocalTransactionsConfidenceTest extends TestCase {
  30. private Injector injector;
  31. private static final String UNIQUE_TEXT_3 =
  32. ManualLocalTransactionsConfidenceTest.class.getSimpleName()
  33. + "CONSTRAINT_VIOLATING some other unique text" + new Date();
  34. @Override
  35. public void setUp() {
  36. injector = Guice.createInjector(new JpaPersistModule("testUnit"));
  37. //startup persistence
  38. injector.getInstance(PersistService.class).start();
  39. }
  40. @Override
  41. public final void tearDown() {
  42. injector.getInstance(PersistService.class).stop();
  43. }
  44. public void testThrowingCleanupInterceptorConfidence() {
  45. Exception e = null;
  46. try {
  47. System.out.println(
  48. "\n\n******************************* EXPECTED EXCEPTION NORMAL TEST BEHAVIOR **********");
  49. injector.getInstance(TransactionalObject.class).runOperationInTxn();
  50. fail();
  51. } catch (RuntimeException re) {
  52. e = re;
  53. System.out.println(
  54. "\n\n******************************* EXPECTED EXCEPTION NORMAL TEST BEHAVIOR **********");
  55. re.printStackTrace(System.out);
  56. System.out.println(
  57. "\n\n**********************************************************************************");
  58. }
  59. assertNotNull("No exception was thrown!", e);
  60. assertTrue("Exception thrown was not what was expected (i.e. commit-time)",
  61. e instanceof PersistenceException);
  62. }
  63. public static class TransactionalObject {
  64. @Inject EntityManager em;
  65. @Transactional
  66. public void runOperationInTxn() {
  67. JpaParentTestEntity entity = new JpaParentTestEntity();
  68. JpaTestEntity child = new JpaTestEntity();
  69. child.setText(UNIQUE_TEXT_3);
  70. em.persist(child);
  71. entity.getChildren().add(child);
  72. em.persist(entity);
  73. entity = new JpaParentTestEntity();
  74. entity.getChildren().add(child);
  75. em.persist(entity);
  76. }
  77. }
  78. }