Skip to content

Commit 5da01b1

Browse files
beiwei30chickenlj
authored andcommitted
[DUBBO-2489] MockClusterInvoker provides local forced mock,I tested it locally, but it doesn't work (#2739)
1 parent f2d73f7 commit 5da01b1

File tree

8 files changed

+151
-94
lines changed

8 files changed

+151
-94
lines changed

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public abstract class AbstractConfig implements Serializable {
5656

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

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

6161
private static final Pattern PATTERN_KEY = Pattern.compile("[*,\\-._0-9a-zA-Z]+");
6262
private static final Map<String, String> legacyProperties = new HashMap<String, String>();

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractInterfaceConfig.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,36 @@ protected void checkInterfaceAndMethods(Class<?> interfaceClass, List<MethodConf
288288
}
289289
}
290290

291-
protected void checkStubAndMock(Class<?> interfaceClass) {
291+
void checkMock(Class<?> interfaceClass) {
292+
if (ConfigUtils.isEmpty(mock)) {
293+
return;
294+
}
295+
296+
String normalizedMock = MockInvoker.normalizeMock(mock);
297+
if (normalizedMock.startsWith(Constants.RETURN_PREFIX)) {
298+
normalizedMock = normalizedMock.substring(Constants.RETURN_PREFIX.length()).trim();
299+
try {
300+
MockInvoker.parseMockValue(normalizedMock);
301+
} catch (Exception e) {
302+
throw new IllegalStateException("Illegal mock return in <dubbo:service/reference ... " +
303+
"mock=\"" + mock + "\" />");
304+
}
305+
} else if (normalizedMock.startsWith(Constants.THROW_PREFIX)) {
306+
normalizedMock = normalizedMock.substring(Constants.THROW_PREFIX.length()).trim();
307+
if (ConfigUtils.isNotEmpty(normalizedMock)) {
308+
try {
309+
MockInvoker.getThrowable(normalizedMock);
310+
} catch (Exception e) {
311+
throw new IllegalStateException("Illegal mock throw in <dubbo:service/reference ... " +
312+
"mock=\"" + mock + "\" />");
313+
}
314+
}
315+
} else {
316+
MockInvoker.getMockObject(normalizedMock, interfaceClass);
317+
}
318+
}
319+
320+
void checkStub(Class<?> interfaceClass) {
292321
if (ConfigUtils.isNotEmpty(local)) {
293322
Class<?> localClass = ConfigUtils.isDefault(local) ? ReflectUtils.forName(interfaceClass.getName() + "Local") : ReflectUtils.forName(local);
294323
if (!interfaceClass.isAssignableFrom(localClass)) {
@@ -311,26 +340,6 @@ protected void checkStubAndMock(Class<?> interfaceClass) {
311340
throw new IllegalStateException("No such constructor \"public " + localClass.getSimpleName() + "(" + interfaceClass.getName() + ")\" in local implementation class " + localClass.getName());
312341
}
313342
}
314-
if (ConfigUtils.isNotEmpty(mock)) {
315-
if (mock.startsWith(Constants.RETURN_PREFIX)) {
316-
String value = mock.substring(Constants.RETURN_PREFIX.length());
317-
try {
318-
MockInvoker.parseMockValue(value);
319-
} catch (Exception e) {
320-
throw new IllegalStateException("Illegal mock json value in <dubbo:service ... mock=\"" + mock + "\" />");
321-
}
322-
} else {
323-
Class<?> mockClass = ConfigUtils.isDefault(mock) ? ReflectUtils.forName(interfaceClass.getName() + "Mock") : ReflectUtils.forName(mock);
324-
if (!interfaceClass.isAssignableFrom(mockClass)) {
325-
throw new IllegalStateException("The mock implementation class " + mockClass.getName() + " not implement interface " + interfaceClass.getName());
326-
}
327-
try {
328-
mockClass.getConstructor(new Class<?>[0]);
329-
} catch (NoSuchMethodException e) {
330-
throw new IllegalStateException("No such empty constructor \"public " + mockClass.getSimpleName() + "()\" in mock implementation class " + mockClass.getName());
331-
}
332-
}
333-
}
334343
}
335344

336345
/**
@@ -525,4 +534,4 @@ public void setScope(String scope) {
525534
this.scope = scope;
526535
}
527536

528-
}
537+
}

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/AbstractMethodConfig.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,14 @@ public void setMock(Boolean mock) {
127127
}
128128

129129
public void setMock(String mock) {
130-
if (mock != null && mock.startsWith(Constants.RETURN_PREFIX)) {
130+
if (mock == null) {
131+
return;
132+
}
133+
134+
if (mock.startsWith(Constants.RETURN_PREFIX) || mock.startsWith(Constants.THROW_PREFIX + " ")) {
131135
checkLength("mock", mock);
136+
} else if (mock.startsWith(Constants.FAIL_PREFIX) || mock.startsWith(Constants.FORCE_PREFIX)) {
137+
checkNameHasSymbol("mock", mock);
132138
} else {
133139
checkName("mock", mock);
134140
}
@@ -168,4 +174,4 @@ public void setParameters(Map<String, String> parameters) {
168174
this.parameters = parameters;
169175
}
170176

171-
}
177+
}

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ReferenceConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ private void init() {
276276
}
277277
}
278278
checkApplication();
279-
checkStubAndMock(interfaceClass);
279+
checkStub(interfaceClass);
280+
checkMock(interfaceClass);
280281
Map<String, String> map = new HashMap<String, String>();
281282
Map<Object, Object> attributes = new HashMap<Object, Object>();
282283
map.put(Constants.SIDE_KEY, Constants.CONSUMER_SIDE);

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ protected synchronized void doExport() {
310310
checkRegistry();
311311
checkProtocol();
312312
appendProperties(this);
313-
checkStubAndMock(interfaceClass);
313+
checkStub(interfaceClass);
314+
checkMock(interfaceClass);
314315
if (path == null || path.length() == 0) {
315316
path = interfaceName;
316317
}

dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractConfigTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ public void checkName() throws Exception {
198198
@Test
199199
public void checkNameHasSymbol() throws Exception {
200200
try {
201-
AbstractConfig.checkNameHasSymbol("hello", ":*,/-0123abcdABCD");
201+
AbstractConfig.checkNameHasSymbol("hello", ":*,/ -0123\tabcdABCD");
202+
AbstractConfig.checkNameHasSymbol("mock", "force:return world");
202203
} catch (Exception e) {
203204
TestCase.fail("the value should be legal.");
204205
}

dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/AbstractInterfaceConfigTest.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,63 +178,72 @@ public void checkInterfaceAndMethod5() throws Exception {
178178
public void checkStubAndMock1() throws Exception {
179179
InterfaceConfig interfaceConfig = new InterfaceConfig();
180180
interfaceConfig.setLocal(GreetingLocal1.class.getName());
181-
interfaceConfig.checkStubAndMock(Greeting.class);
181+
interfaceConfig.checkStub(Greeting.class);
182+
interfaceConfig.checkMock(Greeting.class);
182183
}
183184

184185
@Test(expected = IllegalStateException.class)
185186
public void checkStubAndMock2() throws Exception {
186187
InterfaceConfig interfaceConfig = new InterfaceConfig();
187188
interfaceConfig.setLocal(GreetingLocal2.class.getName());
188-
interfaceConfig.checkStubAndMock(Greeting.class);
189+
interfaceConfig.checkStub(Greeting.class);
190+
interfaceConfig.checkMock(Greeting.class);
189191
}
190192

191193
@Test
192194
public void checkStubAndMock3() throws Exception {
193195
InterfaceConfig interfaceConfig = new InterfaceConfig();
194196
interfaceConfig.setLocal(GreetingLocal3.class.getName());
195-
interfaceConfig.checkStubAndMock(Greeting.class);
197+
interfaceConfig.checkStub(Greeting.class);
198+
interfaceConfig.checkMock(Greeting.class);
196199
}
197200

198201
@Test(expected = IllegalStateException.class)
199202
public void checkStubAndMock4() throws Exception {
200203
InterfaceConfig interfaceConfig = new InterfaceConfig();
201204
interfaceConfig.setStub(GreetingLocal1.class.getName());
202-
interfaceConfig.checkStubAndMock(Greeting.class);
205+
interfaceConfig.checkStub(Greeting.class);
206+
interfaceConfig.checkMock(Greeting.class);
203207
}
204208

205209
@Test(expected = IllegalStateException.class)
206210
public void checkStubAndMock5() throws Exception {
207211
InterfaceConfig interfaceConfig = new InterfaceConfig();
208212
interfaceConfig.setStub(GreetingLocal2.class.getName());
209-
interfaceConfig.checkStubAndMock(Greeting.class);
213+
interfaceConfig.checkStub(Greeting.class);
214+
interfaceConfig.checkMock(Greeting.class);
210215
}
211216

212217
@Test
213218
public void checkStubAndMock6() throws Exception {
214219
InterfaceConfig interfaceConfig = new InterfaceConfig();
215220
interfaceConfig.setStub(GreetingLocal3.class.getName());
216-
interfaceConfig.checkStubAndMock(Greeting.class);
221+
interfaceConfig.checkStub(Greeting.class);
222+
interfaceConfig.checkMock(Greeting.class);
217223
}
218224

219225
@Test(expected = IllegalStateException.class)
220226
public void checkStubAndMock7() throws Exception {
221227
InterfaceConfig interfaceConfig = new InterfaceConfig();
222228
interfaceConfig.setMock("return {a, b}");
223-
interfaceConfig.checkStubAndMock(Greeting.class);
229+
interfaceConfig.checkStub(Greeting.class);
230+
interfaceConfig.checkMock(Greeting.class);
224231
}
225232

226233
@Test(expected = IllegalStateException.class)
227234
public void checkStubAndMock8() throws Exception {
228235
InterfaceConfig interfaceConfig = new InterfaceConfig();
229236
interfaceConfig.setMock(GreetingMock1.class.getName());
230-
interfaceConfig.checkStubAndMock(Greeting.class);
237+
interfaceConfig.checkStub(Greeting.class);
238+
interfaceConfig.checkMock(Greeting.class);
231239
}
232240

233241
@Test(expected = IllegalStateException.class)
234242
public void checkStubAndMock9() throws Exception {
235243
InterfaceConfig interfaceConfig = new InterfaceConfig();
236244
interfaceConfig.setMock(GreetingMock2.class.getName());
237-
interfaceConfig.checkStubAndMock(Greeting.class);
245+
interfaceConfig.checkStub(Greeting.class);
246+
interfaceConfig.checkMock(Greeting.class);
238247
}
239248

240249
@Test

0 commit comments

Comments
 (0)