Debian Codegraph @

junixsocket-rmi/src/test/java/org/newsclub/net/unix/rmi/TestBase.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 java.io.IOException;
21 import java.rmi.AccessException;
22 import java.rmi.AlreadyBoundException;
23 import java.rmi.NotBoundException;
24 import java.rmi.RemoteException;
25 import java.rmi.registry.Registry;
26 import java.rmi.server.RemoteObject;
27
28 import org.junit.jupiter.api.AfterEach;
29 import org.junit.jupiter.api.BeforeEach;
30
31 // must remain public until SUREFIRE-1909 is fixed
32 public class TestBase extends ShutdownHookTestBase {
33 private static final String TEST_SERVICE_NAME = TestService.class.getName();
34 private AFNaming naming;
35 private TestServiceImpl testService;
36
37 protected TestBase() {
38 super();
39 }
40
41 protected AFNaming newNamingTestInstance() throws IOException {
42 return AFUNIXNaming.newPrivateInstance();
43 }
44
45 @BeforeEach
46 public void setUp() throws IOException, AlreadyBoundException {
47 naming = newNamingTestInstance();
48
49 // Create registry
50 Registry registry = naming.createRegistry();
51
52 // Create and bind service
53 AFRMISocketFactory sf = naming.getSocketFactory();
54 if (sf instanceof AFUNIXRMISocketFactory) {
55 testService = new TestServiceImpl((AFUNIXRMISocketFactory) sf);
56 } else {
57 testService = null;
58 }
59 registry.bind(TEST_SERVICE_NAME, RemoteObject.toStub(testService));
60 }
61
62 @AfterEach
63 public void tearDown() throws IOException {
64 if (testService != null) {
65 testService.close();
66 }
67 if (naming != null) {
68 naming.shutdownRegistry();
69 }
70 }
71
72 protected TestService lookupTestService() throws AccessException, RemoteException,
73 NotBoundException {
74 return (TestService) naming.getRegistry().lookup(TEST_SERVICE_NAME);
75 }
76
77 protected AFRMISocketFactory namingSocketFactory() {
78 return naming.getSocketFactory();
79 }
80 }