add tests for hBasePlugin

add tests for hBasePlugin
This commit is contained in:
coderfengyun 2014-04-25 14:56:04 +08:00
parent 11a94e6288
commit 1f708ad1a5
7 changed files with 148 additions and 39 deletions

View File

@ -23,13 +23,13 @@ public class HBasePlugin {
private SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyyMMddhhmmssS");
List<String> labelList;
{
labelList = new ArrayList<String>();
labelList.add("dataType");
labelList.add("generator");
labelList.add("min");
labelList.add("max");
public static List<String> LABEL_LIST;
static {
LABEL_LIST = new ArrayList<String>();
LABEL_LIST.add("dataType");
LABEL_LIST.add("generator");
LABEL_LIST.add("min");
LABEL_LIST.add("max");
}
@Constructor
@ -39,23 +39,22 @@ public class HBasePlugin {
@Parameter(value = "voltage", type = SupportTypes.Table) String voltage,
@Parameter(value = "electricCurrent", type = SupportTypes.Table) String electricCurrent,
@Parameter(value = "electricity", type = SupportTypes.Table) String electricity) {
Row voltageRow = Table.buildTable(voltage, this.labelList).getRow(0);
Row voltageRow = Table.buildTable(voltage, LABEL_LIST).getRow(0);
this.voltageGenerator = RecordGenerator
.buildGeneratorWithRow(voltageRow);
Row electricCurrentRow = Table.buildTable(electricCurrent,
this.labelList).getRow(0);
Row electricCurrentRow = Table.buildTable(electricCurrent, LABEL_LIST)
.getRow(0);
this.electricCurrentGenerator = RecordGenerator
.buildGeneratorWithRow(electricCurrentRow);
Row electricityRow = Table.buildTable(electricity, this.labelList)
.getRow(0);
Row electricityRow = Table.buildTable(electricity, LABEL_LIST).getRow(0);
this.electricityGenerator = RecordGenerator
.buildGeneratorWithRow(electricityRow);
}
@Behavior("Send")
public ElectReturn send(
@Parameter(value = "beginTime", type = SupportTypes.Field) String beginDay,
@Parameter(value = "endTime", type = SupportTypes.Field) String endDay,
@Parameter(value = "beginDay", type = SupportTypes.Field) String beginDay,
@Parameter(value = "endDay", type = SupportTypes.Field) String endDay,
@Parameter(value = "beginUser", type = SupportTypes.Field) String beginUser,
@Parameter(value = "endUser", type = SupportTypes.Field) String endUser) {
Date beginDatetime = new Date(0), endDatetime = new Date(0);

View File

@ -1,22 +1,35 @@
package org.bench4q.agent.plugin.basic.hbase;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
import org.bench4q.agent.plugin.TypeConverter;
import org.bench4q.agent.utils.types.Table.Row;
import org.bench4q.share.exception.Bench4QRunTimeException;
public abstract class RecordGenerator {
protected String dataType;
protected Double minValue;
protected Double maxValue;
private TypeConverter typeConverter = new TypeConverter();
Set<String> legalDataTypes = new LinkedHashSet<String>();
{
legalDataTypes.add("int");
legalDataTypes.add("double");
legalDataTypes.add("long");
}
private RecordGenerator(String dataType, String minValue, String maxValue) {
this.dataType = dataType;
this.dataType = dataType.toLowerCase();
if (!this.legalDataTypes.contains(this.dataType)) {
throw new Bench4QRunTimeException(
"The input dataType is not legal, input dataType is : "
+ dataType);
}
this.minValue = Double.valueOf(minValue);
this.maxValue = Double.valueOf(maxValue);
if (this.maxValue < this.minValue) {
throw new RuntimeException("The minValue LT the maxValue");
throw new Bench4QRunTimeException("The minValue LT the maxValue");
}
}
@ -26,11 +39,19 @@ public abstract class RecordGenerator {
return Double.valueOf(this.getValue());
}
public abstract void reset();
protected String convertAsDataType(Double value) {
return (String) this.typeConverter.convert(value, this.dataType);
protected String convertAsDataType(String toBeConverted) {
if (this.dataType.equalsIgnoreCase("int")) {
return String.valueOf(Double.valueOf(toBeConverted).intValue());
} else if (this.dataType.equalsIgnoreCase("double")) {
return toBeConverted;
} else if (this.dataType.equalsIgnoreCase("long")) {
return String.valueOf(Double.valueOf(toBeConverted).longValue());
} else {
throw new Bench4QRunTimeException("The dataType is not legal");
}
}
public abstract void reset();
/**
* This function build generator as the @generator says, but if it's not
@ -73,17 +94,16 @@ public abstract class RecordGenerator {
@Override
public String getValue() {
Double value = this.random.nextDouble();
Double doublePart = value - value.longValue();
value = doublePart
+ (value.intValue() % (maxValue.intValue() - minValue
.intValue())) + minValue;
return convertAsDataType(value);
Double doublePart = this.random.nextDouble();
int maxValueInt = maxValue.intValue();
int intPart = this.random.nextInt(maxValueInt);
return convertAsDataType(String.valueOf(doublePart
+ (intPart % (maxValueInt - minValue.intValue()))
+ minValue));
}
@Override
public void reset() {
}
}
@ -100,7 +120,7 @@ public abstract class RecordGenerator {
* Math.sin(this.currentAngle)
+ (this.maxValue + this.minValue) / 2;
this.currentAngle += Math.PI / 144;
return convertAsDataType(value);
return convertAsDataType(String.valueOf(value));
}
@Override
@ -122,7 +142,7 @@ public abstract class RecordGenerator {
* Math.cos(this.currentAngle)
+ (this.maxValue + this.minValue) / 2;
this.currentAngle += Math.PI / 144;
return convertAsDataType(value);
return convertAsDataType(String.valueOf(value));
}
@Override

View File

@ -1,4 +1,4 @@
package org.bench4q.agent.test.plugin;
package org.bench4q.agent.test.plugin.hbase;
import java.io.File;
import java.io.IOException;
@ -45,13 +45,12 @@ public class Test_HBasePlugin {
assertEquals(4, behavior.getParameters().size());
assertEquals("USERBEHAVIOR", behavior.getType());
assertEquals("hBase", behavior.getUse());
assertEquals("beginTime", behavior.getParameters().get(0)
assertEquals("beginDay", behavior.getParameters().get(0)
.getKey());
assertEquals("201309111715001", behavior.getParameters().get(0)
assertEquals("201309110000000", behavior.getParameters().get(0)
.getValue());
assertEquals("endTime", behavior.getParameters().get(1)
.getKey());
assertEquals("201309112215001", behavior.getParameters().get(1)
assertEquals("endDay", behavior.getParameters().get(1).getKey());
assertEquals("201309150000000", behavior.getParameters().get(1)
.getValue());
assertEquals(1000 * i + 1 + "", behavior.getParameters().get(2)
.getValue());
@ -129,9 +128,9 @@ public class Test_HBasePlugin {
private static BehaviorModel createABehavior(int i) {
List<ParameterModel> parameters = new ArrayList<ParameterModel>();
parameters.add(ParameterModel.createParameter("beginTime",
"201309111715001"));
"201309110000000"));
parameters.add(ParameterModel.createParameter("endTime",
"201309112215001"));
"201309150000000"));
parameters.add(ParameterModel.createParameter("beginUser", 1000 * i + 1
+ ""));
parameters.add(ParameterModel.createParameter("endUser", 1000 * i + 100

View File

@ -0,0 +1,56 @@
package org.bench4q.agent.test.plugin.hbase;
import static org.junit.Assert.*;
import org.bench4q.agent.plugin.basic.hbase.HBasePlugin;
import org.bench4q.agent.plugin.basic.hbase.RecordGenerator;
import org.bench4q.agent.plugin.basic.hbase.RecordGenerator.RandomGenerator;
import org.bench4q.agent.utils.types.Table;
import org.bench4q.agent.utils.types.Table.Row;
import org.bench4q.share.exception.Bench4QRunTimeException;
import org.junit.Test;
public class Test_RecordGenerator {
@Test
public void test_buildGeneratorWithRandomRightlyInput() {
String input = "dataType=Int|generator=Random|min=10|max=50|;";
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
.getRow(0);
RecordGenerator resultGenerator = RecordGenerator
.buildGeneratorWithRow(inputRow);
assertTrue(resultGenerator instanceof RandomGenerator);
for (int i = 0; i < 100; i++) {
Double resultValue = resultGenerator.getDouble();
System.out.println(resultValue);
assertTrue(resultValue < 50);
assertTrue(resultValue >= 10);
}
}
@Test
public void test_buildGeneratorWithRandomWithMinGTmax() {
String input = "dataType=Int|generator=Random|min=50|max=10|;";
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
.getRow(0);
try {
RecordGenerator.buildGeneratorWithRow(inputRow);
fail("should not come here");
} catch (Exception e) {
assertTrue(e instanceof Bench4QRunTimeException);
}
}
@Test
public void test_buildGeneratorWithRandomWithIllegalDataType() {
String input = "dataType=String|generator=Random|min=50|max=10|;";
Row inputRow = Table.buildTable(input, HBasePlugin.LABEL_LIST)
.getRow(0);
try {
RecordGenerator.buildGeneratorWithRow(inputRow);
fail("should not come here");
} catch (Exception e) {
assertTrue(e instanceof Bench4QRunTimeException);
}
}
}

View File

@ -17,10 +17,12 @@ public class Bench4QRunTimeException extends RuntimeException {
public Bench4QRunTimeException(String message) {
super(message);
logger.error(message);
}
public Bench4QRunTimeException(String message, Throwable e) {
super(message, e);
logger.error(message, e);
}
}

View File

@ -31,5 +31,10 @@
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,28 @@
package org.bench4q.share.exception;
import org.apache.log4j.Logger;
public class Bench4QRunTimeException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 2471911275064112250L;
private static Logger logger = Logger
.getLogger(Bench4QRunTimeException.class);
public Bench4QRunTimeException() {
super();
}
public Bench4QRunTimeException(String message) {
super(message);
logger.error(message);
}
public Bench4QRunTimeException(String message, Throwable e) {
super(message, e);
logger.error(message, e);
}
}