Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify #13

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/ru/nsu/sd/MockBuddy/MockBuddy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.nsu.sd.MockBuddy;

import ru.nsu.sd.MockBuddy.internal.MockBuddyCore;
import ru.nsu.sd.MockBuddy.internal.MockingInfo;
import ru.nsu.sd.MockBuddy.internal.stubbing.Stubber;

public class MockBuddy {
Expand All @@ -26,4 +27,9 @@ public static <T> T spy(Class<T> clazz) {
public static <T> Stubber<T> when(T obj) {
return mockBuddyCore.when(obj);
}

public static <T> T verify(T obj, Integer times) {
return mockBuddyCore.verify(obj, times);
}

}
5 changes: 5 additions & 0 deletions src/main/java/ru/nsu/sd/MockBuddy/internal/MockBuddyCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ru.nsu.sd.MockBuddy.internal.creation.ByteBuddyMockMaker;
import ru.nsu.sd.MockBuddy.internal.handling.DelegationStrategy;
import ru.nsu.sd.MockBuddy.internal.handling.VerificationHandler;
import ru.nsu.sd.MockBuddy.internal.stubbing.Stubber;

public class MockBuddyCore {
Expand All @@ -21,4 +22,8 @@ public <T> T spy(T obj) {
public <T> Stubber<T> when(T obj) {
return new Stubber<>();
}

public <T> T verify(T obj, Integer times) {
return ByteBuddyMockMaker.verify(obj, times);
}
}
38 changes: 38 additions & 0 deletions src/main/java/ru/nsu/sd/MockBuddy/internal/MockingInfo.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package ru.nsu.sd.MockBuddy.internal;

import ru.nsu.sd.MockBuddy.internal.handling.InvocationHolder;
import ru.nsu.sd.MockBuddy.internal.handling.MockInvocationHandler;
import ru.nsu.sd.MockBuddy.internal.matching.ArgumentMatcherStorage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class MockingInfo {

private static final ArgumentMatcherStorage argumentMatcherStorage;
private static MockInvocationHandler lastMockInvocationHandler;

private static HashMap<Integer, List<InvocationHolder>> hashMap;

static {
argumentMatcherStorage = new ArgumentMatcherStorage();
hashMap = new HashMap<>();
}

public static MockInvocationHandler getLastMockInvocationHandler() {
Expand All @@ -23,4 +32,33 @@ public static void setLastMockInvocationHandler(MockInvocationHandler handler) {
public static ArgumentMatcherStorage getArgumentMatcherStorage() {
return argumentMatcherStorage;
}

public static void addObjectToHashMup(Object obj, InvocationHolder holder) {

if (hashMap.containsKey(System.identityHashCode(obj))) {

if (hashMap.get(System.identityHashCode(obj)).stream().noneMatch(x -> x.getMethod().equals(holder.getMethod()) && Arrays.deepEquals(x.getArgs(), holder.getArgs()))) {
List<InvocationHolder> newHolders = hashMap.get(System.identityHashCode(obj));
newHolders.add(holder);
hashMap.put(System.identityHashCode(obj), newHolders);
} else {
List<InvocationHolder> newHolders = hashMap.get(System.identityHashCode(obj));
newHolders.add(holder);
hashMap.put(System.identityHashCode(obj), newHolders);
}

} else {
List<InvocationHolder> newHolders = new ArrayList<>();
newHolders.add(holder);
hashMap.put(System.identityHashCode(obj), newHolders);
}
}

public static void addHolders(Object obj, List<InvocationHolder> holders) {
hashMap.put(System.identityHashCode(obj), holders);
}

public static List<InvocationHolder> getListOfHolders(Object obj) {
return hashMap.get(System.identityHashCode(obj));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@
* Minimizes repetitive mock creation code.
* Makes the test class more readable.
*
* Example:
* <pre><code class="java">
*
*
*
* </code></pre>
*
*/
@Target(FIELD)
@Retention(RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@

/**
* Allows shorthand wrapping of field instances in an spy object.
*
* Example:
* <pre><code class="java">
*
*
*
* </code></pre>
*
*/
@Target(FIELD)
@Retention(RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import org.objenesis.instantiator.ObjectInstantiator;
import ru.nsu.sd.MockBuddy.internal.MockingInfo;
import ru.nsu.sd.MockBuddy.internal.handling.DelegationStrategy;
import ru.nsu.sd.MockBuddy.internal.handling.InvocationHolder;
import ru.nsu.sd.MockBuddy.internal.handling.MockInvocationHandler;
import ru.nsu.sd.MockBuddy.internal.handling.VerificationHandler;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -94,5 +96,23 @@ public static <T> T spy(T obj, MockInvocationHandler mockInvocationHandler) {
return instance;

}
}

public static <T> T verify(T obj, Integer times) {

VerificationHandler verificationHandler = new VerificationHandler(obj, times);

@SuppressWarnings("unchecked")
Class<? extends T> byteBuddy = new ByteBuddy()
.subclass((Class<T>) obj.getClass().getSuperclass())
.method(ElementMatchers.any())
.intercept(MethodDelegation.to(verificationHandler))
.make()
.load(ClassLoader.getSystemClassLoader())
.getLoaded();

// Create object without calling constructor
Objenesis objenesis = new ObjenesisStd();
ObjectInstantiator<? extends T> thingyInstantiator = objenesis.getInstantiatorOf(byteBuddy);
return (T) thingyInstantiator.newInstance();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ru.nsu.sd.MockBuddy.internal.handling;

import java.lang.reflect.Method;
import java.util.Arrays;

public class InvocationHolder {

private final Object[] args;
private final Method method;

private int invocationCounter = 0;

InvocationHolder(Method method, Object[] args) {
this.args = args;
this.method = method;
}

public Object[] getArgs() {
return args;
}

public Method getMethod() {
return method;
}


public int getInvocationCounter() {
return invocationCounter;
}

public void increaseCounter() {
invocationCounter++;

// System.out.println(this.toString() + " counter: " + invocationCounter);
}

public void decreaseCounter() {
if (invocationCounter > 0) {
invocationCounter--;
}

// System.out.println(this.toString() + " counter: " + invocationCounter);

}

@Override
public String toString() {
return "DataHolder{" +
"args=" + Arrays.toString(args) +
", method=" + method +
'}';
}
}
Loading