Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
Display soap message in axis client

client-config.wsdd : should be placed in the root of classpath

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <deployment name="defaultClientConfig" xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
  3.  
  4.     <handler name="log" type="java:org.apache.axis.handlers.LogHandler">
  5.         <!-- If true, output SOAP messages in the console ; otherwise, output in a file. -->
  6.         <parameter name="LogHandler.writeToConsole" value="false" />
  7.         <!-- Specifies the name of the output file when LogHandler.writeToConsole is false -->
  8.         <parameter name="LogHandler.fileName" value="axis.log" />
  9.     </handler>
  10.  
  11.     <globalConfiguration>
  12.         <parameter name="disablePrettyXML" value="false" />
  13.         <requestFlow>
  14.             <handler type="log" />
  15.         </requestFlow>
  16.         <responseFlow>
  17.             <handler type="log" />
  18.         </responseFlow>
  19.     </globalConfiguration>
  20.  
  21.     <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender" />
  22.     <transport name="local" pivot="java:org.apache.axis.transport.local.LocalSender" />
  23.     <transport name="java" pivot="java:org.apache.axis.transport.java.JavaSender" />
  24. </deployment>
JUnit with Spring : orderly test method

When using junit with spring framework for test, to sort execute order of method, will use FixMethodOrder class. It is a little inconvenience because you need to name your test method in correct way.



Execute test method ordered by method name
  1. package com.prhythm.test;
  2.  
  3. import org.junit.FixMethodOrder;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.junit.runners.MethodSorters;
  7. import org.springframework.test.context.ContextConfiguration;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9.  
  10. /**
  11.  * Created by nanashi07 on 15/6/20.
  12.  */
  13. @RunWith(SpringJUnit4ClassRunner.class)
  14. @ContextConfiguration("classpath:spring/application-context.xml")
  15. @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  16. public class OrderedTest {
  17.  
  18.     @Test
  19.     public void test1() {
  20.         System.out.println("test1 executed");
  21.     }
  22.  
  23.     @Test
  24.     public void test2() {
  25.         System.out.println("test2 executed");
  26.     }
  27.  
  28.     @Test
  29.     public void test3() {
  30.         System.out.println("test3 executed");
  31.     }
  32.  
  33.     @Test
  34.     public void test4() {
  35.         System.out.println("test4 executed");
  36.     }
  37. }
Result:
  1. test1 executed
  2. test2 executed
  3. test3 executed
  4. test4 executed


Now there is an another way to do it.



Create a order annotation for customize order
  1. package org.springframework.test.context.junit4;
  2.  
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5.  
  6. /**
  7.  * Created by nanashi07 on 15/6/20.
  8.  */
  9. @Retention(RetentionPolicy.RUNTIME)
  10. public @interface Order {
  11.     int value();
  12. }
Create a junit runner extends SpringJUnit4ClassRunner
  1. package org.springframework.test.context.junit4;
  2.  
  3. import org.junit.runners.model.FrameworkMethod;
  4. import org.junit.runners.model.InitializationError;
  5. import org.springframework.test.context.TestContextManager;
  6.  
  7. import java.util.Collections;
  8. import java.util.Comparator;
  9. import java.util.List;
  10.  
  11. public class SpringJUnit4ClassOrderedRunner extends SpringJUnit4ClassRunner {
  12.  
  13.     /**
  14.      * Constructs a new {@code SpringJUnit4ClassRunner} and initializes a
  15.      * {@link TestContextManager} to provide Spring testing functionality to
  16.      * standard JUnit tests.
  17.      *
  18.      * @param clazz the test class to be run
  19.      * @see #createTestContextManager(Class)
  20.      */
  21.     public SpringJUnit4ClassOrderedRunner(Class<?> clazz) throws InitializationError {
  22.         super(clazz);
  23.     }
  24.  
  25.     @Override
  26.     protected List<FrameworkMethod> computeTestMethods() {
  27.         List<FrameworkMethod> list = super.computeTestMethods();
  28.         Collections.sort(list, new Comparator<FrameworkMethod>() {
  29.             @Override
  30.             public int compare(FrameworkMethod f1, FrameworkMethod f2) {
  31.                 Order o1 = f1.getAnnotation(Order.class);
  32.                 Order o2 = f2.getAnnotation(Order.class);
  33.  
  34.                 if (o1 == null || o2 == null)
  35.                     return -1;
  36.  
  37.                 return o1.value() - o2.value();
  38.             }
  39.         });
  40.         return list;
  41.     }
  42. }
Run test as customized order
  1. package com.prhythm.test;
  2.  
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.test.context.ContextConfiguration;
  6. import org.springframework.test.context.junit4.Order;
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassOrderedRunner;
  8.  
  9. /**
  10.  * Created by nanashi07 on 15/6/20.
  11.  */
  12. @RunWith(SpringJUnit4ClassOrderedRunner.class)
  13. @ContextConfiguration("classpath:spring/application-context.xml")
  14. public class OrderedTest {
  15.  
  16.     @Test
  17.     @Order(value = 4)
  18.     public void test1() {
  19.         System.out.println("test1 executed");
  20.     }
  21.  
  22.     @Test
  23.     @Order(value = 3)
  24.     public void test2() {
  25.         System.out.println("test2 executed");
  26.     }
  27.  
  28.     @Test
  29.     @Order(value = 2)
  30.     public void test3() {
  31.         System.out.println("test3 executed");
  32.     }
  33.  
  34.     @Test
  35.     @Order(value = 1)
  36.     public void test4() {
  37.         System.out.println("test4 executed");
  38.     }
  39. }
Result:
  1. test4 executed
  2. test3 executed
  3. test2 executed
  4. test1 executed
Tired of Hibernate? Try JDBI in your code

JDBI Quick sample



ICategoryDAO.java : create a data access interface (implement is not required)
  1. package com.prhythm.erotic.task.data.dao;
  2.  
  3. import com.prhythm.erotic.entity.source.Category;
  4. import com.prhythm.erotic.task.data.mapper.CategoryMapper;
  5. import org.skife.jdbi.v2.sqlobject.Bind;
  6. import org.skife.jdbi.v2.sqlobject.BindBean;
  7. import org.skife.jdbi.v2.sqlobject.SqlQuery;
  8. import org.skife.jdbi.v2.sqlobject.SqlUpdate;
  9. import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
  10. import org.skife.jdbi.v2.sqlobject.mixins.Transactional;
  11.  
  12. import java.util.List;
  13.  
  14. /**
  15.  * Created by nanashi07 on 15/6/14.
  16.  */
  17. @RegisterMapper(CategoryMapper.class)
  18. public interface ICategoryDAO extends Transactional<ICategoryDAO> {
  19.  
  20.     @SqlUpdate("insert into Category (Source, Url, Category, SubCategory, Enabled) values (:source, :url, :category, :subCategory, :enabled)")
  21.     int insert(@BindBean Category category);
  22.  
  23.     @SqlUpdate("update Category set Source = :source, Category = :category, SubCategory = :subCategory, Enabled = :enabled where Url = :url")
  24.     int update(@BindBean Category category);
  25.  
  26.     @SqlUpdate("update Category set Enabled = :enabled")
  27.     int setEnabled(@Bind("enabled") boolean enabled);
  28.  
  29.     @SqlQuery("select * from Category where Url = :url")
  30.     Category findCategory(@Bind("url") String url);
  31.  
  32.     @SqlQuery("select * from Category")
  33.     List<Category> getCategories();
  34.  
  35.     void close();
  36. }

CategoryMapper.java : create a object mapper for convertion
  1. package com.prhythm.erotic.task.data.mapper;
  2.  
  3. import com.prhythm.erotic.entity.source.Category;
  4. import org.skife.jdbi.v2.StatementContext;
  5. import org.skife.jdbi.v2.tweak.ResultSetMapper;
  6.  
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9.  
  10. /**
  11.  * Created by nanashi07 on 15/6/14.
  12.  */
  13. public class CategoryMapper extends MetaDataSupport implements ResultSetMapper<Category> {
  14.  
  15.     @Override
  16.     public Category map(int index, ResultSet r, StatementContext ctx) throws SQLException {
  17.         Category category = new Category();
  18.         if (hasColumn(r, "source")) category.setSource(r.getString("source"));
  19.         if (hasColumn(r, "url")) category.setUrl(r.getString("url"));
  20.         if (hasColumn(r, "category")) category.setCategory(r.getString("category"));
  21.         if (hasColumn(r, "subCategory")) category.setSubCategory(r.getString("subCategory"));
  22.         if (hasColumn(r, "enabled")) category.setEnabled(r.getBoolean("enabled"));
  23.         return category;
  24.     }
  25.  
  26. }

ICategoryService.java : create a data access service interface
  1. package com.prhythm.erotic.task.data;
  2.  
  3. import com.prhythm.erotic.entity.source.Category;
  4.  
  5. import java.util.Collection;
  6. import java.util.List;
  7.  
  8. /**
  9.  * Created by nanashi07 on 15/6/14.
  10.  */
  11. public interface ICategoryService {
  12.  
  13.     /**
  14.      * Retrive all categories
  15.      *
  16.      * @return
  17.      */
  18.     List<Category> getCategories();
  19.  
  20.     /**
  21.      * Update categories
  22.      *
  23.      * @param categories
  24.      * @return
  25.      */
  26.     int updateCategories(Collection<Category> categories);
  27. }

CategoryServiceImpl.java : Implement the data access interface
  1. package com.prhythm.erotic.task.data.impl;
  2.  
  3. import com.prhythm.erotic.entity.source.Category;
  4. import com.prhythm.erotic.logging.LogHandler;
  5. import com.prhythm.erotic.task.data.ICategoryService;
  6. import com.prhythm.erotic.task.data.dao.ICategoryDAO;
  7. import org.skife.jdbi.v2.Handle;
  8. import org.skife.jdbi.v2.IDBI;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.context.ApplicationContext;
  11. import org.springframework.stereotype.Service;
  12.  
  13. import java.util.Collection;
  14. import java.util.List;
  15.  
  16. /**
  17.  * Created by nanashi07 on 15/6/14.
  18.  */
  19. @Service
  20. public class CategoryServiceImpl implements ICategoryService {
  21.  
  22.     @Autowired
  23.     IDBI dbi;
  24.  
  25.     @Override
  26.     public List<Category> getCategories() {
  27.         Handle h = dbi.open();
  28.         ICategoryDAO dao = h.attach(ICategoryDAO.class);
  29.  
  30.         List<Category> categories = dao.getCategories();
  31.  
  32.         dao.close();
  33.         h.close();
  34.  
  35.         return categories;
  36.     }
  37.  
  38.     @Override
  39.     public int updateCategories(Collection<Category> categories) {
  40.         int count = 0;
  41.  
  42.         Handle handle = dbi.open();
  43.  
  44.         try {
  45.             updateCategories(handle, categories);
  46.             handle.commit();
  47.         } catch (Exception e) {
  48.             LogHandler.error(e);
  49.             handle.rollback();
  50.         } finally {
  51.             handle.close();
  52.         }
  53.  
  54.         return count;
  55.     }
  56.  
  57.     int updateCategories(Handle handle, Collection<Category> categories) {
  58.         int count = 0;
  59.  
  60.         ICategoryDAO dao = handle.attach(ICategoryDAO.class);
  61.  
  62.         // Disable all items
  63.         dao.setEnabled(false);
  64.  
  65.         for (Category c : categories) {
  66.             Category found = dao.findCategory(c.getUrl());
  67.             if (found == null) {
  68.                 count += dao.insert(c);
  69.             } else {
  70.                 count += dao.update(c);
  71.             }
  72.         }
  73.  
  74.         return count;
  75.     }
  76. }

CategoryServiceTester.java : test usage
  1. package com.prhythm.erotic.test.dao;
  2.  
  3. import com.google.common.base.Stopwatch;
  4. import com.prhythm.erotic.entity.source.Category;
  5. import com.prhythm.erotic.logging.LogHandler;
  6. import com.prhythm.erotic.task.data.ICategoryService;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import org.junit.runner.RunWith;
  10. import org.skife.jdbi.v2.IDBI;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.context.ApplicationContext;
  13. import org.springframework.test.context.ContextConfiguration;
  14. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  15.  
  16. import java.util.Arrays;
  17. import java.util.List;
  18.  
  19. /**
  20.  * Created by nanashi07 on 15/6/14.
  21.  */
  22. @RunWith(SpringJUnit4ClassRunner.class)
  23. @ContextConfiguration("classpath:spring/application-context.xml")
  24. public class CategoryServiceTester {
  25.  
  26.     @Autowired
  27.     ICategoryService categoryService;
  28.  
  29.     @Test
  30.     public void testGetCategories() {
  31.         Stopwatch stopwatch = Stopwatch.createStarted();
  32.         List<Category> categories = categoryService.getCategories();
  33.         stopwatch.stop();
  34.         LogHandler.info("%d items loaded in %s", categories.size(), stopwatch);
  35.     }
  36.  
  37.     @Test
  38.     public void testUpdateCategories() {
  39.         // Get current items
  40.         List<Category> categories = categoryService.getCategories();
  41.         // Append 10 new items
  42.         for (int i = 0; i < 10; i++) {
  43.             Category c = new Category();
  44.             c.setSource("Prhythm");
  45.             c.setUrl("http://app.prhtyhm.com/sample/" + UUID.randomUUID());
  46.             c.setCategory("Blog");
  47.             c.setEnabled(true);
  48.             categories.add(c);
  49.         }
  50.  
  51.         Stopwatch stopwatch = Stopwatch.createStarted();
  52.         int count = categoryService.updateCategories(categories);
  53.         stopwatch.stop();
  54.         LogHandler.info("%d items updated in %s", count, stopwatch);
  55.     }
  56.  
  57. }

datasourc.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:tx="http://www.springframework.org/schema/tx"
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.                            http://www.springframework.org/schema/beans/spring-beans.xsd
  7.                            http://www.springframework.org/schema/tx
  8.                            http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9.  
  10.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  11.         <property name="driverClassName" value="${com.prhythm.erotic.datasource.driver}"/>
  12.         <property name="url" value="${com.prhythm.erotic.datasource.temp.url}"/>
  13.         <property name="username" value="${com.prhythm.erotic.datasource.user}"/>
  14.         <property name="password" value="${com.prhythm.erotic.datasource.password}"/>
  15.     </bean>
  16.  
  17.     <tx:annotation-driven transaction-manager="txManager"/>
  18.  
  19.     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  20.         <constructor-arg ref="dataSource"/>
  21.     </bean>
  22.  
  23.     <bean id="txDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
  24.         <constructor-arg ref="dataSource"/>
  25.     </bean>
  26.  
  27.     <bean class="org.skife.jdbi.v2.DBI" depends-on="txManager">
  28.         <constructor-arg ref="txDataSource"/>
  29.         <property name="SQLLog">
  30.             <bean class="com.prhythm.erotic.task.logging.SQLLogAppender"/>
  31.         </property>
  32.     </bean>
  33.  
  34. </beans>