1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.comp.bridgefactory;
25 
26 import com.sun.star.bridge.BridgeExistsException;
27 import com.sun.star.bridge.XBridge;
28 import com.sun.star.comp.connections.PipedConnection;
29 import com.sun.star.connection.XConnection;
30 import com.sun.star.lang.XComponent;
31 import com.sun.star.uno.UnoRuntime;
32 
33 import org.junit.Test;
34 import static org.junit.Assert.*;
35 
36 public final class BridgeFactory_Test {
37     @Test
test()38     public void test() throws Exception {
39         PipedConnection rightSide = new PipedConnection(new Object[0]);
40         PipedConnection leftSide = new PipedConnection(new Object[]{rightSide});
41 
42         BridgeFactory bridgeFactory = new BridgeFactory(); // create the needed bridgeFactory
43 
44         // create a bridge
45         XBridge xBridge = bridgeFactory.createBridge("testbridge", "urp", (XConnection)leftSide, null);
46 
47         // test that we get the same bridge
48         assertTrue("", UnoRuntime.areSame(xBridge,
49                                       bridgeFactory.getBridge("testbridge")));
50 
51         // test that we can not create another bridge with same name
52         try {
53             XBridge dummy = bridgeFactory.createBridge("testbridge", "urp", (XConnection)leftSide, null);
54 
55             fail("");
56         }
57         catch(BridgeExistsException bridgeExistsException) {
58         }
59 
60 
61         // test getExistingBridges
62         XBridge xBridges[] = bridgeFactory.getExistingBridges();
63         assertTrue("", UnoRuntime.areSame(xBridge, xBridges[0]));
64 
65         // dispose the bridge
66         XComponent xComponent = UnoRuntime.queryInterface(XComponent.class, xBridge);
67         xComponent.dispose();
68 
69 
70         // test that the bridge has been removed
71         assertTrue("", bridgeFactory.getBridge("testbridge") == null);
72 
73 
74 
75         rightSide = new PipedConnection(new Object[0]);
76         leftSide = new PipedConnection(new Object[]{rightSide});
77 
78 
79         // test that we really get a new bridge
80         XBridge xBridge_new = bridgeFactory.createBridge("testbridge", "urp", (XConnection)leftSide, null);
81         assertTrue("", !UnoRuntime.areSame(xBridge, xBridge_new));
82 
83         for(int i = 0; i <10000; ++ i) {
84             Object x[] = new Object[100];
85         }
86 
87         // test getExistingBridges
88         xBridges = bridgeFactory.getExistingBridges();
89         assertTrue("",
90                xBridges.length == 1
91                && UnoRuntime.areSame(xBridge_new, xBridges[0]));
92 
93         // dispose the new bridge
94         XComponent xComponent_new = UnoRuntime.queryInterface(XComponent.class, xBridge_new);
95         xComponent_new.dispose();
96     }
97 }
98