test/unit/daemon/NNTPConnectionTest.java
changeset 75 41d6c0cac8b3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/unit/daemon/NNTPConnectionTest.java	Thu Oct 13 03:09:22 2011 +0200
     1.3 @@ -0,0 +1,122 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package test.unit.daemon;
    1.23 +
    1.24 +import java.io.IOException;
    1.25 +import java.lang.reflect.InvocationTargetException;
    1.26 +import java.lang.reflect.Method;
    1.27 +import java.nio.channels.SocketChannel;
    1.28 +import junit.framework.TestCase;
    1.29 +import org.sonews.daemon.NNTPConnection;
    1.30 +import org.sonews.daemon.command.ArticleCommand;
    1.31 +import org.sonews.daemon.command.CapabilitiesCommand;
    1.32 +import org.sonews.daemon.command.GroupCommand;
    1.33 +import org.sonews.daemon.command.UnsupportedCommand;
    1.34 +
    1.35 +/**
    1.36 + * Unit test for class NNTPConnection.
    1.37 + * @author Christian Lins
    1.38 + * @since sonews/0.5.0
    1.39 + */
    1.40 +public class NNTPConnectionTest extends TestCase
    1.41 +{
    1.42 +
    1.43 +  public void testLineReceived()
    1.44 +    throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
    1.45 +  {
    1.46 +    NNTPConnection conn = null;
    1.47 +    
    1.48 +    try
    1.49 +    {
    1.50 +      try
    1.51 +      {
    1.52 +        conn = new NNTPConnection(null);
    1.53 +        fail("Should have raised an IllegalArgumentException");
    1.54 +      }
    1.55 +      catch(IOException ex) {ex.printStackTrace();}
    1.56 +    }
    1.57 +    catch(IllegalArgumentException ex){}
    1.58 +    
    1.59 +    try
    1.60 +    {
    1.61 +      conn = new NNTPConnection(SocketChannel.open());
    1.62 +    }
    1.63 +    catch(IOException ex)
    1.64 +    {
    1.65 +      ex.printStackTrace();
    1.66 +    }
    1.67 +    
    1.68 +    assertNotNull(conn);
    1.69 +    
    1.70 +    // Make interesting methods accessible
    1.71 +    Class  clazz           = conn.getClass();
    1.72 +    Method methTryReadLock = clazz.getDeclaredMethod("tryReadLock", null);
    1.73 +    methTryReadLock.setAccessible(true);
    1.74 +    Method methLineReceived = clazz.getDeclaredMethod("lineReceived", new byte[0].getClass());
    1.75 +    methLineReceived.setAccessible(true);
    1.76 +    
    1.77 +    try
    1.78 +    {
    1.79 +      // conn.lineReceived(null);
    1.80 +      methLineReceived.invoke(conn, null);
    1.81 +      fail("Should have raised an IllegalArgumentException");
    1.82 +    }
    1.83 +    catch(IllegalArgumentException ex){}
    1.84 +    
    1.85 +    try
    1.86 +    {
    1.87 +      // conn.lineReceived(new byte[0]);
    1.88 +      methLineReceived.invoke(conn, new byte[0]);
    1.89 +      fail("Should have raised IllegalStateException");
    1.90 +    }
    1.91 +    catch(InvocationTargetException ex){}
    1.92 +    
    1.93 +    boolean tryReadLock = (Boolean)methTryReadLock.invoke(conn, null);
    1.94 +    assertTrue(tryReadLock);
    1.95 +    
    1.96 +    // conn.lineReceived("MODE READER".getBytes());
    1.97 +    methLineReceived.invoke(conn, "MODE READER".getBytes());
    1.98 +    
    1.99 +    // conn.lineReceived("sdkfsdjnfksjfdng ksdf gksjdfngk nskfng ksndfg ".getBytes());
   1.100 +    methLineReceived.invoke(conn, "sdkfsdjnfksjfdng ksdf ksndfg ".getBytes());
   1.101 +    
   1.102 +    // conn.lineReceived(new byte[1024]); // Too long
   1.103 +    methLineReceived.invoke(conn, new byte[1024]);
   1.104 +    
   1.105 +    Method mpcmdl = conn.getClass().getDeclaredMethod("parseCommandLine", String.class);
   1.106 +    mpcmdl.setAccessible(true);
   1.107 +
   1.108 +    Object result = mpcmdl.invoke(conn, "");
   1.109 +    assertNotNull(result);
   1.110 +    assertTrue(result instanceof UnsupportedCommand);
   1.111 +    
   1.112 +    result = mpcmdl.invoke(conn, "aRtiCle");
   1.113 +    assertNotNull(result);
   1.114 +    assertTrue(result instanceof ArticleCommand);
   1.115 +    
   1.116 +    result = mpcmdl.invoke(conn, "capAbilItIEs");
   1.117 +    assertNotNull(result);
   1.118 +    assertTrue(result instanceof CapabilitiesCommand);
   1.119 +    
   1.120 +    result = mpcmdl.invoke(conn, "grOUp");
   1.121 +    assertNotNull(result);
   1.122 +    assertTrue(result instanceof GroupCommand);
   1.123 +  }
   1.124 +  
   1.125 +}