Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / examples / gnu / classpath / examples / CORBA / SimpleCommunication / communication / DirectTest.java
blob732b00cfb307a49eced626beb9394cee028f4c3f
1 /* DirectTest.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.classpath.examples.CORBA.SimpleCommunication.communication;
41 import org.omg.CORBA.BAD_OPERATION;
42 import org.omg.CORBA.ByteHolder;
43 import org.omg.CORBA.DoubleHolder;
44 import org.omg.CORBA.ORB;
45 import org.omg.CORBA.ShortHolder;
46 import org.omg.CORBA.StringHolder;
47 import org.omg.CORBA.UserException;
49 import java.io.File;
50 import java.io.FileReader;
51 import java.io.IOException;
53 /**
54 * This code uses CORBA to call various methods of the remote object,
55 * passing data structures in both directions. It finds the server by
56 * reading the IOR.txt file that must be present in the folder,
57 * where the program has been started.
59 * The IOR.txt file is written by the server
60 * {@link gnu.classpath.examples.CORBA.SimpleCommunication.DemoServer}.
61 * The server should be reachable over Internet, unless blocked by
62 * security tools.
64 * This code is tested for interoperability with Sun Microsystems
65 * java implementation 1.4.2 (08.b03). Server, client of both can
66 * be started either on Sun's or on Classpath CORBA implementation,
67 * in any combinations.
69 * BE SURE TO START THE SERVER BEFORE STARTING THE CLIENT.
71 * This version uses direct casting. This is the most convenient
72 * method, but it is normally used together with the IDL compiler.
74 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
76 public class DirectTest
79 * The IOR.txt file, used to find the server and the object on the server. is written when starting the accompanying
81 public static final String IOR_FILE = "IOR.txt";
83 /**
84 * The invocation target.
86 DemoTester object;
88 /**
89 * Get the object reference.
91 public static void main(String[] args)
93 try
95 ORB orb = org.omg.CORBA.ORB.init(args, null);
97 File f = new File(IOR_FILE);
98 char[] c = new char[ (int) f.length() ];
99 FileReader fr = new FileReader(f);
100 fr.read(c);
101 fr.close();
103 String ior = new String(c);
104 DirectTest we = new DirectTest();
105 we.object = (DemoTester) orb.string_to_object(ior);
106 we.Demo();
107 orb.shutdown(false);
109 catch (IOException ex)
111 System.out.println("Cannot find or read the IOR file " +
112 "in the current folder"
114 ex.printStackTrace();
118 /** Run all demos. */
119 public void Demo()
121 testHello();
122 testField();
123 testParameters();
124 testStringArray();
125 testStructure();
126 testWideNarrowStrings();
127 testTree();
128 testSystemException();
129 testUserException();
133 * Test the field getter/setter.
135 public void testField()
137 System.out.println("***** Test the remote field getter/setter.");
138 System.out.println("The field value is now " + object.theField());
139 System.out.println("Setting it to 555");
140 object.theField(555);
141 System.out.println("The field value is now " + object.theField());
144 /** The simple invocation of the parameterless remote method. */
145 public void testHello()
147 System.out.println("***** Say hello (see the server console).");
148 object.sayHello();
152 * Test passing multiple parameters in both directions.
154 public void testParameters()
156 System.out.println("***** Pass multiple parameters.");
158 // Holder classes are required to simulate passing
159 // "by reference" (modification is returned back to the server).
160 ByteHolder a_byte = new ByteHolder((byte) 0);
161 ShortHolder a_short = new ShortHolder((short) 3);
162 StringHolder a_string = new StringHolder("[string 4]");
164 // This is an 'out' parameter; the value must not be passed to servant.
165 DoubleHolder a_double = new DoubleHolder(56.789);
167 int returned = object.passSimple(a_byte, 2, a_short, a_string, a_double);
169 System.out.println(" Returned value " + returned);
170 System.out.println(" Returned parameters: ");
171 System.out.println(" octet " + a_byte.value);
172 System.out.println(" short " + a_short.value);
173 System.out.println(" string '" + a_string.value+"'");
174 System.out.println(" double " + a_double.value);
178 * Test passing the string array, flexible size.
180 public void testStringArray()
182 System.out.println("***** Pass string array.");
184 String[] x = new String[] { "one", "two" };
186 // The array is passed as CORBA sequence, variable size is supported.
187 String[] y = object.passStrings(x);
189 for (int i = 0; i < y.length; i++)
191 System.out.println(" Passed " + x [ i ] + ", returned: " + y [ i ]);
196 * Test passing the structures.
198 public void testStructure()
200 System.out.println("***** Pass structure");
202 StructureToPass arg = new StructureToPass();
203 arg.a = "A";
204 arg.b = "B";
206 StructureToReturn r = object.passStructure(arg);
208 System.out.println(" Fields of the returned structure:");
210 System.out.println(" c: " + r.c);
211 System.out.println(" n: " + r.n);
213 // The field r.arra is declared as the fixed size CORBA array.
214 System.out.println(" r[0]: " + r.arra [ 0 ]);
215 System.out.println(" r[1]: " + r.arra [ 1 ]);
216 System.out.println(" r[3]: " + r.arra [ 2 ]);
220 * Test catching the system exception, thrown on the remote side.
222 public void testSystemException()
224 System.out.println("**** Test system exception:");
227 // Negative parameter = system exception.
228 object.throwException(-55);
230 catch (BAD_OPERATION ex)
232 System.out.println(" The expected BAD_OPERATION, minor code " +
233 ex.minor + ", has been thrown on remote side."
236 catch (UserException uex)
238 throw new InternalError();
243 * Test passing the tree structure. Any shape of the tree is
244 * supported without rewriting the code.
246 public void testTree()
248 // Manually create the tree of nodes:
249 // Root
250 // +-- a
251 // |
252 // +-- b
253 // +-- ba
254 // | |
255 // | +-- bac
256 // |
257 // +-- bb
258 System.out.println("***** Pass and return the tree.");
260 TreeNode n = nod("Root");
262 n.children = new TreeNode[] { nod("a"), nod("b") };
263 n.children [ 1 ].children = new TreeNode[] { nod("ba"), nod("bb") };
264 n.children [ 1 ].children [ 0 ].children = new TreeNode[] { nod("bac") };
266 TreeNodeHolder nh = new TreeNodeHolder(n);
268 // The server should add '++' to each TreeNode name.
269 object.passTree(nh);
271 // Convert the returned tree to some strig representation.
272 StringBuffer img = new StringBuffer();
273 getImage(img, nh.value);
275 System.out.println("Returned tree: " + img.toString());
279 * Test catching the user exception, thrown on the remote side.
281 public void testUserException()
283 System.out.println("**** Test user exception:");
286 // The user exception contains one user-defined field that will
287 // be initialised to the passed parameter.
288 object.throwException(123);
289 throw new InternalError();
291 catch (WeThrowThisException uex)
293 System.out.println(" The user exception with field " + uex.ourField +
294 ", has been thrown on remote side."
300 * Passes wide (UTF-16) string and narrow (ISO8859_1) string.
301 * @see gnu.CORBA.GIOP.CharSets_OSF for supported and default
302 * encodings.
304 public void testWideNarrowStrings()
306 System.out.println("**** Test 8 bit and 16 bit char strings");
308 String r = object.passCharacters("wide string", "narrow string");
309 System.out.println(" returned: '" + r + "'");
313 * Get the string representation of the passed tree.
314 * @param b the string buffer to accumulate the representation.
315 * @param n the tree (root TreeNode).
317 private void getImage(StringBuffer b, TreeNode n)
319 b.append(n.name);
320 b.append(": (");
322 for (int i = 0; i < n.children.length; i++)
324 getImage(b, n.children [ i ]);
325 b.append(' ');
327 b.append(") ");
331 * Create a TreeNode with the given header.
333 * @param hdr the TreeNode header.
334 * @return the created TreeNode.
336 private TreeNode nod(String hdr)
338 TreeNode n = new TreeNode();
339 n.children = new TreeNode[ 0 ];
340 n.name = hdr;
342 return n;