Skip to content

Commit

Permalink
192. Merged PrimitiveInstantiator, StringClassInstantiator, JavaLangC…
Browse files Browse the repository at this point in the history
…lassInstantiator into JavaTypeInstantiator.
  • Loading branch information
sta-szek committed Nov 15, 2017
1 parent 62b02c6 commit bf2df93
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
import java.time.ZonedDateTime;

import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor;
import static pl.pojo.tester.api.assertion.Method.*;
import static pl.pojo.tester.api.assertion.Method.CONSTRUCTOR;
import static pl.pojo.tester.api.assertion.Method.EQUALS;
import static pl.pojo.tester.api.assertion.Method.GETTER;
import static pl.pojo.tester.api.assertion.Method.HASH_CODE;
import static pl.pojo.tester.api.assertion.Method.TO_STRING;

class EntityClassPojoTest {

@Test
void Should_Test_Entity_Class() {
final ConstructorParameters constructorParameters = new ConstructorParameters(
new Object[]{LocalDateTime.now(), ZoneOffset.MAX, ZoneId.systemDefault()},
new Class[]{LocalDateTime.class, ZoneOffset.class, ZoneId.class});
new Object[]{ LocalDateTime.now(), ZoneOffset.MAX, ZoneId.systemDefault() },
new Class[]{ LocalDateTime.class, ZoneOffset.class, ZoneId.class });

for (int i = 0; i < 1000; i++) {
for (int i = 0; i < 100; i++) {
assertPojoMethodsFor(EntityClass.class).testing(GETTER, TO_STRING, CONSTRUCTOR, EQUALS, HASH_CODE)
.create(ZonedDateTime.class, constructorParameters)
.areWellImplemented();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ public final class Instantiable {
INSTANTIATORS = new LinkedList<>();
INSTANTIATORS.add(UserDefinedConstructorInstantiator.class);
INSTANTIATORS.add(JavaTypeInstantiator.class);
INSTANTIATORS.add(StringClassInstantiator.class);
INSTANTIATORS.add(CollectionInstantiator.class);
INSTANTIATORS.add(DefaultConstructorInstantiator.class);
INSTANTIATORS.add(PrimitiveInstantiator.class);
INSTANTIATORS.add(EnumInstantiator.class);
INSTANTIATORS.add(ArrayInstantiator.class);
INSTANTIATORS.add(ProxyInstantiator.class);
INSTANTIATORS.add(JavaLangClassInstantiator.class);
INSTANTIATORS.add(BestConstructorInstantiator.class);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,25 +1,94 @@
package pl.pojo.tester.internal.instantiator;


import org.apache.commons.collections4.MultiValuedMap;
import pl.pojo.tester.api.ConstructorParameters;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

class JavaTypeInstantiator extends AbstractObjectInstantiator {

private final Map<String, Object> preparedObjects = new HashMap<>();

{
preparedObjects.put("boolean", Boolean.TRUE);
preparedObjects.put("byte", (byte) -1);
preparedObjects.put("char", 'b');
preparedObjects.put("double", -2.5D);
preparedObjects.put("float", -3.5F);
preparedObjects.put("int", -4);
preparedObjects.put("long", -5L);
preparedObjects.put("short", (short) -6);
preparedObjects.put(Boolean.class.getName(), Boolean.FALSE);
preparedObjects.put(Byte.class.getName(), (byte) 1);
preparedObjects.put(Character.class.getName(), 'a');
preparedObjects.put(Double.class.getName(), 2.5D);
preparedObjects.put(Float.class.getName(), 3.5F);
preparedObjects.put(Integer.class.getName(), 4);
preparedObjects.put(Long.class.getName(), 5L);
preparedObjects.put(Short.class.getName(), (short) 6);

preparedObjects.put(Class.class.getName(), Object.class);
preparedObjects.put(String.class.getName(), "www.pojo.pl");
preparedObjects.put(UUID.class.getName(), UUID.randomUUID());

preparedObjects.put(BigDecimal.class.getName(), BigDecimal.ONE);
preparedObjects.put(BigInteger.class.getName(), BigInteger.ONE);

preparedObjects.put(java.sql.Date.class.getName(), java.sql.Date.valueOf(LocalDate.now()));
preparedObjects.put(Date.class.getName(), Date.from(Instant.now()));


preparedObjects.put(Clock.class.getName(), Clock.systemDefaultZone());
preparedObjects.put(Duration.class.getName(), Duration.ZERO);
preparedObjects.put(Instant.class.getName(), Instant.now());
preparedObjects.put(LocalDate.class.getName(), LocalDate.now());
preparedObjects.put(LocalDateTime.class.getName(), LocalDateTime.now());
preparedObjects.put(LocalTime.class.getName(), LocalTime.now());
preparedObjects.put(MonthDay.class.getName(), MonthDay.now());
preparedObjects.put(OffsetDateTime.class.getName(), OffsetDateTime.now());
preparedObjects.put(OffsetTime.class.getName(), OffsetTime.now());
preparedObjects.put(Period.class.getName(), Period.ZERO);
preparedObjects.put(Year.class.getName(), Year.now());
preparedObjects.put(YearMonth.class.getName(), YearMonth.now());
preparedObjects.put(ZonedDateTime.class.getName(), ZonedDateTime.now());
preparedObjects.put(ZoneId.class.getName(), ZoneId.systemDefault());
preparedObjects.put(ZoneOffset.class.getName(), ZoneOffset.UTC);
}

JavaTypeInstantiator(final Class<?> clazz,
final MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters) {
super(clazz, constructorParameters);
}

@Override
public Object instantiate() {
return ZonedDateTime.now();
final String canonicalName = clazz.getCanonicalName();
return preparedObjects.get(canonicalName);
}

@Override
public boolean canInstantiate() {
// TODO
return ZonedDateTime.class.equals(clazz);
return clazz.isPrimitive() || preparedObjects.containsKey(clazz.getName());
}
}

This file was deleted.

This file was deleted.

10 changes: 2 additions & 8 deletions src/main/java/pl/pojo/tester/internal/utils/ReflectionUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pl.pojo.tester.internal.utils;


import pl.pojo.tester.api.PackageFilterException;

import java.io.File;
Expand All @@ -11,7 +12,7 @@
import java.util.regex.Matcher;
import java.util.stream.Stream;

public class ReflectionUtils {
public final class ReflectionUtils {

private static final String PACKAGE_SEPARATOR = ".";
private static final String FILE_SEPARATOR = "/";
Expand All @@ -20,13 +21,6 @@ public class ReflectionUtils {
private ReflectionUtils() {
}

public static <T> Class<? extends T>[] getClassesFromPackage(final String packageName,
final Class<T> baseClass) throws IOException {
return getFilesFromPackage(packageName).filter(baseClass::isAssignableFrom)
.filter(clazz -> !clazz.equals(baseClass))
.toArray(Class[]::new);
}

public static Class<?>[] getClassesFromPackage(final String packageName) throws IOException {
return getFilesFromPackage(packageName).toArray(Class[]::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.lang.reflect.Field;
import java.util.List;

// TODO write tests
public class ThoroughFieldPermutator implements Permutator {

@Override
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/classesForTest/permutator/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package classesForTest.permutator;

public class A {
private int a;
private int b;
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Stream<DynamicTest> Should_Return_Expected_Instantiator_For_Class() {
new ClassInstantiator(Constructor_Field.class, BestConstructorInstantiator.class),
new ClassInstantiator(Constructor_Stream.class, BestConstructorInstantiator.class),
new ClassInstantiator(Constructor_Thread.class, BestConstructorInstantiator.class),
new ClassInstantiator(String.class, StringClassInstantiator.class),
new ClassInstantiator(String.class, JavaTypeInstantiator.class),
new ClassInstantiator(UserDefinedClass.class, UserDefinedConstructorInstantiator.class),
new ClassInstantiator(Boolean[].class, ArrayInstantiator.class),
new ClassInstantiator(Byte[].class, ArrayInstantiator.class),
Expand All @@ -86,22 +86,22 @@ Stream<DynamicTest> Should_Return_Expected_Instantiator_For_Class() {
new ClassInstantiator(int[].class, ArrayInstantiator.class),
new ClassInstantiator(long[].class, ArrayInstantiator.class),
new ClassInstantiator(short[].class, ArrayInstantiator.class),
new ClassInstantiator(Boolean.class, PrimitiveInstantiator.class),
new ClassInstantiator(Byte.class, PrimitiveInstantiator.class),
new ClassInstantiator(Character.class, PrimitiveInstantiator.class),
new ClassInstantiator(Double.class, PrimitiveInstantiator.class),
new ClassInstantiator(Float.class, PrimitiveInstantiator.class),
new ClassInstantiator(Integer.class, PrimitiveInstantiator.class),
new ClassInstantiator(Long.class, PrimitiveInstantiator.class),
new ClassInstantiator(Short.class, PrimitiveInstantiator.class),
new ClassInstantiator(boolean.class, PrimitiveInstantiator.class),
new ClassInstantiator(byte.class, PrimitiveInstantiator.class),
new ClassInstantiator(char.class, PrimitiveInstantiator.class),
new ClassInstantiator(double.class, PrimitiveInstantiator.class),
new ClassInstantiator(float.class, PrimitiveInstantiator.class),
new ClassInstantiator(int.class, PrimitiveInstantiator.class),
new ClassInstantiator(long.class, PrimitiveInstantiator.class),
new ClassInstantiator(short.class, PrimitiveInstantiator.class),
new ClassInstantiator(Boolean.class, JavaTypeInstantiator.class),
new ClassInstantiator(Byte.class, JavaTypeInstantiator.class),
new ClassInstantiator(Character.class, JavaTypeInstantiator.class),
new ClassInstantiator(Double.class, JavaTypeInstantiator.class),
new ClassInstantiator(Float.class, JavaTypeInstantiator.class),
new ClassInstantiator(Integer.class, JavaTypeInstantiator.class),
new ClassInstantiator(Long.class, JavaTypeInstantiator.class),
new ClassInstantiator(Short.class, JavaTypeInstantiator.class),
new ClassInstantiator(boolean.class, JavaTypeInstantiator.class),
new ClassInstantiator(byte.class, JavaTypeInstantiator.class),
new ClassInstantiator(char.class, JavaTypeInstantiator.class),
new ClassInstantiator(double.class, JavaTypeInstantiator.class),
new ClassInstantiator(float.class, JavaTypeInstantiator.class),
new ClassInstantiator(int.class, JavaTypeInstantiator.class),
new ClassInstantiator(long.class, JavaTypeInstantiator.class),
new ClassInstantiator(short.class, JavaTypeInstantiator.class),

new ClassInstantiator(Stream.class, CollectionInstantiator.class),
new ClassInstantiator(Stack.class, CollectionInstantiator.class),
Expand Down

0 comments on commit bf2df93

Please sign in to comment.