Home | History | Annotate | Download | only in command
      1 /*
      2  * Copyright 2007 the original author or authors.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package org.mockftpserver.core.command;
     17 
     18 import org.slf4j.Logger;
     19 import org.slf4j.LoggerFactory;
     20 import org.easymock.MockControl;
     21 import org.mockftpserver.core.session.Session;
     22 import org.mockftpserver.core.util.AssertFailedException;
     23 import org.mockftpserver.stub.command.AbstractStubCommandHandler;
     24 import org.mockftpserver.test.AbstractTestCase;
     25 
     26 import java.util.ListResourceBundle;
     27 import java.util.ResourceBundle;
     28 
     29 /**
     30  * Tests for the AbstractCommandHandler class. The class name is prefixed with an
     31  * underscore so that it is not filtered out by Maven's Surefire test plugin.
     32  *
     33  * @author Chris Mair
     34  * @version $Revision$ - $Date$
     35  */
     36 public final class _AbstractCommandHandlerTest extends AbstractTestCase {
     37 
     38     private static final Logger LOG = LoggerFactory.getLogger(_AbstractTrackingCommandHandlerTest.class);
     39     private static final int REPLY_CODE1 = 777;
     40     private static final int REPLY_CODE2 = 888;
     41     private static final String REPLY_TEXT1 = "reply1 ... abcdef";
     42     private static final String REPLY_TEXT2 = "abc {0} def";
     43     private static final String MESSAGE_KEY = "key.123";
     44     private static final String MESSAGE_TEXT = "message.123";
     45 
     46     private AbstractCommandHandler commandHandler;
     47 
     48     /**
     49      * Test the quotes utility method
     50      */
     51     public void testQuotes() {
     52         assertEquals("abc", "\"abc\"", AbstractStubCommandHandler.quotes("abc"));
     53         assertEquals("<empty>", "\"\"", AbstractStubCommandHandler.quotes(""));
     54     }
     55 
     56     /**
     57      * Test the quotes utility method, passing in a null
     58      */
     59     public void testQuotes_Null() {
     60         try {
     61             AbstractStubCommandHandler.quotes(null);
     62             fail("Expected AssertFailedException");
     63         }
     64         catch (AssertFailedException expected) {
     65             LOG.info("Expected: " + expected);
     66         }
     67     }
     68 
     69     /**
     70      * Test the assertValidReplyCode() method
     71      */
     72     public void testAssertValidReplyCode() {
     73         // These are valid, so expect no exceptions
     74         commandHandler.assertValidReplyCode(1);
     75         commandHandler.assertValidReplyCode(100);
     76 
     77         // These are invalid
     78         testAssertValidReplyCodeWithInvalid(0);
     79         testAssertValidReplyCodeWithInvalid(-1);
     80     }
     81 
     82     /**
     83      * Test the assertValidReplyCode() method , passing in an invalid replyCode value
     84      *
     85      * @param invalidReplyCode - a reply code that is expected to be invalid
     86      */
     87     private void testAssertValidReplyCodeWithInvalid(int invalidReplyCode) {
     88         try {
     89             commandHandler.assertValidReplyCode(invalidReplyCode);
     90             fail("Expected AssertFailedException");
     91         }
     92         catch (AssertFailedException expected) {
     93             LOG.info("Expected: " + expected);
     94         }
     95     }
     96 
     97     //-------------------------------------------------------------------------
     98     // Test setup
     99     //-------------------------------------------------------------------------
    100 
    101     /**
    102      * Perform initialization before each test
    103      *
    104      * @see org.mockftpserver.test.AbstractTestCase#setUp()
    105      */
    106     protected void setUp() throws Exception {
    107         super.setUp();
    108         Session session = (Session) createMock(Session.class);
    109         control(session).setDefaultMatcher(MockControl.ARRAY_MATCHER);
    110         commandHandler = new AbstractCommandHandler() {
    111             public void handleCommand(Command command, Session session) throws Exception {
    112             }
    113         };
    114         ResourceBundle replyTextBundle = new ListResourceBundle() {
    115             protected Object[][] getContents() {
    116                 return new Object[][]{
    117                         {Integer.toString(REPLY_CODE1), REPLY_TEXT1},
    118                         {Integer.toString(REPLY_CODE2), REPLY_TEXT2},
    119                         {MESSAGE_KEY, MESSAGE_TEXT}
    120                 };
    121             }
    122         };
    123         commandHandler.setReplyTextBundle(replyTextBundle);
    124     }
    125 
    126 }
    127