TEST(MessageFilterTest, testNullMessageFilter) {
  Hedwig::Configuration* conf = new MessageFilterConfiguration();
  std::auto_ptr<Hedwig::Configuration> confptr(conf);
  
  Hedwig::Client* client = new Hedwig::Client(*conf);
  std::auto_ptr<Hedwig::Client> clientptr(client);

  Hedwig::Subscriber& sub = client->getSubscriber();

  std::string topic = "testNullMessageFilter";
  std::string subid = "myTestSubid";

  sub.subscribe(topic, subid, Hedwig::SubscribeRequest::CREATE_OR_ATTACH);

  GapCheckingMessageHandlerCallback * cb =
    new GapCheckingMessageHandlerCallback(sub, 0, 0, 0, true);
  Hedwig::MessageHandlerCallbackPtr handler(cb);
  Hedwig::ClientMessageFilterPtr filter(new ModMessageFilter());

  ASSERT_THROW(sub.startDeliveryWithFilter(topic, subid, handler,
                                           Hedwig::ClientMessageFilterPtr()),
               Hedwig::NullMessageFilterException);
  ASSERT_THROW(sub.startDeliveryWithFilter(topic, subid, 
                                           Hedwig::MessageHandlerCallbackPtr(), filter),
               Hedwig::NullMessageHandlerException);
}
  void testAsyncSubcribeAndUnsubscribe() {
    SimpleWaitCondition* cond1 = new SimpleWaitCondition();
    std::auto_ptr<SimpleWaitCondition> cond1ptr(cond1);
    SimpleWaitCondition* cond2 = new SimpleWaitCondition();
    std::auto_ptr<SimpleWaitCondition> cond2ptr(cond2);

    Hedwig::Configuration* conf = new TestServerConfiguration();
    std::auto_ptr<Hedwig::Configuration> confptr(conf);

    Hedwig::Client* client = new Hedwig::Client(*conf);
    std::auto_ptr<Hedwig::Client> clientptr(client);

    Hedwig::Subscriber& sub = client->getSubscriber();
   
    Hedwig::OperationCallbackPtr testcb1(new TestCallback(cond1));
    Hedwig::OperationCallbackPtr testcb2(new TestCallback(cond2));

    sub.asyncSubscribe("testTopic", "mySubscriberId-4", Hedwig::SubscribeRequest::CREATE_OR_ATTACH, testcb1);
    cond1->wait();
    CPPUNIT_ASSERT(cond1->wasSuccess());
    
    sub.asyncUnsubscribe("testTopic", "mySubscriberId-4", testcb2);
    cond2->wait();
    CPPUNIT_ASSERT(cond2->wasSuccess());
  }
  void testSyncSubscribeAttach() {
    Hedwig::Configuration* conf = new TestServerConfiguration();
    std::auto_ptr<Hedwig::Configuration> confptr(conf);

    Hedwig::Client* client = new Hedwig::Client(*conf);
    std::auto_ptr<Hedwig::Client> clientptr(client);

    Hedwig::Subscriber& sub = client->getSubscriber();
    
    CPPUNIT_ASSERT_THROW(sub.subscribe("iAmATopicWhoDoesNotExist", "mySubscriberId-2", Hedwig::SubscribeRequest::ATTACH), Hedwig::ClientException);
  }
  void testSyncSubscribe() {
    Hedwig::Configuration* conf = new TestServerConfiguration();
    std::auto_ptr<Hedwig::Configuration> confptr(conf);
    
    Hedwig::Client* client = new Hedwig::Client(*conf);
    std::auto_ptr<Hedwig::Client> clientptr(client);

    Hedwig::Subscriber& sub = client->getSubscriber();
    
    sub.subscribe("testTopic", "mySubscriberId-1", Hedwig::SubscribeRequest::CREATE_OR_ATTACH);
  }
  void testUnsubscribeWithoutSubscribe() {
    Hedwig::Configuration* conf = new TestServerConfiguration();
    std::auto_ptr<Hedwig::Configuration> confptr(conf);
    
    Hedwig::Client* client = new Hedwig::Client(*conf);
    std::auto_ptr<Hedwig::Client> clientptr(client);

    Hedwig::Subscriber& sub = client->getSubscriber();
    
    CPPUNIT_ASSERT_THROW(sub.unsubscribe("testTopic", "mySubscriberId-7"), Hedwig::NotSubscribedException);
  }
  void testSubscribeTwice() {
    Hedwig::Configuration* conf = new TestServerConfiguration();
    std::auto_ptr<Hedwig::Configuration> confptr(conf);
    
    Hedwig::Client* client = new Hedwig::Client(*conf);
    std::auto_ptr<Hedwig::Client> clientptr(client);

    Hedwig::Subscriber& sub = client->getSubscriber();
    
    sub.subscribe("testTopic", "mySubscriberId-8", Hedwig::SubscribeRequest::CREATE_OR_ATTACH);
    CPPUNIT_ASSERT_THROW(sub.subscribe("testTopic", "mySubscriberId-8", Hedwig::SubscribeRequest::CREATE_OR_ATTACH), Hedwig::AlreadySubscribedException);
  }
TEST(MessageFilterTest, testMessageFilter) {
  Hedwig::Configuration* conf = new MessageFilterConfiguration();
  std::auto_ptr<Hedwig::Configuration> confptr(conf);
  
  Hedwig::Client* client = new Hedwig::Client(*conf);
  std::auto_ptr<Hedwig::Client> clientptr(client);

  Hedwig::Subscriber& sub = client->getSubscriber();
  Hedwig::Publisher& pub = client->getPublisher();

  std::string topic = "testMessageFilter";
  std::string subid = "myTestSubid";
  sub.subscribe(topic, subid, Hedwig::SubscribeRequest::CREATE_OR_ATTACH);
  sub.closeSubscription(topic, subid);

  publishNums(pub, topic, 0, 100, 2);
  receiveNumModM(sub, topic, subid, 0, 50, 2, true);
}