⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractprotocoltest.java

📁 java 开发的邮件服务器平台。支持以下协议。 协议可以修改为自己的专门标识
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         */        private String readLine( BufferedReader in ) throws Exception        {            try {                return in.readLine();            }            catch ( InterruptedIOException e ) {                String errMsg = "\nLocation: " + _location +                                "\nExpected: " + _msg +                                "\nReason: Server Timeout.";                fail( errMsg );                return "";            }        }        private List getMessageTokens( String message )        {            List tokenList = new ArrayList();            StringTokenizer tokens = new StringTokenizer( message, " \t\n\r\f\"\'{}()[];$", true );            while ( tokens.hasMoreTokens() ) {                tokenList.add( tokens.nextToken() );            }            return tokenList;        }        private List buildElementTests( List tokenList )        {            List elementTests = new ArrayList();            for ( int i = 0; i < tokenList.size(); i++ ) {                if ( ( i < ( tokenList.size() - 3 ) )                        && tokenList.get( i ).equals( "$" )                        && tokenList.get( i+1 ).equals( "{" )                        && tokenList.get( i+3 ).equals( "}" ) ) {                    // For now, assume all special tokens are simple consume tokens.                    String special = (String) tokenList.get( i+2 );                    elementTests.add( buildSpecialTest( special ) );                    i += 3;                }                else {                    elementTests.add( new StringElementTest( (String)tokenList.get( i ) ) );                }            }            return elementTests;        }        /**         * An atomic unit of a ProtocolLine         */        private abstract class ElementTest        {            protected String _description;            void setLocation( String location ) {                _description = "\nLocation: " + location;            }            void test( ListIterator testElements, String fullTestLine ) throws Exception            {                _description += "\nActual  : " + fullTestLine +                                "\nExpected: " + _msg +                                "\nReason: ";                doTest( testElements );            }            void test( ListIterator testElements ) throws Exception            {                _description += "Reason: ";                doTest( testElements );            }              boolean softTest( ListIterator testElements, String line) throws Exception {                return doNonAssertingTest(testElements);            }            abstract void doTest( ListIterator testElements ) throws Exception;            /**             * non Asserting version of doTest that instead of throwing an             * assert, just gently retunrs a boolean              * @param testElements the elements to test with             * @return boolean true if success false if failed             */                abstract boolean doNonAssertingTest( ListIterator testElements)              throws Exception;        }                /**         * An element test which always fails with a null          */                 /**         * An element test which does a simple String comparison with the element.         */        private class StringElementTest extends ElementTest        {            private String _elementValue;            public StringElementTest( String elementValue )            {                _elementValue = elementValue;            }                    //comment in ElementTest             public boolean doNonAssertingTest( ListIterator testElements )             throws Exception {                String next;                if ( testElements.hasNext() ) {                    next = (String) testElements.next();                }                else {                    next = "No more elements";                }                if ( !_elementValue.equals(next) ) {                   //System.err.println("emement value="+_elementValue+                  //" did not =next+"+                  //next);                  return false;                }                  //System.err.println("emement value="+_elementValue+                  //" did =next+"+                  //next);                return true;            }            public void doTest( ListIterator testElements ) throws Exception            {                String next;                if ( testElements.hasNext() ) {                    next = (String) testElements.next();                }                else {                    next = "No more elements";                }                assertEquals( _description, _elementValue, next );            }        }        private ElementTest buildSpecialTest( String testName )        {            if ( testName.startsWith("ignore") ) {                return new ConsumeElementTest( testName );            }            if ( testName.startsWith("rfcDate") ) {                return new RfcDateElementTest( testName );            }            else {                return new StringElementTest( "${" + testName + "}" );            }        }        /**         * A simple element test which simply consumes a specified number of test elements,         * ignoring the actual element values.         */        private class ConsumeElementTest extends ElementTest        {            private int _elementsToConsume;            ConsumeElementTest( String token )            {                if ( token.equals("ignore") ) {                    _elementsToConsume = 1;                }                else if ( token.startsWith( "ignore-") ) {                    _elementsToConsume = Integer.parseInt( token.substring( "ignore-".length() ) );                }                else {                    _elementsToConsume = Integer.parseInt( token );                }            }            ConsumeElementTest(int number)            {                _elementsToConsume = number;            }            public void doTest( ListIterator testElements ) throws Exception            {                for ( int i = 0; i < _elementsToConsume; i++ )                {                    if ( ! testElements.hasNext() ) {                        fail( _description + "Not enough elements to ignore." );                    }                    String ignored = (String)testElements.next();                }            }            public boolean doNonAssertingTest( ListIterator testElements ) throws Exception            {                for ( int i = 0; i < _elementsToConsume; i++ )                {                    if ( ! testElements.hasNext() ) {                        return false;                    }                    String ignored = (String)testElements.next();                }                return true;            }        }        /**         * Accepts an RFC date (or anything with 12 tokens - todo make this better)         */        private class RfcDateElementTest extends ConsumeElementTest        {            public RfcDateElementTest( String token )            {                super( 11 );            }        }        /**         * A Test that ensures that no more tokens are present.         */        private class EndOfLineTest extends ElementTest        {            public void doTest( ListIterator testElements ) throws Exception            {                if ( testElements.hasNext() ) {                    String nextElement = (String)testElements.next();                    fail( _description + "End of line expected, found '" + nextElement + "'" );                }            }                    public boolean doNonAssertingTest( ListIterator testElements )             throws Exception            {                if ( testElements.hasNext() ) {                    String nextElement = (String)testElements.next();                    return false;                }                return true;            }        }    }    protected interface ProtocolLine    {        void testProtocol( PrintWriter out, BufferedReader in ) throws Exception;        void testProtocolBlock(PrintWriter out, BufferedReader in, List list)        throws Exception;    }    protected void addTestFile( String fileName ) throws Exception    {        addTestFile( fileName, _testElements );    }    protected void addTestFile( String fileName, List protocolLines ) throws Exception    {        // Need to find local resource.        InputStream is = this.getClass().getResourceAsStream( fileName );        if ( is == null ) {            throw new Exception("Test Resource '" + fileName + "' not found." );        }        addProtocolLinesFromStream( is, protocolLines, fileName );    }    private void addProtocolLinesFromStream( InputStream is, List protocolLines, String fileName ) throws Exception    {        BufferedReader reader = new BufferedReader( new InputStreamReader( is ) );        String next;        int lineNumber = 1;        while ( ( next = reader.readLine() ) != null ) {            String location = fileName + ":" + lineNumber;            if ( next.startsWith( "C: " ) ) {                String clientMsg = next.substring( 3 );                protocolLines.add( new ClientRequest( clientMsg ) );            }            else if ( next.startsWith( "S: " ) ) {                String serverMsg = next.substring( 3 );                if ( serverMsg.endsWith("//") ) {                    serverMsg = serverMsg.substring( 0, serverMsg.length() - 2 );                    protocolLines.add( new ServerResponse( serverMsg, location, true ) );                }                else {                    protocolLines.add( new ServerResponse( serverMsg, location, false ) );                }            } else if ( next.startsWith("SUB: ") ) {              //System.err.println("Hit SUB ");              List unorderedBlock = new ArrayList(5);              next = reader.readLine();              //System.err.println("next = " + next);              String serverMsg = next.substring( 3 );              while ( !next.startsWith("SUB:") ) {                   unorderedBlock.add(                               new ServerResponse( serverMsg, location, false )                                     );                   next = reader.readLine();                   serverMsg = next.substring( 3 );                   lineNumber++;                   //System.err.println("next = " + next);              }              protocolLines.add(unorderedBlock);              } else if ( next.startsWith( "//" )                      || next.trim().length() == 0 ) {                // ignore these lines.            }            else {                String prefix = next;                if ( next.length() > 3 ) {                    prefix = next.substring( 0, 3 );                }                throw new Exception( "Invalid line prefix: " + prefix );            }            lineNumber++;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -