test/unit/daemon/NNTPConnectionTest.java
author František Kučera <franta-hg@frantovo.cz>
Thu Oct 13 03:09:22 2011 +0200 (2011-10-13)
changeset 75 41d6c0cac8b3
permissions -rwxr-xr-x
Drupal: číštění HTML – Tidy.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package test.unit.daemon;
    20 
    21 import java.io.IOException;
    22 import java.lang.reflect.InvocationTargetException;
    23 import java.lang.reflect.Method;
    24 import java.nio.channels.SocketChannel;
    25 import junit.framework.TestCase;
    26 import org.sonews.daemon.NNTPConnection;
    27 import org.sonews.daemon.command.ArticleCommand;
    28 import org.sonews.daemon.command.CapabilitiesCommand;
    29 import org.sonews.daemon.command.GroupCommand;
    30 import org.sonews.daemon.command.UnsupportedCommand;
    31 
    32 /**
    33  * Unit test for class NNTPConnection.
    34  * @author Christian Lins
    35  * @since sonews/0.5.0
    36  */
    37 public class NNTPConnectionTest extends TestCase
    38 {
    39 
    40   public void testLineReceived()
    41     throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
    42   {
    43     NNTPConnection conn = null;
    44     
    45     try
    46     {
    47       try
    48       {
    49         conn = new NNTPConnection(null);
    50         fail("Should have raised an IllegalArgumentException");
    51       }
    52       catch(IOException ex) {ex.printStackTrace();}
    53     }
    54     catch(IllegalArgumentException ex){}
    55     
    56     try
    57     {
    58       conn = new NNTPConnection(SocketChannel.open());
    59     }
    60     catch(IOException ex)
    61     {
    62       ex.printStackTrace();
    63     }
    64     
    65     assertNotNull(conn);
    66     
    67     // Make interesting methods accessible
    68     Class  clazz           = conn.getClass();
    69     Method methTryReadLock = clazz.getDeclaredMethod("tryReadLock", null);
    70     methTryReadLock.setAccessible(true);
    71     Method methLineReceived = clazz.getDeclaredMethod("lineReceived", new byte[0].getClass());
    72     methLineReceived.setAccessible(true);
    73     
    74     try
    75     {
    76       // conn.lineReceived(null);
    77       methLineReceived.invoke(conn, null);
    78       fail("Should have raised an IllegalArgumentException");
    79     }
    80     catch(IllegalArgumentException ex){}
    81     
    82     try
    83     {
    84       // conn.lineReceived(new byte[0]);
    85       methLineReceived.invoke(conn, new byte[0]);
    86       fail("Should have raised IllegalStateException");
    87     }
    88     catch(InvocationTargetException ex){}
    89     
    90     boolean tryReadLock = (Boolean)methTryReadLock.invoke(conn, null);
    91     assertTrue(tryReadLock);
    92     
    93     // conn.lineReceived("MODE READER".getBytes());
    94     methLineReceived.invoke(conn, "MODE READER".getBytes());
    95     
    96     // conn.lineReceived("sdkfsdjnfksjfdng ksdf gksjdfngk nskfng ksndfg ".getBytes());
    97     methLineReceived.invoke(conn, "sdkfsdjnfksjfdng ksdf ksndfg ".getBytes());
    98     
    99     // conn.lineReceived(new byte[1024]); // Too long
   100     methLineReceived.invoke(conn, new byte[1024]);
   101     
   102     Method mpcmdl = conn.getClass().getDeclaredMethod("parseCommandLine", String.class);
   103     mpcmdl.setAccessible(true);
   104 
   105     Object result = mpcmdl.invoke(conn, "");
   106     assertNotNull(result);
   107     assertTrue(result instanceof UnsupportedCommand);
   108     
   109     result = mpcmdl.invoke(conn, "aRtiCle");
   110     assertNotNull(result);
   111     assertTrue(result instanceof ArticleCommand);
   112     
   113     result = mpcmdl.invoke(conn, "capAbilItIEs");
   114     assertNotNull(result);
   115     assertTrue(result instanceof CapabilitiesCommand);
   116     
   117     result = mpcmdl.invoke(conn, "grOUp");
   118     assertNotNull(result);
   119     assertTrue(result instanceof GroupCommand);
   120   }
   121   
   122 }