gt:                 TestIntfPrx test1 = createTestIntfPrx(adpts);
context: // **********************************************************************
  //
  // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
  //
  // This copy of Ice is licensed to you under the terms described in the
  // ICE_LICENSE file included in this distribution.
  //
  // **********************************************************************
  
  package test.Ice.binding;
  
  import java.io.PrintWriter;
  
  import test.Ice.binding.Test.RemoteCommunicatorPrx;
  import test.Ice.binding.Test.RemoteCommunicatorPrxHelper;
  import test.Ice.binding.Test.RemoteObjectAdapterPrx;
  import test.Ice.binding.Test.TestIntfPrx;
  import test.Ice.binding.Test.TestIntfPrxHelper;
  import test.Ice.binding.Test.Callback_TestIntf_getAdapterName;
  
  public class AllTests
  {
      private static void
      test(boolean b)
      {
          if(!b)
          {
              throw new RuntimeException();
          }
      }
  
      static class GetAdapterNameCB extends Ice.Callback
      {
          synchronized public void
          completed(Ice.AsyncResult result)
          {
              try
              {
                  _name = TestIntfPrxHelper.uncheckedCast(result.getProxy()).end_getAdapterName(result);
                  notify();
              }
              catch(Ice.LocalException ex)
              {
                  test(false);
              }
          }
  
          synchronized public String
          getResult()
          {
              while(_name == null)
              {
                  try
                  {
                      wait();
                  }
                  catch(java.lang.InterruptedException ex)
                  {
                  }
              }
              return _name;
          }
  
          private String _name = null;
      }
  
      private static String
      getAdapterNameWithAMI(TestIntfPrx test)
      {
          GetAdapterNameCB cb = new GetAdapterNameCB();
          test.begin_getAdapterName(cb);
          return cb.getResult();
      }
  
      private static TestIntfPrx
      createTestIntfPrx(java.util.List<RemoteObjectAdapterPrx> adapters)
      {
          java.util.List<Ice.Endpoint> endpoints = new java.util.ArrayList<Ice.Endpoint>();
          TestIntfPrx test = null;
          for(RemoteObjectAdapterPrx p : adapters)
          {
              test = p.getTestIntf();
              Ice.Endpoint[] edpts = test.ice_getEndpoints();
              endpoints.addAll(java.util.Arrays.asList(edpts));
          }
          return TestIntfPrxHelper.uncheckedCast(
              test.ice_endpoints((Ice.Endpoint[])endpoints.toArray(new Ice.Endpoint[endpoints.size()])));
      }
  
      private static void
      deactivate(RemoteCommunicatorPrx communicator, java.util.List<RemoteObjectAdapterPrx> adapters)
      {
          for(RemoteObjectAdapterPrx p : adapters)
          {
              communicator.deactivateObjectAdapter(p);
          }
      }
  
      public static void
      allTests(Ice.Communicator communicator, PrintWriter out)
      {
          String ref = "communicator:default -p 12010";
          RemoteCommunicatorPrx com = RemoteCommunicatorPrxHelper.uncheckedCast(communicator.stringToProxy(ref));
  
  		out.print("testing binding with single endpoint... ");
          out.flush();
          {
              RemoteObjectAdapterPrx adapter = com.createObjectAdapter("Adapter", "default");
  
              TestIntfPrx test1 = adapter.getTestIntf();
              TestIntfPrx test2 = adapter.getTestIntf();
              test(test1.ice_getConnection() == test2.ice_getConnection());
  
              test1.ice_ping();
              test2.ice_ping();
  
              com.deactivateObjectAdapter(adapter);
  
              TestIntfPrx test3 = TestIntfPrxHelper.uncheckedCast(test1);
              test(test3.ice_getConnection() == test1.ice_getConnection());
              test(test3.ice_getConnection() == test2.ice_getConnection());
  
              try
              {
                  test3.ice_ping();
                  test(false);
              }
              catch(Ice.ConnectFailedException ex)
              {
                  //
                  // Usually the actual type of this exception is ConnectionRefusedException,
                  // but not always. See bug 3179.
                  //
              }
          }
          out.println("ok");
  
          out.print("testing binding with multiple endpoints... ");
          out.flush();
          {
              java.util.List<RemoteObjectAdapterPrx> adapters = new java.util.ArrayList<RemoteObjectAdapterPrx>();
              adapters.add(com.createObjectAdapter("Adapter11", "default"));
              adapters.add(com.createObjectAdapter("Adapter12", "default"));
              adapters.add(com.createObjectAdapter("Adapter13", "default"));
  
              //
              // Ensure that when a connection is opened it's reused for new
              // proxies and that all endpoints are eventually tried.
              //
              java.util.Set<String> names = new java.util.HashSet<String>();
              names.add("Adapter11");
              names.add("Adapter12");
              names.add("Adapter13");
              while(!names.isEmpty())
              {
                  java.util.List<RemoteObjectAdapterPrx> adpts =
                      new java.util.ArrayList<RemoteObjectAdapterPrx>(adapters);
  
                  TestIntfPrx test1 = createTestIntfPrx(adpts);
                  java.util.Collections.shuffle(adpts);
                  TestIntfPrx test2 = createTestIntfPrx(adpts);
                  java.util.Collections.shuffle(adpts);
                  TestIntfPrx test3 = createTestIntfPrx(adpts);
  
                  test(test1.ice_getConnection() == test2.ice_getConnection());
                  test(test2.ice_getConnection() == test3.ice_getConnection());
  
                  names.remove(test1.getAdapterName());
                  test1.ice_getConnection().close(false);
              }
  
              //
              // Ensure that the proxy correctly caches the connection (we
              // always send the request over the same connection.)
              //
              {
                  for(RemoteObjectAdapterPrx p : adapters)
                  {
                      p.getTestIntf().ice_ping();
                  }
  
                  TestIntfPrx test = createTestIntfPrx(adapters);
                  String name = test.getAdapterName();
                  final int nRetry = 10;
                  int i;
                  for(i = 0; i < nRetry && test.getAdapterName().equals(name); i++);
                  test(i == nRetry);
  
                  for(RemoteObjectAdapterPrx p : adapters)
                  {
                      p.getTestIntf().ice_getConnection().close(false);
                  }
              }
  
              //
              // Deactivate an adapter and ensure that we can still
              // establish the connection to the remaining adapters.
              //
              com.deactivateObjectAdapter(adapters.get(0));
              names.add("Adapter12");
              names.add("Adapter13");
              while(!names.isEmpty())
              {
                  java.util.List<RemoteObjectAdapterPrx> adpts =
                      new java.util.ArrayList<RemoteObjectAdapterPrx>(adapters);
  
                  TestIntfPrx test1 = createTestIntfPrx(adpts);
                  java.util.Collections.shuffle(adpts);
                  TestIntfPrx test2 = createTestIntfPrx(adpts);
                  java.util.Collections.shuffle(adpts);
                  TestIntfPrx test3 = createTestIntfPrx(adpts);
  
                  test(test1.ice_getConnection() == test2.ice_getConnection());
                  test(test2.ice_getConnection() == test3.ice_getConnection());
  
                  names.remove(test1.getAdapterName());
                  test1.ice_getConnection().close(false);
              }
  
              //
              // Deactivate an adapter and ensure that we can still
              // establish the connection to the remaining adapter.
              //
              com.deactivateObjectAdapter(adapters.get(2));
              TestIntfPrx test = createTestIntfPrx(adapters);
              test(test.getAdapterName().equals("Adapter12"));
  
              deactivate(com, adapters);
          }
          out.println("ok");
  
          out.print("testing binding with multiple random endpoints... ");
          out.flush();
          {
              java.util.Random rand = new java.util.Random();
  
              RemoteObjectAdapterPrx[] adapters = new RemoteObjectAdapterPrx[5];
              adapters[0] = com.createObjectAdapter("AdapterRandom11", "default");
              adapters[1] = com.createObjectAdapter("AdapterRandom12", "default");
              adapters[2] = com.createObjectAdapter("AdapterRandom13", "default");
              adapters[3] = com.createObjectAdapter("AdapterRandom14", "default");
              adapters[4] = com.createObjectAdapter("AdapterRandom15", "default");
  
              boolean shortenTest =
                  System.getProperty("os.name").startsWith("Windows") ||
                  System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0;
  
              int count;
              if(shortenTest)
              {
                  count = 60;
              }
              else
              {
                  count = 20;
              }
  
              int adapterCount = adapters.length;
              while(--count > 0)
              {
                  TestIntfPrx[] proxies;
                  if(shortenTest)
                  {
                      if(count == 10)
                      {
                          com.deactivateObjectAdapter(adapters[4]);
                          --adapterCount;
                      }
                      proxies = new TestIntfPrx[10];
                  }
                  else
                  {
                      if(count < 60 && count % 10 == 0)
                      {
                          com.deactivateObjectAdapter(adapters[count / 10 - 1]);
                          --adapterCount;
                      }
                      proxies = new TestIntfPrx[40];
                  }
  
                  int i;
                  for(i = 0; i < proxies.length; ++i)
                  {
                      RemoteObjectAdapterPrx[] adpts = new RemoteObjectAdapterPrx[rand.nextInt(adapters.length)];
                      if(adpts.length == 0)
                      {
                          adpts = new RemoteObjectAdapterPrx[1];
                      }
                      for(int j = 0; j < adpts.length; ++j)
                      {
                          adpts[j] = adapters[rand.nextInt(adapters.length)];
                      }
                      proxies[i] = createTestIntfPrx(java.util.Arrays.asList((adpts)));
                  }
  
                  for(TestIntfPrx p : proxies)
                  {
                      p.begin_getAdapterName();
                  }
                  for(TestIntfPrx p : proxies)
                  {
                      try
                      {
                          p.ice_ping();
                      }
                      catch(Ice.LocalException ex)
                      {
                      }
                  }
  
                  java.util.Set<Ice.Connection> connections = new java.util.HashSet<Ice.Connection>();
                  for(TestIntfPrx p : proxies)
                  {
                      if(p.ice_getCachedConnection() != null)
                      {
                          connections.add(p.ice_getCachedConnection());
                      }
                  }
                  test(connections.size() <= adapterCount);
  
                  for(RemoteObjectAdapterPrx a : adapters)
                  {
                      try
                      {
                          a.getTestIntf().ice_getConnection().close(false);
                      }
                      catch(Ice.LocalException ex)
                      {
                          // Expected if adapter is down.
                      }
                  }
              }
          }
          out.println("ok");
  
          out.print("testing binding with multiple endpoints and AMI... ");
          out.flush();
          {
              java.util.List<RemoteObjectAdapterPrx> adapters = new java.util.ArrayList<RemoteObjectAdapterPrx>();
              adapters.add(com.createObjectAdapter("AdapterAMI11", "default"));
              adapters.add(com.createObjectAdapter("AdapterAMI12", "default"));
              adapters.add(com.createObjectAdapter("AdapterAMI13", "default"));
  
              //
              // Ensure that when a connection is opened it's reused for new
              // proxies and that all endpoints are eventually tried.
              //
              java.util.Set<String> names = new java.util.HashSet<String>();
              names.add("AdapterAMI11");
              names.add("AdapterAMI12");
              names.add("AdapterAMI13");
              while(!names.isEmpty())
              {
                  java.util.List<RemoteObjectAdapterPrx> adpts =
                      new java.util.ArrayList<RemoteObjectAdapterPrx>(adapters);
  
                  TestIntfPrx test1 = createTestIntfPrx(adpts);
                  java.util.Collections.shuffle(adpts);
                  TestIntfPrx test2 = createTestIntfPrx(adpts);
                  java.util.Collections.shuffle(adpts);
                  TestIntfPrx test3 = createTestIntfPrx(adpts);
  
                  test(test1.ice_getConnection() == test2.ice_getConnection());
                  test(test2.ice_getConnection() == test3.ice_getConnection());
  
                  names.remove(getAdapterNameWithAMI(test1));
                  test1.ice_getConnection().close(false);
              }
  
              //
              // Ensure that the proxy correctly caches the connection (we
              // always send the request over the same connection.)
              //
              {
                  for(RemoteObjectAdapterPrx p : adapters)
                  {
                      p.getTestIntf().ice_ping();
                  }
  
                  TestIntfPrx test = createTestIntfPrx(adapters);
                  String name = getAdapterNameWithAMI(test);
                  final int nRetry = 10;
                  int i;
                  for(i = 0; i < nRetry &&  getAdapterNameWithAMI(test).equals(name); i++);
                  test(i == nRetry);
  
                  for(RemoteObjectAdapterPrx p : adapters)
                  {
                      p.getTestIntf().ice_getConnection().close(false);
                  }
              }
  
              //
              // Deactivate an adapter and ensure that we can still
              // establish the connection to the remaining adapters.
              //
              com.deactivateObjectAdapter(adapters.get(0));
              names.add("AdapterAMI12");
              names.add("AdapterAMI13");
              while(!names.isEmpty())
              {
                  java.util.List<RemoteObjectAdapterPrx> adpts =
                      new java.util.ArrayList<RemoteObjectAdapterPrx>(adapters);
  
  