Skip to content

Commit

Permalink
#143. Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Joński committed Nov 23, 2016
1 parent c9aa11f commit cc63bca
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 30 deletions.
Expand Up @@ -23,7 +23,7 @@ public class ConstructorParameters {
private final Class<?>[] constructorParametersTypes;

/**
* Instantaites {@code ConstructorParameters} with given constructor parameters and constructor parameter's types.
* Instantiates {@code ConstructorParameters} with given constructor parameters and constructor parameter's types.
*
* @param constructorParameters constructor parameters
* @param constructorParametersTypes constructor parameter's types
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/pl/pojo/tester/api/DefaultPackageFilter.java
Expand Up @@ -77,7 +77,7 @@ public Class<?>[] getClasses() {
.map(ClassLoader::loadClass)
.toArray(Class[]::new);
} catch (final IOException e) {
throw new PacakgeFilterException(packageFile.toString(), e);
throw new PackageFilterException(packageFile.toString(), e);
}
}

Expand All @@ -103,7 +103,7 @@ private File getFile(final String packageName) {
.getContextClassLoader()
.getResource(packagePath);
if (fileUrl == null) {
throw new PacakgeFilterException(packagePath, null);
throw new PackageFilterException(packagePath, null);
}
return new File(fileUrl.getFile());
}
Expand Down
Expand Up @@ -9,15 +9,15 @@
* @author Piotr Joński
* @since 0.5.0
*/
public class PacakgeFilterException extends RuntimeException {
public class PackageFilterException extends RuntimeException {

/**
* Instantiates exception.
*
* @param packageName package name or file of package
* @param cause cause, which raised this exception
*/
public PacakgeFilterException(final String packageName, final IOException cause) {
public PackageFilterException(final String packageName, final IOException cause) {
super(createMessage(packageName), cause);
}

Expand Down
Expand Up @@ -110,10 +110,10 @@ public void areWellImplemented() {

/**
* Indicates, that class should be constructed using given constructor parameters. Constructor will be selected
* based on constructor paramter's types.
* based on constructor parameter's types.
*
* @param qualifiedClassName class to instantiate
* @param constructorParameters constructor paramters
* @param constructorParameters constructor parameters
* @param constructorParameterTypes constructor parameter's types
*
* @return itself
Expand All @@ -130,10 +130,10 @@ public AbstractAssertion create(final String qualifiedClassName, final Object[]

/**
* Indicates, that class should be constructed using given constructor parameters. Constructor will be selected
* based on constructor paramter's types.
* based on constructor parameter's types.
*
* @param qualifiedClassName class to instantiate
* @param constructorParameters constructor paramters
* @param constructorParameters constructor parameters
*
* @return itself
*
Expand All @@ -150,10 +150,10 @@ public AbstractAssertion create(final String qualifiedClassName, final Construct

/**
* Indicates, that class should be constructed using given constructor parameters. Constructor will be selected
* based on constructor paramter's types.
* based on constructor parameter's types.
*
* @param clazz class to instantiate
* @param constructorParameters constructor paramters
* @param constructorParameters constructor parameters
* @param constructorParameterTypes constructor parameter's types
*
* @return itself
Expand All @@ -171,10 +171,10 @@ public AbstractAssertion create(final Class<?> clazz, final Object[] constructor

/**
* Indicates, that class should be constructed using given constructor parameters. Constructor will be selected
* based on constructor paramter's types.
* based on constructor parameter's types.
*
* @param clazz class to instantiate
* @param constructorParameters constructor paramters
* @param constructorParameters constructor parameters
*
* @return itself
*
Expand Down
Expand Up @@ -2,7 +2,7 @@

abstract class AbstractObjectInstantiator {

protected Class<?> clazz;
protected final Class<?> clazz;

AbstractObjectInstantiator(final Class<?> clazz) {
this.clazz = clazz;
Expand Down
Expand Up @@ -91,9 +91,7 @@ private static boolean canBeCreatedByDefaultConstructor(final Class<?> clazz) {
final Constructor<?>[] constructors = clazz.getConstructors();
return !qualifiesForProxy(clazz) && Arrays.stream(constructors)
.filter(Instantiable::isNoArgs)
.filter(Instantiable::isPublic)
.findAny()
.isPresent();
.anyMatch(Instantiable::isPublic);
}

private static boolean isPublic(final Constructor<?> constructor) {
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/pl/pojo/tester/internal/tester/AbstractTester.java
Expand Up @@ -19,20 +19,29 @@

public abstract class AbstractTester {

ObjectGenerator objectGenerator;
TestAssertions testAssertions = new TestAssertions();
private MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters = new ArrayListValuedHashMap<>();
private AbstractFieldValueChanger fieldValuesChanger = DefaultFieldValueChanger.INSTANCE;
new

final TestAsserti

public AbstractTester() {
estAsserti
ObjectGenerator obje dMap<
public AbstractTe
t eneCl ss ator;ons = private Multi
ConstructorParamet rs<?>, constructorPar m> private AbstractFieldValueChanger fieldValuesChanger = DefaultFieldValueChanger.INSTANCE; ters = new ArrayListValuedHashMap<

r()
{
this(DefaultFieldValueChanger.INSTANCE);
}


public AbstractTester(final AbstractFieldValueChanger abstractFieldValueChanger) {
objectGenerator = new ObjectGenerator(abstractFieldValueChanger, constructorParameters);
objectGenerator = new ObjectGenerator(abstractFieldValueChanger, constructorParamet rs);
}

TestAsserti
;

public void test(final Class<?> clazz) {
final Predicate<String> predicateAcceptingAllFields = FieldPredicate.includeAllFields(clazz);
test(clazz, predicateAcceptingAllFields);
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/pl/pojo/tester/internal/utils/MethodUtils.java
Expand Up @@ -55,19 +55,20 @@ private static boolean prefixMatchesGettersPrefixAndHasExpectedLength(final Meth
final Class<?> returnType = method.getReturnType();
final String methodName = method.getName();
final int fieldNameLength = fieldName.length();
final String upperCaseFirstLetterfieldName = upperCaseFirstLetter(fieldName);
final String upperCaseFirstLetterFieldName = upperCaseFirstLetter(fieldName);

if (returnType.equals(boolean.class) || returnType.equals(Boolean.class)) {
return (methodName.startsWith("is") && methodName.equals(fieldName))
|| ((methodName.endsWith(upperCaseFirstLetterfieldName))
|| ((methodName.endsWith(upperCaseFirstLetterFieldName))
&& ((methodName.startsWith("is") && (methodName.length() == (fieldNameLength + 2)))
|| (methodName.startsWith("has") && (methodName.length() == (fieldNameLength + 3)))
|| (methodName.startsWith("get") && (methodName.length() == (fieldNameLength + 3)))
|| (methodName.startsWith("have") && (methodName.length() == (fieldNameLength + 4)))
|| (methodName.startsWith("contains") && (methodName.length() == (fieldNameLength + 8)))));
} else {
return methodName.startsWith("get") && methodName.length() == fieldNameLength + 3 && methodName.endsWith(
upperCaseFirstLetterfieldName);
return methodName.startsWith("get")
&& methodName.length() == fieldNameLength + 3
&& methodName.endsWith(upperCaseFirstLetterFieldName);
}
}

Expand All @@ -83,8 +84,9 @@ private static boolean prefixMatchesSettersPrefixAndHasExpectedLength(final Meth
return methodName.startsWith("set") && methodName.endsWith(fieldNameWithoutPrefix);

} else {
return methodName.startsWith("set") && methodName.length() == fieldNameLength + 3 && methodName.endsWith(
upperCaseFirstLetterFieldName);
return methodName.startsWith("set")
&& methodName.length() == fieldNameLength + 3
&& methodName.endsWith(upperCaseFirstLetterFieldName);
}
}

Expand Down
Expand Up @@ -51,6 +51,6 @@ public void Should_Throw_Exception_When_Invalid_Package_Name() {
.getClasses());

// then
assertThat(result).isInstanceOf(PacakgeFilterException.class);
assertThat(result).isInstanceOf(PackageFilterException.class);
}
}

0 comments on commit cc63bca

Please sign in to comment.