1 | /* Copyright (C) 2013 Red Hat
|
---|
2 |
|
---|
3 | This file is part of IcedTea.
|
---|
4 |
|
---|
5 | IcedTea is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation; either version 2, or (at your option)
|
---|
8 | any later version.
|
---|
9 |
|
---|
10 | IcedTea is distributed in the hope that it will be useful, but
|
---|
11 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with IcedTea; see the file COPYING. If not, write to the
|
---|
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
---|
18 | 02110-1301 USA.
|
---|
19 |
|
---|
20 | Linking this library statically or dynamically with other modules is
|
---|
21 | making a combined work based on this library. Thus, the terms and
|
---|
22 | conditions of the GNU General Public License cover the whole
|
---|
23 | combination.
|
---|
24 |
|
---|
25 | As a special exception, the copyright holders of this library give you
|
---|
26 | permission to link this library with independent modules to produce an
|
---|
27 | executable, regardless of the license terms of these independent
|
---|
28 | modules, and to copy and distribute the resulting executable under
|
---|
29 | terms of your choice, provided that you also meet, for each linked
|
---|
30 | independent module, the terms and conditions of the license of that
|
---|
31 | module. An independent module is a module which is not derived from
|
---|
32 | or based on this library. If you modify this library, you may extend
|
---|
33 | this exception to your version of the library, but you are not
|
---|
34 | obligated to do so. If you do not wish to do so, delete this
|
---|
35 | exception statement from your version. */
|
---|
36 |
|
---|
37 | /* Must be in sun.applet to access PluginAppletSecurityContext's constructor and PluginObjectStore */
|
---|
38 | package sun.applet;
|
---|
39 |
|
---|
40 | import java.util.IdentityHashMap;
|
---|
41 |
|
---|
42 | import sun.applet.mock.PluginPipeMock;
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * Convenience class for PluginPipeMock.
|
---|
46 | * Provides convenient methods for installing a custom pipe mock and cleaning it up.
|
---|
47 | *
|
---|
48 | * Provides PipeMessageHandler interface and accompany convenience methods which can
|
---|
49 | * be used to define mocked pipes in a simple manner.
|
---|
50 | * */
|
---|
51 | public class PluginPipeMockUtil {
|
---|
52 |
|
---|
53 | /**************************************************************************
|
---|
54 | * Basic setup & teardown *
|
---|
55 | **************************************************************************/
|
---|
56 |
|
---|
57 | /* Maps PluginPipeMock instances to a ThreadGroup, allowing us to stop all the
|
---|
58 | * message handling threads that we started when setting up the mock pipes. */
|
---|
59 | static private IdentityHashMap<PluginPipeMock, ThreadGroup> pipeToThreadGroup = new IdentityHashMap<PluginPipeMock, ThreadGroup>();
|
---|
60 |
|
---|
61 | /* By providing custom implementations of the input stream & output stream used by PluginStreamHandler,
|
---|
62 | * we are able to mock the C++-side of the plugin. We do this by sending the messages the Java-side expects
|
---|
63 | * to receive. Additionally, we are able to test that the Java-side sends the correct requests.
|
---|
64 | * See PluginPipeMock for more details.
|
---|
65 | */
|
---|
66 | static private PluginPipeMock installPipeMock() {
|
---|
67 | AppletSecurityContextManager.addContext(0, new PluginAppletSecurityContext(0, false /* no security manager */));
|
---|
68 |
|
---|
69 | PluginPipeMock pipeMock = new PluginPipeMock();
|
---|
70 |
|
---|
71 | PluginStreamHandler streamHandler = new PluginStreamHandler(pipeMock.getResponseInputStream(), pipeMock.getRequestOutputStream());
|
---|
72 | PluginAppletViewer.setStreamhandler(streamHandler);
|
---|
73 | PluginAppletViewer.setPluginCallRequestFactory(new PluginCallRequestFactory());
|
---|
74 |
|
---|
75 | streamHandler.startProcessing();
|
---|
76 |
|
---|
77 | return pipeMock;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /* Set up the mocked plugin pipe environment. See installPipeMock for details. */
|
---|
82 | static public PluginPipeMock setupMockedMessageHandling() throws Exception {
|
---|
83 | ThreadGroup pipeThreadGroup = new ThreadGroup("PluginAppletViewerTestThreadGroup") {
|
---|
84 | public void uncaughtException(Thread t, Throwable e) {
|
---|
85 | // Silent death for plugin message handler threads
|
---|
86 | }
|
---|
87 | };
|
---|
88 |
|
---|
89 | final PluginPipeMock[] pipeMock = {null};
|
---|
90 | // Do set-up in a thread so we can pass along our thread-group, used for clean-up.
|
---|
91 | Thread initThread = new Thread(pipeThreadGroup, "InstallPipeMockThread") {
|
---|
92 | @Override
|
---|
93 | public void run() {
|
---|
94 | pipeMock[0] = installPipeMock();
|
---|
95 | }
|
---|
96 | };
|
---|
97 |
|
---|
98 | initThread.start();
|
---|
99 | initThread.join();
|
---|
100 |
|
---|
101 | pipeToThreadGroup.put(pipeMock[0], pipeThreadGroup);
|
---|
102 | return pipeMock[0];
|
---|
103 | }
|
---|
104 |
|
---|
105 | /* Kill any message handling threads started when setting up the mocked pipes */
|
---|
106 | @SuppressWarnings("deprecation")
|
---|
107 | static public void cleanUpMockedMessageHandling(PluginPipeMock pipeMock) throws Exception {
|
---|
108 | ThreadGroup pipeThreadGroup = pipeToThreadGroup.get(pipeMock);
|
---|
109 | if (pipeThreadGroup != null) {
|
---|
110 | pipeThreadGroup.stop();
|
---|
111 | }
|
---|
112 | pipeToThreadGroup.remove(pipeMock);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**************************************************************************
|
---|
116 | * Object store utilities *
|
---|
117 | **************************************************************************/
|
---|
118 | /*
|
---|
119 | * Helpers for manipulating the object mapping using to refer to objects in
|
---|
120 | * the plugin
|
---|
121 | */
|
---|
122 | public static Object getPluginStoreObject(int id) {
|
---|
123 | return PluginObjectStore.getInstance().getObject(id);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /* Stores the object if it is not yet stored */
|
---|
127 | public static int getPluginStoreId(Object obj) {
|
---|
128 | PluginObjectStore.getInstance().reference(obj);
|
---|
129 | return PluginObjectStore.getInstance().getIdentifier(obj);
|
---|
130 | }
|
---|
131 | }
|
---|