Skip to content

Commit e336919

Browse files
committed
Issue #130: Add new tests to check every possible ordering of JSON
The DSP implementation created some new tests for checking every order of fields in a JSON message. This commit adapts the same test for LSP messages. Signed-off-by: Jonah Graham <[email protected]>
1 parent 8be8e5a commit e336919

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

org.eclipse.lsp4j.jsonrpc/src/test/java/org/eclipse/lsp4j/jsonrpc/test/json/MessageJsonHandlerTest.java

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
*******************************************************************************/
88
package org.eclipse.lsp4j.jsonrpc.test.json;
99

10+
import java.util.HashMap;
11+
import java.util.HashSet;
1012
import java.util.LinkedHashMap;
1113
import java.util.List;
1214
import java.util.Map;
1315
import java.util.Set;
16+
import java.util.function.Consumer;
1417

1518
import org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod;
1619
import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler;
1720
import org.eclipse.lsp4j.jsonrpc.messages.Either;
1821
import org.eclipse.lsp4j.jsonrpc.messages.Message;
22+
import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage;
1923
import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage;
2024
import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage;
2125
import org.junit.Assert;
@@ -496,4 +500,164 @@ public void testMultiParamsParsing_04() {
496500
Assert.assertEquals("[1, 2]", parameters.get(1).toString());
497501
Assert.assertNull(parameters.get(2));
498502
}
503+
504+
public static final <T> void swap(T[] a, int i, int j) {
505+
T t = a[i];
506+
a[i] = a[j];
507+
a[j] = t;
508+
}
509+
510+
public <T> void testAllPermutationsInner(T[] array, int i, int n, Consumer<T[]> test) {
511+
int j;
512+
if (i == n) {
513+
test.accept(array);
514+
} else {
515+
for (j = i; j <= n; j++) {
516+
swap(array, i, j);
517+
testAllPermutationsInner(array, i + 1, n, test);
518+
swap(array, i, j);
519+
}
520+
}
521+
}
522+
523+
public <T> void testAllPermutationsStart(T[] array, Consumer<T[]> test) {
524+
testAllPermutationsInner(array, 0, array.length - 1, test);
525+
}
526+
527+
public void testAllPermutations(String[] properties, Consumer<String> test) {
528+
testAllPermutationsStart(properties, mutatedProperties -> {
529+
StringBuilder json = new StringBuilder();
530+
json.append("{");
531+
for (int k = 0; k < mutatedProperties.length; k++) {
532+
json.append(mutatedProperties[k]);
533+
if (k != mutatedProperties.length - 1) {
534+
json.append(",");
535+
}
536+
537+
}
538+
json.append("}");
539+
String jsonString = json.toString();
540+
try {
541+
test.accept(jsonString);
542+
} catch (Exception | AssertionError e) {
543+
// To make it easier to debug a failing test, add another exception
544+
// layer that shows the version of the json used -- you may
545+
// need to turn off "Filter Stack Trace" in JUnit view in Eclipse
546+
// to see the underlying error.
547+
throw new AssertionError("Failed with this input json: " + jsonString, e);
548+
}
549+
});
550+
}
551+
552+
@Test
553+
public void testThePermutationsTest() {
554+
// make sure that the testAllPermutations works as expected
555+
Set<String> collectedPermutations = new HashSet<>();
556+
Set<String> expectedPermutations = new HashSet<>();
557+
expectedPermutations.add("{a,b,c}");
558+
expectedPermutations.add("{a,c,b}");
559+
expectedPermutations.add("{b,a,c}");
560+
expectedPermutations.add("{b,c,a}");
561+
expectedPermutations.add("{c,a,b}");
562+
expectedPermutations.add("{c,b,a}");
563+
testAllPermutations(new String[] {"a", "b", "c"}, perm -> collectedPermutations.add(perm));
564+
Assert.assertEquals(expectedPermutations, collectedPermutations);
565+
}
566+
567+
@Test
568+
public void testRequest_AllOrders() {
569+
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
570+
supportedMethods.put("foo", JsonRpcMethod.request("foo",
571+
new TypeToken<Void>() {}.getType(),
572+
new TypeToken<Location>() {
573+
}.getType()));
574+
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
575+
handler.setMethodProvider((id) -> "foo");
576+
String[] properties = new String[] {
577+
"\"jsonrpc\":\"2.0\"",
578+
"\"id\":2",
579+
"\"method\":\"foo\"",
580+
"\"params\": {\"uri\": \"dummy://mymodel.mydsl\"}"
581+
};
582+
testAllPermutations(properties, json -> {
583+
RequestMessage message = (RequestMessage) handler.parseMessage(json);
584+
Object params = message.getParams();
585+
Class<? extends Object> class1 = params.getClass();
586+
Assert.assertEquals(Location.class, class1);
587+
Assert.assertEquals("dummy://mymodel.mydsl", ((Location)params).uri);
588+
});
589+
}
590+
591+
@Test
592+
public void testNormalResponse_AllOrders() {
593+
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
594+
supportedMethods.put("foo", JsonRpcMethod.request("foo",
595+
new TypeToken<Location>() {}.getType(),
596+
new TypeToken<Void>() {
597+
}.getType()));
598+
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
599+
handler.setMethodProvider((id) -> "foo");
600+
String[] properties = new String[] {
601+
"\"jsonrpc\":\"2.0\"",
602+
"\"id\":2",
603+
"\"result\": {\"uri\": \"dummy://mymodel.mydsl\"}"
604+
};
605+
testAllPermutations(properties, json -> {
606+
ResponseMessage message = (ResponseMessage) handler.parseMessage(json);
607+
Object result = message.getResult();
608+
Class<? extends Object> class1 = result.getClass();
609+
Assert.assertEquals(Location.class, class1);
610+
Assert.assertEquals("dummy://mymodel.mydsl", ((Location)result).uri);
611+
Assert.assertNull(message.getError());
612+
});
613+
}
614+
615+
@Test
616+
public void testErrorResponse_AllOrders() {
617+
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
618+
supportedMethods.put("foo", JsonRpcMethod.request("foo",
619+
new TypeToken<Location>() {}.getType(),
620+
new TypeToken<Void>() {
621+
}.getType()));
622+
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
623+
handler.setMethodProvider((id) -> "foo");
624+
String[] properties = new String[] {
625+
"\"jsonrpc\":\"2.0\"",
626+
"\"id\":2",
627+
"\"message\": \"failed\"",
628+
"\"error\": {\"code\": 123456, \"message\": \"failed\", \"data\": {\"uri\": \"failed\"}}"
629+
};
630+
testAllPermutations(properties, json -> {
631+
ResponseMessage message = (ResponseMessage) handler.parseMessage(json);
632+
Assert.assertEquals("failed", message.getError().getMessage());
633+
Object data = message.getError().getData();
634+
Map<String, String> expected = new HashMap<>();
635+
expected.put("uri", "failed");
636+
Assert.assertEquals(expected, data);
637+
Assert.assertNull(message.getResult());
638+
});
639+
}
640+
641+
@Test
642+
public void testNotification_AllOrders() {
643+
Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
644+
supportedMethods.put("foo", JsonRpcMethod.request("foo",
645+
new TypeToken<Void>() {}.getType(),
646+
new TypeToken<Location>() {
647+
}.getType()));
648+
MessageJsonHandler handler = new MessageJsonHandler(supportedMethods);
649+
handler.setMethodProvider((id) -> "foo");
650+
String[] properties = new String[] {
651+
"\"jsonrpc\":\"2.0\"",
652+
"\"method\":\"foo\"",
653+
"\"params\": {\"uri\": \"dummy://mymodel.mydsl\"}"
654+
};
655+
testAllPermutations(properties, json -> {
656+
NotificationMessage message = (NotificationMessage) handler.parseMessage(json);
657+
Object params = message.getParams();
658+
Class<? extends Object> class1 = params.getClass();
659+
Assert.assertEquals(Location.class, class1);
660+
Assert.assertEquals("dummy://mymodel.mydsl", ((Location)params).uri);
661+
});
662+
}
499663
}

0 commit comments

Comments
 (0)