Skip to content

Commit

Permalink
feat 格式调整,为了扩展性去除JmockManager,放入到mockerConfig中。代码性能优化,减少new对象开销。
Browse files Browse the repository at this point in the history
  • Loading branch information
huayanYu committed Jan 21, 2018
1 parent 1b8bb68 commit 4992ca5
Show file tree
Hide file tree
Showing 25 changed files with 128 additions and 127 deletions.
59 changes: 54 additions & 5 deletions src/main/java/com/github/jsonzou/jmockdata/MockConfig.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package com.github.jsonzou.jmockdata;


import com.github.jsonzou.jmockdata.mocker.BigDecimalMocker;
import com.github.jsonzou.jmockdata.mocker.BigIntegerMocker;
import com.github.jsonzou.jmockdata.mocker.BooleanMocker;
import com.github.jsonzou.jmockdata.mocker.ByteMocker;
import com.github.jsonzou.jmockdata.mocker.CharacterMocker;
import com.github.jsonzou.jmockdata.mocker.DateMocker;
import com.github.jsonzou.jmockdata.mocker.DoubleMocker;
import com.github.jsonzou.jmockdata.mocker.FloatMocker;
import com.github.jsonzou.jmockdata.mocker.IntegerMocker;
import com.github.jsonzou.jmockdata.mocker.LongMocker;
import com.github.jsonzou.jmockdata.mocker.ShortMocker;
import com.github.jsonzou.jmockdata.mocker.StringMocker;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -12,6 +27,19 @@
*/
public class MockConfig {

private static final ByteMocker BYTE_MOCKER = new ByteMocker();
private static final BooleanMocker BOOLEAN_MOCKER = new BooleanMocker();
private static final CharacterMocker CHARACTER_MOCKER = new CharacterMocker();
private static final ShortMocker SHORT_MOCKER = new ShortMocker();
private static final IntegerMocker INTEGER_MOCKER = new IntegerMocker();
private static final LongMocker LONG_MOCKER = new LongMocker();
private static final FloatMocker FLOAT_MOCKER = new FloatMocker();
private static final DoubleMocker DOUBLE_MOCKER = new DoubleMocker();
private static final BigIntegerMocker BIG_INTEGER_MOCKER = new BigIntegerMocker();
private static final BigDecimalMocker BIG_DECIMAL_MOCKER = new BigDecimalMocker();
private static final StringMocker STRING_MOCKER = new StringMocker();
private static final DateMocker DATE_MOCKER = new DateMocker("1970-01-01", "2100-12-31");

/**
* Bean缓存
*/
Expand All @@ -20,14 +48,16 @@ public class MockConfig {
* TypeVariable缓存
*/
private Map<String, Type> typeVariableCache = new HashMap<>();
private Map<Class<?>, Mocker> mockerContext = new HashMap<>();
private byte[] byteRange = {0, 127};
private short[] shortRange = {0, 1000};
private int[] intRange = {0, 10000};
private float[] floatRange = {0.0f, 10000.00f};
private double[] doubleRange = {0.0, 10000.00};
private long[] longRange = {0L, 10000L};
private String[] dateRange = {"1970-01-02", "2100-12-31"};
private String[] dateRange = {"1970-01-01", "2100-12-31"};
private int[] sizeRange = {1, 10};

private char[] charSeed =
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
Expand All @@ -38,6 +68,18 @@ public class MockConfig {
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

public MockConfig() {
registerMocker(BYTE_MOCKER, byte.class, Byte.class);
registerMocker(BOOLEAN_MOCKER, boolean.class, Boolean.class);
registerMocker(CHARACTER_MOCKER, char.class, Character.class);
registerMocker(SHORT_MOCKER, short.class, Short.class);
registerMocker(INTEGER_MOCKER, Integer.class, int.class);
registerMocker(LONG_MOCKER, long.class, Long.class);
registerMocker(FLOAT_MOCKER, float.class, Float.class);
registerMocker(DOUBLE_MOCKER, double.class, Double.class);
registerMocker(BIG_INTEGER_MOCKER, BigInteger.class);
registerMocker(BIG_DECIMAL_MOCKER, BigDecimal.class);
registerMocker(STRING_MOCKER, String.class);
registerMocker(DATE_MOCKER, Date.class);
}

public void addBeanCache(String name, Object object) {
Expand Down Expand Up @@ -105,6 +147,7 @@ public MockConfig longRange(long min, long max) {
public MockConfig dateRange(String min, String max) {
this.dateRange[0] = min;
this.dateRange[1] = max;
registerMocker(new DateMocker(min, max), Date.class);
return this;
}

Expand Down Expand Up @@ -148,10 +191,6 @@ public long[] getLongRange() {
return longRange;
}

public String[] getDateRange() {
return dateRange;
}

public int[] getSizeRange() {
return sizeRange;
}
Expand All @@ -164,4 +203,14 @@ public String[] getStringSeed() {
return stringSeed;
}

public void registerMocker(Mocker mocker, Class<?>... clazzs) {
for (Class<?> clazz : clazzs) {
mockerContext.put(clazz, mocker);
}
}

public Mocker getMocker(Class<?> clazz) {
return mockerContext.get(clazz);
}

}
4 changes: 0 additions & 4 deletions src/main/java/com/github/jsonzou/jmockdata/MockException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

public class MockException extends RuntimeException {

public MockException() {
super();
}

public MockException(String message) {
super(message);
}
Expand Down
53 changes: 0 additions & 53 deletions src/main/java/com/github/jsonzou/jmockdata/MockerManager.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.jsonzou.jmockdata.mocker;

import com.github.jsonzou.jmockdata.MockException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class AbstractDateMock {

private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");
protected Long startTime;
protected Long endTime;

public AbstractDateMock(String startTimePattern, String endTimePattern) {
try {
this.startTime = FORMAT.parse(startTimePattern).getTime();
this.endTime = FORMAT.parse(endTimePattern).getTime();
} catch (ParseException e) {
throw new MockException("时间格式设置错误,设置如下格式yyyy-MM-dd ", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* 数组模拟器
*/
@SuppressWarnings("unchecked")
// TODO 2018/1/20 代码还需要整理
public class ArrayMocker implements Mocker<Object> {

private Type type;
Expand All @@ -40,13 +39,14 @@ private Object array(MockConfig mockConfig) {
int size = RandomUtils.nextSize(mockConfig.getSizeRange()[0], mockConfig.getSizeRange()[1]);
Class componentClass = ((Class) type).getComponentType();
Object result = Array.newInstance(componentClass, size);
BaseMocker baseMocker = new BaseMocker(componentClass);
for (int index = 0; index < size; index++) {
Object value = new BaseMocker(componentClass).mock(mockConfig);
Array.set(result, index, value);
Array.set(result, index, baseMocker.mock(mockConfig));
}
return result;
}

// TODO 代码还需要整理
// 由于GenericArrayType无法获得Class,所以递归创建多维数组
private Object createGenericArray(MockConfig mockConfig) {
GenericArrayType genericArrayType = (GenericArrayType) this.type;
Expand Down Expand Up @@ -103,4 +103,5 @@ private Map<Integer, Map<Class, Type[]>> map(MockConfig mockConfig, GenericArray
result.put(dimension, map);
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public T mock(MockConfig mockConfig) {
} else if (type instanceof GenericArrayType) {
mocker = new ArrayMocker(type);
} else if (type instanceof TypeVariable) {
Type actualType = mockConfig.getVariableType(((TypeVariable) type).getName());
mocker = new BaseMocker(actualType);
mocker = new BaseMocker(mockConfig.getVariableType(((TypeVariable) type).getName()));
} else {
mocker = new ClassMocker((Class) type, genericTypes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.github.jsonzou.jmockdata.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Map.Entry;

public class BeanMocker implements Mocker<Object> {
Expand All @@ -31,16 +30,13 @@ public Object mock(MockConfig mockConfig) {
for (Class<?> currentClass = clazz; currentClass != Object.class; currentClass = currentClass.getSuperclass()) {
// 模拟有setter方法的字段
for (Entry<Field, Method> entry : ReflectionUtils.fieldAndSetterMethod(currentClass).entrySet()) {
Field field = entry.getKey();
Method method = entry.getValue();
Type genericType = field.getGenericType();
Object value = new BaseMocker(genericType).mock(mockConfig);
ReflectionUtils.setRefValue(result, method, value);
ReflectionUtils.setRefValue(result, entry.getValue(), new BaseMocker(entry.getKey().getGenericType()).mock(mockConfig));
}
}
return result;
} catch (Exception e) {
throw new MockException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
*/
public class BigDecimalMocker implements Mocker<BigDecimal> {

public static final BigDecimalMocker INSTANCE = new BigDecimalMocker();

@Override
public BigDecimal mock(MockConfig mockConfig) {
return BigDecimal.valueOf(RandomUtils.nextDouble(mockConfig.getDoubleRange()[0], mockConfig.getDoubleRange()[1]));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
*/
public class BigIntegerMocker implements Mocker<BigInteger> {

public static final BigIntegerMocker INSTANCE = new BigIntegerMocker();

@Override
public BigInteger mock(MockConfig mockConfig) {
return BigInteger.valueOf(RandomUtils.nextLong(mockConfig.getLongRange()[0], mockConfig.getLongRange()[1]));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
*/
public class BooleanMocker implements Mocker<Boolean> {

public static final BooleanMocker INSTANCE = new BooleanMocker();

@Override
public Boolean mock(MockConfig mockConfig) {
return RandomUtils.nextBoolean();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
*/
public class ByteMocker implements Mocker<Byte> {

public static final ByteMocker INSTANCE = new ByteMocker();

@Override
public Byte mock(MockConfig mockConfig) {
return (byte) RandomUtils.nextInt(mockConfig.getByteRange()[0], mockConfig.getByteRange()[1]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
*/
public class CharacterMocker implements Mocker<Character> {

public static final CharacterMocker INSTANCE = new CharacterMocker();

@Override
public Character mock(MockConfig mockConfig) {
char[] charSeed = mockConfig.getCharSeed();
return charSeed[RandomUtils.nextInt(0, charSeed.length)];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.jsonzou.jmockdata.MockConfig;
import com.github.jsonzou.jmockdata.Mocker;
import com.github.jsonzou.jmockdata.MockerManager;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;
Expand All @@ -28,11 +27,12 @@ public Object mock(MockConfig mockConfig) {
} else if (Collection.class.isAssignableFrom(clazz)) {
mocker = new CollectionMocker(clazz, genericTypes[0]);
} else {
mocker = MockerManager.getMocker(clazz);
mocker = mockConfig.getMocker(clazz);
if (mocker == null) {
mocker = new BeanMocker(clazz);
}
}
return mocker.mock(mockConfig);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ public Object mock(MockConfig mockConfig) {
} else {
result = new HashSet<>(size);
}
BaseMocker baseMocker = new BaseMocker(genericType);
for (int index = 0; index < size; index++) {
Object value = new BaseMocker(genericType).mock(mockConfig);
result.add(value);
result.add(baseMocker.mock(mockConfig));
}
return result;
}

}
Loading

0 comments on commit 4992ca5

Please sign in to comment.