[myblog] r448 committed - finish a basic test code.

2 views
Skip to first unread message

myb...@googlecode.com

unread,
Jun 16, 2011, 9:46:48 AM6/16/11
to mybl...@googlegroups.com
Revision: 448
Author: Ror...@gmail.com
Date: Thu Jun 16 06:43:49 2011
Log: finish a basic test code.
http://code.google.com/p/myblog/source/detail?r=448

Added:

/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/service/UserServiceImpl.java
/branches/myblog-springmvc/src/test/java/com
/branches/myblog-springmvc/src/test/java/com/jdkcn
/branches/myblog-springmvc/src/test/java/com/jdkcn/myblog
/branches/myblog-springmvc/src/test/java/com/jdkcn/myblog/AbstractTest.java

/branches/myblog-springmvc/src/test/java/com/jdkcn/myblog/UserServiceTest.java
/branches/myblog-springmvc/src/test/resources/META-INF
/branches/myblog-springmvc/src/test/resources/META-INF/persistence.xml
/branches/myblog-springmvc/src/test/resources/test-context.xml
Modified:
/branches/myblog-springmvc/pom.xml

/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/service/BlogManagerImpl.java

/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/service/UserService.java
/branches/myblog-springmvc/src/main/resources/spring/global-context.xml

=======================================
--- /dev/null
+++
/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/service/UserServiceImpl.java
Thu Jun 16 06:43:49 2011
@@ -0,0 +1,49 @@
+package com.jdkcn.myblog.service;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.jdkcn.myblog.domain.User;
+import com.jdkcn.myblog.repository.UserDao;
+
+/**
+ * @author <a href="mailto:ror...@gmail.com">Rory</a>
+ * @version $Id$
+ */
+@Service
+public class UserServiceImpl implements UserService {
+
+ @Autowired
+ private UserDao userDao;
+
+ /**
+ * {@inheritDoc}
+ */
+ public User saveOrUpdate(User user) {
+ return userDao.saveOrUpdate(user);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public User get(String id) {
+ return userDao.get(id);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public User getByUsername(String username) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public User getByEmail(String email) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
=======================================
--- /dev/null
+++
/branches/myblog-springmvc/src/test/java/com/jdkcn/myblog/AbstractTest.java
Thu Jun 16 06:43:49 2011
@@ -0,0 +1,15 @@
+package com.jdkcn.myblog;
+
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+/**
+ * @author <a href="mailto:ror...@gmail.com">Rory</a>
+ * @version $Id$
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration({"/test-context.xml"})
+public class AbstractTest {
+
+}
=======================================
--- /dev/null
+++
/branches/myblog-springmvc/src/test/java/com/jdkcn/myblog/UserServiceTest.java
Thu Jun 16 06:43:49 2011
@@ -0,0 +1,30 @@
+package com.jdkcn.myblog;
+
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import com.jdkcn.myblog.domain.User;
+import com.jdkcn.myblog.service.UserService;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author <a href="mailto:ror...@gmail.com">Rory</a>
+ * @version $Id$
+ */
+public class UserServiceTest extends AbstractTest {
+
+ @Autowired
+ private UserService userService;
+
+ @Test
+ public void testCreate() throws Exception {
+ User user = new User();
+ user.setUsername("myblog");
+ user.setPassword("secret");
+ user.setEmail("tes...@example.com");
+ assertNull(user.getId());
+ user = userService.saveOrUpdate(user);
+ assertNotNull(user.getId());
+ }
+}
=======================================
--- /dev/null
+++ /branches/myblog-springmvc/src/test/resources/META-INF/persistence.xml
Thu Jun 16 06:43:49 2011
@@ -0,0 +1,34 @@
+<?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">
+
+ <!-- JPA test "unit" -->
+ <persistence-unit name="myblogTestUnit"
transaction-type="RESOURCE_LOCAL" >
+ <provider>org.hibernate.ejb.HibernatePersistence</provider>
+ <class>com.jdkcn.myblog.domain.Entry</class>
+ <class>com.jdkcn.myblog.domain.Category</class>
+ <class>com.jdkcn.myblog.domain.User</class>
+ <class>com.jdkcn.myblog.domain.Account</class>
+ <class>com.jdkcn.myblog.domain.Blog</class>
+ <class>com.jdkcn.myblog.domain.Tag</class>
+ <class>com.jdkcn.myblog.domain.Comment</class>
+ <exclude-unlisted-classes>true</exclude-unlisted-classes>
+ <properties>
+ <property name="hibernate.connection.driver_class"
value="org.hsqldb.jdbcDriver" />
+ <property name="hibernate.connection.url"
value="jdbc:hsqldb:mem:myblog" />
+ <property name="hibernate.connection.username" value="sa" />
+ <property name="hibernate.connection.password" value="" />
+ <property name="hibernate.connection.pool_size" value="5"/>
+ <property name="hibernate.dialect"
value="org.hibernate.dialect.HSQLDialect" />
+ <property name="hibernate.hbm2ddl.auto" value="update"/>
+ <property name="hibernate.show_sql" value="false"/>
+ <property name="hibernate.format_sql" value="true"/>
+ <property name="hibernate.cache.provider_class"
value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" />
+ <property name="hibernate.cache.use_query_cache" value="true"
/>
+ <property name="hibernate.cache.use_second_level_cache"
value="true" />
+ </properties>
+ </persistence-unit>
+
+</persistence>
=======================================
--- /dev/null
+++ /branches/myblog-springmvc/src/test/resources/test-context.xml Thu Jun
16 06:43:49 2011
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:tx="http://www.springframework.org/schema/tx"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:aop="http://www.springframework.org/schema/aop"
+ xmlns:context="http://www.springframework.org/schema/context"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
+ http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
+ http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
+
+ <context:annotation-config/>
+
+ <context:component-scan
base-package="com.jdkcn.myblog.repository,com.jdkcn.myblog.service"/>
+
+ <bean id="entityManagerFactory"
+ class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
+ <property name="persistenceUnitName" value="myblogTestUnit" />
+ </bean>
+
+ <bean id="jpaTransactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
+ <property name="entityManagerFactory" ref="entityManagerFactory" />
+ </bean>
+
+ <aop:config>
+ <aop:pointcut id="serviceMethods"
+ expression="execution(* com.jdkcn.myblog.service.*.*(..))" />
+ <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
+ </aop:config>
+
+ <tx:advice id="txAdvice" transaction-manager="jpaTransactionManager">
+ <tx:attributes>
+ <tx:method name="increasePrice*" propagation="REQUIRED" />
+ <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW" />
+ <tx:method name="*" propagation="SUPPORTS" read-only="true" />
+ </tx:attributes>
+ </tx:advice>
+
+</beans>
=======================================
--- /branches/myblog-springmvc/pom.xml Thu Jun 16 05:52:43 2011
+++ /branches/myblog-springmvc/pom.xml Thu Jun 16 06:43:49 2011
@@ -291,5 +291,26 @@
<scope>compile</scope>
</dependency>

+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.2</version>
+ <type>jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <version>3.0.5.RELEASE</version>
+ <type>jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>1.8.0.10</version>
+ <type>jar</type>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
=======================================
---
/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/service/BlogManagerImpl.java
Tue Jun 14 10:33:56 2011
+++
/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/service/BlogManagerImpl.java
Thu Jun 16 06:43:49 2011
@@ -1,6 +1,11 @@
package com.jdkcn.myblog.service;


+import java.util.List;
+import java.util.Random;
+
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.security.acls.domain.BasePermission;
import org.springframework.security.acls.domain.ObjectIdentityImpl;
import org.springframework.security.acls.domain.PrincipalSid;
@@ -15,16 +20,11 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.transaction.annotation.Transactional;
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.util.Assert;

import com.jdkcn.myblog.domain.Blog;
import com.jdkcn.myblog.repository.BlogDao;

-import java.util.List;
-import java.util.Random;
-

/**
* Concrete implementation of {@link BlogManager}.
=======================================
---
/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/service/UserService.java
Thu Jun 16 05:52:43 2011
+++
/branches/myblog-springmvc/src/main/java/com/jdkcn/myblog/service/UserService.java
Thu Jun 16 06:43:49 2011
@@ -1,9 +1,41 @@
package com.jdkcn.myblog.service;

+import com.jdkcn.myblog.domain.User;
+
/**
* @author <a href="mailto:ror...@gmail.com">Rory</a>
* @version $Id$
*/
public interface UserService {
-
-}
+ /**
+ * Save or update the user.
+ *
+ * @param user
+ * user to save or update
+ * @return The saved user
+ */
+ User saveOrUpdate(User user);
+
+ /**
+ * Get user by id
+ *
+ * @param id
+ * user's id
+ * @return user with this id or null if not exist
+ */
+ User get(String id);
+
+ /**
+ * Get user by username
+ * @param username the user's username
+ * @return user with this username or null if not exist
+ */
+ User getByUsername(String username);
+
+ /**
+ * Get user by email
+ * @param email the user's email
+ * @return user with this email or null if not exist
+ */
+ User getByEmail(String email);
+}
=======================================
--- /branches/myblog-springmvc/src/main/resources/spring/global-context.xml
Thu Jun 16 05:52:43 2011
+++ /branches/myblog-springmvc/src/main/resources/spring/global-context.xml
Thu Jun 16 06:43:49 2011
@@ -1,14 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
-
-<!-- Application context containing business beans. Used by all artifacts.
-->
-
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+ http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
+ http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
@@ -22,6 +21,8 @@
</bean>

<context:annotation-config/>
+
+ <context:component-scan base-package="com.jdkcn.myblog"/>

<tx:annotation-driven transaction-manager="transactionManager" />

@@ -47,4 +48,22 @@
<property name="mutableAclService" ref="aclService"/>
</bean>

+ <bean id="jpaTransactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
+ <property name="entityManagerFactory" ref="entityManagerFactory" />
+ </bean>
+
+ <aop:config>
+ <aop:pointcut id="serviceMethods"
+ expression="execution(* com.jdkcn.myblog.service.*.*(..))" />
+ <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
+ </aop:config>
+
+ <tx:advice id="txAdvice" transaction-manager="jpaTransactionManager">
+ <tx:attributes>
+ <tx:method name="increasePrice*" propagation="REQUIRED" />
+ <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW" />
+ <tx:method name="*" propagation="SUPPORTS" read-only="true" />
+ </tx:attributes>
+ </tx:advice>
+
</beans>

Reply all
Reply to author
Forward
0 new messages