alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (SocketTest.groovy)

This example Groovy source code file (SocketTest.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

bytearrayoutputstream, groovytestcase, inputstream, inputstream, mockinputstream, mockinputstream, mocksocket, mocksocket, outputstream, outputstream, socket, sockettest

The Groovy SocketTest.groovy source code

package groovy
/**
 * check that groovy Socket methods do their job.
 *
 * @author <a href="mailto:jeremy.rayner@bigfoot.com">Jeremy Rayner
 * @version $Revision: 4996 $
 */

class SocketTest extends GroovyTestCase {
    def mySocket
    
    void setUp() {
        mySocket = new MockSocket()
    }
    
    void testSocketAppendBytes() {
        def myBytes = "mooky".getBytes()
                  
        mySocket << myBytes
                  
        def result = mySocket.outputStream.toByteArray()
        assert result != null
        assert Arrays.equals(myBytes,result)
    }
    void testSocketAppendTwoByteArrays() {
        def myBytes1 = "foo".getBytes()
        def myBytes2 = "bar".getBytes()
                  
        mySocket << myBytes1 << myBytes2
                  
        def result = mySocket.outputStream.toByteArray()
        assert result != null
        assert result.size() == myBytes1.size() + myBytes2.size()          
    }
    
    void testSocketAppend() {
        mySocket << "mooky"
        assert "mooky" == mySocket.outputStream.toString()
    }
    
    void testSocketWithStreamsClosure() {
        mySocket.withStreams {i,o->
            assert i instanceof InputStream
            assert i != null
                      
            assert o instanceof OutputStream          
            assert o != null          
        }
    }
    
    void tearDown() {
        mySocket.close()
    }
}

/**
 * simple, unconnected Socket, used purely for test cases
 */
class MockSocket extends Socket {
    private def i
    private def o

    public MockSocket() {
        i = new MockInputStream()
        o = new ByteArrayOutputStream()
    }

    public InputStream getInputStream() { return i }
    public OutputStream getOutputStream() { return o }
}

/**
 * only needed for workaround in groovy, 
 *     new ByteArrayInputStream(myByteArray) doesn't work at mo... (28-Sep-2004)
 */
class MockInputStream extends InputStream {
    int read() { return -1 }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy SocketTest.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.