分类 Java 下的文章

使用testng和spring进行单元测试

前提:了解spring的相关配置。

需要使用 spring-test 的jar包。<!--more-->

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.annotations.Test;

//如果不需要事务,继承 AbstractTestNGSpringContextTests 即可
//新建了一个spring配置文件,配置了测试用的数据源,自动扫描的包路径或定义了需要使用的bean

@ContextConfiguration(locations = {"classpath*:spring/spring-test.xml"})
public class PrizeTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    @Qualifier(value = "prizeService")
    PrizeService prizeService;

    @Test(threadPoolSize = 100)
    @Rollback(false)  //默认为 true ,即执行完以后自动回滚。乌鸦需要执行完以后去数据库看结果,故设置false
    public void addTestAddValid() throws Exception {
        System.out.println(prizeService);
        Pool pool = new Pool();
        System.out.println(prizeService.addValid(pool));
    }

}

- 阅读剩余部分 -

使用static SimpleDateFormat引发的同步问题

测试一个接口,发送的报文仅id不同,其他的值都是相同的,但是接口返回的是“日期格式错误”。经核实,接口中校验日期格式的方法用的是SimpleDateFormat的parse()方法。
查看jdk api:

日期格式是不同步的。建议为每个线程创建独立的格式实例。如果多个线程同时访问一个格式,则它必须是外部同步的。

下面为事件还原代码:<!--more-->

package com.test;

import java.text.SimpleDateFormat;

/**
 * @author: 乌鸦
 * Date: 2016/5/26 16:32
 * Description:
 */
public class DateFormatTest extends Thread  {
    private String name;

    public DateFormatTest(String name) {
        this.name = name;
    }

    public void run() {
        String dateStr = "20160526163700";
        boolean result = DateFormatTest.checkDate(dateStr,DateFormatTest.SDF14);
        System.out.println(name + " :  " + result);
    }

    public static void main(String[] args) throws InterruptedException {
        /*多线程可能报错*/
        for(int i=0; i<100; i++){
            new DateFormatTest("B"+i).start();
        }
        
        /*单线程不会报错*/
/*        for(int i=0; i<100; i++){
            String dateStr = "20160526163700";
            boolean result = DateFormatTest.checkDate(dateStr,DateFormatTest.SDF14);
            System.out.println(i + " : " + result);
        }*/
    }

    /* 以下是接口中校验日期的方法 */
    private static final String DATE_FORMAT14 = "yyyyMMddHHmmss";
    public static final SimpleDateFormat SDF14 = new SimpleDateFormat(DATE_FORMAT14);
    
    public static boolean checkDate(String date, SimpleDateFormat format) {
        try {
            format.parse(date);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

}

在java中,多线程是很重要的一块,看来多线程处处是坑啊。

参考:
http://www.cnblogs.com/zemliu/archive/2013/08/29/3290585.html



- 阅读剩余部分 -

tomcat启动报错This file is needed to run this program

背景:服务器是别人的,估计是用过安装版的tomcat,后来把目录删除了。
我把压缩版的tomcat放到服务器上,启动时./startup.sh ,报错

Cannot find /usr/tomcat/apache-tomcat-9.0.0.M3/bin/setclasspath.sh
This file is needed to run this program

找了一下,这个报错信息是catalina.sh中164行的报错,试着打印了$CATALINA_HOME,果然是/usr/tomcat/apache-tomcat-9.0.0.M3,这个估计是就是原来安装版的tomcat目录。


- 阅读剩余部分 -

java操作cookie

同一域名下的子域名的cookie可以互相访问。
在设置cookie的时候,有以下几个方法:
setDomain() 设置cookie的有效domain范围,可以指定当前的domain,也可以指定domain的父级domain(当前domain为二级域名的时候,可以指定有效范围有一级domain)
setPath() 设置cookie的有效路径范围,可以指定根路径(/),说明所有的path都有效;或者指定某一个path(/test),则/test及下级路径都有效
setMaxAge() 设置cookie的最大值,未验证。





- 阅读剩余部分 -