@Test
  public void testGetBindingNames() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    String divertName = RandomUtil.randomString();

    session.createQueue(address, queue, false);

    CoreMessagingProxy proxy = createProxy(address);
    Object[] bindingNames = (Object[]) proxy.retrieveAttributeValue("bindingNames");
    assertEquals(1, bindingNames.length);
    assertEquals(queue.toString(), bindingNames[0]);

    server
        .getActiveMQServerControl()
        .createDivert(
            divertName,
            randomString(),
            address.toString(),
            RandomUtil.randomString(),
            false,
            null,
            null);

    bindingNames = (Object[]) proxy.retrieveAttributeValue("bindingNames");
    assertEquals(2, bindingNames.length);

    session.deleteQueue(queue);

    bindingNames = (Object[]) proxy.retrieveAttributeValue("bindingNames");
    assertEquals(1, bindingNames.length);
    assertEquals(divertName.toString(), bindingNames[0]);
  }
  @Test
  public void testCreateDurableQueueUsingJMXAndRestartServer() throws Exception {
    String queueName = RandomUtil.randomString();
    String binding = RandomUtil.randomString();

    ActiveMQTestBase.checkNoBinding(context, binding);
    checkNoResource(ObjectNameBuilder.DEFAULT.getJMSQueueObjectName(queueName));

    JMSServerControl control = ManagementControlHelper.createJMSServerControl(mbeanServer);
    control.createQueue(queueName, binding);

    Object o = ActiveMQTestBase.checkBinding(context, binding);
    Assert.assertTrue(o instanceof Queue);
    Queue queue = (Queue) o;
    Assert.assertEquals(queueName, queue.getQueueName());
    checkResource(ObjectNameBuilder.DEFAULT.getJMSQueueObjectName(queueName));

    serverManager.stop();

    ActiveMQTestBase.checkNoBinding(context, binding);
    checkNoResource(ObjectNameBuilder.DEFAULT.getJMSQueueObjectName(queueName));

    serverManager = createJMSServer();
    serverManager.start();

    o = ActiveMQTestBase.checkBinding(context, binding);
    Assert.assertTrue(o instanceof Queue);
    queue = (Queue) o;
    Assert.assertEquals(queueName, queue.getQueueName());
    checkResource(ObjectNameBuilder.DEFAULT.getJMSQueueObjectName(queueName));
  }
  @Test
  public void testGetQueueNames() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    SimpleString anotherQueue = RandomUtil.randomSimpleString();

    session.createQueue(address, queue, true);

    CoreMessagingProxy proxy = createProxy(address);
    Object[] queueNames = (Object[]) proxy.retrieveAttributeValue("queueNames");
    Assert.assertEquals(1, queueNames.length);
    Assert.assertEquals(queue.toString(), queueNames[0]);

    session.createQueue(address, anotherQueue, false);
    queueNames = (Object[]) proxy.retrieveAttributeValue("queueNames");
    Assert.assertEquals(2, queueNames.length);

    session.deleteQueue(queue);

    queueNames = (Object[]) proxy.retrieveAttributeValue("queueNames");
    Assert.assertEquals(1, queueNames.length);
    Assert.assertEquals(anotherQueue.toString(), queueNames[0]);

    session.deleteQueue(anotherQueue);
  }
  @Test
  public void testDeadLetterAddressWithOverridenSublevelAddressSettings() throws Exception {
    int defaultDeliveryAttempt = 3;
    int specificeDeliveryAttempt = defaultDeliveryAttempt + 1;

    SimpleString address = new SimpleString("prefix.address");
    SimpleString queue = RandomUtil.randomSimpleString();
    SimpleString defaultDeadLetterAddress = RandomUtil.randomSimpleString();
    SimpleString defaultDeadLetterQueue = RandomUtil.randomSimpleString();
    SimpleString specificDeadLetterAddress = RandomUtil.randomSimpleString();
    SimpleString specificDeadLetterQueue = RandomUtil.randomSimpleString();

    AddressSettings defaultAddressSettings =
        new AddressSettings()
            .setMaxDeliveryAttempts(defaultDeliveryAttempt)
            .setDeadLetterAddress(defaultDeadLetterAddress);
    server.getAddressSettingsRepository().addMatch("*", defaultAddressSettings);
    AddressSettings specificAddressSettings =
        new AddressSettings()
            .setMaxDeliveryAttempts(specificeDeliveryAttempt)
            .setDeadLetterAddress(specificDeadLetterAddress);
    server.getAddressSettingsRepository().addMatch(address.toString(), specificAddressSettings);

    clientSession.createQueue(address, queue, false);
    clientSession.createQueue(defaultDeadLetterAddress, defaultDeadLetterQueue, false);
    clientSession.createQueue(specificDeadLetterAddress, specificDeadLetterQueue, false);

    ClientProducer producer = clientSession.createProducer(address);
    ClientMessage clientMessage = createTextMessage(clientSession, "heyho!");
    producer.send(clientMessage);

    clientSession.start();
    ClientConsumer clientConsumer = clientSession.createConsumer(queue);
    ClientConsumer defaultDeadLetterConsumer = clientSession.createConsumer(defaultDeadLetterQueue);
    ClientConsumer specificDeadLetterConsumer =
        clientSession.createConsumer(specificDeadLetterQueue);

    for (int i = 0; i < defaultDeliveryAttempt; i++) {
      ClientMessage m = clientConsumer.receive(500);
      Assert.assertNotNull(m);
      Assert.assertEquals(i + 1, m.getDeliveryCount());
      m.acknowledge();
      clientSession.rollback();
    }

    Assert.assertNull(defaultDeadLetterConsumer.receiveImmediate());
    Assert.assertNull(specificDeadLetterConsumer.receiveImmediate());

    // one more redelivery attempt:
    ClientMessage m = clientConsumer.receive(500);
    Assert.assertNotNull(m);
    Assert.assertEquals(specificeDeliveryAttempt, m.getDeliveryCount());
    m.acknowledge();
    clientSession.rollback();

    Assert.assertNull(defaultDeadLetterConsumer.receiveImmediate());
    Assert.assertNotNull(specificDeadLetterConsumer.receive(500));
  }
  @Test
  public void testGetAddress() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();

    session.createQueue(address, queue, false);

    CoreMessagingProxy proxy = createProxy(address);

    Assert.assertEquals(address.toString(), proxy.retrieveAttributeValue("address"));

    session.deleteQueue(queue);
  }
  @Test
  public void testMapWithArrayValues() throws Exception {
    String resource = RandomUtil.randomString();
    String operationName = RandomUtil.randomString();

    Map<String, Object> map = new HashMap<>();
    String key1 = RandomUtil.randomString();
    String[] val1 = new String[] {"a", "b", "c"};

    ManagementHelperTest.log.info("val1 type is " + Arrays.toString(val1));

    String key2 = RandomUtil.randomString();
    Long[] val2 = new Long[] {1L, 2L, 3L, 4L, 5L};

    ManagementHelperTest.log.info("val2 type is " + Arrays.toString(val2));

    map.put(key1, val1);
    map.put(key2, val2);

    Object[] params = new Object[] {"hello", map};

    Message msg = new ClientMessageImpl((byte) 0, false, 0, 0, (byte) 4, 1000);
    ManagementHelper.putOperationInvocation(msg, resource, operationName, params);

    Object[] parameters = ManagementHelper.retrieveOperationParameters(msg);

    Assert.assertEquals(params.length, parameters.length);

    Assert.assertEquals("hello", parameters[0]);

    Map map2 = (Map) parameters[1];
    Assert.assertEquals(2, map2.size());

    Object[] arr1 = (Object[]) map2.get(key1);
    Assert.assertEquals(val1.length, arr1.length);
    Assert.assertEquals(arr1[0], val1[0]);
    Assert.assertEquals(arr1[1], val1[1]);
    Assert.assertEquals(arr1[2], val1[2]);

    Object[] arr2 = (Object[]) map2.get(key2);
    Assert.assertEquals(val2.length, arr2.length);
    Assert.assertEquals(arr2[0], val2[0]);
    Assert.assertEquals(arr2[1], val2[1]);
    Assert.assertEquals(arr2[2], val2[2]);
  }
  @Test
  public void testCreateDurableQueueUsingJMSAndRestartServer() throws Exception {
    String queueName = RandomUtil.randomString();
    String binding = RandomUtil.randomString();

    ActiveMQTestBase.checkNoBinding(context, binding);
    checkNoResource(ObjectNameBuilder.DEFAULT.getJMSQueueObjectName(queueName));

    TransportConfiguration config =
        new TransportConfiguration(InVMConnectorFactory.class.getName());
    Connection connection =
        ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, config)
            .createConnection();
    connection.start();
    Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");
    QueueSession session = (QueueSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    QueueRequestor requestor = new QueueRequestor(session, managementQueue);
    Message message = session.createMessage();
    JMSManagementHelper.putOperationInvocation(
        message, "jms.server", "createQueue", queueName, binding);
    Message reply = requestor.request(message);
    Assert.assertTrue(JMSManagementHelper.hasOperationSucceeded(reply));
    connection.close();

    Object o = ActiveMQTestBase.checkBinding(context, binding);
    Assert.assertTrue(o instanceof Queue);
    Queue queue = (Queue) o;
    Assert.assertEquals(queueName, queue.getQueueName());
    checkResource(ObjectNameBuilder.DEFAULT.getJMSQueueObjectName(queueName));

    serverManager.stop();

    ActiveMQTestBase.checkNoBinding(context, binding);
    checkNoResource(ObjectNameBuilder.DEFAULT.getJMSQueueObjectName(queueName));

    serverManager = createJMSServer();
    serverManager.start();

    o = ActiveMQTestBase.checkBinding(context, binding);
    Assert.assertTrue(o instanceof Queue);
    queue = (Queue) o;
    Assert.assertEquals(queueName, queue.getQueueName());
    checkResource(ObjectNameBuilder.DEFAULT.getJMSQueueObjectName(queueName));
  }
  @Test
  public void testSECURITY_PERMISSION_VIOLATION() throws Exception {
    SimpleString queue = RandomUtil.randomSimpleString();
    SimpleString address = RandomUtil.randomSimpleString();

    // guest can not create queue
    Role role =
        new Role(
            "roleCanNotCreateQueue", true, true, false, true, false, true, true, true, true, true);
    Set<Role> roles = new HashSet<>();
    roles.add(role);
    server.getSecurityRepository().addMatch(address.toString(), roles);
    ActiveMQJAASSecurityManager securityManager =
        (ActiveMQJAASSecurityManager) server.getSecurityManager();
    securityManager.getConfiguration().addRole("guest", "roleCanNotCreateQueue");

    SecurityNotificationTest.flush(notifConsumer);

    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession guestSession = sf.createSession("guest", "guest", false, true, true, false, 1);

    try {
      guestSession.createQueue(address, queue, true);
      Assert.fail(
          "session creation must fail and a notification of security violation must be sent");
    } catch (Exception e) {
    }

    ClientMessage[] notifications = SecurityNotificationTest.consumeMessages(1, notifConsumer);
    Assert.assertEquals(
        SECURITY_PERMISSION_VIOLATION.toString(),
        notifications[0].getObjectProperty(ManagementHelper.HDR_NOTIFICATION_TYPE).toString());
    Assert.assertEquals(
        "guest", notifications[0].getObjectProperty(ManagementHelper.HDR_USER).toString());
    Assert.assertEquals(
        address.toString(),
        notifications[0].getObjectProperty(ManagementHelper.HDR_ADDRESS).toString());
    Assert.assertEquals(
        CheckType.CREATE_DURABLE_QUEUE.toString(),
        notifications[0].getObjectProperty(ManagementHelper.HDR_CHECK_TYPE).toString());

    guestSession.close();
  }
  @Test
  public void testGetRoles() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    Role role =
        new Role(
            RandomUtil.randomString(),
            RandomUtil.randomBoolean(),
            RandomUtil.randomBoolean(),
            RandomUtil.randomBoolean(),
            RandomUtil.randomBoolean(),
            RandomUtil.randomBoolean(),
            RandomUtil.randomBoolean(),
            RandomUtil.randomBoolean());

    session.createQueue(address, queue, true);

    CoreMessagingProxy proxy = createProxy(address);
    Object[] roles = (Object[]) proxy.retrieveAttributeValue("roles");
    for (Object role2 : roles) {
      System.out.println(((Object[]) role2)[0]);
    }
    Assert.assertEquals(0, roles.length);

    Set<Role> newRoles = new HashSet<>();
    newRoles.add(role);
    server.getSecurityRepository().addMatch(address.toString(), newRoles);

    roles = (Object[]) proxy.retrieveAttributeValue("roles");
    Assert.assertEquals(1, roles.length);
    Object[] r = (Object[]) roles[0];
    Assert.assertEquals(role.getName(), r[0]);
    Assert.assertEquals(CheckType.SEND.hasRole(role), r[1]);
    Assert.assertEquals(CheckType.CONSUME.hasRole(role), r[2]);
    Assert.assertEquals(CheckType.CREATE_DURABLE_QUEUE.hasRole(role), r[3]);
    Assert.assertEquals(CheckType.DELETE_DURABLE_QUEUE.hasRole(role), r[4]);
    Assert.assertEquals(CheckType.CREATE_NON_DURABLE_QUEUE.hasRole(role), r[5]);
    Assert.assertEquals(CheckType.DELETE_NON_DURABLE_QUEUE.hasRole(role), r[6]);
    Assert.assertEquals(CheckType.MANAGE.hasRole(role), r[7]);

    session.deleteQueue(queue);
  }
  @Test
  public void testSECURITY_AUTHENTICATION_VIOLATION() throws Exception {
    String unknownUser = RandomUtil.randomString();

    SecurityNotificationTest.flush(notifConsumer);

    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sf = createSessionFactory(locator);

    try {
      sf.createSession(unknownUser, RandomUtil.randomString(), false, true, true, false, 1);
      Assert.fail("authentication must fail and a notification of security violation must be sent");
    } catch (Exception e) {
    }

    ClientMessage[] notifications = SecurityNotificationTest.consumeMessages(1, notifConsumer);
    Assert.assertEquals(
        SECURITY_AUTHENTICATION_VIOLATION.toString(),
        notifications[0].getObjectProperty(ManagementHelper.HDR_NOTIFICATION_TYPE).toString());
  }
  @Test
  public void testDeadlLetterAddressWithDefaultAddressSettings() throws Exception {
    int deliveryAttempt = 3;

    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    SimpleString deadLetterAddress = RandomUtil.randomSimpleString();
    SimpleString deadLetterQueue = RandomUtil.randomSimpleString();
    AddressSettings addressSettings =
        new AddressSettings()
            .setMaxDeliveryAttempts(deliveryAttempt)
            .setDeadLetterAddress(deadLetterAddress);
    server.getAddressSettingsRepository().setDefault(addressSettings);

    clientSession.createQueue(address, queue, false);
    clientSession.createQueue(deadLetterAddress, deadLetterQueue, false);

    ClientProducer producer = clientSession.createProducer(address);
    ClientMessage clientMessage = createTextMessage(clientSession, "heyho!");
    producer.send(clientMessage);

    clientSession.start();
    ClientConsumer clientConsumer = clientSession.createConsumer(queue);
    for (int i = 0; i < deliveryAttempt; i++) {
      ClientMessage m = clientConsumer.receive(500);
      Assert.assertNotNull(m);
      DeadLetterAddressTest.log.info("i is " + i);
      DeadLetterAddressTest.log.info("delivery cout is " + m.getDeliveryCount());
      Assert.assertEquals(i + 1, m.getDeliveryCount());
      m.acknowledge();
      clientSession.rollback();
    }
    ClientMessage m = clientConsumer.receive(500);
    Assert.assertNull("not expecting a message", m);
    clientConsumer.close();

    clientConsumer = clientSession.createConsumer(deadLetterQueue);
    m = clientConsumer.receive(500);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");
  }
  @Override
  @Before
  public void setUp() throws Exception {
    super.setUp();

    Configuration config = createDefaultInVMConfig().setSecurityEnabled(true);
    server = addServer(ActiveMQServers.newActiveMQServer(config, false));
    server.start();

    notifQueue = RandomUtil.randomSimpleString();

    ActiveMQJAASSecurityManager securityManager =
        (ActiveMQJAASSecurityManager) server.getSecurityManager();
    securityManager.getConfiguration().addUser("admin", "admin");
    securityManager.getConfiguration().addUser("guest", "guest");
    securityManager.getConfiguration().setDefaultUser("guest");

    Role role = new Role("notif", true, true, true, true, true, true, true, true, true, true);
    Set<Role> roles = new HashSet<>();
    roles.add(role);
    server
        .getSecurityRepository()
        .addMatch(
            ActiveMQDefaultConfiguration.getDefaultManagementNotificationAddress().toString(),
            roles);

    securityManager.getConfiguration().addRole("admin", "notif");

    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sf = createSessionFactory(locator);
    adminSession = sf.createSession("admin", "admin", false, true, true, false, 1);
    adminSession.start();

    adminSession.createTemporaryQueue(
        ActiveMQDefaultConfiguration.getDefaultManagementNotificationAddress(), notifQueue);

    notifConsumer = adminSession.createConsumer(notifQueue);
  }
  @Test
  public void testArrayOfStringParameter() throws Exception {
    String resource = RandomUtil.randomString();
    String operationName = RandomUtil.randomString();
    String param = RandomUtil.randomString();
    String[] params =
        new String[] {
          RandomUtil.randomString(), RandomUtil.randomString(), RandomUtil.randomString()
        };
    Message msg = new ClientMessageImpl((byte) 0, false, 0, 0, (byte) 4, 1000);
    ManagementHelper.putOperationInvocation(msg, resource, operationName, param, params);

    Object[] parameters = ManagementHelper.retrieveOperationParameters(msg);
    Assert.assertEquals(2, parameters.length);
    Assert.assertEquals(param, parameters[0]);
    Object parameter_2 = parameters[1];
    ManagementHelperTest.log.info("type " + parameter_2);
    Assert.assertTrue(parameter_2 instanceof Object[]);
    Object[] retrievedParams = (Object[]) parameter_2;
    Assert.assertEquals(params.length, retrievedParams.length);
    for (int i = 0; i < retrievedParams.length; i++) {
      Assert.assertEquals(params[i], retrievedParams[i]);
    }
  }
  /**
   * @param sf
   * @param NMSGS
   * @throws ActiveMQException
   * @throws InterruptedException
   * @throws Throwable
   */
  private void produceMessages(final ClientSessionFactory sf, final int NMSGS) throws Throwable {

    final int TIMEOUT = 5000;

    System.out.println("sending " + NMSGS + " messages");

    final ClientSession sessionSend = sf.createSession(true, true);

    ClientProducer prod2 = sessionSend.createProducer("slow-queue");

    try {
      sessionSend.createQueue("Queue", "Queue", true);
    } catch (Exception ignored) {
    }

    final ClientSession sessionReceive = sf.createSession(true, true);
    sessionReceive.start();

    final ArrayList<Throwable> errors = new ArrayList<>();

    Thread tReceive =
        new Thread() {
          @Override
          public void run() {
            try {
              ClientConsumer consumer = sessionReceive.createConsumer("Queue");

              for (int i = 0; i < NMSGS; i++) {
                if (i % 500 == 0) {
                  double percent = (double) i / (double) NMSGS;
                  System.out.println(
                      "msgs " + i + " of " + NMSGS + ", " + (int) (percent * 100) + "%");
                  Thread.sleep(100);
                }

                ClientMessage msg = consumer.receive(TIMEOUT);
                if (msg == null) {
                  errors.add(new Exception("Didn't receive msgs"));
                  break;
                }
                msg.acknowledge();
              }
            } catch (Exception e) {
              errors.add(e);
            }
          }
        };

    tReceive.start();

    ClientProducer prod = sessionSend.createProducer("Queue");

    Random random = new Random();

    for (int i = 0; i < NMSGS; i++) {
      ClientMessage msg = sessionSend.createMessage(true);

      int size = RandomUtil.randomPositiveInt() % 10024;

      if (size == 0) {
        size = 10 * 1024;
      }

      byte[] buffer = new byte[size];

      random.nextBytes(buffer);

      msg.getBodyBuffer().writeBytes(buffer);

      prod.send(msg);

      if (i % 5000 == 0) {
        prod2.send(msg);
        System.out.println("Sending slow message");
      }
    }

    tReceive.join();

    sessionReceive.close();
    sessionSend.close();
    sf.close();

    for (Throwable e : errors) {
      throw e;
    }
  }
  private void testSettersThrowException(final ActiveMQConnectionFactory cf) {

    String discoveryAddress = RandomUtil.randomString();
    int discoveryPort = RandomUtil.randomPositiveInt();
    long discoveryRefreshTimeout = RandomUtil.randomPositiveLong();
    String clientID = RandomUtil.randomString();
    long clientFailureCheckPeriod = RandomUtil.randomPositiveLong();
    long connectionTTL = RandomUtil.randomPositiveLong();
    long callTimeout = RandomUtil.randomPositiveLong();
    int minLargeMessageSize = RandomUtil.randomPositiveInt();
    int consumerWindowSize = RandomUtil.randomPositiveInt();
    int consumerMaxRate = RandomUtil.randomPositiveInt();
    int confirmationWindowSize = RandomUtil.randomPositiveInt();
    int producerMaxRate = RandomUtil.randomPositiveInt();
    boolean blockOnAcknowledge = RandomUtil.randomBoolean();
    boolean blockOnDurableSend = RandomUtil.randomBoolean();
    boolean blockOnNonDurableSend = RandomUtil.randomBoolean();
    boolean autoGroup = RandomUtil.randomBoolean();
    boolean preAcknowledge = RandomUtil.randomBoolean();
    String loadBalancingPolicyClassName = RandomUtil.randomString();
    int dupsOKBatchSize = RandomUtil.randomPositiveInt();
    int transactionBatchSize = RandomUtil.randomPositiveInt();
    long initialWaitTimeout = RandomUtil.randomPositiveLong();
    boolean useGlobalPools = RandomUtil.randomBoolean();
    int scheduledThreadPoolMaxSize = RandomUtil.randomPositiveInt();
    int threadPoolMaxSize = RandomUtil.randomPositiveInt();
    long retryInterval = RandomUtil.randomPositiveLong();
    double retryIntervalMultiplier = RandomUtil.randomDouble();
    int reconnectAttempts = RandomUtil.randomPositiveInt();
    boolean failoverOnServerShutdown = RandomUtil.randomBoolean();

    try {
      cf.setClientID(clientID);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setClientFailureCheckPeriod(clientFailureCheckPeriod);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setConnectionTTL(connectionTTL);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setCallTimeout(callTimeout);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setMinLargeMessageSize(minLargeMessageSize);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setConsumerWindowSize(consumerWindowSize);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setConsumerMaxRate(consumerMaxRate);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setConfirmationWindowSize(confirmationWindowSize);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setProducerMaxRate(producerMaxRate);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setBlockOnAcknowledge(blockOnAcknowledge);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setBlockOnDurableSend(blockOnDurableSend);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setBlockOnNonDurableSend(blockOnNonDurableSend);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setAutoGroup(autoGroup);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setPreAcknowledge(preAcknowledge);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setConnectionLoadBalancingPolicyClassName(loadBalancingPolicyClassName);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setDupsOKBatchSize(dupsOKBatchSize);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setTransactionBatchSize(transactionBatchSize);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setUseGlobalPools(useGlobalPools);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setScheduledThreadPoolMaxSize(scheduledThreadPoolMaxSize);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setThreadPoolMaxSize(threadPoolMaxSize);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setRetryInterval(retryInterval);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setRetryIntervalMultiplier(retryIntervalMultiplier);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }
    try {
      cf.setReconnectAttempts(reconnectAttempts);
      Assert.fail("Should throw exception");
    } catch (IllegalStateException e) {
      // OK
    }

    cf.getStaticConnectors();
    cf.getClientID();
    cf.getClientFailureCheckPeriod();
    cf.getConnectionTTL();
    cf.getCallTimeout();
    cf.getMinLargeMessageSize();
    cf.getConsumerWindowSize();
    cf.getConsumerMaxRate();
    cf.getConfirmationWindowSize();
    cf.getProducerMaxRate();
    cf.isBlockOnAcknowledge();
    cf.isBlockOnDurableSend();
    cf.isBlockOnNonDurableSend();
    cf.isAutoGroup();
    cf.isPreAcknowledge();
    cf.getConnectionLoadBalancingPolicyClassName();
    cf.getDupsOKBatchSize();
    cf.getTransactionBatchSize();
    cf.isUseGlobalPools();
    cf.getScheduledThreadPoolMaxSize();
    cf.getThreadPoolMaxSize();
    cf.getRetryInterval();
    cf.getRetryIntervalMultiplier();
    cf.getReconnectAttempts();

    cf.close();
  }
  @Test
  public void testGettersAndSetters() {
    ActiveMQConnectionFactory cf =
        ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, liveTC);

    long clientFailureCheckPeriod = RandomUtil.randomPositiveLong();
    long connectionTTL = RandomUtil.randomPositiveLong();
    long callTimeout = RandomUtil.randomPositiveLong();
    int minLargeMessageSize = RandomUtil.randomPositiveInt();
    int consumerWindowSize = RandomUtil.randomPositiveInt();
    int consumerMaxRate = RandomUtil.randomPositiveInt();
    int confirmationWindowSize = RandomUtil.randomPositiveInt();
    int producerMaxRate = RandomUtil.randomPositiveInt();
    boolean blockOnAcknowledge = RandomUtil.randomBoolean();
    boolean blockOnDurableSend = RandomUtil.randomBoolean();
    boolean blockOnNonDurableSend = RandomUtil.randomBoolean();
    boolean autoGroup = RandomUtil.randomBoolean();
    boolean preAcknowledge = RandomUtil.randomBoolean();
    String loadBalancingPolicyClassName = RandomUtil.randomString();
    boolean useGlobalPools = RandomUtil.randomBoolean();
    int scheduledThreadPoolMaxSize = RandomUtil.randomPositiveInt();
    int threadPoolMaxSize = RandomUtil.randomPositiveInt();
    long retryInterval = RandomUtil.randomPositiveLong();
    double retryIntervalMultiplier = RandomUtil.randomDouble();
    int reconnectAttempts = RandomUtil.randomPositiveInt();
    cf.setClientFailureCheckPeriod(clientFailureCheckPeriod);
    cf.setConnectionTTL(connectionTTL);
    cf.setCallTimeout(callTimeout);
    cf.setMinLargeMessageSize(minLargeMessageSize);
    cf.setConsumerWindowSize(consumerWindowSize);
    cf.setConsumerMaxRate(consumerMaxRate);
    cf.setConfirmationWindowSize(confirmationWindowSize);
    cf.setProducerMaxRate(producerMaxRate);
    cf.setBlockOnAcknowledge(blockOnAcknowledge);
    cf.setBlockOnDurableSend(blockOnDurableSend);
    cf.setBlockOnNonDurableSend(blockOnNonDurableSend);
    cf.setAutoGroup(autoGroup);
    cf.setPreAcknowledge(preAcknowledge);
    cf.setConnectionLoadBalancingPolicyClassName(loadBalancingPolicyClassName);
    cf.setUseGlobalPools(useGlobalPools);
    cf.setScheduledThreadPoolMaxSize(scheduledThreadPoolMaxSize);
    cf.setThreadPoolMaxSize(threadPoolMaxSize);
    cf.setRetryInterval(retryInterval);
    cf.setRetryIntervalMultiplier(retryIntervalMultiplier);
    cf.setReconnectAttempts(reconnectAttempts);
    Assert.assertEquals(clientFailureCheckPeriod, cf.getClientFailureCheckPeriod());
    Assert.assertEquals(connectionTTL, cf.getConnectionTTL());
    Assert.assertEquals(callTimeout, cf.getCallTimeout());
    Assert.assertEquals(minLargeMessageSize, cf.getMinLargeMessageSize());
    Assert.assertEquals(consumerWindowSize, cf.getConsumerWindowSize());
    Assert.assertEquals(consumerMaxRate, cf.getConsumerMaxRate());
    Assert.assertEquals(confirmationWindowSize, cf.getConfirmationWindowSize());
    Assert.assertEquals(producerMaxRate, cf.getProducerMaxRate());
    Assert.assertEquals(blockOnAcknowledge, cf.isBlockOnAcknowledge());
    Assert.assertEquals(blockOnDurableSend, cf.isBlockOnDurableSend());
    Assert.assertEquals(blockOnNonDurableSend, cf.isBlockOnNonDurableSend());
    Assert.assertEquals(autoGroup, cf.isAutoGroup());
    Assert.assertEquals(preAcknowledge, cf.isPreAcknowledge());
    Assert.assertEquals(
        loadBalancingPolicyClassName, cf.getConnectionLoadBalancingPolicyClassName());
    Assert.assertEquals(useGlobalPools, cf.isUseGlobalPools());
    Assert.assertEquals(scheduledThreadPoolMaxSize, cf.getScheduledThreadPoolMaxSize());
    Assert.assertEquals(threadPoolMaxSize, cf.getThreadPoolMaxSize());
    Assert.assertEquals(retryInterval, cf.getRetryInterval());
    Assert.assertEquals(retryIntervalMultiplier, cf.getRetryIntervalMultiplier(), 0.0001);
    Assert.assertEquals(reconnectAttempts, cf.getReconnectAttempts());

    cf.close();
  }
  @Test
  public void testParams() throws Exception {
    String resource = RandomUtil.randomString();
    String operationName = RandomUtil.randomString();

    long i = RandomUtil.randomInt();
    String s = RandomUtil.randomString();
    double d = RandomUtil.randomDouble();
    boolean b = RandomUtil.randomBoolean();
    long l = RandomUtil.randomLong();
    Map<String, Object> map = new HashMap<>();
    String key1 = RandomUtil.randomString();
    int value1 = RandomUtil.randomInt();
    String key2 = RandomUtil.randomString();
    double value2 = RandomUtil.randomDouble();
    String key3 = RandomUtil.randomString();
    String value3 = RandomUtil.randomString();
    String key4 = RandomUtil.randomString();
    boolean value4 = RandomUtil.randomBoolean();
    String key5 = RandomUtil.randomString();
    long value5 = RandomUtil.randomLong();
    map.put(key1, value1);
    map.put(key2, value2);
    map.put(key3, value3);
    map.put(key4, value4);
    map.put(key5, value5);

    Map<String, Object> map2 = new HashMap<>();
    String key2_1 = RandomUtil.randomString();
    int value2_1 = RandomUtil.randomInt();
    String key2_2 = RandomUtil.randomString();
    double value2_2 = RandomUtil.randomDouble();
    String key2_3 = RandomUtil.randomString();
    String value2_3 = RandomUtil.randomString();
    String key2_4 = RandomUtil.randomString();
    boolean value2_4 = RandomUtil.randomBoolean();
    String key2_5 = RandomUtil.randomString();
    long value2_5 = RandomUtil.randomLong();
    map2.put(key2_1, value2_1);
    map2.put(key2_2, value2_2);
    map2.put(key2_3, value2_3);
    map2.put(key2_4, value2_4);
    map2.put(key2_5, value2_5);

    Map<String, Object> map3 = new HashMap<>();
    String key3_1 = RandomUtil.randomString();
    int value3_1 = RandomUtil.randomInt();
    String key3_2 = RandomUtil.randomString();
    double value3_2 = RandomUtil.randomDouble();
    String key3_3 = RandomUtil.randomString();
    String value3_3 = RandomUtil.randomString();
    String key3_4 = RandomUtil.randomString();
    boolean value3_4 = RandomUtil.randomBoolean();
    String key3_5 = RandomUtil.randomString();
    long value3_5 = RandomUtil.randomLong();
    map3.put(key3_1, value3_1);
    map3.put(key3_2, value3_2);
    map3.put(key3_3, value3_3);
    map3.put(key3_4, value3_4);
    map3.put(key3_5, value3_5);

    Map[] maps = new Map[] {map2, map3};

    String strElem0 = RandomUtil.randomString();
    String strElem1 = RandomUtil.randomString();
    String strElem2 = RandomUtil.randomString();

    String[] strArray = new String[] {strElem0, strElem1, strElem2};

    Object[] params = new Object[] {i, s, d, b, l, map, strArray, maps};

    Message msg = new ClientMessageImpl((byte) 0, false, 0, 0, (byte) 4, 1000);
    ManagementHelper.putOperationInvocation(msg, resource, operationName, params);

    Object[] parameters = ManagementHelper.retrieveOperationParameters(msg);

    Assert.assertEquals(params.length, parameters.length);

    Assert.assertEquals(i, parameters[0]);
    Assert.assertEquals(s, parameters[1]);
    Assert.assertEquals(d, parameters[2]);
    Assert.assertEquals(b, parameters[3]);
    Assert.assertEquals(l, parameters[4]);
    Map mapRes = (Map) parameters[5];
    Assert.assertEquals(map.size(), mapRes.size());
    Assert.assertEquals((long) value1, mapRes.get(key1));
    Assert.assertEquals(value2, mapRes.get(key2));
    Assert.assertEquals(value3, mapRes.get(key3));
    Assert.assertEquals(value4, mapRes.get(key4));
    Assert.assertEquals(value5, mapRes.get(key5));

    Object[] strArr2 = (Object[]) parameters[6];
    Assert.assertEquals(strArray.length, strArr2.length);
    Assert.assertEquals(strElem0, strArr2[0]);
    Assert.assertEquals(strElem1, strArr2[1]);
    Assert.assertEquals(strElem2, strArr2[2]);

    Object[] mapArray = (Object[]) parameters[7];
    Assert.assertEquals(2, mapArray.length);
    Map mapRes2 = (Map) mapArray[0];
    Assert.assertEquals(map2.size(), mapRes2.size());
    Assert.assertEquals((long) value2_1, mapRes2.get(key2_1));
    Assert.assertEquals(value2_2, mapRes2.get(key2_2));
    Assert.assertEquals(value2_3, mapRes2.get(key2_3));
    Assert.assertEquals(value2_4, mapRes2.get(key2_4));
    Assert.assertEquals(value2_5, mapRes2.get(key2_5));

    Map mapRes3 = (Map) mapArray[1];
    Assert.assertEquals(map3.size(), mapRes3.size());
    Assert.assertEquals((long) value3_1, mapRes3.get(key3_1));
    Assert.assertEquals(value3_2, mapRes3.get(key3_2));
    Assert.assertEquals(value3_3, mapRes3.get(key3_3));
    Assert.assertEquals(value3_4, mapRes3.get(key3_4));
    Assert.assertEquals(value3_5, mapRes3.get(key3_5));
  }