Hi

Can't you save whatever data you need in your asserts. and do your asserts
outside of messageReceived ?
Something like:

@Test
   public void ensureCorrectPacketSent() {

       final List<Contact> contacts = ... create list and put contacts in
it...;
       final List<Whatever> received = ...
       final CountDownLatch latch = new CountDownLatch(1);*
*

       IoAcceptor server = startServer(9090, new IoHandlerAdapter() {
         @Override
         public void sessionOpened(IoSession session) throws Exception {
           session.write(new ContactListPacket(contacts));
         }
       });

       IoConnector client = startClient(9090, new IoHandlerAdapter() {
           @Override
           public void messageRecieved(IoSession session, Object message) {
               Whatever whatever = extractWhateverFrom(message);
               received.add(whatever);
               latch.countDown();
           }
       });

       latch.await();
       assertTrue( received.contains(...));
}

Maarten

On Mon, Jun 8, 2009 at 10:10 PM, Michael Ossareh <[email protected]> wrote:

> On Mon, Jun 8, 2009 at 12:40 PM, David Rosenstrauch<[email protected]>
> wrote:
> > Michael Ossareh wrote:
> >>
> >> I've uncovered what I believe to be a problem in the framework of
> >> MINA. I'm using 2.0.0-m5. As it currently stands any classes that
> >> subclass IoHandlerAdapter are crippled by the methods throwing
> >> Exception and the framework catching it. In my case I cannot write
> >> JUnit test cases because the Exception thrown by Assert.fail() is
> >> caught up in the framework therefore not allowing me to verify
> >> failures.
>
> .. snip..
>
> >> Thoughts? Depending what we come up with I'm happy to implement a fix.
> >>
> >> Cheers,
> >>
> >> mike
> >
> > Can you elaborate (or give an example) of exactly what the problem is?
> I've
> > written numerous junit tests for my MINA server, though I suppose I did
> have
> > to do it in a somewhat roundabout way:  I sent a simulated received
> message
> > down the filter chain, and then verify that it triggers sending a
> particular
> > response.  Code attached.
> >
> > HTH,
> >
> > DR
> >
> > ----
>
> Hi David, In my case
>
> public class SomeTest() {
>
>
>    @Test
>    public void ensureCorrectPacketSent() {
>
>        final List<Contact> contacts = ... create list and put
> contacts in it...;
>
>        IoAcceptor server = startServer(9090, new IoHandlerAdapter() {
>          @Override
>          public void sessionOpened(IoSession session) throws Exception {
>            session.write(new ContactListPacket(contacts));
>          }
>        });
>
>        IoConnector client = startClient(9090, new IoHandlerAdapter() {
>            @Override
>            public void messageRecieved(IoSession session, Object message) {
>                // tests to ensure list contents are the same as sent.
>            }
>        });
>
>
>    }
>
>    public IoAcceptor startServer(int port, IoHandlerAdapter serverLogic) {
>        IoAcceptor ia = new NioSocketAcceptor();
>
>
>        ia.getFilterChain().addLast(... add protocol codec filter for
> our protocol ...);
>        ia.setHandler(serverLogic);
>
>        // bind and return ia
>    }
>
>    public IoConnector startClient(int port, IoHandlerAdapter clientLogic) {
>        // start client, set handler, etc.
>    }
> }
>
> In the case above the code in messageReceived() cannot trigger
> assertion failures, the exception they throw is trapped by the
> framework and JUnit thinks the test passed. I'm using Assert.fail()
> because the test I'm doing isn't part of JUnit's base tests (though I
> understand I should start checking out the hamcrest matchers for my
> tests:
> http://stackoverflow.com/questions/958460/creating-a-assertclass-method-in-junit
> ) however using "normal" assertion fails suffer the same issue, for
> example assertEquals(1, 2) results in the test passing beacause junit
> doesn't ever see the exception.
>
> Clearly you'll see the power of this type of testing, the 20 or tests
> I have to write are all structured the same. Connect client to server,
> either client or server send data, ensure data is rendered same on the
> receiving side.
>
> I'm midway through building a solution to this, however some of it
> really reinventing the wheel and all because
> IoHandlerAdapter.messageReceived / sessionOpened / sessionCreated all
> throw Exception and/or the framework doesn't allow the ability to
> distinguish different Exceptions from this layer.
>
> mike
>
> --
> god loves atheists, Fact:
> http://www.mrwiggleslovesyou.com/comics/rehab477.jpg
>

Reply via email to