Skip to content

Commit

Permalink
[DUBBO-2489] MockClusterInvoker provides local forced mock,I tested i…
Browse files Browse the repository at this point in the history
…t locally, but it doesn't work (apache#2742)
  • Loading branch information
beiwei30 authored and CrazyHZM committed Dec 6, 2018
1 parent de59613 commit 99ecafa
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public abstract class AbstractConfig implements Serializable {

private static final Pattern PATTERN_PATH = Pattern.compile("[/\\-$._0-9a-zA-Z]+");

private static final Pattern PATTERN_NAME_HAS_SYMBOL = Pattern.compile("[:*,/\\-._0-9a-zA-Z]+");
private static final Pattern PATTERN_NAME_HAS_SYMBOL = Pattern.compile("[:*,\\s/\\-._0-9a-zA-Z]+");

private static final Pattern PATTERN_KEY = Pattern.compile("[*,\\-._0-9a-zA-Z]+");
private static final Map<String, String> legacyProperties = new HashMap<String, String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,36 @@ protected void checkInterfaceAndMethods(Class<?> interfaceClass, List<MethodConf
}
}

protected void checkStubAndMock(Class<?> interfaceClass) {
void checkMock(Class<?> interfaceClass) {
if (ConfigUtils.isEmpty(mock)) {
return;
}

String normalizedMock = MockInvoker.normalizeMock(mock);
if (normalizedMock.startsWith(Constants.RETURN_PREFIX)) {
normalizedMock = normalizedMock.substring(Constants.RETURN_PREFIX.length()).trim();
try {
MockInvoker.parseMockValue(normalizedMock);
} catch (Exception e) {
throw new IllegalStateException("Illegal mock return in <dubbo:service/reference ... " +
"mock=\"" + mock + "\" />");
}
} else if (normalizedMock.startsWith(Constants.THROW_PREFIX)) {
normalizedMock = normalizedMock.substring(Constants.THROW_PREFIX.length()).trim();
if (ConfigUtils.isNotEmpty(normalizedMock)) {
try {
MockInvoker.getThrowable(normalizedMock);
} catch (Exception e) {
throw new IllegalStateException("Illegal mock throw in <dubbo:service/reference ... " +
"mock=\"" + mock + "\" />");
}
}
} else {
MockInvoker.getMockObject(normalizedMock, interfaceClass);
}
}

void checkStub(Class<?> interfaceClass) {
if (ConfigUtils.isNotEmpty(local)) {
Class<?> localClass = ConfigUtils.isDefault(local) ? ReflectUtils.forName(interfaceClass.getName() + "Local") : ReflectUtils.forName(local);
if (!interfaceClass.isAssignableFrom(localClass)) {
Expand All @@ -309,26 +338,6 @@ protected void checkStubAndMock(Class<?> interfaceClass) {
throw new IllegalStateException("No such constructor \"public " + localClass.getSimpleName() + "(" + interfaceClass.getName() + ")\" in local implementation class " + localClass.getName());
}
}
if (ConfigUtils.isNotEmpty(mock)) {
if (mock.startsWith(Constants.RETURN_PREFIX)) {
String value = mock.substring(Constants.RETURN_PREFIX.length());
try {
MockInvoker.parseMockValue(value);
} catch (Exception e) {
throw new IllegalStateException("Illegal mock json value in <dubbo:service ... mock=\"" + mock + "\" />");
}
} else {
Class<?> mockClass = ConfigUtils.isDefault(mock) ? ReflectUtils.forName(interfaceClass.getName() + "Mock") : ReflectUtils.forName(mock);
if (!interfaceClass.isAssignableFrom(mockClass)) {
throw new IllegalStateException("The mock implementation class " + mockClass.getName() + " not implement interface " + interfaceClass.getName());
}
try {
mockClass.getConstructor(new Class<?>[0]);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No such empty constructor \"public " + mockClass.getSimpleName() + "()\" in mock implementation class " + mockClass.getName());
}
}
}
}

/**
Expand Down Expand Up @@ -522,4 +531,4 @@ public String getScope() {
public void setScope(String scope) {
this.scope = scope;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,14 @@ public void setMock(Boolean mock) {
}

public void setMock(String mock) {
if (mock != null && mock.startsWith(Constants.RETURN_PREFIX)) {
if (mock == null) {
return;
}

if (mock.startsWith(Constants.RETURN_PREFIX) || mock.startsWith(Constants.THROW_PREFIX + " ")) {
checkLength("mock", mock);
} else if (mock.startsWith(Constants.FAIL_PREFIX) || mock.startsWith(Constants.FORCE_PREFIX)) {
checkNameHasSymbol("mock", mock);
} else {
checkName("mock", mock);
}
Expand Down Expand Up @@ -181,4 +187,4 @@ public void setParameters(Map<String, String> parameters) {
this.parameters = parameters;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ private void init() {
}
}
checkApplication();
checkStubAndMock(interfaceClass);
checkStub(interfaceClass);
checkMock(interfaceClass);
Map<String, String> map = new HashMap<String, String>();
resolveAsyncInterface(interfaceClass, map);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ protected synchronized void doExport() {
checkRegistry();
checkProtocol();
appendProperties(this);
checkStubAndMock(interfaceClass);
checkStub(interfaceClass);
checkMock(interfaceClass);
if (path == null || path.length() == 0) {
path = interfaceName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ public void checkName() throws Exception {
@Test
public void checkNameHasSymbol() throws Exception {
try {
AbstractConfig.checkNameHasSymbol("hello", ":*,/-0123abcdABCD");
AbstractConfig.checkNameHasSymbol("hello", ":*,/ -0123\tabcdABCD");
AbstractConfig.checkNameHasSymbol("mock", "force:return world");
} catch (Exception e) {
TestCase.fail("the value should be legal.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,63 +185,72 @@ public void checkInterfaceAndMethod5() throws Exception {
public void checkStubAndMock1() throws Exception {
InterfaceConfig interfaceConfig = new InterfaceConfig();
interfaceConfig.setLocal(GreetingLocal1.class.getName());
interfaceConfig.checkStubAndMock(Greeting.class);
interfaceConfig.checkStub(Greeting.class);
interfaceConfig.checkMock(Greeting.class);
}

@Test(expected = IllegalStateException.class)
public void checkStubAndMock2() throws Exception {
InterfaceConfig interfaceConfig = new InterfaceConfig();
interfaceConfig.setLocal(GreetingLocal2.class.getName());
interfaceConfig.checkStubAndMock(Greeting.class);
interfaceConfig.checkStub(Greeting.class);
interfaceConfig.checkMock(Greeting.class);
}

@Test
public void checkStubAndMock3() throws Exception {
InterfaceConfig interfaceConfig = new InterfaceConfig();
interfaceConfig.setLocal(GreetingLocal3.class.getName());
interfaceConfig.checkStubAndMock(Greeting.class);
interfaceConfig.checkStub(Greeting.class);
interfaceConfig.checkMock(Greeting.class);
}

@Test(expected = IllegalStateException.class)
public void checkStubAndMock4() throws Exception {
InterfaceConfig interfaceConfig = new InterfaceConfig();
interfaceConfig.setStub(GreetingLocal1.class.getName());
interfaceConfig.checkStubAndMock(Greeting.class);
interfaceConfig.checkStub(Greeting.class);
interfaceConfig.checkMock(Greeting.class);
}

@Test(expected = IllegalStateException.class)
public void checkStubAndMock5() throws Exception {
InterfaceConfig interfaceConfig = new InterfaceConfig();
interfaceConfig.setStub(GreetingLocal2.class.getName());
interfaceConfig.checkStubAndMock(Greeting.class);
interfaceConfig.checkStub(Greeting.class);
interfaceConfig.checkMock(Greeting.class);
}

@Test
public void checkStubAndMock6() throws Exception {
InterfaceConfig interfaceConfig = new InterfaceConfig();
interfaceConfig.setStub(GreetingLocal3.class.getName());
interfaceConfig.checkStubAndMock(Greeting.class);
interfaceConfig.checkStub(Greeting.class);
interfaceConfig.checkMock(Greeting.class);
}

@Test(expected = IllegalStateException.class)
public void checkStubAndMock7() throws Exception {
InterfaceConfig interfaceConfig = new InterfaceConfig();
interfaceConfig.setMock("return {a, b}");
interfaceConfig.checkStubAndMock(Greeting.class);
interfaceConfig.checkStub(Greeting.class);
interfaceConfig.checkMock(Greeting.class);
}

@Test(expected = IllegalStateException.class)
public void checkStubAndMock8() throws Exception {
InterfaceConfig interfaceConfig = new InterfaceConfig();
interfaceConfig.setMock(GreetingMock1.class.getName());
interfaceConfig.checkStubAndMock(Greeting.class);
interfaceConfig.checkStub(Greeting.class);
interfaceConfig.checkMock(Greeting.class);
}

@Test(expected = IllegalStateException.class)
public void checkStubAndMock9() throws Exception {
InterfaceConfig interfaceConfig = new InterfaceConfig();
interfaceConfig.setMock(GreetingMock2.class.getName());
interfaceConfig.checkStubAndMock(Greeting.class);
interfaceConfig.checkStub(Greeting.class);
interfaceConfig.checkMock(Greeting.class);
}

@Test
Expand Down
Loading

0 comments on commit 99ecafa

Please sign in to comment.