chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: chris@1: package test.unit.daemon; chris@1: chris@1: import java.io.IOException; chris@1: import java.lang.reflect.InvocationTargetException; chris@1: import java.lang.reflect.Method; chris@1: import java.nio.channels.SocketChannel; chris@1: import junit.framework.TestCase; chris@1: import org.sonews.daemon.NNTPConnection; chris@1: import org.sonews.daemon.command.ArticleCommand; chris@1: import org.sonews.daemon.command.CapabilitiesCommand; chris@1: import org.sonews.daemon.command.GroupCommand; chris@1: import org.sonews.daemon.command.UnsupportedCommand; chris@1: chris@1: /** chris@1: * Unit test for class NNTPConnection. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ chris@1: public class NNTPConnectionTest extends TestCase chris@1: { chris@1: chris@1: public void testLineReceived() chris@1: throws NoSuchMethodException, IllegalAccessException, InvocationTargetException chris@1: { chris@1: NNTPConnection conn = null; chris@1: chris@1: try chris@1: { chris@1: try chris@1: { chris@1: conn = new NNTPConnection(null); chris@1: fail("Should have raised an IllegalArgumentException"); chris@1: } chris@1: catch(IOException ex) {ex.printStackTrace();} chris@1: } chris@1: catch(IllegalArgumentException ex){} chris@1: chris@1: try chris@1: { chris@1: conn = new NNTPConnection(SocketChannel.open()); chris@1: } chris@1: catch(IOException ex) chris@1: { chris@1: ex.printStackTrace(); chris@1: } chris@1: chris@1: assertNotNull(conn); chris@1: chris@1: // Make interesting methods accessible chris@1: Class clazz = conn.getClass(); chris@1: Method methTryReadLock = clazz.getDeclaredMethod("tryReadLock", null); chris@1: methTryReadLock.setAccessible(true); chris@1: Method methLineReceived = clazz.getDeclaredMethod("lineReceived", new byte[0].getClass()); chris@1: methLineReceived.setAccessible(true); chris@1: chris@1: try chris@1: { chris@1: // conn.lineReceived(null); chris@1: methLineReceived.invoke(conn, null); chris@1: fail("Should have raised an IllegalArgumentException"); chris@1: } chris@1: catch(IllegalArgumentException ex){} chris@1: chris@1: try chris@1: { chris@1: // conn.lineReceived(new byte[0]); chris@1: methLineReceived.invoke(conn, new byte[0]); chris@1: fail("Should have raised IllegalStateException"); chris@1: } chris@1: catch(InvocationTargetException ex){} chris@1: chris@1: boolean tryReadLock = (Boolean)methTryReadLock.invoke(conn, null); chris@1: assertTrue(tryReadLock); chris@1: chris@1: // conn.lineReceived("MODE READER".getBytes()); chris@1: methLineReceived.invoke(conn, "MODE READER".getBytes()); chris@1: chris@1: // conn.lineReceived("sdkfsdjnfksjfdng ksdf gksjdfngk nskfng ksndfg ".getBytes()); chris@1: methLineReceived.invoke(conn, "sdkfsdjnfksjfdng ksdf ksndfg ".getBytes()); chris@1: chris@1: // conn.lineReceived(new byte[1024]); // Too long chris@1: methLineReceived.invoke(conn, new byte[1024]); chris@1: chris@1: Method mpcmdl = conn.getClass().getDeclaredMethod("parseCommandLine", String.class); chris@1: mpcmdl.setAccessible(true); chris@1: chris@1: Object result = mpcmdl.invoke(conn, ""); chris@1: assertNotNull(result); chris@1: assertTrue(result instanceof UnsupportedCommand); chris@1: chris@1: result = mpcmdl.invoke(conn, "aRtiCle"); chris@1: assertNotNull(result); chris@1: assertTrue(result instanceof ArticleCommand); chris@1: chris@1: result = mpcmdl.invoke(conn, "capAbilItIEs"); chris@1: assertNotNull(result); chris@1: assertTrue(result instanceof CapabilitiesCommand); chris@1: chris@1: result = mpcmdl.invoke(conn, "grOUp"); chris@1: assertNotNull(result); chris@1: assertTrue(result instanceof GroupCommand); chris@1: } chris@1: chris@1: }