Skip to content

Solution #1 (take 3) - Apply Pluggable Selector Pattern by Kent Beck,... #13

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

Open
wants to merge 3 commits into
base: master
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
33 changes: 25 additions & 8 deletions java/src/main/java/it/tug/Main/Formatter.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
package it.tug.Main;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Formatter {

private Service service;
private static final MethodReflector<Formatter> methodReflector = new MethodReflector<>(Formatter.class);

private final Service service;

public Formatter(Service service) {
this.service = service;
}

public String doTheJob(String theInput) {
String response = service.askForPermission();
switch (response) {
case "FAIL":
return "error";
case "OK":
return String.format("%s%s", theInput, theInput);
default:
return null;

try {
String methodName = response.toLowerCase();
Method method = methodReflector.getMethod(methodName);

return (String) method.invoke(this, theInput);
} catch (IllegalAccessException
| IllegalArgumentException
| InvocationTargetException
| NoSuchMethodException ex) {
return null;
}
}

private String fail(String theInput) {
return "error";
}

private String ok(String theInput) {
return String.format("%s%s", theInput, theInput);
}
}
30 changes: 30 additions & 0 deletions java/src/main/java/it/tug/Main/MethodReflector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package it.tug.Main;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
*
* @author Gianluca Costa
*/
public class MethodReflector<T> { //TODO: is this class really needed? We should definitely profile the reflection call
private final Class<T> targetClass;
private final Map<String, Method> methodCache = new HashMap<>();

public MethodReflector(Class<T> targetClass) {
this.targetClass = targetClass;
}

//TODO: this method is not thread-safe, but that should NOT be an issue, generally speaking - and introducing a lock would probably be worse
public Method getMethod(String methodName) throws NoSuchMethodException {
Method method = methodCache.get(methodName);

if (method == null) {
method = targetClass.getDeclaredMethod(methodName, String.class);
methodCache.put(methodName, method);
}

return method;
}
}
30 changes: 22 additions & 8 deletions java/src/test/java/it/tug/FormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,45 @@ public void setup() {
service = mock(Service.class);
sut = new Formatter(service);
}

@Test
public void shouldReturnAnErrorMessageIfServiceReturnsNull() {
public void shouldReturnAnErrorMessageIfServiceReturnsFail() {
when(this.service.askForPermission()).thenReturn("FAIL");

String actual = sut.doTheJob("foo");

assertEquals("error", actual);
}

@Test
public void shouldReturnAnTheStringDoubledIfServiceReturnsOK() {
when(this.service.askForPermission()).thenReturn("OK");

String actual = sut.doTheJob("foobar");

assertEquals("foobarfoobar", actual);
}

@Test
public void shouldReturnAnTheStringDoubledIfServiceReturnsOK_AndShouldWorkTwice() {
when(this.service.askForPermission()).thenReturn("OK");

String actual;

actual = sut.doTheJob("foobar");
actual = sut.doTheJob(actual);

assertEquals("foobarfoobarfoobarfoobar", actual);


}

@Test
public void shouldReturnNullIfServiceRepliesDifferentlyThanOKOrFAIL() {
when(this.service.askForPermission()).thenReturn("luchino");

String actual = sut.doTheJob("foobar");

assertEquals(null, actual);
}
}