Skip to content
Permalink
Browse files

cleaned up redundant code

  • Loading branch information...
maciejmikosik committed Feb 22, 2018
2 parents e9a706f + ddb1ad2 commit 558523be532bfe0667b9398d7ad405f4c37a3dbf
Showing with 431 additions and 658 deletions.
  1. +1 −1 main/java/org/testory/Testory.java
  2. +0 −7 main/java/org/testory/common/Checks.java
  3. +13 −27 main/java/org/testory/common/Classes.java
  4. +0 −14 main/java/org/testory/common/Closeables.java
  5. +0 −6 main/java/org/testory/common/Collections.java
  6. +2 −2 main/java/org/testory/common/DelegatingMatcher.java
  7. +5 −5 main/java/org/testory/common/Effect.java
  8. +2 −7 main/java/org/testory/common/Matchers.java
  9. +1 −1 main/java/org/testory/common/ObjectFormatter.java
  10. +2 −3 main/java/org/testory/common/Optional.java
  11. +0 −28 main/java/org/testory/common/Strings.java
  12. +3 −3 main/java/org/testory/common/Throwables.java
  13. +15 −11 main/java/org/testory/plumbing/facade/ConfigurableFacade.java
  14. +1 −10 main/java/org/testory/plumbing/history/FilteredHistory.java
  15. +2 −1 main/java/org/testory/plumbing/mock/Mocked.java
  16. +2 −1 main/java/org/testory/plumbing/mock/NiceMockMaker.java
  17. +2 −2 main/java/org/testory/plumbing/mock/SaneMockMaker.java
  18. +2 −1 main/java/org/testory/plumbing/mock/Stubbed.java
  19. +2 −1 main/java/org/testory/plumbing/mock/UniqueNamer.java
  20. +4 −2 main/java/org/testory/proxy/Invocation.java
  21. +4 −2 main/java/org/testory/proxy/Typing.java
  22. +4 −3 test/java/org/testory/TestAnyA.java
  23. +4 −3 test/java/org/testory/TestAnyClass.java
  24. +2 −1 test/java/org/testory/TestAnyInstanceOf.java
  25. +3 −2 test/java/org/testory/TestAnyThe.java
  26. +5 −4 test/java/org/testory/TestInvocationMatchers.java
  27. +5 −4 test/java/org/testory/TestMatchingInvocations.java
  28. +2 −1 test/java/org/testory/TestThenCalled.java
  29. +10 −9 test/java/org/testory/TestThenCalledAllOverloadings.java
  30. +7 −4 test/java/org/testory/TestThenCalledInOrder.java
  31. +3 −2 test/java/org/testory/TestThenCalledNever.java
  32. +3 −3 test/java/org/testory/TestThenCalledTimesInteger.java
  33. +4 −3 test/java/org/testory/TestThenCalledTimesMatcher.java
  34. +23 −15 test/java/org/testory/TestThenEqual.java
  35. +20 −12 test/java/org/testory/TestThenObjectMatcher.java
  36. +6 −3 test/java/org/testory/TestThenReturned.java
  37. +38 −24 test/java/org/testory/TestThenReturnedMatcher.java
  38. +36 −24 test/java/org/testory/TestThenReturnedObject.java
  39. +8 −5 test/java/org/testory/TestThenThrown.java
  40. +38 −24 test/java/org/testory/TestThenThrownClass.java
  41. +41 −26 test/java/org/testory/TestThenThrownMatcher.java
  42. +27 −17 test/java/org/testory/TestThenThrownThrowable.java
  43. +6 −4 test/java/org/testory/TestWhen.java
  44. +11 −6 test/java/org/testory/TestWhenChained.java
  45. +12 −7 test/java/org/testory/TestWhenClosure.java
  46. +7 −4 test/java/org/testory/TestWhenVoidClosure.java
  47. +0 −31 test/java/org/testory/common/TestChecksCheckNotNull.java
  48. +2 −1 test/java/org/testory/common/TestClassesCanAssign.java
  49. +2 −1 test/java/org/testory/common/TestClassesCanInvoke.java
  50. +2 −1 test/java/org/testory/common/TestClassesCanReturn.java
  51. +0 −86 test/java/org/testory/common/TestClassesHasMethod.java
  52. +0 −54 test/java/org/testory/common/TestCloseables.java
  53. +0 −60 test/java/org/testory/common/TestCollectionsReverse.java
  54. +2 −1 test/java/org/testory/common/TestDelegatingMatcher.java
  55. +4 −3 test/java/org/testory/common/TestEffect.java
  56. +6 −3 test/java/org/testory/common/TestMatchersArrayOf.java
  57. +2 −1 test/java/org/testory/common/TestMatchersEqualDeep.java
  58. +6 −3 test/java/org/testory/common/TestMatchersListOf.java
  59. +2 −1 test/java/org/testory/common/TestMatchersSame.java
  60. +6 −5 test/java/org/testory/common/TestObjectFormatter.java
  61. +1 −1 test/java/org/testory/common/TestSequenceFormatter.java
  62. +0 −56 test/java/org/testory/common/TestStringsJoin.java
  63. +3 −1 test/java/org/testory/proxy/TestInvocation.java
  64. +2 −1 test/java/org/testory/proxy/proxer/Tester.java
  65. +3 −3 test/java/org/testory/testing/HamcrestMatchers.java
@@ -160,7 +160,7 @@ public static InvocationMatcher onReturn(Class<?> type) {
return getFacade().onReturn(type);
}

public static InvocationMatcher onRequest(Class<?> type, final Object... arguments) {
public static InvocationMatcher onRequest(Class<?> type, Object... arguments) {
return getFacade().onRequest(type, arguments);
}

@@ -1,13 +1,6 @@
package org.testory.common;

public class Checks {
public static <T> T checkNotNull(T instance) {
if (instance == null) {
throw new NullPointerException();
}
return instance;
}

public static void checkArgument(boolean condition) {
if (!condition) {
throw new IllegalArgumentException();
@@ -1,21 +1,20 @@
package org.testory.common;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableMap;
import static java.util.Objects.requireNonNull;
import static org.testory.common.Checks.checkArgument;
import static org.testory.common.Checks.checkNotNull;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Classes {
public static void setAccessible(final AccessibleObject accessible) {
checkNotNull(accessible);
requireNonNull(accessible);
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
accessible.setAccessible(true);
@@ -24,21 +23,8 @@ public Void run() {
});
}

public static boolean hasMethod(String name, Class<?>[] parameters, Class<?> type) {
checkNotNull(name);
checkNotNull(parameters);
checkArgument(!asList(parameters).contains(null));
checkNotNull(type);
try {
type.getMethod(name, parameters);
return true;
} catch (NoSuchMethodException e) {
return false;
}
}

public static boolean canAssign(@Nullable Object instance, Class<?> type) {
checkNotNull(type);
requireNonNull(type);
return type.isPrimitive()
? canConvert(instance, type)
: instance == null || type.isAssignableFrom(instance.getClass());
@@ -74,13 +60,13 @@ void method(double a) {}
}

public static boolean canReturn(@Nullable Object object, Method method) {
checkNotNull(method);
requireNonNull(method);
return canAssign(object, method.getReturnType());
}

public static boolean canThrow(Throwable throwable, Method method) {
checkNotNull(throwable);
checkNotNull(method);
requireNonNull(throwable);
requireNonNull(method);
for (Class<?> exceptionType : method.getExceptionTypes()) {
if (exceptionType.isInstance(throwable)) {
return true;
@@ -90,8 +76,8 @@ public static boolean canThrow(Throwable throwable, Method method) {
}

public static boolean canInvoke(Method method, @Nullable Object instance, Object... arguments) {
checkNotNull(method);
checkNotNull(arguments);
requireNonNull(method);
requireNonNull(arguments);
return correctInstance(instance, method) && correctArguments(arguments, method);
}

@@ -114,7 +100,7 @@ private static boolean correctArguments(Object[] arguments, Method method) {
}

public static <T> T defaultValue(Class<T> type) {
checkNotNull(type);
requireNonNull(type);
return (T) defaultValues.get(type);
}

@@ -130,11 +116,11 @@ private static boolean correctArguments(Object[] arguments, Method method) {
map.put(long.class, 0L);
map.put(float.class, 0f);
map.put(double.class, 0.0);
return Collections.unmodifiableMap(map);
return unmodifiableMap(map);
}

public static Class<?> tryWrap(Class<?> type) {
checkNotNull(type);
requireNonNull(type);
return wrapping.containsKey(type)
? wrapping.get(type)
: type;
@@ -153,6 +139,6 @@ private static boolean correctArguments(Object[] arguments, Method method) {
map.put(long.class, Long.class);
map.put(float.class, Float.class);
map.put(double.class, Double.class);
return Collections.unmodifiableMap(map);
return unmodifiableMap(map);
}
}

This file was deleted.

@@ -27,10 +27,4 @@
public static <E> Set<E> immutable(Set<? extends E> set) {
return java.util.Collections.unmodifiableSet(new HashSet<>(set));
}

public static <T> List<T> reverse(List<T> list) {
ArrayList<T> result = new ArrayList<>(list);
java.util.Collections.reverse(result);
return result;
}
}
@@ -1,12 +1,12 @@
package org.testory.common;

import static org.testory.common.Checks.checkNotNull;
import static java.util.Objects.requireNonNull;

public class DelegatingMatcher implements Matcher {
private final Matcher matcher;

public DelegatingMatcher(Matcher matcher) {
this.matcher = checkNotNull(matcher);
this.matcher = requireNonNull(matcher);
}

public boolean matches(Object item) {
@@ -1,6 +1,7 @@
package org.testory.common;

import static org.testory.common.Checks.checkNotNull;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public abstract class Effect {
private Effect() {}
@@ -18,7 +19,7 @@ private ReturnedObject(Object object) {
}

public String toString() {
return "returned(" + object + ")";
return format("returned(%s)", object);
}
}

@@ -38,7 +39,7 @@ private Thrown(Throwable throwable) {
}

public String toString() {
return "thrown(" + throwable + ")";
return format("thrown(%s)", throwable);
}
}

@@ -51,7 +52,6 @@ public static Effect returnedVoid() {
}

public static Thrown thrown(Throwable throwable) {
checkNotNull(throwable);
return new Thrown(throwable);
return new Thrown(requireNonNull(throwable));
}
}
@@ -4,7 +4,6 @@
import static java.util.Arrays.asList;
import static java.util.Objects.deepEquals;
import static org.testory.common.Checks.checkArgument;
import static org.testory.common.Checks.checkNotNull;
import static org.testory.common.Classes.setAccessible;
import static org.testory.common.ObjectFormatter.objectFormatter;
import static org.testory.common.SequenceFormatter.sequence;
@@ -55,10 +54,8 @@ public String toString() {
}

public static Matcher arrayOf(List<Matcher> elementsMatchers) {
for (Matcher matcher : elementsMatchers) {
checkNotNull(matcher);
}
final List<Matcher> matchers = new ArrayList<>(elementsMatchers);
checkArgument(!matchers.contains(null));
return new Matcher() {
public boolean matches(Object item) {
return item != null && item.getClass().isArray()
@@ -81,10 +78,8 @@ public String toString() {
}

public static Matcher listOf(List<Matcher> elementsMatchers) {
for (Matcher matcher : elementsMatchers) {
checkNotNull(matcher);
}
final List<Matcher> matchers = new ArrayList<>(elementsMatchers);
checkArgument(!matchers.contains(null));
return new Matcher() {
public boolean matches(Object item) {
return item instanceof List<?> && matchers.size() == ((List<?>) item).size()
@@ -23,7 +23,7 @@ public String format(@Nullable Object object) {
}

private String formatArray(Object array) {
return "[" + sequence(", ", this).format(arrayAsIterable(array)) + "]";
return String.format("[%s]", sequence(", ", this).format(arrayAsIterable(array)));
}

private Iterable<Object> arrayAsIterable(final Object array) {
@@ -1,6 +1,6 @@
package org.testory.common;

import static org.testory.common.Checks.checkNotNull;
import static java.util.Objects.requireNonNull;

import java.util.NoSuchElementException;

@@ -27,7 +27,6 @@ public T get() {
}

public static <T> Optional<T> of(T value) {
checkNotNull(value);
return new Optional<>(value);
return new Optional<>(requireNonNull(value));
}
}

This file was deleted.

@@ -1,20 +1,20 @@
package org.testory.common;

import static org.testory.common.Checks.checkNotNull;
import static java.util.Objects.requireNonNull;

import java.io.PrintWriter;
import java.io.StringWriter;

public class Throwables {
/**
* usage
*
*
* <pre>
* throw gently(anyThrowable);
* </pre>
*/
public static RuntimeException gently(Throwable throwable) {
checkNotNull(throwable);
requireNonNull(throwable);
if (throwable instanceof RuntimeException) {
throw (RuntimeException) throwable;
} else if (throwable instanceof Error) {
@@ -1,5 +1,7 @@
package org.testory.plumbing.facade;

import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Objects.deepEquals;
import static org.testory.TestoryAssertionError.assertionError;
import static org.testory.common.Classes.defaultValue;
@@ -10,6 +12,8 @@
import static org.testory.common.Matchers.asMatcher;
import static org.testory.common.Matchers.isDiagnosticMatcher;
import static org.testory.common.Matchers.isMatcher;
import static org.testory.common.ObjectFormatter.objectFormatter;
import static org.testory.common.SequenceFormatter.sequence;
import static org.testory.common.Throwables.gently;
import static org.testory.common.Throwables.printStackTrace;
import static org.testory.plumbing.Inspecting.inspecting;
@@ -22,6 +26,9 @@
import static org.testory.proxy.handler.ReturningHandler.returning;
import static org.testory.proxy.handler.ThrowingHandler.throwing;

import java.util.ArrayList;
import java.util.List;

import org.testory.common.Closure;
import org.testory.common.Effect;
import org.testory.common.Effect.ReturnedObject;
@@ -138,7 +145,7 @@ public void given(Handler handler, InvocationMatcher invocationMatcher) {
configuration.history.add(stubbed(invocationMatcher, handler));
}

public Handler willReturn(final Object object) {
public Handler willReturn(Object object) {
return returning(object);
}

@@ -150,7 +157,7 @@ public Object handle(Invocation invocation) throws Throwable {
};
}

public Handler willRethrow(final Throwable throwable) {
public Handler willRethrow(Throwable throwable) {
return throwing(throwable);
}

@@ -229,7 +236,7 @@ public boolean matches(Invocation invocation) {
}

public String toString() {
return "onInstance(" + mock + ")";
return format("onInstance(%s)", mock);
}
};
}
@@ -241,7 +248,7 @@ public boolean matches(Invocation invocation) {
}

public String toString() {
return "onReturn(" + type.getName() + ")";
return format("onReturn(%s)", type.getName());
}
};
}
@@ -254,13 +261,10 @@ public boolean matches(Invocation invocation) {
}

public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("onRequest(").append(type.getName());
for (Object argument : arguments) {
builder.append(", ").append(argument);
}
builder.append(")");
return builder.toString();
List<Object> elements = new ArrayList<>();
elements.add(type.getName());
elements.addAll(asList(arguments));
return format("onRequest(%s)", sequence(", ", objectFormatter()).format(elements));
}
};
}

0 comments on commit 558523b

Please sign in to comment.
You can’t perform that action at this time.