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

Groovy example source code file (ReferenceSerializationTest.groovy)

This example Groovy source code file (ReferenceSerializationTest.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

button, bytearrayoutputstream, clicklistener, clicklistener, custompogoperson, custompogopet, custompogopet, minou, objectinputstream, objectoutputstream, serializable, serializable, string, string

The Groovy ReferenceSerializationTest.groovy source code

package groovy.lang

/**
 * GROOVY-4305: Make groovy.lang.Reference implement Serializable
 *
 * @author Guillaume Laforge
 */
class ReferenceSerializationTest extends GroovyTestCase implements Serializable {

    private static final long serialVersionUID = 10L

    private serializeDeserialize(obj) {
        // serialize
        ByteArrayOutputStream out = new ByteArrayOutputStream()
        ObjectOutputStream oos = new ObjectOutputStream(out)
        oos.writeObject(obj)
        oos.close()

        //deserialize
        byte[] bytes = out.toByteArray()
        InputStream is = new ByteArrayInputStream(bytes)
        ObjectInputStream ois = new ObjectInputStream(is)

        // return back what's read from the stream
        return ois.readObject()
    }

    void testSimplePogoSerializationToObjectOutputStream() {
        int age = 33
        String name = "Guillaume"

        def person = new CustomPogoPerson(name: name, age: age, pet: new CustomPogoPet(nickname: "Minou", kind: "cat"))

        def personDeserialized = serializeDeserialize(person)

        assert personDeserialized.name == name
        assert personDeserialized.age == age
        assert personDeserialized.pet.nickname == "Minou"
        assert personDeserialized.pet.kind == "cat"
    }

    void testClosureSerializationWithAReferenceToALocalVariable() {
        int number = 2
        def doubler = { it * number }

        def closure = serializeDeserialize(doubler)

        assert closure(2) == 4
        assert closure(3) == 6
    }

    void testAICReferencingLocalVariableTest() {
        long count = 0
        def button = new Button()
        button.listener = new ClickAdapter() {
            long onClick() {
                count++
                return count
            }
        }
        assert button.listener.onClick() == 1
        assert button.listener.onClick() == 2

        def buttonCopy = serializeDeserialize(button)
        assert buttonCopy.listener.onClick() == 3
        assert buttonCopy.listener.onClick() == 4
    }
}

class CustomPogoPerson implements Serializable {
    private static final long serialVersionUID = 1L

    String name
    int age
    CustomPogoPet pet
}

class CustomPogoPet implements Serializable {
    private static final long serialVersionUID = 2L

    String nickname
    String kind
}

class Button implements Serializable {
    private static final long serialVersionUID = 3L
    ClickListener listener
}

interface ClickListener {
    long onClick()
}

abstract class ClickAdapter implements ClickListener, Serializable {
    private static final long serialVersionUID = 4L
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy ReferenceSerializationTest.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.