1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 package test.Ice.admin;
6 
7 import java.io.PrintWriter;
8 import test.Ice.admin.Test.*;
9 
10 public class AllTests
11 {
12     private static void
test(boolean b)13     test(boolean b)
14     {
15         if(!b)
16         {
17             throw new RuntimeException();
18         }
19     }
20 
21     static void
testFacets(Ice.Communicator com, boolean builtInFacets)22     testFacets(Ice.Communicator com, boolean builtInFacets)
23     {
24         if(builtInFacets)
25         {
26             test(com.findAdminFacet("Properties") != null);
27             test(com.findAdminFacet("Process") != null);
28             test(com.findAdminFacet("Logger") != null);
29             test(com.findAdminFacet("Metrics") != null);
30         }
31 
32         TestFacet f1 = new TestFacetI();
33         TestFacet f2 = new TestFacetI();
34         TestFacet f3 = new TestFacetI();
35 
36         com.addAdminFacet(f1, "Facet1");
37         com.addAdminFacet(f2, "Facet2");
38         com.addAdminFacet(f3, "Facet3");
39 
40         test(com.findAdminFacet("Facet1") == f1);
41         test(com.findAdminFacet("Facet2") == f2);
42         test(com.findAdminFacet("Facet3") == f3);
43         test(com.findAdminFacet("Bogus") == null);
44 
45         java.util.Map<String, Ice.Object> facetMap = com.findAllAdminFacets();
46         if(builtInFacets)
47         {
48             test(facetMap.size() == 7);
49             test(facetMap.containsKey("Properties"));
50             test(facetMap.containsKey("Process"));
51             test(facetMap.containsKey("Logger"));
52             test(facetMap.containsKey("Metrics"));
53         }
54         else
55         {
56             test(facetMap.size() >= 3);
57         }
58         test(facetMap.containsKey("Facet1"));
59         test(facetMap.containsKey("Facet2"));
60         test(facetMap.containsKey("Facet3"));
61 
62         try
63         {
64             com.addAdminFacet(f1, "Facet1");
65             test(false);
66         }
67         catch(Ice.AlreadyRegisteredException ex)
68         {
69             // Expected
70         }
71 
72         try
73         {
74             com.removeAdminFacet("Bogus");
75             test(false);
76         }
77         catch(Ice.NotRegisteredException ex)
78         {
79             // Expected
80         }
81 
82         com.removeAdminFacet("Facet1");
83         com.removeAdminFacet("Facet2");
84         com.removeAdminFacet("Facet3");
85 
86         try
87         {
88             com.removeAdminFacet("Facet1");
89             test(false);
90         }
91         catch(Ice.NotRegisteredException ex)
92         {
93             // Expected
94         }
95     }
96 
97     public static void
allTests(test.TestHelper helper)98     allTests(test.TestHelper helper)
99     {
100         PrintWriter out = helper.getWriter();
101 
102         out.print("testing communicator operations... ");
103         out.flush();
104         {
105             //
106             // Test: Exercise addAdminFacet, findAdminFacet, removeAdminFacet with a typical configuration.
107             //
108             Ice.InitializationData init = new Ice.InitializationData();
109             init.properties = Ice.Util.createProperties();
110             init.properties.setProperty("Ice.Admin.Endpoints", "tcp -h 127.0.0.1");
111             init.properties.setProperty("Ice.Admin.InstanceName", "Test");
112             try(Ice.Communicator com = Ice.Util.initialize(init))
113             {
114                 testFacets(com, true);
115             }
116         }
117         {
118             //
119             // Test: Verify that the operations work correctly in the presence of facet filters.
120             //
121             Ice.InitializationData init = new Ice.InitializationData();
122             init.properties = Ice.Util.createProperties();
123             init.properties.setProperty("Ice.Admin.Endpoints", "tcp -h 127.0.0.1");
124             init.properties.setProperty("Ice.Admin.InstanceName", "Test");
125             init.properties.setProperty("Ice.Admin.Facets", "Properties");
126             try(Ice.Communicator com = Ice.Util.initialize(init))
127             {
128                 testFacets(com, false);
129             }
130         }
131         {
132             //
133             // Test: Verify that the operations work correctly with the Admin object disabled.
134             //
135             try(Ice.Communicator com = Ice.Util.initialize())
136             {
137                 testFacets(com, false);
138             }
139         }
140         {
141             //
142             // Test: Verify that the operations work correctly when Ice.Admin.Enabled is set
143             //
144             Ice.InitializationData init = new Ice.InitializationData();
145             init.properties = Ice.Util.createProperties();
146             init.properties.setProperty("Ice.Admin.Enabled", "1");
147             try(Ice.Communicator com = Ice.Util.initialize(init))
148             {
149                 test(com.getAdmin() == null);
150                 Ice.Identity id = Ice.Util.stringToIdentity("test-admin");
151                 try
152                 {
153                     com.createAdmin(null, id);
154                     test(false);
155                 }
156                 catch(Ice.InitializationException ex)
157                 {
158                 }
159                 Ice.ObjectAdapter adapter = com.createObjectAdapter("");
160                 test(com.createAdmin(adapter, id) != null);
161                 test(com.getAdmin() != null);
162                 testFacets(com, true);
163             }
164         }
165         {
166             //
167             // Test: Verify that the operations work correctly when creation of the Admin object is delayed.
168             //
169             Ice.InitializationData init = new Ice.InitializationData();
170             init.properties = Ice.Util.createProperties();
171             init.properties.setProperty("Ice.Admin.Endpoints", "tcp -h 127.0.0.1");
172             init.properties.setProperty("Ice.Admin.InstanceName", "Test");
173             init.properties.setProperty("Ice.Admin.DelayCreation", "1");
174             try(Ice.Communicator com = Ice.Util.initialize(init))
175             {
176                 testFacets(com, true);
177                 com.getAdmin();
178                 testFacets(com, true);
179             }
180         }
181         out.println("ok");
182 
183         String ref = "factory:" + helper.getTestEndpoint(0) + " -t 10000";
184         RemoteCommunicatorFactoryPrx factory =
185             RemoteCommunicatorFactoryPrxHelper.uncheckedCast(helper.communicator().stringToProxy(ref));
186 
187         out.print("testing process facet... ");
188         out.flush();
189         {
190             //
191             // Test: Verify that Process::shutdown() operation shuts down the communicator.
192             //
193             java.util.Map<String, String> props = new java.util.HashMap<String, String>();
194             props.put("Ice.Admin.Endpoints", "tcp -h 127.0.0.1");
195             props.put("Ice.Admin.InstanceName", "Test");
196             RemoteCommunicatorPrx com = factory.createCommunicator(props);
197             Ice.ObjectPrx obj = com.getAdmin();
198             Ice.ProcessPrx proc = Ice.ProcessPrxHelper.checkedCast(obj, "Process");
199             proc.shutdown();
200             com.waitForShutdown();
201             com.destroy();
202         }
203         out.println("ok");
204 
205         out.print("testing properties facet... ");
206         out.flush();
207         {
208             java.util.Map<String, String> props = new java.util.HashMap<String, String>();
209             props.put("Ice.Admin.Endpoints", "tcp -h 127.0.0.1");
210             props.put("Ice.Admin.InstanceName", "Test");
211             props.put("Prop1", "1");
212             props.put("Prop2", "2");
213             props.put("Prop3", "3");
214             RemoteCommunicatorPrx com = factory.createCommunicator(props);
215             Ice.ObjectPrx obj = com.getAdmin();
216             Ice.PropertiesAdminPrx pa = Ice.PropertiesAdminPrxHelper.checkedCast(obj, "Properties");
217 
218             //
219             // Test: PropertiesAdmin::getProperty()
220             //
221             test(pa.getProperty("Prop2").equals("2"));
222             test(pa.getProperty("Bogus").equals(""));
223 
224             //
225             // Test: PropertiesAdmin::getProperties()
226             //
227             java.util.Map<String, String> pd = pa.getPropertiesForPrefix("");
228             test(pd.size() == 5);
229             test(pd.get("Ice.Admin.Endpoints").equals("tcp -h 127.0.0.1"));
230             test(pd.get("Ice.Admin.InstanceName").equals("Test"));
231             test(pd.get("Prop1").equals("1"));
232             test(pd.get("Prop2").equals("2"));
233             test(pd.get("Prop3").equals("3"));
234 
235             java.util.Map<String, String> changes;
236 
237             //
238             // Test: PropertiesAdmin::setProperties()
239             //
240             java.util.Map<String, String> setProps = new java.util.HashMap<String, String>();
241             setProps.put("Prop1", "10"); // Changed
242             setProps.put("Prop2", "20"); // Changed
243             setProps.put("Prop3", ""); // Removed
244             setProps.put("Prop4", "4"); // Added
245             setProps.put("Prop5", "5"); // Added
246             pa.setProperties(setProps);
247             test(pa.getProperty("Prop1").equals("10"));
248             test(pa.getProperty("Prop2").equals("20"));
249             test(pa.getProperty("Prop3").equals(""));
250             test(pa.getProperty("Prop4").equals("4"));
251             test(pa.getProperty("Prop5").equals("5"));
252             changes = com.getChanges();
253             test(changes.size() == 5);
254             test(changes.get("Prop1").equals("10"));
255             test(changes.get("Prop2").equals("20"));
256             test(changes.get("Prop3").equals(""));
257             test(changes.get("Prop4").equals("4"));
258             test(changes.get("Prop5").equals("5"));
259             pa.setProperties(setProps);
260             changes = com.getChanges();
261             test(changes.isEmpty());
262 
263             com.destroy();
264         }
265         out.println("ok");
266 
267         out.print("testing logger facet... ");
268         out.flush();
269         {
270             java.util.Map<String, String> props = new java.util.HashMap<String, String>();
271             props.put("Ice.Admin.Endpoints", "tcp -h 127.0.0.1");
272             props.put("Ice.Admin.InstanceName", "Test");
273             props.put("NullLogger", "1");
274             RemoteCommunicatorPrx com = factory.createCommunicator(props);
275 
276             com.trace("testCat", "trace");
277             com.warning("warning");
278             com.error("error");
279             com.print("print");
280 
281             Ice.ObjectPrx obj = com.getAdmin();
282             Ice.LoggerAdminPrx logger = Ice.LoggerAdminPrxHelper.checkedCast(obj, "Logger");
283             test(logger != null);
284 
285             Ice.StringHolder prefix = new Ice.StringHolder();
286 
287             //
288             // Get all
289             //
290             Ice.LogMessage[] logMessages = logger.getLog(null, null, -1, prefix);
291 
292             test(logMessages.length == 4);
293             test(prefix.value.equals("NullLogger"));
294             test(logMessages[0].traceCategory.equals("testCat") && logMessages[0].message.equals("trace"));
295             test(logMessages[1].message.equals("warning"));
296             test(logMessages[2].message.equals("error"));
297             test(logMessages[3].message.equals("print"));
298 
299             //
300             // Get only errors and warnings
301             //
302             com.error("error2");
303             com.print("print2");
304             com.trace("testCat", "trace2");
305             com.warning("warning2");
306 
307             Ice.LogMessageType[] messageTypes = {Ice.LogMessageType.ErrorMessage, Ice.LogMessageType.WarningMessage};
308 
309             logMessages = logger.getLog(messageTypes, null, -1, prefix);
310             test(logMessages.length == 4);
311             test(prefix.value.equals("NullLogger"));
312 
313             for(Ice.LogMessage msg : java.util.Arrays.asList(logMessages))
314             {
315                 test(msg.type == Ice.LogMessageType.ErrorMessage || msg.type == Ice.LogMessageType.WarningMessage);
316             }
317 
318             //
319             // Get only errors and traces with Cat = "testCat"
320             //
321             com.trace("testCat2", "A");
322             com.trace("testCat", "trace3");
323             com.trace("testCat2", "B");
324 
325             messageTypes = new Ice.LogMessageType[]{Ice.LogMessageType.ErrorMessage, Ice.LogMessageType.TraceMessage};
326             String[] categories = {"testCat"};
327             logMessages = logger.getLog(messageTypes, categories, -1, prefix);
328             test(logMessages.length == 5);
329             test(prefix.value.equals("NullLogger"));
330 
331             for(Ice.LogMessage msg : java.util.Arrays.asList(logMessages))
332             {
333                 test(msg.type == Ice.LogMessageType.ErrorMessage ||
334                      (msg.type == Ice.LogMessageType.TraceMessage && msg.traceCategory.equals("testCat")));
335             }
336 
337             //
338             // Same, but limited to last 2 messages (trace3 + error3)
339             //
340             com.error("error3");
341 
342             logMessages = logger.getLog(messageTypes, categories, 2, prefix);
343             test(logMessages.length == 2);
344             test(prefix.value.equals("NullLogger"));
345 
346             test(logMessages[0].message.equals("trace3"));
347             test(logMessages[1].message.equals("error3"));
348 
349             //
350             // Now, test RemoteLogger
351             //
352             Ice.ObjectAdapter adapter =
353                 helper.communicator().createObjectAdapterWithEndpoints("RemoteLoggerAdapter", "tcp -h localhost");
354 
355             RemoteLoggerI remoteLogger = new RemoteLoggerI();
356 
357             Ice.RemoteLoggerPrx myProxy = Ice.RemoteLoggerPrxHelper.uncheckedCast(adapter.addWithUUID(remoteLogger));
358 
359             adapter.activate();
360 
361             //
362             // No filtering
363             //
364             logMessages = logger.getLog(null, null, -1, prefix);
365 
366             try
367             {
368                 logger.attachRemoteLogger(myProxy, null, null, -1);
369             }
370             catch(Ice.RemoteLoggerAlreadyAttachedException ex)
371             {
372                 test(false);
373             }
374             remoteLogger.wait(1);
375 
376             for(int i = 0; i < logMessages.length; ++i)
377             {
378                 Ice.LogMessage m = logMessages[i];
379                 remoteLogger.checkNextInit(prefix.value, m.type, m.message, m.traceCategory);
380             }
381 
382             com.trace("testCat", "rtrace");
383             com.warning("rwarning");
384             com.error("rerror");
385             com.print("rprint");
386 
387             remoteLogger.wait(4);
388 
389             remoteLogger.checkNextLog(Ice.LogMessageType.TraceMessage, "rtrace", "testCat");
390             remoteLogger.checkNextLog(Ice.LogMessageType.WarningMessage, "rwarning", "");
391             remoteLogger.checkNextLog(Ice.LogMessageType.ErrorMessage, "rerror", "");
392             remoteLogger.checkNextLog(Ice.LogMessageType.PrintMessage, "rprint", "");
393 
394             test(logger.detachRemoteLogger(myProxy));
395             test(!logger.detachRemoteLogger(myProxy));
396 
397             //
398             // Use Error + Trace with "traceCat" filter with 4 limit
399             //
400             logMessages = logger.getLog(messageTypes, categories, 4, prefix);
401             test(logMessages.length == 4);
402 
403             try
404             {
405                 logger.attachRemoteLogger(myProxy, messageTypes, categories, 4);
406             }
407             catch(Ice.RemoteLoggerAlreadyAttachedException ex)
408             {
409                 test(false);
410             }
411 
412             remoteLogger.wait(1);
413             for(int i = 0; i < logMessages.length; ++i)
414             {
415                 Ice.LogMessage m = logMessages[i];
416                 remoteLogger.checkNextInit(prefix.value, m.type, m.message, m.traceCategory);
417             }
418 
419             com.warning("rwarning2");
420             com.trace("testCat", "rtrace2");
421             com.warning("rwarning3");
422             com.error("rerror2");
423             com.print("rprint2");
424 
425             remoteLogger.wait(2);
426 
427             remoteLogger.checkNextLog(Ice.LogMessageType.TraceMessage, "rtrace2", "testCat");
428             remoteLogger.checkNextLog(Ice.LogMessageType.ErrorMessage, "rerror2", "");
429 
430             //
431             // Attempt reconnection with slightly different proxy
432             //
433             try
434             {
435                 logger.attachRemoteLogger(Ice.RemoteLoggerPrxHelper.uncheckedCast(myProxy.ice_oneway()),
436                                           messageTypes, categories, 4);
437                 test(false);
438             }
439             catch(Ice.RemoteLoggerAlreadyAttachedException ex)
440             {
441                 // expected
442             }
443 
444             com.destroy();
445         }
446         out.println("ok");
447 
448         out.print("testing custom facet... ");
449         out.flush();
450         {
451             //
452             // Test: Verify that the custom facet is present.
453             //
454             java.util.Map<String, String> props = new java.util.HashMap<String, String>();
455             props.put("Ice.Admin.Endpoints", "tcp -h 127.0.0.1");
456             props.put("Ice.Admin.InstanceName", "Test");
457             RemoteCommunicatorPrx com = factory.createCommunicator(props);
458             Ice.ObjectPrx obj = com.getAdmin();
459             TestFacetPrx tf = TestFacetPrxHelper.checkedCast(obj, "TestFacet");
460             tf.op();
461             com.destroy();
462         }
463         out.println("ok");
464 
465         out.print("testing facet filtering... ");
466         out.flush();
467         {
468             //
469             // Test: Set Ice.Admin.Facets to expose only the Properties facet,
470             // meaning no other facet is available.
471             //
472             java.util.Map<String, String> props = new java.util.HashMap<String, String>();
473             props.put("Ice.Admin.Endpoints", "tcp -h 127.0.0.1");
474             props.put("Ice.Admin.InstanceName", "Test");
475             props.put("Ice.Admin.Facets", "Properties");
476             RemoteCommunicatorPrx com = factory.createCommunicator(props);
477             Ice.ObjectPrx obj = com.getAdmin();
478             Ice.ProcessPrx proc = Ice.ProcessPrxHelper.checkedCast(obj, "Process");
479             test(proc == null);
480             TestFacetPrx tf = TestFacetPrxHelper.checkedCast(obj, "TestFacet");
481             test(tf == null);
482             com.destroy();
483         }
484         {
485             //
486             // Test: Set Ice.Admin.Facets to expose only the Process facet,
487             // meaning no other facet is available.
488             //
489             java.util.Map<String, String> props = new java.util.HashMap<String, String>();
490             props.put("Ice.Admin.Endpoints", "tcp -h 127.0.0.1");
491             props.put("Ice.Admin.InstanceName", "Test");
492             props.put("Ice.Admin.Facets", "Process");
493             RemoteCommunicatorPrx com = factory.createCommunicator(props);
494             Ice.ObjectPrx obj = com.getAdmin();
495             Ice.PropertiesAdminPrx pa = Ice.PropertiesAdminPrxHelper.checkedCast(obj, "Properties");
496             test(pa == null);
497             TestFacetPrx tf = TestFacetPrxHelper.checkedCast(obj, "TestFacet");
498             test(tf == null);
499             com.destroy();
500         }
501         {
502             //
503             // Test: Set Ice.Admin.Facets to expose only the TestFacet facet,
504             // meaning no other facet is available.
505             //
506             java.util.Map<String, String> props = new java.util.HashMap<String, String>();
507             props.put("Ice.Admin.Endpoints", "tcp -h 127.0.0.1");
508             props.put("Ice.Admin.InstanceName", "Test");
509             props.put("Ice.Admin.Facets", "TestFacet");
510             RemoteCommunicatorPrx com = factory.createCommunicator(props);
511             Ice.ObjectPrx obj = com.getAdmin();
512             Ice.PropertiesAdminPrx pa = Ice.PropertiesAdminPrxHelper.checkedCast(obj, "Properties");
513             test(pa == null);
514             Ice.ProcessPrx proc = Ice.ProcessPrxHelper.checkedCast(obj, "Process");
515             test(proc == null);
516             com.destroy();
517         }
518         {
519             //
520             // Test: Set Ice.Admin.Facets to expose two facets. Use whitespace to separate the
521             // facet names.
522             //
523             java.util.Map<String, String> props = new java.util.HashMap<String, String>();
524             props.put("Ice.Admin.Endpoints", "tcp -h 127.0.0.1");
525             props.put("Ice.Admin.InstanceName", "Test");
526             props.put("Ice.Admin.Facets", "Properties TestFacet");
527             RemoteCommunicatorPrx com = factory.createCommunicator(props);
528             Ice.ObjectPrx obj = com.getAdmin();
529             Ice.PropertiesAdminPrx pa = Ice.PropertiesAdminPrxHelper.checkedCast(obj, "Properties");
530             test(pa.getProperty("Ice.Admin.InstanceName").equals("Test"));
531             TestFacetPrx tf = TestFacetPrxHelper.checkedCast(obj, "TestFacet");
532             tf.op();
533             Ice.ProcessPrx proc = Ice.ProcessPrxHelper.checkedCast(obj, "Process");
534             test(proc == null);
535             com.destroy();
536         }
537         {
538             //
539             // Test: Set Ice.Admin.Facets to expose two facets. Use a comma to separate the
540             // facet names.
541             //
542             java.util.Map<String, String> props = new java.util.HashMap<String, String>();
543             props.put("Ice.Admin.Endpoints", "tcp -h 127.0.0.1");
544             props.put("Ice.Admin.InstanceName", "Test");
545             props.put("Ice.Admin.Facets", "TestFacet, Process");
546             RemoteCommunicatorPrx com = factory.createCommunicator(props);
547             Ice.ObjectPrx obj = com.getAdmin();
548             Ice.PropertiesAdminPrx pa = Ice.PropertiesAdminPrxHelper.checkedCast(obj, "Properties");
549             test(pa == null);
550             TestFacetPrx tf = TestFacetPrxHelper.checkedCast(obj, "TestFacet");
551             tf.op();
552             Ice.ProcessPrx proc = Ice.ProcessPrxHelper.checkedCast(obj, "Process");
553             proc.shutdown();
554             com.waitForShutdown();
555             com.destroy();
556         }
557         out.println("ok");
558 
559         factory.shutdown();
560     }
561 }
562