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