|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.internal.rpc; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.openrewrite.Tree; |
| 21 | +import org.openrewrite.java.tree.J; |
| 22 | +import org.openrewrite.java.tree.JContainer; |
| 23 | +import org.openrewrite.java.tree.JavaType; |
| 24 | +import org.openrewrite.java.tree.Space; |
| 25 | +import org.openrewrite.marker.Markers; |
| 26 | +import org.openrewrite.rpc.RpcObjectData; |
| 27 | +import org.openrewrite.rpc.RpcReceiveQueue; |
| 28 | +import org.openrewrite.rpc.RpcSendQueue; |
| 29 | + |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.util.*; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | + |
| 35 | +class JavaReceiverTest { |
| 36 | + |
| 37 | + private Deque<List<RpcObjectData>> batches; |
| 38 | + private RpcSendQueue sq; |
| 39 | + private RpcReceiveQueue rq; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + void setUp() { |
| 43 | + batches = new ArrayDeque<>(); |
| 44 | + String sourceFileType = J.CompilationUnit.class.getName(); |
| 45 | + IdentityHashMap<Object, Integer> localRefs = new IdentityHashMap<>(); |
| 46 | + sq = new RpcSendQueue(1, e -> batches.addLast(encode(e)), localRefs, sourceFileType, false); |
| 47 | + rq = new RpcReceiveQueue(new HashMap<>(), batches::removeFirst, sourceFileType, null); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void methodInvocationRoundTripPreservesNameType() { |
| 52 | + // given |
| 53 | + JavaType.Method methodType = new JavaType.Method( |
| 54 | + null, 0, null, "foo", null, |
| 55 | + Collections.emptyList(), Collections.emptyList(), |
| 56 | + Collections.emptyList(), Collections.emptyList(), |
| 57 | + null, null |
| 58 | + ); |
| 59 | + J.Identifier name = new J.Identifier( |
| 60 | + Tree.randomId(), Space.EMPTY, Markers.EMPTY, |
| 61 | + Collections.emptyList(), "foo", methodType, null |
| 62 | + ); |
| 63 | + J.MethodInvocation original = new J.MethodInvocation( |
| 64 | + Tree.randomId(), Space.EMPTY, Markers.EMPTY, |
| 65 | + null, null, name, |
| 66 | + JContainer.build(Space.EMPTY, Collections.emptyList(), Markers.EMPTY), |
| 67 | + methodType |
| 68 | + ); |
| 69 | + |
| 70 | + // when: send as ADD (from null) and receive — exercises the actual JavaSender/JavaReceiver |
| 71 | + sq.send(original, null, null); |
| 72 | + sq.flush(); |
| 73 | + J.MethodInvocation received = rq.receive(null); |
| 74 | + |
| 75 | + // then |
| 76 | + assertThat(received.getMethodType()).isNotNull(); |
| 77 | + assertThat(received.getName().getType()) |
| 78 | + .as("Name identifier type must survive the sender/receiver round trip") |
| 79 | + .isNotNull(); |
| 80 | + } |
| 81 | + |
| 82 | + private List<RpcObjectData> encode(List<RpcObjectData> batch) { |
| 83 | + List<RpcObjectData> encoded = new ArrayList<>(); |
| 84 | + for (RpcObjectData data : batch) { |
| 85 | + if (data.getValue() instanceof UUID || data.getValue() instanceof Path) { |
| 86 | + encoded.add(new RpcObjectData(data.getState(), data.getValueType(), data.getValue().toString(), data.getRef(), false)); |
| 87 | + } else { |
| 88 | + encoded.add(data); |
| 89 | + } |
| 90 | + } |
| 91 | + return encoded; |
| 92 | + } |
| 93 | +} |
0 commit comments