001/*
002 * (c) 2005, 2009, 2010 ThoughtWorks Ltd
003 * All rights reserved.
004 *
005 * The software in this package is published under the terms of the BSD
006 * style license a copy of which has been included with this distribution in
007 * the LICENSE.txt file.
008 * 
009 * Created on 30-Jul-2005
010 */
011package proxytoys.examples.overview;
012
013import java.io.File;
014import java.util.List;
015
016import com.thoughtworks.proxy.ProxyFactory;
017import com.thoughtworks.proxy.factory.CglibProxyFactory;
018import com.thoughtworks.proxy.toys.nullobject.Null;
019
020
021/**
022 * @author Jörg Schaible
023 */
024public class NullToyExample {
025
026    public static void packageOverviewExample1() {
027        ProxyFactory factory = new CglibProxyFactory();
028        File file = Null.proxy(File.class).build(factory);
029        System.out.println("Length is: " + file.length());
030        System.out.println("Exists: " + file.exists());
031        System.out.println("Array is empty: " + file.list().length);
032        System.out.println("toURI returns null, since URI is final: " + (file.toURI() == null));
033        System.out.println("Parent file is Null proxy: " + Null.isNullObject(file.getParentFile(), factory));
034    }
035
036    public static void listExample() {
037        ProxyFactory factory = new CglibProxyFactory();
038        @SuppressWarnings("unchecked")
039        List<String> list = Null.proxy(List.class).build(factory);
040        System.out.println("\n\nLength is: " + list.size());
041        System.out.println("contains: " + list.contains("FOO"));
042        List<?> one = Null.proxy(List.class).build(factory);
043        List<?> other = Null.proxy(List.class).build(factory);
044        System.out.println("two are the same? " + (one == other));
045        System.out.println("two are equal? " + (one.equals(other)));
046        try {
047            System.out.println("and you can't add to lists: " + list.add("Bar"));
048        } catch (UnsupportedOperationException expected) {
049            System.out.println("got this expected exception: " + expected);
050        }
051    }
052
053    public static void main(String[] args) {
054        System.out.println();
055        System.out.println();
056        System.out.println("Running Null Toy Examples");
057        System.out.println();
058        System.out.println("Example 1 of Package Overview:");
059        packageOverviewExample1();
060        listExample();
061    }
062}