Creating project to demonstrate problems with lookup inconsistencies
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:51:12 +0200
changeset 26913d1d0a7bdf
parent 25 efe17ee2507d
child 27 699c5a4f0fab
Creating project to demonstrate problems with lookup inconsistencies
samples/consistency/build.xml
samples/consistency/nbproject/project.xml
samples/consistency/src-api1.0/api/Lookup.java
samples/consistency/src-api1.0/api/Lookups.java
samples/consistency/src-api2.0/api/Lookup.java
samples/consistency/src-api2.0/api/Lookups.java
samples/consistency/src-test/api/ArithmeticaTest.java
samples/consistency/src-test/api/FactorialTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/consistency/build.xml	Sat Jun 14 09:51:12 2008 +0200
     1.3 @@ -0,0 +1,71 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<project name="Build Script" default="test" basedir=".">
     1.6 +    <target name="clean">
     1.7 +        <delete dir="build"/>
     1.8 +    </target>
     1.9 +    
    1.10 +    <target name="build" depends="-libraries">
    1.11 +        <antcall target="-build-one">
    1.12 +            <param name="version" value="api1.0"/>
    1.13 +        </antcall>
    1.14 +        <antcall target="-build-one">
    1.15 +            <param name="version" value="api2.0"/>
    1.16 +        </antcall>
    1.17 +        
    1.18 +        <antcall target="-build-one">
    1.19 +            <param name="version" value="test"/>
    1.20 +            <param name="cp" value="build/api1.0/classes:${junit.jar}"/>
    1.21 +        </antcall>
    1.22 +    </target>
    1.23 +    
    1.24 +    <target name="test" depends="build">
    1.25 +        <echo level="info" message="Running the Implementation against Version 1.0 of the API. This should succeeds."/>
    1.26 +        <antcall target="-run-one">
    1.27 +            <param name="version" value="api1.0"/>
    1.28 +        </antcall>
    1.29 +        <echo level="info" message="Running the Implementation against Version 2.0 of the API. This should fail."/>
    1.30 +        <antcall target="-run-one">
    1.31 +            <param name="version" value="api2.0"/>
    1.32 +        </antcall>
    1.33 +    </target>
    1.34 +    
    1.35 +    <!-- support methods -->
    1.36 +    
    1.37 +    <target name="-libraries">
    1.38 +        <ant dir="../libs/"/>
    1.39 +        
    1.40 +        <property name="junit.jar" location="../libs/dist/junit-4.4.jar"/>
    1.41 +    </target>
    1.42 +    
    1.43 +    <target name="-run-one">
    1.44 +        <fail message="You need to specify API version number" unless="version"/>
    1.45 +        <mkdir dir="build/testresults"/>
    1.46 +        <junit dir="build/test/classes" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" showoutput="true">
    1.47 +            <batchtest todir="build/testresults">
    1.48 +                <fileset dir="build/test/classes">
    1.49 +                    <filename name="**/*Test.class"/>
    1.50 +                </fileset>
    1.51 +            </batchtest>
    1.52 +            <classpath>
    1.53 +                <path location="build/${version}/classes"/>
    1.54 +                <path location="build/test/classes"/>
    1.55 +                <path location="${junit.jar}"/>
    1.56 +            </classpath>
    1.57 +            <formatter type="brief" usefile="false"/>
    1.58 +            <formatter type="xml"/>
    1.59 +        </junit>
    1.60 +    </target>
    1.61 +    
    1.62 +    <target name="-build-one">
    1.63 +        <fail message="You need to specify version number" unless="version"/>
    1.64 +        
    1.65 +        <mkdir dir="build/${version}/classes"/>
    1.66 +        <property name="cp" value=""/>
    1.67 +        <javac 
    1.68 +            srcdir="src-${version}" 
    1.69 +            destdir="build/${version}/classes" 
    1.70 +            source="1.5" target="1.5"
    1.71 +            classpath="${cp}"
    1.72 +        />
    1.73 +    </target>
    1.74 +</project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/consistency/nbproject/project.xml	Sat Jun 14 09:51:12 2008 +0200
     2.3 @@ -0,0 +1,89 @@
     2.4 +<?xml version="1.0" encoding="UTF-8"?>
     2.5 +<project xmlns="http://www.netbeans.org/ns/project/1" xmlns:ns4="null">
     2.6 +    <type>org.netbeans.modules.ant.freeform</type>
     2.7 +    <configuration>
     2.8 +        <general-data xmlns="http://www.netbeans.org/ns/freeform-project/1">
     2.9 +            <name>consistency</name>
    2.10 +        </general-data>
    2.11 +        <general-data xmlns="http://www.netbeans.org/ns/freeform-project/2">
    2.12 +            <!-- Do not use Project Properties customizer when editing this file manually. -->
    2.13 +            <name>consistency</name>
    2.14 +            <properties/>
    2.15 +            <folders>
    2.16 +                <source-folder>
    2.17 +                    <label>src-api1.0</label>
    2.18 +                    <type>java</type>
    2.19 +                    <location>src-api1.0</location>
    2.20 +                    <encoding>UTF-8</encoding>
    2.21 +                </source-folder>
    2.22 +                <source-folder>
    2.23 +                    <label>src-api2.0</label>
    2.24 +                    <type>java</type>
    2.25 +                    <location>src-api2.0</location>
    2.26 +                    <encoding>UTF-8</encoding>
    2.27 +                </source-folder>
    2.28 +                <source-folder>
    2.29 +                    <label>test</label>
    2.30 +                    <type>java</type>
    2.31 +                    <location>src-test</location>
    2.32 +                    <encoding>UTF-8</encoding>
    2.33 +                </source-folder>
    2.34 +            </folders>
    2.35 +            <ide-actions>
    2.36 +                <action name="build">
    2.37 +                    <target>build</target>
    2.38 +                </action>
    2.39 +                <action name="clean">
    2.40 +                    <target>clean</target>
    2.41 +                </action>
    2.42 +                <action name="test">
    2.43 +                    <target>test</target>
    2.44 +                </action>
    2.45 +                <action name="rebuild">
    2.46 +                    <target>clean</target>
    2.47 +                    <target>build</target>
    2.48 +                </action>
    2.49 +            </ide-actions>
    2.50 +            <view>
    2.51 +                <items>
    2.52 +                    <source-folder style="packages">
    2.53 +                        <label>API Version 1.0</label>
    2.54 +                        <location>src-api1.0</location>
    2.55 +                    </source-folder>
    2.56 +                    <source-folder style="packages">
    2.57 +                        <label>API Version 2.0</label>
    2.58 +                        <location>src-api2.0</location>
    2.59 +                    </source-folder>
    2.60 +                    <source-folder style="packages">
    2.61 +                        <label>Usage of the API</label>
    2.62 +                        <location>src-test</location>
    2.63 +                    </source-folder>
    2.64 +                    <source-file>
    2.65 +                        <location>build.xml</location>
    2.66 +                    </source-file>
    2.67 +                </items>
    2.68 +                <context-menu>
    2.69 +                    <ide-action name="build"/>
    2.70 +                    <ide-action name="rebuild"/>
    2.71 +                    <ide-action name="clean"/>
    2.72 +                    <ide-action name="test"/>
    2.73 +                </context-menu>
    2.74 +            </view>
    2.75 +        </general-data>
    2.76 +        <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/1">
    2.77 +            <compilation-unit>
    2.78 +                <package-root>src-api1.0</package-root>
    2.79 +                <source-level>1.5</source-level>
    2.80 +            </compilation-unit>
    2.81 +            <compilation-unit>
    2.82 +                <package-root>src-api2.0</package-root>
    2.83 +                <source-level>1.5</source-level>
    2.84 +            </compilation-unit>
    2.85 +            <compilation-unit>
    2.86 +                <package-root>src-test</package-root>
    2.87 +                <classpath mode="compile">src-api1.0:../libs/dist/junit-4.4.jar</classpath>
    2.88 +                <source-level>1.5</source-level>
    2.89 +            </compilation-unit>
    2.90 +        </java-data>
    2.91 +    </configuration>
    2.92 +</project>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/consistency/src-api1.0/api/Lookup.java	Sat Jun 14 09:51:12 2008 +0200
     3.3 @@ -0,0 +1,26 @@
     3.4 +package api;
     3.5 +
     3.6 +import java.util.Collection;
     3.7 +import java.util.Set;
     3.8 +
     3.9 +/** Simplified version of NetBeans 
    3.10 + * <a href="http://bits.netbeans.org/6.0/javadoc/org-openide-util/org/openide/util/Lookup.html">Lookup</a> class.
    3.11 + *
    3.12 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.13 + * @version 1.0
    3.14 + */
    3.15 +// BEGIN: design.consistency.1.0
    3.16 +public abstract class Lookup {
    3.17 +    // BEGIN: design.consistency.lookup.1.0
    3.18 +    public abstract <T> T lookup(Class<T> clazz);
    3.19 +    // END: design.consistency.lookup.1.0
    3.20 +
    3.21 +    // BEGIN: design.consistency.lookupAll.1.0
    3.22 +    public abstract <T> Collection<? extends T> lookupAll(Class<T> clazz);
    3.23 +    // END: design.consistency.lookupAll.1.0
    3.24 +
    3.25 +    // BEGIN: design.consistency.lookupAllClasses.1.0
    3.26 +    public abstract <T> Set<Class<? extends T>> lookupAllClasses(Class<T> clazz);
    3.27 +    // END: design.consistency.lookupAllClasses.1.0
    3.28 +}
    3.29 +// END: design.consistency.1.0
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/consistency/src-api1.0/api/Lookups.java	Sat Jun 14 09:51:12 2008 +0200
     4.3 @@ -0,0 +1,18 @@
     4.4 +package api;
     4.5 +
     4.6 +import java.util.Collection;
     4.7 +import java.util.Set;
     4.8 +
     4.9 +/** Factory to create various types of lookup instances.
    4.10 + *
    4.11 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.12 + * @version 1.0
    4.13 + */
    4.14 +public final class Lookups {
    4.15 +    private Lookups() {
    4.16 +    }
    4.17 +    
    4.18 +    public static Lookup fixed(Object... instances) {
    4.19 +        return null;
    4.20 +    }
    4.21 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/consistency/src-api2.0/api/Lookup.java	Sat Jun 14 09:51:12 2008 +0200
     5.3 @@ -0,0 +1,38 @@
     5.4 +package api;
     5.5 +
     5.6 +import java.util.Collection;
     5.7 +import java.util.Set;
     5.8 +
     5.9 +/** Simplified version of NetBeans 
    5.10 + * <a href="http://bits.netbeans.org/6.0/javadoc/org-openide-util/org/openide/util/Lookup.html">Lookup</a>
    5.11 + * reimplemented to separate the API for clients
    5.12 + * from the API for implementators while guaranteeing
    5.13 + * consistency among all there methods.
    5.14 + *
    5.15 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.16 + * @version 2.0
    5.17 + */
    5.18 +// BEGIN: design.consistency.2.0
    5.19 +public final class Lookup {
    5.20 +    Lookup() {
    5.21 +    }
    5.22 +    
    5.23 +    // BEGIN: design.consistency.lookup.2.0
    5.24 +    public <T> T lookup(Class<T> clazz) {
    5.25 +        return null;
    5.26 +    }
    5.27 +    // END: design.consistency.lookup.2.0
    5.28 +
    5.29 +    // BEGIN: design.consistency.lookupAll.2.0
    5.30 +    public <T> Collection<? extends T> lookupAll(Class<T> clazz) {
    5.31 +        return null;
    5.32 +    }
    5.33 +    // END: design.consistency.lookupAll.2.0
    5.34 +
    5.35 +    // BEGIN: design.consistency.lookupAllClasses.2.0
    5.36 +    public <T> Set<Class<? extends T>> lookupAllClasses(Class<T> clazz) {
    5.37 +        return null;
    5.38 +    }
    5.39 +    // END: design.consistency.lookupAllClasses.2.0
    5.40 +}
    5.41 +// END: design.consistency.2.0
    5.42 \ No newline at end of file
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/samples/consistency/src-api2.0/api/Lookups.java	Sat Jun 14 09:51:12 2008 +0200
     6.3 @@ -0,0 +1,26 @@
     6.4 +package api;
     6.5 +
     6.6 +import java.util.Collection;
     6.7 +import java.util.Set;
     6.8 +
     6.9 +/** Factory to create various types of lookup instances.
    6.10 + *
    6.11 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    6.12 + * @version 2.0
    6.13 + */
    6.14 +public final class Lookups {
    6.15 +    private Lookups() {
    6.16 +    }
    6.17 +    
    6.18 +    public static Lookup fixed(Object... instances) {
    6.19 +        return null;
    6.20 +    }
    6.21 +    
    6.22 +    public static Lookup dynamic(Dynamic provider) {
    6.23 +        return null;
    6.24 +    }
    6.25 +    
    6.26 +    public interface Dynamic {
    6.27 +        public <T> void computeInstances(Class<T> requestedType, Collection<? super T> addInstancesTo);
    6.28 +    }
    6.29 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/samples/consistency/src-test/api/ArithmeticaTest.java	Sat Jun 14 09:51:12 2008 +0200
     7.3 @@ -0,0 +1,43 @@
     7.4 +package api;
     7.5 +
     7.6 +import junit.framework.TestCase;
     7.7 +
     7.8 +/**
     7.9 + *
    7.10 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    7.11 + */
    7.12 +public class ArithmeticaTest extends TestCase {
    7.13 +    
    7.14 +    public ArithmeticaTest(String testName) {
    7.15 +        super(testName);
    7.16 +    }            
    7.17 +
    7.18 +    @Override
    7.19 +    protected void setUp() throws Exception {
    7.20 +        super.setUp();
    7.21 +    }
    7.22 +
    7.23 +    @Override
    7.24 +    protected void tearDown() throws Exception {
    7.25 +        super.tearDown();
    7.26 +    }
    7.27 +
    7.28 +    //BEGIN: design.composition.arith.test
    7.29 +    public void testSumTwo() {
    7.30 +        Arithmetica instance = new Arithmetica();
    7.31 +        assertEquals("+", 5, instance.sumTwo(3, 2));
    7.32 +    }
    7.33 +
    7.34 +    public void testSumAll() {
    7.35 +        Arithmetica instance = new Arithmetica();
    7.36 +        assertEquals("+", 6, instance.sumAll(3, 2, 1));
    7.37 +    }
    7.38 +
    7.39 +    public void testSumRange() {
    7.40 +        Arithmetica instance = new Arithmetica();
    7.41 +        assertEquals("+", 6, instance.sumRange(1, 3));
    7.42 +        assertEquals("10", 55, instance.sumRange(1, 10));
    7.43 +    }
    7.44 +    //END: design.composition.arith.test
    7.45 +
    7.46 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/samples/consistency/src-test/api/FactorialTest.java	Sat Jun 14 09:51:12 2008 +0200
     8.3 @@ -0,0 +1,59 @@
     8.4 +/*
     8.5 + * Žluťoučký kůň je naše hříbátko.
     8.6 + * and open the template in the editor.
     8.7 + */
     8.8 +
     8.9 +package api;
    8.10 +
    8.11 +import junit.framework.TestCase;
    8.12 +
    8.13 +/**
    8.14 + *
    8.15 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    8.16 + */
    8.17 +public class FactorialTest extends TestCase {
    8.18 +    
    8.19 +    public FactorialTest(String testName) {
    8.20 +        super(testName);
    8.21 +    }            
    8.22 +
    8.23 +    @Override
    8.24 +    protected void setUp() throws Exception {
    8.25 +        super.setUp();
    8.26 +    }
    8.27 +
    8.28 +    @Override
    8.29 +    protected void tearDown() throws Exception {
    8.30 +        super.tearDown();
    8.31 +    }
    8.32 +    
    8.33 +    public void testFactorial3() {
    8.34 +        assertEquals(6, Factorial.factorial(3));
    8.35 +    }
    8.36 +    
    8.37 +    public void testFactorial4() {
    8.38 +        assertEquals(24, Factorial.factorial(4));
    8.39 +    }
    8.40 +    
    8.41 +    public void testFactorial5() {
    8.42 +        assertEquals(120, Factorial.factorial(5));
    8.43 +    }
    8.44 +
    8.45 +    /** Class showing inventive, non-expected use of 
    8.46 +     * Arithmetica methods to do multiplication instead of
    8.47 +     * addition.
    8.48 +     */
    8.49 +    //BEGIN: design.composition.arith.factorial
    8.50 +    public static final class Factorial extends Arithmetica {
    8.51 +        public static int factorial(int n) {
    8.52 +            return new Factorial().sumRange(1, n);
    8.53 +        }
    8.54 +        @Override
    8.55 +        public int sumTwo(int one, int second) {
    8.56 +            return one * second;
    8.57 +        }
    8.58 +    }
    8.59 +    //END: design.composition.arith.factorial
    8.60 +
    8.61 +    
    8.62 +}