Debian Codegraph @

junixsocket-rmi/src/test/java/org/newsclub/net/unix/rmi/RemoteCloseableTest.java

raw
1 /*
2 * junixsocket
3 *
4 * Copyright 2009-2022 Christian Kohlschütter
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.newsclub.net.unix.rmi;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import java.io.IOException;
24 import java.rmi.NoSuchObjectException;
25 import java.rmi.NotBoundException;
26 import java.util.Objects;
27
28 import org.eclipse.jdt.annotation.NonNull;
29 import org.junit.jupiter.api.Test;
30 import org.newsclub.net.unix.AFSocketCapability;
31 import org.newsclub.net.unix.rmi.RemoteCloseableThing.IsCloseable;
32 import org.newsclub.net.unix.rmi.RemoteCloseableThing.NotCloseable;
33
34 import com.kohlschutter.annotations.compiletime.SuppressFBWarnings;
35
36 /**
37 * Tests {@link RemoteCloseable}.
38 *
39 * @author Christian Kohlschütter
40 */
41 @SuppressFBWarnings({
42 "THROWS_METHOD_THROWS_CLAUSE_THROWABLE", "THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION"})
43 @AFSocketCapabilityRequirement({AFSocketCapability.CAPABILITY_UNIX_DOMAIN})
44 public class RemoteCloseableTest extends TestBase {
45 public RemoteCloseableTest() throws IOException {
46 super();
47 }
48
49 @Test
50 public void testRemoteCloseableWithACloseableThing() throws IOException, NotBoundException {
51 TestService svc = lookupTestService();
52
53 svc.remoteCloseableThingResetNumberOfCloseCalls(IsCloseable.class);
54
55 try (RemoteCloseable<? extends IsCloseable> remoteCloseable = svc.remoteCloseable(
56 IsCloseable.class)) {
57 try (IsCloseable testCloseable = remoteCloseable.get()) {
58 assertEquals(0, svc.remoteCloseableThingNumberOfCloseCalls(IsCloseable.class));
59 // forcibly calling close here unexports the RemoteCloseable
60 remoteCloseable.close();
61 assertEquals(1, svc.remoteCloseableThingNumberOfCloseCalls(IsCloseable.class));
62
63 assertThrows(NoSuchObjectException.class, () -> {
64 remoteCloseable.close();
65 });
66 }
67 } catch (NoSuchObjectException e) {
68 // expected — since the object was forcibly closed above, it was unexported already.
69 // ideally, RMI could gracefully handle calling #close() on an proxy that points to an
70 // unexported object.
71 }
72 assertEquals(1, svc.remoteCloseableThingNumberOfCloseCalls(IsCloseable.class));
73
74 try (RemoteCloseable<? extends IsCloseable> remoteCloseable = svc.remoteCloseable(
75 IsCloseable.class)) {
76 try (IsCloseable testCloseable = remoteCloseable.get()) {
77 // no exception thrown
78 }
79 }
80
81 // The underlying object still remains on the server-side, so the call count keeps going up
82 assertEquals(2, svc.remoteCloseableThingNumberOfCloseCalls(IsCloseable.class));
83 }
84
85 @Test
86 public void testRemoteCloseableWithANotCloseableThing() throws IOException, NotBoundException {
87 TestService svc = lookupTestService();
88
89 svc.remoteCloseableThingResetNumberOfCloseCalls(NotCloseable.class);
90
91 try (RemoteCloseable<? extends NotCloseable> remoteCloseable = svc.remoteCloseable(
92 NotCloseable.class)) {
93 @SuppressWarnings("null")
94 @NonNull
95 RemoteCloseableThing testNotCloseable = remoteCloseable.get();
96 Objects.requireNonNull(testNotCloseable.toString());
97 assertEquals(0, svc.remoteCloseableThingNumberOfCloseCalls(NotCloseable.class));
98 // forcibly calling close here unexports the RemoteCloseable
99 remoteCloseable.close();
100 assertEquals(0, svc.remoteCloseableThingNumberOfCloseCalls(NotCloseable.class));
101
102 assertThrows(NoSuchObjectException.class, () -> {
103 remoteCloseable.close();
104 });
105 } catch (NoSuchObjectException e) {
106 // expected — since the object was forcibly closed above, it was unexported already.
107 // ideally, RMI could gracefully handle calling #close() on an proxy that points to an
108 // unexported object.
109 }
110 assertEquals(0, svc.remoteCloseableThingNumberOfCloseCalls(NotCloseable.class));
111
112 try (RemoteCloseable<? extends NotCloseable> remoteCloseable = svc.remoteCloseable(
113 NotCloseable.class)) {
114 // no exception thrown
115 }
116
117 // The underlying object still remains on the server-side, so the call count keeps going up
118 assertEquals(0, svc.remoteCloseableThingNumberOfCloseCalls(NotCloseable.class));
119 }
120 }