/aop/src/test/java/org/openlogics/aop/jdbc/DataStoreTransactionInterceptorTest.java

https://bitbucket.org/miguelvega/openapi · Java · 143 lines · 102 code · 25 blank · 16 comment · 0 complexity · 00e293abbd9828a2b7b8b2fa4f3b85cc MD5 · raw file

  1. package org.openlogics.aop.jdbc;
  2. import com.google.common.base.Strings;
  3. import com.google.inject.AbstractModule;
  4. import com.google.inject.Guice;
  5. import com.google.inject.Inject;
  6. import com.google.inject.Injector;
  7. import com.google.inject.matcher.Matchers;
  8. import guice.aop0.VideoRental;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. import org.openlogics.aop.jdbc.service.CommonService;
  12. import org.openlogics.aop.jdbc.pojo.Foo;
  13. import org.openlogics.aop.jdbc.pojo.Product;
  14. import org.slf4j.Logger;
  15. import java.sql.SQLException;
  16. import java.sql.Timestamp;
  17. import static java.lang.System.currentTimeMillis;
  18. import static org.junit.Assert.assertEquals;
  19. /**
  20. * @author Miguel Vega
  21. * @version $Id: DataStoreTransactionInterceptorTest.java 0, 2013-08-14 6:19 PM mvega $
  22. * @deprecated using an older and not developed version of ObservableModule
  23. */
  24. @Deprecated
  25. public class DataStoreTransactionInterceptorTest {
  26. @Inject
  27. private VideoRental videoRental;
  28. @Inject
  29. CommonService commonService;
  30. private Logger logger = org.slf4j.LoggerFactory.getLogger(DataStoreTransactionInterceptorTest.class);
  31. @Before
  32. public void setup() {
  33. Injector injector = Guice.createInjector(new AbstractModule() {
  34. @Override
  35. protected void configure() {
  36. DataStoreTransactionInterceptor dataStoreTransactionInterceptor = new DataStoreTransactionInterceptor();
  37. requestInjection(dataStoreTransactionInterceptor);
  38. bindInterceptor(
  39. Matchers.any(),
  40. Matchers.annotatedWith(Transactional.class),
  41. dataStoreTransactionInterceptor);
  42. }
  43. }, new DataStoreModule());
  44. injector.injectMembers(this);
  45. injector.injectMembers(commonService);
  46. }
  47. @Test
  48. public void testRentMovie() throws Exception {
  49. try {
  50. videoRental.rentMovie(1);
  51. } catch (Exception x) {
  52. x.printStackTrace();
  53. }
  54. }
  55. /**
  56. * This test mjst generate an SQLException, due to the insertion with NULL values
  57. * @throws SQLException
  58. */
  59. @Test(expected = SQLException.class)
  60. public void testInsertTransaction() throws SQLException {
  61. logger.info(Strings.repeat(System.getProperty("line.separator"), 10));
  62. logger.info("Inicio Insercion ... " + Strings.repeat("*", 50));
  63. commonService.showFoos();
  64. logger.info(Strings.repeat("*", 50));
  65. try {
  66. Foo foo = new Foo();
  67. //this is supposed to throw an exception, because it can not accept NULL for NAME
  68. commonService.multipleTransactionDoer(foo);
  69. } catch (SQLException e) {
  70. throw e;
  71. } finally {
  72. logger.info("Fin Insercion ... " + Strings.repeat("*", 50));
  73. commonService.showFoos();
  74. logger.info(Strings.repeat("*", 50));
  75. }
  76. commonService.showFoos();
  77. }
  78. @Test
  79. public void testComplexTransaction() {
  80. try {
  81. //initially the number of products and foos is as follows
  82. assertEquals(5, commonService.getFooCount());
  83. assertEquals(4, commonService.getProductCount());
  84. //let's try to insert a record to each table
  85. Foo foo = new Foo();
  86. foo.setFname("Foo FName");
  87. foo.setLname("Foo LName");
  88. foo.setAddDate(new Timestamp(currentTimeMillis()));
  89. foo.setRate(100f);
  90. commonService.insertFooAndProduct(
  91. foo,
  92. new Product().setProdName("New Product 1").setProdPrice(50.1f));
  93. //initially the number of products and foos is as follows
  94. assertEquals(6, commonService.getFooCount());
  95. assertEquals(5, commonService.getProductCount());
  96. //let's try to insert a record to each table
  97. commonService.insertFooAndProduct(
  98. new Foo(),//because of the CONSTRAINTS an exception will be thrown
  99. new Product().setProdName("New Product 2").setProdPrice(55.1f));
  100. } catch(SQLException x){
  101. //do anything
  102. }
  103. finally {
  104. //as the last transaction has a failure, the number of records shouldn't change after the first insertion
  105. try {
  106. assertEquals(6, commonService.getFooCount());
  107. assertEquals(5, commonService.getProductCount());
  108. } catch (SQLException e) {
  109. e.printStackTrace();
  110. }
  111. }
  112. }
  113. @Test
  114. public void testObjectDataStore() throws SQLException {
  115. commonService.deleteAll();
  116. commonService.showFoos();
  117. }
  118. }