Merge pull request #166 from earayu/test-case

bugfix&test case. #165
This commit is contained in:
hengyunabc 2018-09-27 16:13:52 +08:00 committed by GitHub
commit 97518b3d60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 157 additions and 7 deletions

View File

@ -1,20 +1,27 @@
package com.taobao.arthas.core.util;
import java.util.List;
import com.taobao.arthas.core.shell.cli.CliToken;
/**
* tokens处理的辅助类
* tokenizer helper
*
* @author gehui 2017年7月27日 上午11:39:56
* @author gehui 2017-07-27 11:39:56
*/
public class TokenUtils {
/**
* find the first text token
* @param tokens
* @return
*/
public static CliToken findFirstTextToken(List<CliToken> tokens) {
if(tokens==null || tokens.isEmpty()){
return null;
}
CliToken first = null;
for (CliToken token : tokens) {
if (token.isText()) {
if (token!=null && token.isText()) {
first = token;
break;
}
@ -22,20 +29,37 @@ public class TokenUtils {
return first;
}
/**
* find the last text token
* @param tokens
* @return
*/
public static CliToken findLastTextToken(List<CliToken> tokens) {
for (int i = tokens.size() - 1; i > 0; i--) {
if(tokens==null || tokens.isEmpty()){
return null;
}
//#165
for (int i = tokens.size() - 1; i >= 0; i--) {
CliToken token = tokens.get(i);
if (token.isText()) {
if (token!=null && token.isText()) {
return token;
}
}
return null;
}
/**
* find the second text token's text
* @param tokens
* @return
*/
public static String findSecondTokenText(List<CliToken> tokens) {
if(tokens==null || tokens.isEmpty()){
return null;
}
boolean first = true;
for (CliToken token : tokens) {
if (token.isText()) {
if (token!=null && token.isText()) {
if (first) {
first = false;
} else {

View File

@ -0,0 +1,126 @@
package com.taobao.arthas.core.util;
import com.taobao.arthas.core.shell.cli.CliToken;
import com.taobao.arthas.core.shell.cli.impl.CliTokenImpl;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author earayu
*/
public class TokenUtilsTest {
private List<CliToken> newCliTokenList(CliToken ... tokens){
List<CliToken> cliTokens = new ArrayList<CliToken>();
if(tokens!=null) {
Collections.addAll(cliTokens, tokens);
}
return cliTokens;
}
@Test
public void testFindFirstTextToken(){
CliToken textCliToken = new CliTokenImpl(true,"textCliToken");
CliToken nonTextCliToken = new CliTokenImpl(false,"nonTextCliToken");
//null list
Assert.assertEquals(null, TokenUtils.findFirstTextToken(null));
//empty list
Assert.assertEquals(null, TokenUtils.findFirstTextToken(new ArrayList<CliToken>()));
//list with null value
Assert.assertEquals(null,
TokenUtils.findFirstTextToken(newCliTokenList(new CliToken[]{null})));
Assert.assertEquals(textCliToken,
TokenUtils.findFirstTextToken(newCliTokenList(new CliToken[]{null, textCliToken})));
Assert.assertEquals(textCliToken,
TokenUtils.findFirstTextToken(newCliTokenList(new CliToken[]{null, nonTextCliToken, textCliToken})));
Assert.assertEquals(textCliToken,
TokenUtils.findFirstTextToken(newCliTokenList(new CliToken[]{nonTextCliToken, null, textCliToken})));
//list with normal inputs
Assert.assertEquals(textCliToken,
TokenUtils.findFirstTextToken(newCliTokenList(new CliToken[]{textCliToken})));
Assert.assertEquals(textCliToken,
TokenUtils.findFirstTextToken(newCliTokenList(new CliToken[]{nonTextCliToken, textCliToken})));
Assert.assertEquals(textCliToken,
TokenUtils.findFirstTextToken(newCliTokenList(new CliToken[]{textCliToken, nonTextCliToken})));
}
@Test
public void testFindLastTextToken(){
CliToken textCliToken = new CliTokenImpl(true,"textCliToken");
CliToken nonTextCliToken = new CliTokenImpl(false,"nonTextCliToken");
//null list
Assert.assertEquals(null, TokenUtils.findLastTextToken(null));
//empty list
Assert.assertEquals(null, TokenUtils.findLastTextToken(new ArrayList<CliToken>()));
//list with null value
Assert.assertEquals(null,
TokenUtils.findLastTextToken(newCliTokenList(new CliToken[]{null})));
Assert.assertEquals(textCliToken,
TokenUtils.findLastTextToken(newCliTokenList(new CliToken[]{null, textCliToken})));
Assert.assertEquals(textCliToken,
TokenUtils.findLastTextToken(newCliTokenList(new CliToken[]{null, nonTextCliToken, textCliToken})));
Assert.assertEquals(textCliToken,
TokenUtils.findLastTextToken(newCliTokenList(new CliToken[]{nonTextCliToken, null, textCliToken})));
Assert.assertEquals(textCliToken,
TokenUtils.findLastTextToken(newCliTokenList(new CliToken[]{textCliToken, null, nonTextCliToken})));
//list with normal inputs
Assert.assertEquals(textCliToken,
TokenUtils.findLastTextToken(newCliTokenList(new CliToken[]{textCliToken})));
Assert.assertEquals(null,
TokenUtils.findLastTextToken(newCliTokenList(new CliToken[]{nonTextCliToken})));
Assert.assertEquals(textCliToken,
TokenUtils.findLastTextToken(newCliTokenList(new CliToken[]{nonTextCliToken, textCliToken})));
Assert.assertEquals(textCliToken,
TokenUtils.findLastTextToken(newCliTokenList(new CliToken[]{textCliToken, nonTextCliToken})));
}
@Test
public void testFindSecondTextToken(){
CliToken textCliToken = new CliTokenImpl(true,"textCliToken");
CliToken nonTextCliToken = new CliTokenImpl(false,"nonTextCliToken");
//null list
Assert.assertEquals(null, TokenUtils.findSecondTokenText(null));
//empty list
Assert.assertEquals(null, TokenUtils.findSecondTokenText(new ArrayList<CliToken>()));
//list with null value
Assert.assertEquals(null,
TokenUtils.findSecondTokenText(newCliTokenList(new CliToken[]{null})));
Assert.assertEquals(null,
TokenUtils.findSecondTokenText(newCliTokenList(new CliToken[]{null, textCliToken})));
Assert.assertEquals(null,
TokenUtils.findSecondTokenText(newCliTokenList(new CliToken[]{null, nonTextCliToken, textCliToken})));
Assert.assertEquals(textCliToken.value(),
TokenUtils.findSecondTokenText(newCliTokenList(new CliToken[]{null, nonTextCliToken, textCliToken, textCliToken})));
//list with normal inputs
Assert.assertEquals(null,
TokenUtils.findSecondTokenText(newCliTokenList(new CliToken[]{textCliToken})));
Assert.assertEquals(null,
TokenUtils.findSecondTokenText(newCliTokenList(new CliToken[]{nonTextCliToken})));
Assert.assertEquals(null,
TokenUtils.findSecondTokenText(newCliTokenList(new CliToken[]{nonTextCliToken, textCliToken})));
Assert.assertEquals(textCliToken.value(),
TokenUtils.findSecondTokenText(newCliTokenList(new CliToken[]{textCliToken, nonTextCliToken, textCliToken})));
}
}