├── .gitignore ├── README.md ├── client ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── googlecode │ └── excavator │ └── demo │ └── client │ ├── AsyncLogServiceClient.java │ ├── LogServiceClient.java │ └── TimeServiceClient.java ├── common ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── googlecode │ └── excavator │ └── demo │ └── common │ ├── ErrorCodeConstants.java │ ├── LogException.java │ ├── TimeException.java │ ├── domain │ └── ResultDO.java │ └── service │ ├── LogService.java │ └── TimeService.java ├── core ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── googlecode │ └── excavator │ └── demo │ └── core │ ├── ErrorCodeException.java │ ├── manager │ ├── FormatManager.java │ └── impl │ │ └── FormatManagerImpl.java │ └── service │ └── impl │ ├── LogServiceImpl.java │ └── TimeServiceImpl.java ├── pom.xml └── qatest ├── pom.xml └── src └── test ├── java └── com │ └── googlecode │ └── excavator │ └── demo │ └── qatest │ ├── BaseTestCase.java │ └── service │ ├── AsyncLogServiceTestCase.java │ ├── LogServiceTestCase.java │ └── TimeServiceTestCase.java └── resources ├── excavator.properties ├── log4j.properties └── spring ├── applicationContext.xml ├── log-service-spring.xml └── time-service-spring.xml /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | qatest/qatest.iml 3 | core/core.iml 4 | client/client.iml 5 | common/common.iml 6 | common/target 7 | target 8 | core/target 9 | client/target 10 | qatest/target 11 | .idea 12 | excavator-demo.iml -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldmanpushcart/excavator-demo/288fb2f516e9053b873b96d4524e9d7216082139/README.md -------------------------------------------------------------------------------- /client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.googlecode.excavator.demo 8 | excavator-demo 9 | 1.0.0-SNAPSHOT 10 | 11 | pom 12 | com.googlecode.excavator.demo 13 | client 14 | ${excavator-demo-client.version} 15 | client 16 | http://maven.apache.org 17 | 18 | UTF-8 19 | 20 | 21 | 22 | com.googlecode.excavator.demo 23 | common 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /client/src/main/java/com/googlecode/excavator/demo/client/AsyncLogServiceClient.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.client; 2 | 3 | import com.googlecode.excavator.demo.common.LogException; 4 | import com.googlecode.excavator.demo.common.domain.ResultDO; 5 | 6 | import java.io.Serializable; 7 | import java.util.concurrent.BlockingQueue; 8 | import java.util.concurrent.LinkedBlockingQueue; 9 | import java.util.concurrent.atomic.AtomicBoolean; 10 | import java.util.logging.Level; 11 | import java.util.logging.Logger; 12 | 13 | import static java.lang.String.format; 14 | import static java.util.logging.Level.INFO; 15 | import static java.util.logging.Level.WARNING; 16 | 17 | /** 18 | * 日志信息 19 | */ 20 | class Log { 21 | 22 | private final Level level; 23 | private final String format; 24 | private final Serializable[] args; 25 | 26 | public Log(Level level, String format, Serializable[] args) { 27 | this.level = level; 28 | this.format = format; 29 | this.args = args; 30 | } 31 | 32 | public Level getLevel() { 33 | return level; 34 | } 35 | 36 | public String getFormat() { 37 | return format; 38 | } 39 | 40 | public Serializable[] getArgs() { 41 | return args; 42 | } 43 | } 44 | 45 | /** 46 | * 异步日志服务客户端 47 | */ 48 | public class AsyncLogServiceClient extends LogServiceClient { 49 | 50 | private final Logger logger = Logger.getAnonymousLogger(); 51 | 52 | /* 53 | * 初始化标记 54 | */ 55 | private final AtomicBoolean isInit = new AtomicBoolean(false); 56 | 57 | /* 58 | * HA日志队列 59 | */ 60 | private final BlockingQueue logQueue; 61 | 62 | public AsyncLogServiceClient() { 63 | logQueue = new LinkedBlockingQueue(); 64 | } 65 | 66 | /** 67 | * 记录INFO级别日志 68 | * 69 | * @param format 日志模版(同String.format()) 70 | * @param args 日志参数 71 | * @return 记录日志是否成功 72 | * @throws LogException 记录日志发生异常 73 | */ 74 | @Override 75 | public ResultDO info(String format, Serializable... args) throws LogException { 76 | 77 | checkInit(); 78 | 79 | while (!logQueue.offer(new Log(INFO, format, args))) ; 80 | final ResultDO result = new ResultDO(); 81 | result.setSuccess(true); 82 | return result; 83 | } 84 | 85 | /** 86 | * 检查参数配置 87 | */ 88 | private void checkInit() { 89 | 90 | if (!isInit.get()) { 91 | throw new IllegalStateException("AsyncLogServiceClient was not init yet."); 92 | } 93 | 94 | } 95 | 96 | /** 97 | * 初始化异步客户端 98 | */ 99 | public void init() { 100 | 101 | if (!isInit.compareAndSet(false, true)) { 102 | return; 103 | } 104 | 105 | final Thread logWriterDaemon = new Thread(new Runnable() { 106 | 107 | @Override 108 | public void run() { 109 | logger.info("log-writer-daemon started."); 110 | while (true) { 111 | 112 | try { 113 | final Log log = logQueue.take(); 114 | 115 | while (true) { 116 | 117 | try { 118 | 119 | final ResultDO result; 120 | if (log.getLevel().equals(INFO)) { 121 | result = AsyncLogServiceClient.super.info(log.getFormat(), log.getArgs()); 122 | } else { 123 | if (logger.isLoggable(WARNING)) { 124 | logger.log(WARNING, format("log level[%s] not support, ignore this log", log.getLevel())); 125 | } 126 | continue; 127 | } 128 | 129 | if (!result.isSuccess()) { 130 | if (logger.isLoggable(WARNING)) { 131 | logger.log(WARNING, format("log failed, errorCode=%s", result.getErrorCode())); 132 | } 133 | } 134 | 135 | break; 136 | } 137 | 138 | // got an Exception need re-try 139 | catch (Exception e) { 140 | if (logger.isLoggable(WARNING)) { 141 | logger.log(WARNING, "log failed, need re-try", e); 142 | } 143 | continue; 144 | } 145 | 146 | } 147 | 148 | 149 | } catch (InterruptedException e) { 150 | Thread.currentThread().interrupt(); 151 | } 152 | 153 | }//while 154 | 155 | } 156 | 157 | }, "log-writer-daemon"); 158 | 159 | logWriterDaemon.setDaemon(true); 160 | logWriterDaemon.start(); 161 | 162 | } 163 | 164 | } 165 | -------------------------------------------------------------------------------- /client/src/main/java/com/googlecode/excavator/demo/client/LogServiceClient.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.client; 2 | 3 | import com.googlecode.excavator.demo.common.LogException; 4 | import com.googlecode.excavator.demo.common.domain.ResultDO; 5 | import com.googlecode.excavator.demo.common.service.LogService; 6 | 7 | import java.io.Serializable; 8 | 9 | /** 10 | * 日志服务客户端 11 | */ 12 | public class LogServiceClient { 13 | 14 | private LogService logService; 15 | 16 | /** 17 | * 记录INFO级别日志 18 | * 19 | * @param format 日志模版(同String.format()) 20 | * @param args 日志参数 21 | * @return 记录日志是否成功 22 | * @throws com.googlecode.excavator.demo.common.LogException 记录日志发生异常 23 | */ 24 | public ResultDO info(String format, Serializable... args) throws LogException { 25 | return logService.info(format, args); 26 | } 27 | 28 | public void setLogService(LogService logService) { 29 | this.logService = logService; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /client/src/main/java/com/googlecode/excavator/demo/client/TimeServiceClient.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.client; 2 | 3 | import com.googlecode.excavator.demo.common.TimeException; 4 | import com.googlecode.excavator.demo.common.domain.ResultDO; 5 | import com.googlecode.excavator.demo.common.service.TimeService; 6 | 7 | /** 8 | * Created by vlinux on 14/12/2. 9 | */ 10 | public class TimeServiceClient { 11 | 12 | private TimeService timeService; 13 | 14 | public ResultDO now(String format) throws TimeException { 15 | return timeService.now(format); 16 | } 17 | 18 | public void setTimeService(TimeService timeService) { 19 | this.timeService = timeService; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.googlecode.excavator.demo 8 | excavator-demo 9 | 1.0.0-SNAPSHOT 10 | 11 | pom 12 | com.googlecode.excavator.demo 13 | common 14 | ${excavator-demo-common.version} 15 | common 16 | http://maven.apache.org 17 | 18 | UTF-8 19 | 20 | 21 | -------------------------------------------------------------------------------- /common/src/main/java/com/googlecode/excavator/demo/common/ErrorCodeConstants.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.common; 2 | 3 | /** 4 | * define ErrorCode constants 5 | */ 6 | public class ErrorCodeConstants { 7 | 8 | /** 9 | * 错误的时间格式 10 | */ 11 | public static final String ERROR_CODE_ILLEGAL_TIME_FORMAT = "ERROR_CODE_ILLEGAL_TIME_FORMAT"; 12 | 13 | /** 14 | * 错误的字符串模版 15 | */ 16 | public static final String ERROR_CODE_ILLEGAL_STRING_FORMAT = "ERROR_CODE_ILLEGAL_STRING_FORMAT"; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /common/src/main/java/com/googlecode/excavator/demo/common/LogException.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.common; 2 | 3 | /** 4 | * 日志异常 5 | */ 6 | public class LogException extends Exception { 7 | 8 | private static final long serialVersionUID = 8662590152085441700L; 9 | 10 | public LogException(String message, Throwable cause) { 11 | super(message, cause); 12 | } 13 | 14 | public LogException(String message) { 15 | super(message); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /common/src/main/java/com/googlecode/excavator/demo/common/TimeException.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.common; 2 | 3 | /** 4 | * Time Service's Exception 5 | */ 6 | public class TimeException extends Exception { 7 | 8 | private static final long serialVersionUID = -7440950710204474476L; 9 | 10 | public TimeException(String msg, Throwable cause) { 11 | super(msg, cause); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /common/src/main/java/com/googlecode/excavator/demo/common/domain/ResultDO.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.common.domain; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * 返回对象 7 | * Created by vlinux on 14/12/2. 8 | */ 9 | public class ResultDO implements Serializable { 10 | 11 | private static final long serialVersionUID = -3518453422409559359L; 12 | 13 | /* 14 | * 返回模型 15 | */ 16 | private T module; 17 | 18 | /* 19 | * 错误码 20 | */ 21 | private String errorCode; 22 | 23 | /* 24 | * 是否业务成功 25 | */ 26 | private boolean success; 27 | 28 | public T getModule() { 29 | return module; 30 | } 31 | 32 | public void setModule(T module) { 33 | this.module = module; 34 | } 35 | 36 | public String getErrorCode() { 37 | return errorCode; 38 | } 39 | 40 | public void setErrorCode(String errorCode) { 41 | this.errorCode = errorCode; 42 | } 43 | 44 | public boolean isSuccess() { 45 | return success; 46 | } 47 | 48 | public void setSuccess(boolean success) { 49 | this.success = success; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /common/src/main/java/com/googlecode/excavator/demo/common/service/LogService.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.common.service; 2 | 3 | import com.googlecode.excavator.demo.common.LogException; 4 | import com.googlecode.excavator.demo.common.domain.ResultDO; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * 日志服务 10 | */ 11 | public interface LogService { 12 | 13 | /** 14 | * 记录INFO级别日志 15 | * 16 | * @param format 日志模版(同String.format()) 17 | * @param args 日志参数 18 | * @return 记录日志是否成功 19 | * @throws LogException 记录日志发生异常 20 | */ 21 | ResultDO info(String format, Serializable... args) throws LogException; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /common/src/main/java/com/googlecode/excavator/demo/common/service/TimeService.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.common.service; 2 | 3 | import com.googlecode.excavator.demo.common.TimeException; 4 | import com.googlecode.excavator.demo.common.domain.ResultDO; 5 | 6 | /** 7 | * 时间服务 8 | */ 9 | public interface TimeService { 10 | 11 | /** 12 | * 获取当前服务器时间(格式化) 13 | * 14 | * @param format 日期格式化模版 15 | * @return 返回被格式化后的当前服务器时间字符串 16 | * @throws TimeException 发生内部错误抛出异常 17 | */ 18 | ResultDO now(String format) throws TimeException; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.googlecode.excavator.demo 8 | excavator-demo 9 | 1.0.0-SNAPSHOT 10 | 11 | jar 12 | com.googlecode.excavator.demo 13 | core 14 | ${excavator-demo-core.version} 15 | core 16 | http://maven.apache.org 17 | 18 | UTF-8 19 | 20 | 21 | 22 | com.googlecode.excavator.demo 23 | common 24 | 25 | 26 | com.googlecode.excavator 27 | excavator 28 | 29 | 30 | org.springframework 31 | spring 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /core/src/main/java/com/googlecode/excavator/demo/core/ErrorCodeException.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.core; 2 | 3 | /** 4 | * 错误码异常 5 | */ 6 | public class ErrorCodeException extends Exception { 7 | 8 | /* 9 | * 错误码 10 | */ 11 | private final String errorCode; 12 | 13 | /** 14 | * 错误码异常 15 | * 16 | * @param errorCode 错误码 17 | */ 18 | public ErrorCodeException(String errorCode) { 19 | this.errorCode = errorCode; 20 | } 21 | 22 | /** 23 | * 获取错误码 24 | * 25 | * @return 26 | */ 27 | public String getErrorCode() { 28 | return errorCode; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /core/src/main/java/com/googlecode/excavator/demo/core/manager/FormatManager.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.core.manager; 2 | 3 | import com.googlecode.excavator.demo.core.ErrorCodeException; 4 | 5 | /** 6 | * 格式化 7 | */ 8 | public interface FormatManager { 9 | 10 | /** 11 | * 对字符串进行格式化 12 | * 13 | * @param format 格式化模版 14 | * @param args 参数列表 15 | * @return 格式化后的字符串 16 | * @throws ErrorCodeException 格式化失败抛出错误码 17 | */ 18 | String format(String format, Object... args) throws ErrorCodeException; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /core/src/main/java/com/googlecode/excavator/demo/core/manager/impl/FormatManagerImpl.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.core.manager.impl; 2 | 3 | import com.googlecode.excavator.demo.core.ErrorCodeException; 4 | import com.googlecode.excavator.demo.core.manager.FormatManager; 5 | 6 | import java.util.logging.Logger; 7 | 8 | import static com.googlecode.excavator.demo.common.ErrorCodeConstants.ERROR_CODE_ILLEGAL_STRING_FORMAT; 9 | import static java.util.logging.Level.WARNING; 10 | 11 | /** 12 | * Created by vlinux on 14/12/6. 13 | */ 14 | public class FormatManagerImpl implements FormatManager { 15 | 16 | private Logger logger = Logger.getAnonymousLogger(); 17 | 18 | @Override 19 | public String format(String format, Object... args) throws ErrorCodeException { 20 | 21 | try { 22 | return String.format(format, args); 23 | } catch(Exception e) { 24 | if( logger.isLoggable(WARNING) ) { 25 | logger.log(WARNING, String.format("format string[%s] failed.", format), e); 26 | } 27 | throw new ErrorCodeException(ERROR_CODE_ILLEGAL_STRING_FORMAT); 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /core/src/main/java/com/googlecode/excavator/demo/core/service/impl/LogServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.core.service.impl; 2 | 3 | import com.googlecode.excavator.demo.common.LogException; 4 | import com.googlecode.excavator.demo.common.domain.ResultDO; 5 | import com.googlecode.excavator.demo.common.service.LogService; 6 | import com.googlecode.excavator.demo.core.ErrorCodeException; 7 | import com.googlecode.excavator.demo.core.manager.FormatManager; 8 | import org.springframework.beans.factory.DisposableBean; 9 | import org.springframework.beans.factory.InitializingBean; 10 | 11 | import java.io.IOException; 12 | import java.io.RandomAccessFile; 13 | import java.io.Serializable; 14 | 15 | /** 16 | * 日志服务默认实现 17 | */ 18 | public class LogServiceImpl implements LogService, InitializingBean, DisposableBean { 19 | 20 | /* 21 | * 日志文件路径 22 | */ 23 | private String logFilePath; 24 | 25 | private FormatManager formatManager; 26 | 27 | /* 28 | * 日志文件句柄 29 | */ 30 | private RandomAccessFile logFile; 31 | 32 | @Override 33 | public ResultDO info(String format, Serializable... args) throws LogException { 34 | 35 | final ResultDO result = new ResultDO(); 36 | 37 | try { 38 | 39 | final String log = formatManager.format(format, args); 40 | logFile.writeBytes(log+"\n"); 41 | result.setSuccess(true); 42 | 43 | } 44 | 45 | // 发生可处理内部错误,需要转成错误码 46 | catch (ErrorCodeException ece) { 47 | result.setSuccess(false); 48 | result.setErrorCode(ece.getErrorCode()); 49 | } 50 | 51 | // 发生不可处理内部错误,需要外部重试 52 | catch (IOException e) { 53 | throw new LogException("log failed.", e); 54 | } 55 | 56 | return result; 57 | } 58 | 59 | @Override 60 | public void afterPropertiesSet() throws Exception { 61 | if (null == logFilePath 62 | || logFilePath.length() == 0) { 63 | throw new IllegalArgumentException("logFilePath was empty."); 64 | } 65 | this.logFile = new RandomAccessFile(logFilePath, "rw"); 66 | logFile.seek(logFile.length()); 67 | 68 | } 69 | 70 | @Override 71 | public void destroy() throws Exception { 72 | 73 | if (null != logFile) { 74 | logFile.close(); 75 | } 76 | 77 | } 78 | 79 | public void setLogFilePath(String logFilePath) { 80 | this.logFilePath = logFilePath; 81 | } 82 | 83 | public void setFormatManager(FormatManager formatManager) { 84 | this.formatManager = formatManager; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /core/src/main/java/com/googlecode/excavator/demo/core/service/impl/TimeServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.core.service.impl; 2 | 3 | import com.googlecode.excavator.demo.common.TimeException; 4 | import com.googlecode.excavator.demo.common.domain.ResultDO; 5 | import com.googlecode.excavator.demo.common.service.TimeService; 6 | 7 | import java.text.SimpleDateFormat; 8 | import java.util.Date; 9 | import java.util.logging.Level; 10 | import java.util.logging.Logger; 11 | 12 | import static com.googlecode.excavator.demo.common.ErrorCodeConstants.ERROR_CODE_ILLEGAL_TIME_FORMAT; 13 | import static java.lang.String.format; 14 | 15 | /** 16 | * 时间服务默认实现 17 | */ 18 | public class TimeServiceImpl implements TimeService { 19 | 20 | private Logger logger = Logger.getAnonymousLogger(); 21 | 22 | @Override 23 | public ResultDO now(String format) throws TimeException { 24 | 25 | final ResultDO result = new ResultDO(); 26 | 27 | try { 28 | final SimpleDateFormat sdf = new SimpleDateFormat(format); 29 | final String resultStr = sdf.format(new Date()); 30 | result.setSuccess(true); 31 | result.setModule(resultStr); 32 | } catch (Exception e) { 33 | if (logger.isLoggable(Level.WARNING)) { 34 | logger.log(Level.WARNING, format("error@now, format=%s", format), e); 35 | } 36 | result.setErrorCode(ERROR_CODE_ILLEGAL_TIME_FORMAT); 37 | result.setSuccess(false); 38 | } 39 | 40 | return result; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.googlecode.excavator.demo 7 | excavator-demo 8 | 1.0.0-SNAPSHOT 9 | pom 10 | 11 | excavator-demo 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.0.0-SNAPSHOT 17 | 1.0.0-SNAPSHOT 18 | 1.0.0-SNAPSHOT 19 | 20 | 21 | 22 | 23 | mybatis-snapshot 24 | Sonatype Snapshot Repository 25 | https://oss.sonatype.org/content/repositories/snapshots 26 | 27 | 28 | 29 | 30 | 31 | 32 | com.googlecode.excavator 33 | excavator 34 | 1.1.5-SNAPSHOT 35 | 36 | 37 | org.springframework 38 | spring 39 | 2.5.6 40 | 41 | 42 | com.googlecode.excavator.demo 43 | common 44 | ${excavator-demo-common.version} 45 | 46 | 47 | com.googlecode.excavator.demo 48 | client 49 | ${excavator-demo-client.version} 50 | 51 | 52 | com.googlecode.excavator.demo 53 | core 54 | ${excavator-demo-core.version} 55 | 56 | 57 | 58 | 59 | 60 | 61 | common 62 | client 63 | core 64 | qatest 65 | 66 | -------------------------------------------------------------------------------- /qatest/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.googlecode.excavator.demo 8 | excavator-demo 9 | 1.0.0-SNAPSHOT 10 | 11 | com.googlecode.excavator.demo 12 | qatest 13 | 1.0.0-SNAPSHOT 14 | qatest 15 | http://maven.apache.org 16 | 17 | UTF-8 18 | 19 | 20 | 21 | junit 22 | junit 23 | 4.4 24 | test 25 | 26 | 27 | org.springframework 28 | spring-test 29 | test 30 | 2.5.6 31 | 32 | 33 | com.googlecode.excavator.demo 34 | client 35 | 36 | 37 | com.googlecode.excavator.demo 38 | core 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /qatest/src/test/java/com/googlecode/excavator/demo/qatest/BaseTestCase.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.qatest; 2 | 3 | import com.googlecode.excavator.demo.common.domain.ResultDO; 4 | import junit.framework.Assert; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.test.context.ContextConfiguration; 8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 | 10 | /** 11 | * Created by vlinux on 14/12/6. 12 | */ 13 | @RunWith(SpringJUnit4ClassRunner.class) 14 | @ContextConfiguration(locations = {"classpath*:spring/applicationContext.xml"}) 15 | public class BaseTestCase { 16 | 17 | @Test 18 | public void _test() { 19 | Assert.assertTrue(true); 20 | } 21 | 22 | 23 | /** 24 | * 断言返回值成功 25 | * 26 | * @param result 被断言返回值 27 | */ 28 | protected void assertSuccess(ResultDO result) { 29 | Assert.assertNotNull(result); 30 | Assert.assertTrue(result.isSuccess()); 31 | Assert.assertNull(result.getErrorCode()); 32 | } 33 | 34 | /** 35 | * 断言返回值失败 36 | * 37 | * @param result 被断言返回值 38 | */ 39 | protected void assertFailed(ResultDO result) { 40 | Assert.assertNotNull(result); 41 | Assert.assertFalse(result.isSuccess()); 42 | Assert.assertNotNull(result.getErrorCode()); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /qatest/src/test/java/com/googlecode/excavator/demo/qatest/service/AsyncLogServiceTestCase.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.qatest.service; 2 | 3 | import com.googlecode.excavator.demo.client.AsyncLogServiceClient; 4 | import com.googlecode.excavator.demo.common.domain.ResultDO; 5 | import com.googlecode.excavator.demo.qatest.BaseTestCase; 6 | import org.junit.Test; 7 | 8 | import javax.annotation.Resource; 9 | 10 | /** 11 | * Async LogService TestCase 12 | */ 13 | public class AsyncLogServiceTestCase extends BaseTestCase { 14 | 15 | @Resource 16 | private AsyncLogServiceClient asyncLogServiceClient; 17 | 18 | 19 | @Test 20 | public void test_log_for_success() throws Exception { 21 | 22 | final String logFormat = "log info message[%s:%s!] for test."; 23 | final String words = "hello"; 24 | final String names = "world"; 25 | 26 | final ResultDO result = asyncLogServiceClient.info(logFormat, words, names); 27 | assertSuccess(result); 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /qatest/src/test/java/com/googlecode/excavator/demo/qatest/service/LogServiceTestCase.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.qatest.service; 2 | 3 | import com.googlecode.excavator.demo.client.LogServiceClient; 4 | import com.googlecode.excavator.demo.common.domain.ResultDO; 5 | import com.googlecode.excavator.demo.qatest.BaseTestCase; 6 | import junit.framework.Assert; 7 | import org.junit.Test; 8 | 9 | import javax.annotation.Resource; 10 | 11 | import static com.googlecode.excavator.demo.common.ErrorCodeConstants.ERROR_CODE_ILLEGAL_STRING_FORMAT; 12 | 13 | /** 14 | * LogService TestCase 15 | */ 16 | public class LogServiceTestCase extends BaseTestCase { 17 | 18 | @Resource 19 | private LogServiceClient logServiceClient; 20 | 21 | @Test 22 | public void test_log_for_success() throws Exception { 23 | 24 | final String logFormat = "log info message[%s:%s!] for test."; 25 | final String words = "hello"; 26 | final String names = "world"; 27 | 28 | final ResultDO result = logServiceClient.info(logFormat, words, names); 29 | assertSuccess(result); 30 | 31 | } 32 | 33 | @Test 34 | public void test_log_for_failed() throws Exception { 35 | 36 | final ResultDO result = logServiceClient.info("this is a test %@s", "a", "b"); 37 | assertFailed(result); 38 | Assert.assertEquals(result.getErrorCode(), ERROR_CODE_ILLEGAL_STRING_FORMAT); 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /qatest/src/test/java/com/googlecode/excavator/demo/qatest/service/TimeServiceTestCase.java: -------------------------------------------------------------------------------- 1 | package com.googlecode.excavator.demo.qatest.service; 2 | 3 | import com.googlecode.excavator.demo.client.TimeServiceClient; 4 | import com.googlecode.excavator.demo.common.domain.ResultDO; 5 | import com.googlecode.excavator.demo.qatest.BaseTestCase; 6 | import junit.framework.Assert; 7 | import org.junit.Test; 8 | 9 | import javax.annotation.Resource; 10 | 11 | import static com.googlecode.excavator.demo.common.ErrorCodeConstants.ERROR_CODE_ILLEGAL_TIME_FORMAT; 12 | 13 | /** 14 | * TimeService TestCase 15 | */ 16 | public class TimeServiceTestCase extends BaseTestCase { 17 | 18 | @Resource 19 | private TimeServiceClient timeServiceClient; 20 | 21 | @Test 22 | public void test_for_success() throws Exception { 23 | 24 | final ResultDO result = timeServiceClient.now("yyyy-MM-dd"); 25 | assertSuccess(result); 26 | Assert.assertTrue(result.getModule().matches("\\d{4}-\\d{2}-\\d{2}") ); 27 | 28 | } 29 | 30 | 31 | @Test 32 | public void test_for_failed() throws Exception { 33 | 34 | final ResultDO result = timeServiceClient.now("fuck you!"); 35 | assertFailed(result); 36 | Assert.assertEquals(result.getErrorCode(), ERROR_CODE_ILLEGAL_TIME_FORMAT); 37 | 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /qatest/src/test/resources/excavator.properties: -------------------------------------------------------------------------------- 1 | excavator.appname=excavator_demo 2 | excavator.zookeeper.servers=127.0.0.1:2181 3 | excavator.zookeeper.sessionTimeout=60000 4 | excavator.zookeeper.connectTimeout=500 5 | 6 | 7 | excavator.provider.port=3659 8 | excavator.provider.workers=250 9 | 10 | 11 | excavator.consumer.connectTimeout=500 12 | excavator.monitor.enable=true 13 | excavator.serializer.name=hessian 14 | excavator.profiler.enable=true 15 | excavator.profiler.limit=1 -------------------------------------------------------------------------------- /qatest/src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,CONSOLE 2 | 3 | # CONSOLE 4 | log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 5 | log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%t] %-5p %C{1}@%M:%m%n 7 | -------------------------------------------------------------------------------- /qatest/src/test/resources/spring/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /qatest/src/test/resources/spring/log-service-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /qatest/src/test/resources/spring/time-service-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | --------------------------------------------------------------------------------