~ubuntu-branches/ubuntu/quantal/zeroc-ice/quantal

« back to all changes in this revision

Viewing changes to java/test/Ice/classLoader/AllTests.java

  • Committer: Bazaar Package Importer
  • Author(s): Cleto Martin Angelina
  • Date: 2011-04-25 18:44:24 UTC
  • mfrom: (6.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110425184424-sep9i9euu434vq4c
Tags: 3.4.1-7
* Bug fix: "libdb5.1-java.jar was renamed to db.jar", thanks to Ondřej
  Surý (Closes: #623555).
* Bug fix: "causes noise in php5", thanks to Jayen Ashar (Closes:
  #623533).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// **********************************************************************
 
2
//
 
3
// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
 
4
//
 
5
// This copy of Ice is licensed to you under the terms described in the
 
6
// ICE_LICENSE file included in this distribution.
 
7
//
 
8
// **********************************************************************
 
9
 
 
10
package test.Ice.classLoader;
 
11
 
 
12
import java.io.PrintWriter;
 
13
 
 
14
import test.Ice.classLoader.Test.AbstractClass;
 
15
import test.Ice.classLoader.Test.ConcreteClass;
 
16
import test.Ice.classLoader.Test.E;
 
17
import test.Ice.classLoader.Test.InitialPrx;
 
18
import test.Ice.classLoader.Test.InitialPrxHelper;
 
19
 
 
20
public class AllTests
 
21
{
 
22
    private static class MyObjectFactory implements Ice.ObjectFactory
 
23
    {
 
24
        public Ice.Object create(String type)
 
25
        {
 
26
            if(type.equals("::Test::AbstractClass"))
 
27
            {
 
28
                return new AbstractClassI();
 
29
            }
 
30
 
 
31
            assert (false); // Should never be reached
 
32
            return null;
 
33
        }
 
34
 
 
35
        public void destroy()
 
36
        {
 
37
            // Nothing to do
 
38
        }
 
39
    }
 
40
 
 
41
    private static class MyClassLoader extends ClassLoader
 
42
    {
 
43
        protected Class<?> loadClass(String name, boolean resolve)
 
44
            throws ClassNotFoundException
 
45
        {
 
46
            _names.add(name);
 
47
            return super.loadClass(name, resolve);
 
48
        }
 
49
 
 
50
        void reset()
 
51
        {
 
52
            _names.clear();
 
53
        }
 
54
 
 
55
        boolean check(String name)
 
56
        {
 
57
            return _names.contains(name);
 
58
        }
 
59
 
 
60
        private java.util.List<String> _names = new java.util.LinkedList<String>();
 
61
    }
 
62
 
 
63
    private static void
 
64
    test(boolean b)
 
65
    {
 
66
        if(!b)
 
67
        {
 
68
            throw new RuntimeException();
 
69
        }
 
70
    }
 
71
 
 
72
    public static void
 
73
    allTests(boolean collocated, PrintWriter out, Ice.InitializationData initData)
 
74
    {
 
75
        //
 
76
        // Verify that the class loader is used for Slice packages.
 
77
        //
 
78
        {
 
79
            out.print("testing package... ");
 
80
            out.flush();
 
81
            Ice.InitializationData init = (Ice.InitializationData)initData.clone();
 
82
            MyClassLoader classLoader = new MyClassLoader();
 
83
            init.classLoader = classLoader;
 
84
            Ice.Communicator communicator = Ice.Util.initialize(init);
 
85
            test(classLoader.check("test.Ice.classLoader.Test._Marker"));
 
86
            communicator.destroy();
 
87
            out.println("ok");
 
88
        }
 
89
 
 
90
        //
 
91
        // Verify that the class loader is used for Ice plug-ins.
 
92
        //
 
93
        {
 
94
            out.print("testing plug-in... ");
 
95
            out.flush();
 
96
            Ice.InitializationData init = (Ice.InitializationData)initData.clone();
 
97
            init.properties = (Ice.Properties)initData.properties._clone();
 
98
            init.properties.setProperty("Ice.Plugin.Test", "test.Ice.classLoader.PluginFactoryI");
 
99
            MyClassLoader classLoader = new MyClassLoader();
 
100
            init.classLoader = classLoader;
 
101
            Ice.Communicator communicator = Ice.Util.initialize(init);
 
102
            test(classLoader.check("test.Ice.classLoader.PluginFactoryI"));
 
103
            communicator.destroy();
 
104
            out.println("ok");
 
105
        }
 
106
 
 
107
        //
 
108
        // Verify that the class loader is used for IceSSL certificate verifiers and password callbacks.
 
109
        //
 
110
        if(initData.properties.getProperty("Ice.Default.Protocol").equals("ssl"))
 
111
        {
 
112
            out.print("testing IceSSL certificate verifier and password callback... ");
 
113
            out.flush();
 
114
            Ice.InitializationData init = (Ice.InitializationData)initData.clone();
 
115
            init.properties = (Ice.Properties)initData.properties._clone();
 
116
            init.properties.setProperty("IceSSL.CertVerifier", "test.Ice.classLoader.CertificateVerifierI");
 
117
            init.properties.setProperty("IceSSL.PasswordCallback", "test.Ice.classLoader.PasswordCallbackI");
 
118
            MyClassLoader classLoader = new MyClassLoader();
 
119
            init.classLoader = classLoader;
 
120
            Ice.Communicator communicator = Ice.Util.initialize(init);
 
121
            test(classLoader.check("test.Ice.classLoader.CertificateVerifierI"));
 
122
            test(classLoader.check("test.Ice.classLoader.PasswordCallbackI"));
 
123
            communicator.destroy();
 
124
            out.println("ok");
 
125
        }
 
126
 
 
127
        //
 
128
        // Marshaling tests.
 
129
        //
 
130
        {
 
131
            Ice.InitializationData init = (Ice.InitializationData)initData.clone();
 
132
            MyClassLoader classLoader = new MyClassLoader();
 
133
            init.classLoader = classLoader;
 
134
            Ice.Communicator communicator = Ice.Util.initialize(init);
 
135
 
 
136
            String ref = "initial:default -p 12010";
 
137
            Ice.ObjectPrx base = communicator.stringToProxy(ref);
 
138
            test(base != null);
 
139
 
 
140
            InitialPrx initial = InitialPrxHelper.checkedCast(base);
 
141
            test(initial != null);
 
142
 
 
143
            //
 
144
            // Verify that the class loader is used for concrete classes.
 
145
            //
 
146
            {
 
147
                out.print("testing concrete class... ");
 
148
                out.flush();
 
149
                ConcreteClass cc = initial.getConcreteClass();
 
150
                test(cc != null);
 
151
                test(classLoader.check("Test.ConcreteClass"));
 
152
                test(classLoader.check("test.Ice.classLoader.Test.ConcreteClass"));
 
153
                classLoader.reset();
 
154
                out.println("ok");
 
155
            }
 
156
 
 
157
            //
 
158
            // Verify that the class loader is invoked when a factory is not installed, and is
 
159
            // not invoked when a factory is installed.
 
160
            //
 
161
            {
 
162
                out.print("testing abstract class... ");
 
163
                out.flush();
 
164
 
 
165
                try
 
166
                {
 
167
                    initial.getAbstractClass();
 
168
                }
 
169
                catch(Ice.NoObjectFactoryException ex)
 
170
                {
 
171
                    // Expected.
 
172
                }
 
173
                test(classLoader.check("Test.AbstractClass"));
 
174
                test(classLoader.check("test.Ice.classLoader.Test.AbstractClass"));
 
175
                classLoader.reset();
 
176
 
 
177
                communicator.addObjectFactory(new MyObjectFactory(), "::Test::AbstractClass");
 
178
                AbstractClass ac = initial.getAbstractClass();
 
179
                test(ac != null);
 
180
                test(!classLoader.check("Test.AbstractClass"));
 
181
                test(!classLoader.check("test.Ice.classLoader.Test.AbstractClass"));
 
182
                classLoader.reset();
 
183
 
 
184
                out.println("ok");
 
185
            }
 
186
 
 
187
            //
 
188
            // Verify that the class loader is used for user exceptions.
 
189
            //
 
190
            out.print("testing user exception... ");
 
191
            out.flush();
 
192
            try
 
193
            {
 
194
                initial.throwException();
 
195
                test(false);
 
196
            }
 
197
            catch(E ex)
 
198
            {
 
199
            }
 
200
            test(classLoader.check("Test.E"));
 
201
            test(classLoader.check("test.Ice.classLoader.Test.E"));
 
202
            out.println("ok");
 
203
 
 
204
            initial.shutdown();
 
205
            communicator.destroy();
 
206
        }
 
207
    }
 
208
}