public void testQueuePriority() throws Exception {
    // Set up queue with 5 priorities
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueuePriority.queues(-1).queue(-1).name(-1)", "atest");
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueuePriority.queues.queue.atest(-1).exchange",
            "amq.direct");
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueuePriority.queues.queue.atest.priorities", "5");

    // Set up queue with JMS style priorities
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueuePriority.queues(-1).queue(-1).name(-1)", "ptest");
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueuePriority.queues.queue.ptest(-1).exchange",
            "amq.direct");
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueuePriority.queues.queue.ptest.priority", "true");

    // Set up queue with no priorities
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueuePriority.queues(-1).queue(-1).name(-1)", "ntest");
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueuePriority.queues.queue.ntest(-1).exchange",
            "amq.direct");
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueuePriority.queues.queue.ntest.priority", "false");

    // Start the broker now.
    super.createBroker();

    VirtualHost vhost =
        ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost(getName());

    // Check that atest was a priority queue with 5 priorities
    AMQQueue atest = vhost.getQueueRegistry().getQueue(new AMQShortString("atest"));
    assertTrue(atest instanceof AMQPriorityQueue);
    assertEquals(5, ((AMQPriorityQueue) atest).getPriorities());

    // Check that ptest was a priority queue with 10 priorities
    AMQQueue ptest = vhost.getQueueRegistry().getQueue(new AMQShortString("ptest"));
    assertTrue(ptest instanceof AMQPriorityQueue);
    assertEquals(10, ((AMQPriorityQueue) ptest).getPriorities());

    // Check that ntest wasn't a priority queue
    AMQQueue ntest = vhost.getQueueRegistry().getQueue(new AMQShortString("ntest"));
    assertFalse(ntest instanceof AMQPriorityQueue);
  }
  public void testQueueAlerts() throws Exception {
    // Set up queue with 5 priorities
    getConfigXml()
        .addProperty("virtualhosts.virtualhost.testQueueAlerts.queues.exchange", "amq.topic");
    getConfigXml()
        .addProperty("virtualhosts.virtualhost.testQueueAlerts.queues.maximumQueueDepth", "1");
    getConfigXml()
        .addProperty("virtualhosts.virtualhost.testQueueAlerts.queues.maximumMessageSize", "2");
    getConfigXml()
        .addProperty("virtualhosts.virtualhost.testQueueAlerts.queues.maximumMessageAge", "3");

    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueueAlerts.queues(-1).queue(1).name(1)", "atest");
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueueAlerts.queues.queue.atest(-1).exchange",
            "amq.direct");
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueueAlerts.queues.queue.atest(-1).maximumQueueDepth",
            "4");
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueueAlerts.queues.queue.atest(-1).maximumMessageSize",
            "5");
    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueueAlerts.queues.queue.atest(-1).maximumMessageAge",
            "6");

    getConfigXml()
        .addProperty(
            "virtualhosts.virtualhost.testQueueAlerts.queues(-1).queue(-1).name(-1)", "btest");

    // Start the broker now.
    super.createBroker();

    VirtualHost vhost =
        ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost(getName());

    // Check specifically configured values
    AMQQueue aTest = vhost.getQueueRegistry().getQueue(new AMQShortString("atest"));
    assertEquals(4, aTest.getMaximumQueueDepth());
    assertEquals(5, aTest.getMaximumMessageSize());
    assertEquals(6, aTest.getMaximumMessageAge());

    // Check default values
    AMQQueue bTest = vhost.getQueueRegistry().getQueue(new AMQShortString("btest"));
    assertEquals(1, bTest.getMaximumQueueDepth());
    assertEquals(2, bTest.getMaximumMessageSize());
    assertEquals(3, bTest.getMaximumMessageAge());
  }
  protected AMQQueue createQueue(
      final AMQShortString queueName,
      QueueDeclareBody body,
      VirtualHost virtualHost,
      final AMQProtocolSession session)
      throws AMQException {
    final QueueRegistry registry = virtualHost.getQueueRegistry();
    AMQShortString owner = body.getExclusive() ? session.getContextKey() : null;

    final AMQQueue queue =
        AMQQueueFactory.createAMQQueueImpl(
            queueName,
            body.getDurable(),
            owner,
            body.getAutoDelete(),
            body.getExclusive(),
            virtualHost,
            body.getArguments());

    if (body.getExclusive() && !body.getDurable()) {
      final AMQProtocolSession.Task deleteQueueTask =
          new AMQProtocolSession.Task() {
            public void doTask(AMQProtocolSession session) throws AMQException {
              if (registry.getQueue(queueName) == queue) {
                queue.delete();
              }
            }
          };

      session.addSessionCloseTask(deleteQueueTask);

      queue.addQueueDeleteTask(
          new AMQQueue.Task() {
            public void doTask(AMQQueue queue) {
              session.removeSessionCloseTask(deleteQueueTask);
            }
          });
    }

    return queue;
  }
  public static AMQQueue createAMQQueueImpl(
      String queueName,
      boolean durable,
      String owner,
      boolean autoDelete,
      boolean exclusive,
      VirtualHost virtualHost,
      Map<String, Object> arguments)
      throws AMQSecurityException {
    // Access check
    if (!virtualHost
        .getSecurityManager()
        .authoriseCreateQueue(
            autoDelete, durable, exclusive, null, null, new AMQShortString(queueName), owner)) {
      String description = "Permission denied: queue-name '" + queueName + "'";
      throw new AMQSecurityException(description);
    }

    int priorities = 1;
    String conflationKey = null;
    if (arguments != null) {
      if (arguments.containsKey(QPID_LAST_VALUE_QUEUE)
          || arguments.containsKey(QPID_LAST_VALUE_QUEUE_KEY)) {
        conflationKey = (String) arguments.get(QPID_LAST_VALUE_QUEUE_KEY);
        if (conflationKey == null) {
          conflationKey = QPID_LVQ_KEY;
        }
      } else if (arguments.containsKey(X_QPID_PRIORITIES.toString())) {
        Object prioritiesObj = arguments.get(X_QPID_PRIORITIES.toString());
        if (prioritiesObj instanceof Number) {
          priorities = ((Number) prioritiesObj).intValue();
        }
      }
    }

    AMQQueue q;
    if (conflationKey != null) {
      q =
          new ConflationQueue(
              queueName,
              durable,
              owner,
              autoDelete,
              exclusive,
              virtualHost,
              arguments,
              conflationKey);
    } else if (priorities > 1) {
      q =
          new AMQPriorityQueue(
              queueName, durable, owner, autoDelete, exclusive, virtualHost, priorities, arguments);
    } else {
      q =
          new SimpleAMQQueue(
              queueName, durable, owner, autoDelete, exclusive, virtualHost, arguments);
    }

    // Register the new queue
    virtualHost.getQueueRegistry().registerQueue(q);
    q.configure(virtualHost.getConfiguration().getQueueConfiguration(queueName));

    if (arguments != null) {
      for (QueueProperty p : DECLAREABLE_PROPERTIES) {
        if (arguments.containsKey(p.getArgumentName().toString())) {
          p.setPropertyValue(q, arguments.get(p.getArgumentName().toString()));
        }
      }
    }

    return q;
  }
  public void methodReceived(AMQStateManager stateManager, QueueDeclareBody body, int channelId)
      throws AMQException {
    final AMQProtocolSession protocolConnection = stateManager.getProtocolSession();
    final AMQSessionModel session = protocolConnection.getChannel(channelId);
    VirtualHost virtualHost = protocolConnection.getVirtualHost();
    ExchangeRegistry exchangeRegistry = virtualHost.getExchangeRegistry();
    QueueRegistry queueRegistry = virtualHost.getQueueRegistry();
    DurableConfigurationStore store = virtualHost.getDurableConfigurationStore();

    final AMQShortString queueName;

    // if we aren't given a queue name, we create one which we return to the client
    if ((body.getQueue() == null) || (body.getQueue().length() == 0)) {
      queueName = createName();
    } else {
      queueName = body.getQueue().intern();
    }

    AMQQueue queue;

    // TODO: do we need to check that the queue already exists with exactly the same
    // "configuration"?

    synchronized (queueRegistry) {
      queue = queueRegistry.getQueue(queueName);

      AMQSessionModel owningSession = null;

      if (queue != null) {
        owningSession = queue.getExclusiveOwningSession();
      }

      if (queue == null) {
        if (body.getPassive()) {
          String msg = "Queue: " + queueName + " not found on VirtualHost(" + virtualHost + ").";
          throw body.getChannelException(AMQConstant.NOT_FOUND, msg);
        } else {
          queue = createQueue(queueName, body, virtualHost, protocolConnection);
          queue.setAuthorizationHolder(protocolConnection);
          if (queue.isDurable() && !queue.isAutoDelete()) {
            store.createQueue(queue, body.getArguments());

            // Tell Andes kernel to create queue
            QpidAndesBridge.createQueue(queue);
          }
          if (body.getAutoDelete()) {
            queue.setDeleteOnNoConsumers(true);
          }
          queueRegistry.registerQueue(queue);
          if (body.getExclusive()) {
            queue.setExclusiveOwningSession(protocolConnection.getChannel(channelId));
            queue.setAuthorizationHolder(protocolConnection);

            if (!body.getDurable()) {
              final AMQQueue q = queue;
              final AMQProtocolSession.Task sessionCloseTask =
                  new AMQProtocolSession.Task() {
                    public void doTask(AMQProtocolSession session) throws AMQException {
                      q.setExclusiveOwningSession(null);
                    }
                  };
              protocolConnection.addSessionCloseTask(sessionCloseTask);
              queue.addQueueDeleteTask(
                  new AMQQueue.Task() {
                    public void doTask(AMQQueue queue) throws AMQException {
                      protocolConnection.removeSessionCloseTask(sessionCloseTask);
                    }
                  });
            }
          }
          if (autoRegister) {
            Exchange defaultExchange = exchangeRegistry.getDefaultExchange();

            virtualHost
                .getBindingFactory()
                .addBinding(
                    String.valueOf(queueName), queue, defaultExchange, Collections.EMPTY_MAP);
            _logger.info(
                "Queue "
                    + queueName
                    + " bound to default exchange("
                    + defaultExchange.getNameShortString()
                    + ")");
          }
        }
      } else if (queue.isExclusive()
          && !queue.isDurable()
          && (owningSession == null || owningSession.getConnectionModel() != protocolConnection)) {
        throw body.getConnectionException(
            AMQConstant.NOT_ALLOWED,
            "Queue "
                + queue.getNameShortString()
                + " is exclusive, but not created on this Connection.");
      } else if (!body.getPassive() && ((queue.isExclusive()) != body.getExclusive())) {

        throw body.getChannelException(
            AMQConstant.ALREADY_EXISTS,
            "Cannot re-declare queue '"
                + queue.getNameShortString()
                + "' with different exclusivity (was: "
                + queue.isExclusive()
                + " requested "
                + body.getExclusive()
                + ")");
      } else if (!body.getPassive()
          && body.getExclusive()
          && !(queue.isDurable()
              ? String.valueOf(queue.getOwner()).equals(session.getClientID())
              : (owningSession == null
                  || owningSession.getConnectionModel() == protocolConnection))) {
        throw body.getChannelException(
            AMQConstant.ALREADY_EXISTS,
            "Cannot declare queue('"
                + queueName
                + "'), "
                + "as exclusive queue with same name "
                + "declared on another client ID('"
                + queue.getOwner()
                + "') your clientID('"
                + session.getClientID()
                + "')");

      } else if (!body.getPassive() && queue.isAutoDelete() != body.getAutoDelete()) {
        throw body.getChannelException(
            AMQConstant.ALREADY_EXISTS,
            "Cannot re-declare queue '"
                + queue.getNameShortString()
                + "' with different auto-delete (was: "
                + queue.isAutoDelete()
                + " requested "
                + body.getAutoDelete()
                + ")");
      } else if (!body.getPassive() && queue.isDurable() != body.getDurable()) {
        throw body.getChannelException(
            AMQConstant.ALREADY_EXISTS,
            "Cannot re-declare queue '"
                + queue.getNameShortString()
                + "' with different durability (was: "
                + queue.isDurable()
                + " requested "
                + body.getDurable()
                + ")");
      }

      AMQChannel channel = protocolConnection.getChannel(channelId);

      if (channel == null) {
        throw body.getChannelNotFoundException(channelId);
      }

      // set this as the default queue on the channel:
      channel.setDefaultQueue(queue);
    }

    if (!body.getNowait()) {
      MethodRegistry methodRegistry = protocolConnection.getMethodRegistry();
      QueueDeclareOkBody responseBody =
          methodRegistry.createQueueDeclareOkBody(
              queueName, queue.getMessageCount(), queue.getConsumerCount());
      protocolConnection.writeFrame(responseBody.generateFrame(channelId));

      _logger.info("Queue " + queueName + " declared successfully");
    }
  }