Popular Posts
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... Word break tag : <wbr/> (HTML5) The  HTML  <wbr>  tag  is  used  defines  a  potential  line  break  point  if  needed.  This  stands  for  Word  BReak. This  is  u... 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

<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig" xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

    <handler name="log" type="java:org.apache.axis.handlers.LogHandler">
        <!-- If true, output SOAP messages in the console ; otherwise, output in a file. -->
        <parameter name="LogHandler.writeToConsole" value="false" />
        <!-- Specifies the name of the output file when LogHandler.writeToConsole is false -->
        <parameter name="LogHandler.fileName" value="axis.log" />
    </handler>

    <globalConfiguration>
        <parameter name="disablePrettyXML" value="false" />
        <requestFlow>
            <handler type="log" />
        </requestFlow>
        <responseFlow>
            <handler type="log" />
        </responseFlow>
    </globalConfiguration>

    <transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender" />
    <transport name="local" pivot="java:org.apache.axis.transport.local.LocalSender" />
    <transport name="java" pivot="java:org.apache.axis.transport.java.JavaSender" />
</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
package com.prhythm.test;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by nanashi07 on 15/6/20.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/application-context.xml")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OrderedTest {

    @Test
    public void test1() {
        System.out.println("test1 executed");
    }

    @Test
    public void test2() {
        System.out.println("test2 executed");
    }

    @Test
    public void test3() {
        System.out.println("test3 executed");
    }

    @Test
    public void test4() {
        System.out.println("test4 executed");
    }
}
Result:
test1 executed
test2 executed
test3 executed
test4 executed


Now there is an another way to do it.



Create a order annotation for customize order
package org.springframework.test.context.junit4;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by nanashi07 on 15/6/20.
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface Order {
    int value();
}
Create a junit runner extends SpringJUnit4ClassRunner
package org.springframework.test.context.junit4;

import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.springframework.test.context.TestContextManager;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
public class SpringJUnit4ClassOrderedRunner extends SpringJUnit4ClassRunner {

    /**
     * Constructs a new {@code SpringJUnit4ClassRunner} and initializes a
     * {@link TestContextManager} to provide Spring testing functionality to
     * standard JUnit tests.
     *
     * @param clazz the test class to be run
     * @see #createTestContextManager(Class)
     */
    public SpringJUnit4ClassOrderedRunner(Class<?> clazz) throws InitializationError {
        super(clazz);
    }

    @Override
    protected List<FrameworkMethod> computeTestMethods() {
        List<FrameworkMethod> list = super.computeTestMethods();
        Collections.sort(list, new Comparator<FrameworkMethod>() {
            @Override
            public int compare(FrameworkMethod f1, FrameworkMethod f2) {
                Order o1 = f1.getAnnotation(Order.class);
                Order o2 = f2.getAnnotation(Order.class);

                if (o1 == null || o2 == null)
                    return -1;

                return o1.value() - o2.value();
            }
        });
        return list;
    }
}
Run test as customized order
package com.prhythm.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.Order;
import org.springframework.test.context.junit4.SpringJUnit4ClassOrderedRunner;

/**
 * Created by nanashi07 on 15/6/20.
 */
@RunWith(SpringJUnit4ClassOrderedRunner.class)
@ContextConfiguration("classpath:spring/application-context.xml")
public class OrderedTest {

    @Test
    @Order(value = 4)
    public void test1() {
        System.out.println("test1 executed");
    }

    @Test
    @Order(value = 3)
    public void test2() {
        System.out.println("test2 executed");
    }

    @Test
    @Order(value = 2)
    public void test3() {
        System.out.println("test3 executed");
    }

    @Test
    @Order(value = 1)
    public void test4() {
        System.out.println("test4 executed");
    }
}
Result:
test4 executed
test3 executed
test2 executed
test1 executed
Tired of Hibernate? Try JDBI in your code

JDBI Quick sample



ICategoryDAO.java : create a data access interface (implement is not required)
package com.prhythm.erotic.task.data.dao;

import com.prhythm.erotic.entity.source.Category;
import com.prhythm.erotic.task.data.mapper.CategoryMapper;
import org.skife.jdbi.v2.sqlobject.Bind;
import org.skife.jdbi.v2.sqlobject.BindBean;
import org.skife.jdbi.v2.sqlobject.SqlQuery;
import org.skife.jdbi.v2.sqlobject.SqlUpdate;
import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
import org.skife.jdbi.v2.sqlobject.mixins.Transactional;

import java.util.List;

/**
 * Created by nanashi07 on 15/6/14.
 */
@RegisterMapper(CategoryMapper.class)
public interface ICategoryDAO extends Transactional<ICategoryDAO> {

    @SqlUpdate("insert into Category (Source, Url, Category, SubCategory, Enabled) values (:source, :url, :category, :subCategory, :enabled)")
    int insert(@BindBean Category category);

    @SqlUpdate("update Category set Source = :source, Category = :category, SubCategory = :subCategory, Enabled = :enabled where Url = :url")
    int update(@BindBean Category category);

    @SqlUpdate("update Category set Enabled = :enabled")
    int setEnabled(@Bind("enabled") boolean enabled);

    @SqlQuery("select * from Category where Url = :url")
    Category findCategory(@Bind("url") String url);

    @SqlQuery("select * from Category")
    List<Category> getCategories();

    void close();
}

CategoryMapper.java : create a object mapper for convertion
package com.prhythm.erotic.task.data.mapper;

import com.prhythm.erotic.entity.source.Category;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.ResultSetMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by nanashi07 on 15/6/14.
 */
public class CategoryMapper extends MetaDataSupport implements ResultSetMapper<Category> {

    @Override
    public Category map(int index, ResultSet r, StatementContext ctx) throws SQLException {
        Category category = new Category();
        if (hasColumn(r, "source")) category.setSource(r.getString("source"));
        if (hasColumn(r, "url")) category.setUrl(r.getString("url"));
        if (hasColumn(r, "category")) category.setCategory(r.getString("category"));
        if (hasColumn(r, "subCategory")) category.setSubCategory(r.getString("subCategory"));
        if (hasColumn(r, "enabled")) category.setEnabled(r.getBoolean("enabled"));
        return category;
    }

}

ICategoryService.java : create a data access service interface
package com.prhythm.erotic.task.data;

import com.prhythm.erotic.entity.source.Category;

import java.util.Collection;
import java.util.List;

/**
 * Created by nanashi07 on 15/6/14.
 */
public interface ICategoryService {

    /**
     * Retrive all categories
     *
     * @return
     */
    List<Category> getCategories();

    /**
     * Update categories
     *
     * @param categories
     * @return
     */
    int updateCategories(Collection<Category> categories);
}

CategoryServiceImpl.java : Implement the data access interface
package com.prhythm.erotic.task.data.impl;

import com.prhythm.erotic.entity.source.Category;
import com.prhythm.erotic.logging.LogHandler;
import com.prhythm.erotic.task.data.ICategoryService;
import com.prhythm.erotic.task.data.dao.ICategoryDAO;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.IDBI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * Created by nanashi07 on 15/6/14.
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    IDBI dbi;

    @Override
    public List<Category> getCategories() {
        Handle h = dbi.open();
        ICategoryDAO dao = h.attach(ICategoryDAO.class);

        List<Category> categories = dao.getCategories();

        dao.close();
        h.close();

        return categories;
    }

    @Override
    public int updateCategories(Collection<Category> categories) {
        int count = 0;

        Handle handle = dbi.open();

        try {
            updateCategories(handle, categories);
            handle.commit();
        } catch (Exception e) {
            LogHandler.error(e);
            handle.rollback();
        } finally {
            handle.close();
        }

        return count;
    }

    int updateCategories(Handle handle, Collection<Category> categories) {
        int count = 0;

        ICategoryDAO dao = handle.attach(ICategoryDAO.class);

        // Disable all items
        dao.setEnabled(false);

        for (Category c : categories) {
            Category found = dao.findCategory(c.getUrl());
            if (found == null) {
                count += dao.insert(c);
            } else {
                count += dao.update(c);
            }
        }

        return count;
    }
}

CategoryServiceTester.java : test usage
package com.prhythm.erotic.test.dao;

import com.google.common.base.Stopwatch;
import com.prhythm.erotic.entity.source.Category;
import com.prhythm.erotic.logging.LogHandler;
import com.prhythm.erotic.task.data.ICategoryService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.skife.jdbi.v2.IDBI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Arrays;
import java.util.List;

/**
 * Created by nanashi07 on 15/6/14.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/application-context.xml")
public class CategoryServiceTester {

    @Autowired
    ICategoryService categoryService;

    @Test
    public void testGetCategories() {
        Stopwatch stopwatch = Stopwatch.createStarted();
        List<Category> categories = categoryService.getCategories();
        stopwatch.stop();
        LogHandler.info("%d items loaded in %s", categories.size(), stopwatch);
    }

    @Test
    public void testUpdateCategories() {
        // Get current items
        List<Category> categories = categoryService.getCategories();
        // Append 10 new items
        for (int i = 0; i < 10; i++) {
            Category c = new Category();
            c.setSource("Prhythm");
            c.setUrl("http://app.prhtyhm.com/sample/" + UUID.randomUUID());
            c.setCategory("Blog");
            c.setEnabled(true);
            categories.add(c);
        }

        Stopwatch stopwatch = Stopwatch.createStarted();
        int count = categoryService.updateCategories(categories);
        stopwatch.stop();
        LogHandler.info("%d items updated in %s", count, stopwatch);
    }

}

datasourc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${com.prhythm.erotic.datasource.driver}"/>
        <property name="url" value="${com.prhythm.erotic.datasource.temp.url}"/>
        <property name="username" value="${com.prhythm.erotic.datasource.user}"/>
        <property name="password" value="${com.prhythm.erotic.datasource.password}"/>
    </bean>

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

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <constructor-arg ref="dataSource"/>
    </bean>

    <bean id="txDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
        <constructor-arg ref="dataSource"/>
    </bean>

    <bean class="org.skife.jdbi.v2.DBI" depends-on="txManager">
        <constructor-arg ref="txDataSource"/>
        <property name="SQLLog">
            <bean class="com.prhythm.erotic.task.logging.SQLLogAppender"/>
        </property>
    </bean>

</beans>