First two versions of client provider Visitor
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:59:16 +0200
changeset 1760f658628beac
parent 175 7aaa7b256c7e
child 177 67d6dceb1002
First two versions of client provider Visitor
samples/visitor/21-clientprovider/build.xml
samples/visitor/21-clientprovider/nbproject/project.xml
samples/visitor/21-clientprovider/src-api1.0/org/apidesign/visitor/Language.java
samples/visitor/21-clientprovider/src-api2.0/org/apidesign/visitor/Language.java
samples/visitor/21-clientprovider/src-test/org/apidesign/test/visitor/PrintTest.java
samples/visitor/21-clientprovider/src-test2.0/org/apidesign/test/visitor/PrintOfMinusStructureTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/visitor/21-clientprovider/build.xml	Sat Jun 14 09:59:16 2008 +0200
     1.3 @@ -0,0 +1,96 @@
     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="compile" depends="build"/>
    1.11 +    <target name="build" depends="-libraries">
    1.12 +        <antcall target="-build-one">
    1.13 +            <param name="version" value="api1.0"/>
    1.14 +        </antcall>
    1.15 +        <antcall target="-build-one">
    1.16 +            <param name="version" value="api2.0"/>
    1.17 +        </antcall>
    1.18 +        
    1.19 +        <antcall target="-build-one">
    1.20 +            <param name="version" value="test"/>
    1.21 +            <param name="cp" value="build/api1.0/classes:${junit.jar}"/>
    1.22 +        </antcall>
    1.23 +
    1.24 +        <echo level="info" message="Next compilation will fails, as adding methods into interface is not binary compatible"/>
    1.25 +        <antcall target="-build-one">
    1.26 +            <param name="version" value="test"/>
    1.27 +            <param name="out" value="will-not-compile"/>
    1.28 +            <param name="cp" value="build/api2.0/classes:${junit.jar}"/>
    1.29 +            <param name="failonerror" value="false"/>
    1.30 +        </antcall>
    1.31 +
    1.32 +        <antcall target="-build-one">
    1.33 +            <param name="version" value="test2.0"/>
    1.34 +            <param name="cp" value="build/api2.0/classes:build/test/classes:${junit.jar}"/>
    1.35 +        </antcall>
    1.36 +    </target>
    1.37 +    
    1.38 +    <target name="test" depends="build">
    1.39 +        <echo level="info" message="PrintVisitor on old API. This should succeeds."/>
    1.40 +        <antcall target="-run-one">
    1.41 +            <param name="version" value="api1.0"/>
    1.42 +        </antcall>
    1.43 +        <echo level="info" message="PrintVisitor on old API. This would not compile, but it runs as it does not deal with minus at all."/>
    1.44 +        <antcall target="-run-one">
    1.45 +            <param name="version" value="api2.0"/>
    1.46 +        </antcall>
    1.47 +        <echo level="info" message="PrintVisitor on new API. This yields runtime error."/>
    1.48 +        <antcall target="-run-one">
    1.49 +            <param name="test" value="test2.0"/>
    1.50 +            <param name="extra.cp" value="build/test/classes"/>
    1.51 +            <param name="version" value="api2.0"/>
    1.52 +        </antcall>
    1.53 +    </target>
    1.54 +    
    1.55 +    <!-- support methods -->
    1.56 +    
    1.57 +    <target name="-libraries">
    1.58 +        <ant dir="../../libs/"/>
    1.59 +        
    1.60 +        <property name="junit.jar" location="../../libs/dist/junit-4.4.jar"/>
    1.61 +    </target>
    1.62 +    
    1.63 +    <target name="-run-one">
    1.64 +        <fail message="You need to specify API version number" unless="version"/>
    1.65 +        <property name="test" value="test"/>
    1.66 +        <mkdir dir="build/${test}results${version}"/>
    1.67 +        <junit dir="build/test/classes" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" showoutput="true">
    1.68 +            <batchtest todir="build/${test}results${version}">
    1.69 +                <fileset dir="build/${test}/classes">
    1.70 +                    <filename name="**/*Test.class"/>
    1.71 +                </fileset>
    1.72 +            </batchtest>
    1.73 +            <classpath>
    1.74 +                <path location="build/${version}/classes"/>
    1.75 +                <path location="build/${test}/classes"/>
    1.76 +                <path location="${junit.jar}"/>
    1.77 +                <path path="${extra.cp}"/>
    1.78 +            </classpath>
    1.79 +            <formatter type="brief" usefile="false"/>
    1.80 +            <formatter type="xml"/>
    1.81 +        </junit>
    1.82 +    </target>
    1.83 +    
    1.84 +    <target name="-build-one">
    1.85 +        <fail message="You need to specify version number" unless="version"/>
    1.86 +        
    1.87 +        <property name="cp" value=""/>
    1.88 +        <property name="out" value="${version}"/>
    1.89 +        <property name="failonerror" value="true"/>
    1.90 +        <mkdir dir="build/${out}/classes"/>
    1.91 +        <javac 
    1.92 +            srcdir="src-${version}" 
    1.93 +            destdir="build/${out}/classes" 
    1.94 +            source="1.5" target="1.5"
    1.95 +            classpath="${cp}"
    1.96 +            failonerror="${failonerror}"
    1.97 +        />
    1.98 +    </target>
    1.99 +</project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/visitor/21-clientprovider/nbproject/project.xml	Sat Jun 14 09:59:16 2008 +0200
     2.3 @@ -0,0 +1,107 @@
     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>clientprovider</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>clientprovider</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 +                <source-folder>
    2.35 +                    <label>test-2.0</label>
    2.36 +                    <type>java</type>
    2.37 +                    <location>src-test2.0</location>
    2.38 +                    <encoding>UTF-8</encoding>
    2.39 +                </source-folder>
    2.40 +            </folders>
    2.41 +            <ide-actions>
    2.42 +                <action name="build">
    2.43 +                    <target>build</target>
    2.44 +                </action>
    2.45 +                <action name="clean">
    2.46 +                    <target>clean</target>
    2.47 +                </action>
    2.48 +                <action name="test">
    2.49 +                    <target>test</target>
    2.50 +                </action>
    2.51 +                <action name="run">
    2.52 +                    <target>test</target>
    2.53 +                </action>
    2.54 +                <action name="rebuild">
    2.55 +                    <target>clean</target>
    2.56 +                    <target>build</target>
    2.57 +                </action>
    2.58 +            </ide-actions>
    2.59 +            <view>
    2.60 +                <items>
    2.61 +                    <source-folder style="packages">
    2.62 +                        <label>API Version 1.0</label>
    2.63 +                        <location>src-api1.0</location>
    2.64 +                    </source-folder>
    2.65 +                    <source-folder style="packages">
    2.66 +                        <label>API Version 2.0</label>
    2.67 +                        <location>src-api2.0</location>
    2.68 +                    </source-folder>
    2.69 +                    <source-folder style="packages">
    2.70 +                        <label>Usage of the API</label>
    2.71 +                        <location>src-test</location>
    2.72 +                    </source-folder>
    2.73 +                    <source-folder style="packages">
    2.74 +                        <label>Usage of new structures with old visitor</label>
    2.75 +                        <location>src-test2.0</location>
    2.76 +                    </source-folder>
    2.77 +                    <source-file>
    2.78 +                        <location>build.xml</location>
    2.79 +                    </source-file>
    2.80 +                </items>
    2.81 +                <context-menu>
    2.82 +                    <ide-action name="build"/>
    2.83 +                    <ide-action name="rebuild"/>
    2.84 +                    <ide-action name="clean"/>
    2.85 +                    <ide-action name="test"/>
    2.86 +                </context-menu>
    2.87 +            </view>
    2.88 +        </general-data>
    2.89 +        <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/1">
    2.90 +            <compilation-unit>
    2.91 +                <package-root>src-api1.0</package-root>
    2.92 +                <source-level>1.5</source-level>
    2.93 +            </compilation-unit>
    2.94 +            <compilation-unit>
    2.95 +                <package-root>src-api2.0</package-root>
    2.96 +                <source-level>1.5</source-level>
    2.97 +            </compilation-unit>
    2.98 +            <compilation-unit>
    2.99 +                <package-root>src-test</package-root>
   2.100 +                <classpath mode="compile">src-api1.0:../../libs/dist/junit-4.4.jar</classpath>
   2.101 +                <source-level>1.5</source-level>
   2.102 +            </compilation-unit>
   2.103 +            <compilation-unit>
   2.104 +                <package-root>src-test2.0</package-root>
   2.105 +                <classpath mode="compile">src-api2.0:src-test:../../libs/dist/junit-4.4.jar</classpath>
   2.106 +                <source-level>1.5</source-level>
   2.107 +            </compilation-unit>
   2.108 +        </java-data>
   2.109 +    </configuration>
   2.110 +</project>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/visitor/21-clientprovider/src-api1.0/org/apidesign/visitor/Language.java	Sat Jun 14 09:59:16 2008 +0200
     3.3 @@ -0,0 +1,49 @@
     3.4 +package org.apidesign.visitor;
     3.5 +
     3.6 +public final class Language {
     3.7 +    private Language() { }
     3.8 +    
     3.9 +    // BEGIN: visitor.clientprovider.v1
    3.10 +    public interface Expression {
    3.11 +        public abstract void visit(Visitor v);
    3.12 +    }
    3.13 +    public interface Plus extends Expression {
    3.14 +        public Expression getFirst();
    3.15 +        public Expression getSecond();
    3.16 +    }
    3.17 +    public interface Number extends Expression {
    3.18 +        public int getValue();
    3.19 +    }
    3.20 +
    3.21 +    public static abstract class Visitor {
    3.22 +        Visitor() {}
    3.23 +
    3.24 +        public Visitor create(Version1_0 v) {
    3.25 +            return create1_0(v);
    3.26 +        }
    3.27 +
    3.28 +        public interface Version1_0 {
    3.29 +            public boolean visitUnknown(Expression e);
    3.30 +            public void visitPlus(Plus s);
    3.31 +            public void visitNumber(Number n);
    3.32 +        }
    3.33 +
    3.34 +        public abstract void dispatchPlus(Plus p);
    3.35 +        public abstract void dispatchNumber(Number n);
    3.36 +    }
    3.37 +    // END: visitor.clientprovider.v1
    3.38 +    
    3.39 +    static Visitor create1_0(final Visitor.Version1_0 v) {
    3.40 +        return new Visitor() {
    3.41 +            @Override
    3.42 +            public void dispatchPlus(Plus p) {
    3.43 +                v.visitPlus(p);
    3.44 +            }
    3.45 +
    3.46 +            @Override
    3.47 +            public void dispatchNumber(Number n) {
    3.48 +                v.visitNumber(n);
    3.49 +            }
    3.50 +        };
    3.51 +    }
    3.52 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/visitor/21-clientprovider/src-api2.0/org/apidesign/visitor/Language.java	Sat Jun 14 09:59:16 2008 +0200
     4.3 @@ -0,0 +1,93 @@
     4.4 +package org.apidesign.visitor;
     4.5 +
     4.6 +public final class Language {
     4.7 +    private Language() { }
     4.8 +    
     4.9 +    public interface Expression {
    4.10 +        public abstract void visit(Visitor v);
    4.11 +    }
    4.12 +    public interface Plus extends Expression {
    4.13 +        public Expression getFirst();
    4.14 +        public Expression getSecond();
    4.15 +    }
    4.16 +    public interface Number extends Expression {
    4.17 +        public int getValue();
    4.18 +    }
    4.19 +    // BEGIN: visitor.clientprovider.v2
    4.20 +    /** @since 2.0 */
    4.21 +    public interface Minus extends Expression {
    4.22 +        public Expression getFirst();
    4.23 +        public Expression getSecond();
    4.24 +    }
    4.25 +    
    4.26 +    public static abstract class Visitor {
    4.27 +        Visitor() {}
    4.28 +        /** @since 2.0 */
    4.29 +        public static Visitor create(Version2_0 v) {
    4.30 +            return create2_0(v);
    4.31 +        }
    4.32 +
    4.33 +        /** @since 2.0 */
    4.34 +        public interface Version2_0 extends Version1_0 {
    4.35 +            public void visitMinus(Minus m);
    4.36 +        }
    4.37 +
    4.38 +
    4.39 +        /** @since 2.0 */
    4.40 +        public abstract void dispatchNumber(Number n);
    4.41 +    // FINISH: visitor.clientprovider.v2
    4.42 +        
    4.43 +        public Visitor create(Version1_0 v) {
    4.44 +            return create1_0(v);
    4.45 +        }
    4.46 +
    4.47 +        public interface Version1_0 {
    4.48 +            public boolean visitUnknown(Expression e);
    4.49 +            public void visitPlus(Plus s);
    4.50 +            public void visitNumber(Number n);
    4.51 +        }
    4.52 +
    4.53 +        public abstract void dispatchPlus(Plus p);
    4.54 +        public abstract void dispatchMinus(Minus m);
    4.55 +    }
    4.56 +    
    4.57 +    static Visitor create1_0(final Visitor.Version1_0 v) {
    4.58 +        return new Visitor() {
    4.59 +            @Override
    4.60 +            public void dispatchPlus(Plus p) {
    4.61 +                v.visitPlus(p);
    4.62 +            }
    4.63 +
    4.64 +            @Override
    4.65 +            public void dispatchNumber(Number n) {
    4.66 +                v.visitNumber(n);
    4.67 +            }
    4.68 +
    4.69 +            @Override
    4.70 +            public void dispatchMinus(Minus m) {
    4.71 +                if (v.visitUnknown(m)) {
    4.72 +                    m.getFirst().visit(this);
    4.73 +                    m.getSecond().visit(this);
    4.74 +                }
    4.75 +            }
    4.76 +        };
    4.77 +    }
    4.78 +    static Visitor create2_0(final Visitor.Version2_0 v) {
    4.79 +        return new Visitor() {
    4.80 +            @Override
    4.81 +            public void dispatchPlus(Plus p) {
    4.82 +                v.visitPlus(p);
    4.83 +            }
    4.84 +
    4.85 +            @Override
    4.86 +            public void dispatchNumber(Number n) {
    4.87 +                v.visitNumber(n);
    4.88 +            }
    4.89 +
    4.90 +            @Override
    4.91 +            public void dispatchMinus(Minus m) {
    4.92 +                v.visitMinus(m);
    4.93 +            }
    4.94 +        };
    4.95 +    }
    4.96 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/visitor/21-clientprovider/src-test/org/apidesign/test/visitor/PrintTest.java	Sat Jun 14 09:59:16 2008 +0200
     5.3 @@ -0,0 +1,49 @@
     5.4 +package org.apidesign.test.visitor;
     5.5 +
     5.6 +import static junit.framework.Assert.*;
     5.7 +import org.apidesign.visitor.notevolutionready.Language.Expression;
     5.8 +import org.apidesign.visitor.notevolutionready.Language.Number;
     5.9 +import org.apidesign.visitor.notevolutionready.Language.Plus;
    5.10 +import org.apidesign.visitor.notevolutionready.Language.Visitor;
    5.11 +import org.junit.Test;
    5.12 +
    5.13 +public class PrintTest {
    5.14 +
    5.15 +    // BEGIN: visitor.notevolutionready.print
    5.16 +    public static class PrintVisitor implements Visitor {
    5.17 +        StringBuffer sb = new StringBuffer();
    5.18 +        
    5.19 +        public void visitPlus(Plus s) {
    5.20 +            s.getFirst().visit(this);
    5.21 +            sb.append(" + ");
    5.22 +            s.getSecond().visit(this);
    5.23 +        }
    5.24 +
    5.25 +        public void visitNumber(Number n) {
    5.26 +            sb.append (n.getValue());
    5.27 +        }
    5.28 +    }
    5.29 +    
    5.30 +    @Test public void printOnePlusOne() {
    5.31 +        Number one = new Number(1);
    5.32 +        Expression plus = new Plus(one, one);
    5.33 +        
    5.34 +        PrintVisitor print = new PrintVisitor();
    5.35 +        plus.visit(print);
    5.36 +        
    5.37 +        assertEquals("1 + 1", print.sb.toString());
    5.38 +    }
    5.39 +    // END: visitor.notevolutionready.print
    5.40 +
    5.41 +    @Test public void printOnePlusTwoPlusThree() {
    5.42 +        Number one = new Number(1);
    5.43 +        Number two = new Number(2);
    5.44 +        Number three = new Number(3);
    5.45 +        Expression plus = new Plus(one, new Plus(two, three));
    5.46 +        
    5.47 +        PrintVisitor print = new PrintVisitor();
    5.48 +        plus.visit(print);
    5.49 +        
    5.50 +        assertEquals("1 + 2 + 3", print.sb.toString());
    5.51 +    }
    5.52 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/samples/visitor/21-clientprovider/src-test2.0/org/apidesign/test/visitor/PrintOfMinusStructureTest.java	Sat Jun 14 09:59:16 2008 +0200
     6.3 @@ -0,0 +1,25 @@
     6.4 +package org.apidesign.test.visitor;
     6.5 +
     6.6 +import org.apidesign.test.visitor.PrintTest.PrintVisitor;
     6.7 +import static junit.framework.Assert.*;
     6.8 +import org.apidesign.visitor.notevolutionready.Language.Expression;
     6.9 +import org.apidesign.visitor.notevolutionready.Language.Minus;
    6.10 +import org.apidesign.visitor.notevolutionready.Language.Number;
    6.11 +import org.apidesign.visitor.notevolutionready.Language.Plus;
    6.12 +import org.apidesign.visitor.notevolutionready.Language.Visitor;
    6.13 +import org.junit.Test;
    6.14 +
    6.15 +public class PrintOfMinusStructureTest {
    6.16 +    @Test public void printOneMinusTwo() {
    6.17 +        // BEGIN: visitor.notevolutionready.oldwithnew
    6.18 +        Number one = new Number(1);
    6.19 +        Number two = new Number(2);
    6.20 +        Expression plus = new Minus(one, two);
    6.21 +        
    6.22 +        PrintVisitor print = new PrintVisitor();
    6.23 +        plus.visit(print); // fails with AbstractMethodError
    6.24 +        
    6.25 +        assertEquals("1 - 2", print.sb.toString());
    6.26 +        // END: visitor.notevolutionready.oldwithnew
    6.27 +    }
    6.28 +}