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

View File

@ -1,22 +1,35 @@
package org.bench4q.agent.plugin.basic.hbase; package org.bench4q.agent.plugin.basic.hbase;
import java.util.LinkedHashSet;
import java.util.Random; 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.agent.utils.types.Table.Row;
import org.bench4q.share.exception.Bench4QRunTimeException;
public abstract class RecordGenerator { public abstract class RecordGenerator {
protected String dataType; protected String dataType;
protected Double minValue; protected Double minValue;
protected Double maxValue; 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) { 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.minValue = Double.valueOf(minValue);
this.maxValue = Double.valueOf(maxValue); this.maxValue = Double.valueOf(maxValue);
if (this.maxValue < this.minValue) { if (this.maxValue < this.minValue) {
throw new RuntimeException("The minValue LT the maxValue"); throw new Bench4QRunTimeException("The minValue LT the maxValue");
} }
} }
@ -26,12 +39,20 @@ public abstract class RecordGenerator {
return Double.valueOf(this.getValue()); return Double.valueOf(this.getValue());
} }
public abstract void reset(); protected String convertAsDataType(String toBeConverted) {
if (this.dataType.equalsIgnoreCase("int")) {
protected String convertAsDataType(Double value) { return String.valueOf(Double.valueOf(toBeConverted).intValue());
return (String) this.typeConverter.convert(value, this.dataType); } 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 * This function build generator as the @generator says, but if it's not
* legal, then build the random generator * legal, then build the random generator
@ -73,17 +94,16 @@ public abstract class RecordGenerator {
@Override @Override
public String getValue() { public String getValue() {
Double value = this.random.nextDouble(); Double doublePart = this.random.nextDouble();
Double doublePart = value - value.longValue(); int maxValueInt = maxValue.intValue();
value = doublePart int intPart = this.random.nextInt(maxValueInt);
+ (value.intValue() % (maxValue.intValue() - minValue return convertAsDataType(String.valueOf(doublePart
.intValue())) + minValue; + (intPart % (maxValueInt - minValue.intValue()))
return convertAsDataType(value); + minValue));
} }
@Override @Override
public void reset() { public void reset() {
} }
} }
@ -100,7 +120,7 @@ public abstract class RecordGenerator {
* Math.sin(this.currentAngle) * Math.sin(this.currentAngle)
+ (this.maxValue + this.minValue) / 2; + (this.maxValue + this.minValue) / 2;
this.currentAngle += Math.PI / 144; this.currentAngle += Math.PI / 144;
return convertAsDataType(value); return convertAsDataType(String.valueOf(value));
} }
@Override @Override
@ -122,7 +142,7 @@ public abstract class RecordGenerator {
* Math.cos(this.currentAngle) * Math.cos(this.currentAngle)
+ (this.maxValue + this.minValue) / 2; + (this.maxValue + this.minValue) / 2;
this.currentAngle += Math.PI / 144; this.currentAngle += Math.PI / 144;
return convertAsDataType(value); return convertAsDataType(String.valueOf(value));
} }
@Override @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.File;
import java.io.IOException; import java.io.IOException;
@ -45,13 +45,12 @@ public class Test_HBasePlugin {
assertEquals(4, behavior.getParameters().size()); assertEquals(4, behavior.getParameters().size());
assertEquals("USERBEHAVIOR", behavior.getType()); assertEquals("USERBEHAVIOR", behavior.getType());
assertEquals("hBase", behavior.getUse()); assertEquals("hBase", behavior.getUse());
assertEquals("beginTime", behavior.getParameters().get(0) assertEquals("beginDay", behavior.getParameters().get(0)
.getKey()); .getKey());
assertEquals("201309111715001", behavior.getParameters().get(0) assertEquals("201309110000000", behavior.getParameters().get(0)
.getValue()); .getValue());
assertEquals("endTime", behavior.getParameters().get(1) assertEquals("endDay", behavior.getParameters().get(1).getKey());
.getKey()); assertEquals("201309150000000", behavior.getParameters().get(1)
assertEquals("201309112215001", behavior.getParameters().get(1)
.getValue()); .getValue());
assertEquals(1000 * i + 1 + "", behavior.getParameters().get(2) assertEquals(1000 * i + 1 + "", behavior.getParameters().get(2)
.getValue()); .getValue());
@ -129,9 +128,9 @@ public class Test_HBasePlugin {
private static BehaviorModel createABehavior(int i) { private static BehaviorModel createABehavior(int i) {
List<ParameterModel> parameters = new ArrayList<ParameterModel>(); List<ParameterModel> parameters = new ArrayList<ParameterModel>();
parameters.add(ParameterModel.createParameter("beginTime", parameters.add(ParameterModel.createParameter("beginTime",
"201309111715001")); "201309110000000"));
parameters.add(ParameterModel.createParameter("endTime", parameters.add(ParameterModel.createParameter("endTime",
"201309112215001")); "201309150000000"));
parameters.add(ParameterModel.createParameter("beginUser", 1000 * i + 1 parameters.add(ParameterModel.createParameter("beginUser", 1000 * i + 1
+ "")); + ""));
parameters.add(ParameterModel.createParameter("endUser", 1000 * i + 100 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) { public Bench4QRunTimeException(String message) {
super(message); super(message);
logger.error(message);
} }
public Bench4QRunTimeException(String message, Throwable e) { public Bench4QRunTimeException(String message, Throwable e) {
super(message, e); super(message, e);
logger.error(message, e);
} }
} }

View File

@ -31,5 +31,10 @@
<artifactId>commons-httpclient</artifactId> <artifactId>commons-httpclient</artifactId>
<version>3.1</version> <version>3.1</version>
</dependency> </dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies> </dependencies>
</project> </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);
}
}