| 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.AlreadyBoundException; |
| 25 |
import java.rmi.NotBoundException; |
| 26 |
import java.rmi.server.ExportException; |
| 27 |
import java.util.Arrays; |
| 28 |
import java.util.HashSet; |
| 29 |
import java.util.concurrent.TimeUnit; |
| 30 |
|
| 31 |
import org.junit.jupiter.api.Test; |
| 32 |
import org.newsclub.net.unix.AFSocketCapability; |
| 33 |
|
| 34 |
import com.kohlschutter.annotations.compiletime.SuppressFBWarnings; |
| 35 |
|
| 36 |
@SuppressFBWarnings({ |
| 37 |
"THROWS_METHOD_THROWS_CLAUSE_THROWABLE", "THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION"}) |
| 38 |
@AFSocketCapabilityRequirement({AFSocketCapability.CAPABILITY_UNIX_DOMAIN}) |
| 39 |
public class RegistryTest extends ShutdownHookTestBase { |
| 40 |
@Test |
| 41 |
public void testDoubleCreateRegistry() throws IOException { |
| 42 |
AFNaming naming = AFUNIXNaming.newPrivateInstance(); |
| 43 |
naming.createRegistry(); |
| 44 |
naming.createRegistry(); |
| 45 |
naming.shutdownRegistry(); |
| 46 |
} |
| 47 |
|
| 48 |
@Test |
| 49 |
public void testExportAndBind() throws IOException, AlreadyBoundException, NotBoundException { |
| 50 |
AFUNIXNaming naming = AFUNIXNaming.newPrivateInstance(); |
| 51 |
|
| 52 |
assertEquals(naming.getRegistrySocketDir(), naming.getSocketFactory().getSocketDir()); |
| 53 |
|
| 54 |
naming.createRegistry(); |
| 55 |
assertEquals(Arrays.asList(AFRMIService.class.getName()), Arrays.asList(naming.list())); |
| 56 |
|
| 57 |
Hello hello = new HelloImpl(); |
| 58 |
|
| 59 |
assertThrows(NotBoundException.class, () -> naming.lookup("hello", 1, TimeUnit.MILLISECONDS)); |
| 60 |
|
| 61 |
naming.exportAndBind("hello", hello); |
| 62 |
assertThrows(ExportException.class, () -> naming.exportAndBind("hello", hello)); |
| 63 |
naming.bind("hello2", hello); |
| 64 |
naming.rebind("hello2", hello); |
| 65 |
assertThrows(AlreadyBoundException.class, () -> naming.bind("hello2", hello)); |
| 66 |
assertEquals(new HashSet<>(Arrays.asList(AFRMIService.class.getName(), "hello", "hello2")), |
| 67 |
new HashSet<>(Arrays.asList(naming.list()))); |
| 68 |
|
| 69 |
assertEquals("Hello", ((Hello) naming.lookup("hello2")).hello()); |
| 70 |
assertEquals("Hello", ((Hello) naming.lookup("hello2", 1, TimeUnit.MILLISECONDS)).hello()); |
| 71 |
|
| 72 |
naming.exportAndRebind("hello2", new HelloImpl()); |
| 73 |
|
| 74 |
naming.unbind("hello2"); |
| 75 |
naming.unexportAndUnbind("hello", hello); |
| 76 |
assertEquals(Arrays.asList(AFRMIService.class.getName()), Arrays.asList(naming.list())); |
| 77 |
|
| 78 |
naming.shutdownRegistry(); |
| 79 |
|
| 80 |
assertEquals(Arrays.asList(), Arrays.asList(naming.list())); |
| 81 |
} |
| 82 |
} |