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

What this is

This file 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.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.options;

import org.netbeans.junit.*;
import junit.textui.TestRunner;
import org.openide.util.SharedClassObject;
import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;

/** Test system options (their serialization and deserialization specifically).
 * @author Jesse Glick
 */
public class SystemOptionTest extends NbTestCase {
    
    public SystemOptionTest(String name) {
        super(name);
    }
    
    public static void main(String[] args) {
        TestRunner.run(new NbTestSuite(SystemOptionTest.class));
    }
    
    /** Test manipulation of a SystemOption in memory.
     */
    public void testBasicUsage() throws Exception {
        assertNull(SharedClassObject.findObject(SimpleOption.class, false));
        SimpleOption o = (SimpleOption)SharedClassObject.findObject(SimpleOption.class, true);
        assertEquals(3, o.getX());
        assertEquals("hello", o.getY());
        o.setX(5);
        o.setY("nue");
        assertEquals(5, o.getX());
        assertEquals("nue", o.getY());
    }
    
    /** Test deserializability of a simple option.
     * Uses both SCO properties and static vars for storage; SystemOption
     * should treat them the same because the vars match the property types.
     */
    public void testDeserialization() throws Exception {
        InputStream is = getClass().getResourceAsStream("simpleOption2.ser");
        assertNotNull("simpleOption2.ser exists", is);
        try {
            ObjectInputStream ois = new ObjectInputStream(is);
            SimpleOption2 o = (SimpleOption2)ois.readObject();
            assertEquals(4, o.getX());
            assertEquals(4, o.getX2());
            assertEquals("four", o.getY());
            assertEquals("four", o.getY2());
        } finally {
            is.close();
        }
    }
    
    /** Test deserializability of an option which stores props in a special way.
     * Note that it keeps properties but they are not assignable to the property type.
     * I.e. the property type is for client use only, not for storage.
     * The Cell's are actually stored; we keep track of the "natural state"; the .ser
     * was stored with saveNatural turned on, so we check that it is really deserializing
     * the Cell's and not just calling the getters with the public values.
     */
    public void testMixedTypeDeserialization() throws Exception {
        InputStream is = getClass().getResourceAsStream("mixedTypeOption.ser");
        assertNotNull("mixedTypeOption.ser exists", is);
        try {
            ObjectInputStream ois = new ObjectInputStream(is);
            MixedTypeOption o = (MixedTypeOption)ois.readObject();
            assertEquals("25", o.getX());
            assertEquals(25, o.getY());
            // Makes a network connection: assertEquals(new URL("http://openide.netbeans.org/"), o.getZ());
            assertEquals("openide.netbeans.org", o.getZ().getHost());
            assertTrue(o.isNatural("x"));
            assertTrue(o.isNatural("y"));
            assertTrue(o.isNatural("z"));
            o.setY(26);
            assertEquals(26, o.getY());
            assertFalse(o.isNatural("y"));
        } finally {
            is.close();
        }
    }
    
    // XXX test that serialization works and matches deserialization
    // (hint: use MaskingURLClassLoader from SharedClassObjectTest)
    
    // XXX test that SharedClassObject.find(optionClass, true) asks Lookup for the singleton
    
    // XXX test that the BeanInfo property descriptors are really used to determine
    // property names and writability (and that these can override getter/setter name munging)
    
    // XXX test that isReadExternal/isWriteExternal work
    
    // XXX test that read-only properties are not stored
    
    // XXX test that deser failure of one property does not break others
    
    // XXX ContextSystemOptionTest: test that child options are stored correctly, bean context
    // works, serialization stores all of them together
    
    // XXX VetoSystemOptionTest: test that you can veto some property changes
    
    public static final class SimpleOption extends SystemOption {
        public String displayName() {
            return "SimpleOption";
        }
        protected void initialize() {
            super.initialize();
            setX(3);
            setY("hello");
        }
        public int getX() {
            return ((Integer)getProperty("x")).intValue();
        }
        public void setX(int x) {
            putProperty("x", new Integer(x), true);
        }
        public String getY() {
            return (String)getProperty("y");
        }
        public void setY(String y) {
            putProperty("y", y, true);
        }
    }
    
    public static final class SimpleOption2 extends SystemOption {
        private static final long serialVersionUID = 2456964509644026223L;
        public String displayName() {
            return "SimpleOption2";
        }
        private static int x2 = 3;
        private static String y2 = "hello";
        protected void initialize() {
            super.initialize();
            setX(3);
            setY("hello");
        }
        public int getX() {
            return ((Integer)getProperty("x")).intValue();
        }
        public void setX(int x) {
            putProperty("x", new Integer(x), true);
        }
        public String getY() {
            return (String)getProperty("y");
        }
        public void setY(String y) {
            putProperty("y", y, true);
        }
        public int getX2() {
            return x2;
        }
        public void setX2(int nue) {
            int old = x2;
            x2 = nue;
            firePropertyChange("x2", new Integer(old), new Integer(nue));
        }
        public String getY2() {
            return y2;
        }
        public void setY2(String nue) {
            String old = y2;
            y2 = nue;
            firePropertyChange("y2", old, nue);
        }
    }
    
    public static class MixedTypeOption extends SystemOption {
        private static final class Cell implements Serializable {
            private static final long serialVersionUID = -2882494319143608556L;
            public final Object o;
            public final boolean natural;
            public Cell(Object o, boolean natural) {
                this.o = o;
                this.natural = natural;
            }
        }
        private static final long serialVersionUID = 262688904041933263L;
        public static boolean saveNatural = false;
        public boolean isNatural(String prop) {
            return ((Cell)getProperty(prop)).natural;
        }
        public String displayName() {
            return "MixedTypeOption";
        }
        protected void initialize() {
            super.initialize();
            putProperty("x", new Cell(new Integer(12), true));
            putProperty("y", new Cell("12", true));
            putProperty("z", new Cell("http://www.netbeans.org/", true));
        }
        public String getX() {
            return ((Integer)((Cell)getProperty("x")).o).toString();
        }
        public void setX(String x) {
            putProperty("x", new Cell(new Integer(x), saveNatural));
        }
        public int getY() {
            return Integer.parseInt((String)((Cell)getProperty("y")).o);
        }
        public void setY(int i) {
            putProperty("y", new Cell(String.valueOf(i), saveNatural));
        }
        public URL getZ() {
            try {
                return new URL((String)((Cell)getProperty("z")).o);
            } catch (MalformedURLException mfue) {
                throw new IllegalStateException(mfue.toString());
            }
        }
        public void setZ(URL z) {
            putProperty("z", new Cell(z.toString(), saveNatural));
        }
    }
    
}
... 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.