Commits

Hector Miuler Malpica Gallegos committed baf0f94

test transactional service

Comments (0)

Files changed (11)

miuler-demo-scala+spring.iml

         </fileset>
       </configuration>
     </facet>
+    <facet type="jpa" name="JPA">
+      <configuration>
+        <setting name="validation-enabled" value="true" />
+        <setting name="provider-name" value="" />
+        <datasource-mapping />
+        <deploymentDescriptor name="persistence.xml" url="file://$MODULE_DIR$/src/main/webapp/META-INF/persistence.xml" />
+      </configuration>
+    </facet>
+    <facet type="hibernate" name="Hibernate">
+      <configuration>
+        <datasource-map />
+      </configuration>
+    </facet>
   </component>
   <component name="NewModuleRootManager" inherit-compiler-output="false">
     <output url="file://$MODULE_DIR$/target/classes" />
       <sourceFolder url="file://$MODULE_DIR$/src/main/scala" isTestSource="false" />
       <sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
       <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
+      <sourceFolder url="file://$MODULE_DIR$/src/test/scala" isTestSource="true" />
       <excludeFolder url="file://$MODULE_DIR$/target" />
     </content>
     <orderEntry type="inheritedJdk" />
     <orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" />
     <orderEntry type="library" name="Maven: org.springframework:spring-expression:3.2.2.RELEASE" level="project" />
     <orderEntry type="library" name="Maven: org.springframework:spring-web:3.2.2.RELEASE" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-orm:3.2.2.RELEASE" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-jdbc:3.2.2.RELEASE" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-tx:3.2.2.RELEASE" level="project" />
+    <orderEntry type="library" name="Maven: org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final" level="project" />
+    <orderEntry type="library" name="Maven: org.hibernate:hibernate-entitymanager:4.1.8.Final" level="project" />
+    <orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.1.0.GA" level="project" />
+    <orderEntry type="library" name="Maven: org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:1.0.0.Final" level="project" />
+    <orderEntry type="library" name="Maven: dom4j:dom4j:1.6.1" level="project" />
+    <orderEntry type="library" name="Maven: org.hibernate:hibernate-core:4.1.8.Final" level="project" />
+    <orderEntry type="library" name="Maven: antlr:antlr:2.7.7" level="project" />
+    <orderEntry type="library" name="Maven: org.javassist:javassist:3.15.0-GA" level="project" />
+    <orderEntry type="library" name="Maven: org.hibernate.common:hibernate-commons-annotations:4.0.1.Final" level="project" />
+    <orderEntry type="library" name="Maven: com.h2database:h2:1.3.162" level="project" />
+    <orderEntry type="library" scope="TEST" name="Maven: org.specs2:specs2_2.10:1.14" level="project" />
+    <orderEntry type="library" scope="TEST" name="Maven: org.specs2:specs2-scalaz-core_2.10:7.0.0" level="project" />
+    <orderEntry type="library" scope="TEST" name="Maven: org.specs2:specs2-scalaz-concurrent_2.10:7.0.0" level="project" />
+    <orderEntry type="library" scope="TEST" name="Maven: org.specs2:scalaz-effect_2.10:7.0.0" level="project" />
+    <orderEntry type="library" scope="TEST" name="Maven: org.specs2:scalaz-core_2.10:7.0.0" level="project" />
+    <orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:3.1.2.RELEASE" level="project" />
   </component>
 </module>
 
             <artifactId>scala-library</artifactId>
             <version>2.10.1</version>
         </dependency>
+
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-webmvc</artifactId>
             <version>3.2.2.RELEASE</version>
         </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-orm</artifactId>
+            <version>3.2.2.RELEASE</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-tx</artifactId>
+            <version>3.2.2.RELEASE</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.hibernate.javax.persistence</groupId>
+            <artifactId>hibernate-jpa-2.0-api</artifactId>
+            <version>1.0.1.Final</version>
+        </dependency>
+        <dependency>
+            <groupId>org.hibernate</groupId>
+            <artifactId>hibernate-entitymanager</artifactId>
+            <version>4.1.8.Final</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.h2database</groupId>
+            <artifactId>h2</artifactId>
+            <version>1.3.162</version>
+        </dependency>
+
+        <!-- =====================TEST===================== -->
+        <dependency>
+            <groupId>org.specs2</groupId>
+            <artifactId>specs2_2.10</artifactId>
+            <version>1.14</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-test</artifactId>
+            <version>3.1.2.RELEASE</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
             </plugin>
         </plugins>
     </build>
+
+
+    <repositories>
+        <repository>
+            <id>oss.sonatype.releases</id>
+            <name>releases</name>
+            <url>http://oss.sonatype.org/content/repositories/releases</url>
+        </repository>
+
+        <repository>
+            <id>oss.sonatype.snapshots</id>
+            <name>snapshots</name>
+            <url>http://oss.sonatype.org/content/repositories/snapshots</url>
+        </repository>
+
+        <repository>
+            <id>central</id>
+            <name>Maven repository</name>
+            <url>http://repo1.maven.org/maven2</url>
+        </repository>
+
+        <repository>
+            <id>oss.sonatype.springsource</id>
+            <name>SpringSource Releases Proxy</name>
+            <url>https://oss.sonatype.org/content/groups/springsource-all/</url>
+        </repository>
+
+        <repository>
+            <id>com.springsource.repository</id>
+            <name>SpringSource Repository</name>
+            <url>http://repository.springsource.com/maven/libraries/external</url>
+        </repository>
+
+    </repositories>
+
 </project>

src/main/java/com/miuler/demo/scalaspring/service/JavaServiceImpl.java

 package com.miuler.demo.scalaspring.service;
 
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 /**
  * Created with IntelliJ IDEA.
 public class JavaServiceImpl implements JavaService {
 
     @Override
+    @Transactional
     public String obtenerNombre() {
         return "Miuler - Java";
     }

src/main/java/com/miuler/demo/scalaspring/web/JavaController.java

 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.RequestMapping;
 
+import com.miuler.demo.scalaspring.domain.Usuario;
 import com.miuler.demo.scalaspring.service.JavaService;
 import com.miuler.demo.scalaspring.service.ScalaService;
 

src/main/resources/script_schema.sql

+CREATE TABLE usuario (id INT, name VARCHAR(200));

src/main/scala/com/miuler/demo/scalaspring/domain/Usuario.scala

+package com.miuler.demo.scalaspring.domain
+
+import beans.BeanProperty
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: miuler
+ * Date: 27/03/13
+ * Time: 04:05 PM
+ * To change this template use File | Settings | File Templates.
+ */
+case class Usuario(
+  @BeanProperty var id: Int,
+  @BeanProperty var nombre: String,
+  @BeanProperty var edad: Int
+)

src/main/scala/com/miuler/demo/scalaspring/service/ScalaServiceImpl.scala

 package com.miuler.demo.scalaspring.service
 
 import org.springframework.stereotype.Service
+import com.miuler.demo.scalaspring.domain.Usuario
+import org.springframework.transaction.annotation.Transactional
 
 /**
  * Created with IntelliJ IDEA.
 @Service
 class ScalaServiceImpl extends ScalaService {
 
-  def obtenerNombre(): String = "Miuler - Scala"
+  @Transactional
+  def obtenerNombre(): String = {
+    var usuario = Usuario(0, "Miuler", 22)
+    usuario.nombre
+    usuario.nombre = "Hector"
+
+    "Miuler - Scala"
+  }
 
 }

src/main/scala/com/miuler/demo/scalaspring/web/ScalaController.scala

 import org.springframework.beans.factory.annotation.Autowired
 import com.miuler.demo.scalaspring.service.{JavaService, ScalaService}
 import org.springframework.ui.Model
+import com.miuler.demo.scalaspring.domain.Usuario
 
 
 @Controller
   @Autowired
   var javaService: JavaService = _
 
-  @RequestMapping(Array("/prueba/scala.html"))
+  @RequestMapping(Array{"/prueba/scala.html"})
   def index(model:Model) {
     model.addAttribute("nombre_scala", scalaService.obtenerNombre)
     model.addAttribute("nombre_java", javaService.obtenerNombre)
+
+    val usuario = Usuario(1, "pepito", 22)
   }
 
 }

src/main/webapp/META-INF/persistence.xml

+<?xml version='1.0' encoding='UTF-8'?>
+<persistence  xmlns='http://java.sun.com/xml/ns/persistence'
+              xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
+              xsi:schemaLocation='http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd'
+              version='1.0'>
+
+    <persistence-unit name="local" transaction-type="RESOURCE_LOCAL">
+        <provider>org.hibernate.ejb.HibernatePersistence</provider>
+        <properties>
+            <property name="show_sql" value="true" />
+            <property name="format_sql" value="true" />
+        </properties>
+    </persistence-unit>
+</persistence>

src/main/webapp/WEB-INF/scalaspring.xml

 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
-       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
-                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
-        >
+       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
+       xmlns:tx="http://www.springframework.org/schema/tx"
+       xsi:schemaLocation="
+            http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
+            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
+            http://www.springframework.org/schema/jdbc    http://www.springframework.org/schema/tx/spring-jdbc-3.2.xsd
+            http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
+            ">
 
+    <!-- =================== Spring =================== -->
     <context:annotation-config />
     <context:component-scan base-package="com.miuler.demo.scalaspring" />
+
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/view/" />
         <property name="suffix" value=".jsp" />
     </bean>
 
+    <!-- =================== DB - Datasource =================== -->
+    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
+        <property name="driverClassName" value="org.h2.Driver" />
+        <property name="url" value="jdbc:h2:file:test;DB_CLOSE_DELAY=-1" />
+        <!--<property name="url" value="jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'src/main/resources/script.sql';DB_CLOSE_DELAY=-1" />-->
+        <property name="username" value="adm" />
+        <property name="password" value="adm" />
+    </bean>
+    <jdbc:initialize-database data-source="dataSource">
+        <jdbc:script location="classpath:script_schema.sql"/>
+        <!--<jdbc:script location="classpath:script_others.sql"/>-->
+    </jdbc:initialize-database>
+
+
+    <!-- =================== Hibernate =================== -->
+    <bean id="factory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
+        <property name="dataSource" ref="dataSource"/>
+        <property name="packagesToScan" value="com.miuler.demo.scalaspring.domain" />
+        <property name="hibernateProperties">
+            <props>
+                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
+            </props>
+        </property>
+    </bean>
+    <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
+        <property name="sessionFactory" ref="factory"/>
+    </bean>
+    <tx:annotation-driven transaction-manager="hibernateTransactionManager" />
+
 </beans>

src/test/scala/HibernateTest.scala

+import com.miuler.demo.scalaspring.service.{JavaService, ScalaService}
+import org.specs2.mutable.Specification
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.test.context.ContextConfiguration
+import org.springframework.transaction.annotation.Transactional
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: miuler
+ * Date: 28/03/13
+ * Time: 10:48 AM
+ * To change this template use File | Settings | File Templates.
+ */
+//@ContextConfiguration(locations = Array{
+//  "classpath:"
+//})
+@Transactional
+class HibernateTest extends Specification {
+
+  @Autowired
+  var scalaService:ScalaService = _
+
+  @Autowired
+  var javaService:JavaService = _
+
+
+  "Transaction" should {
+
+    "Maintaining Transaction in Scala Service" in {
+      val name = scalaService.obtenerNombre
+    }
+
+    "Maintaining Transaction in Java Service" in {
+      val name = javaService.obtenerNombre
+    }
+
+  }
+
+}