Clean definition of the version using interfaces
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:58:52 +0200
changeset 1693be14291c9f8
parent 168 d900c707916e
child 170 206cdfeefeec
Clean definition of the version using interfaces
samples/visitor/11-cleanversion/build.xml
samples/visitor/11-cleanversion/nbproject/project.xml
samples/visitor/11-cleanversion/src-api1.0/org/apidesign/visitor/Language.java
samples/visitor/11-cleanversion/src-api2.0/org/apidesign/visitor/Language.java
samples/visitor/11-cleanversion/src-test/org/apidesign/test/visitor/PrintTest.java
samples/visitor/11-cleanversion/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/11-cleanversion/build.xml	Sat Jun 14 09:58:52 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/11-cleanversion/nbproject/project.xml	Sat Jun 14 09:58:52 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>cleanversion</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>cleanversion</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/11-cleanversion/src-api1.0/org/apidesign/visitor/Language.java	Sat Jun 14 09:58:52 2008 +0200
     3.3 @@ -0,0 +1,36 @@
     3.4 +package org.apidesign.visitor;
     3.5 +
     3.6 +public final class Language {
     3.7 +    private Language() { }
     3.8 +    
     3.9 +    public static abstract class Expression {
    3.10 +        Expression() {}
    3.11 +        public abstract void visit(Visitor v);
    3.12 +    }
    3.13 +    public static final class Plus extends Expression {
    3.14 +        private final Expression first;
    3.15 +        private final Expression second;
    3.16 +        
    3.17 +        public Plus(Expression first, Expression second) {
    3.18 +            this.first = first;
    3.19 +            this.second = second;
    3.20 +        }
    3.21 +        public Expression getFirst() { return first; }
    3.22 +        public Expression getSecond() { return second; }
    3.23 +        @Override
    3.24 +        public void visit(Visitor v) { v.visitPlus(this); }
    3.25 +    }
    3.26 +    public static final class Number extends Expression {
    3.27 +        private final int value;
    3.28 +        public Number(int value) { this.value = value; }
    3.29 +        public int getValue() { return value; }
    3.30 +        @Override
    3.31 +        public void visit(Visitor v) { v.visitNumber(this); }
    3.32 +    }
    3.33 +
    3.34 +    public interface Visitor {
    3.35 +        public void visitUnknown(Expression e);
    3.36 +        public void visitPlus(Plus s);
    3.37 +        public void visitNumber(Number n);
    3.38 +    }
    3.39 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/samples/visitor/11-cleanversion/src-api2.0/org/apidesign/visitor/Language.java	Sat Jun 14 09:58:52 2008 +0200
     4.3 @@ -0,0 +1,63 @@
     4.4 +package org.apidesign.visitor;
     4.5 +
     4.6 +public final class Language {
     4.7 +    private Language() { }
     4.8 +    
     4.9 +    public static abstract class Expression {
    4.10 +        public abstract void visit(Visitor v);
    4.11 +    }
    4.12 +    public static final class Plus extends Expression {
    4.13 +        private final Expression first;
    4.14 +        private final Expression second;
    4.15 +        
    4.16 +        public Plus(Expression first, Expression second) {
    4.17 +            this.first = first;
    4.18 +            this.second = second;
    4.19 +        }
    4.20 +        public Expression getFirst() { return first; }
    4.21 +        public Expression getSecond() { return second; }
    4.22 +        @Override
    4.23 +        public void visit(Visitor v) { v.visitPlus(this); }
    4.24 +    }
    4.25 +    public static final class Number extends Expression {
    4.26 +        private final int value;
    4.27 +        public Number(int value) { this.value = value; }
    4.28 +        public int getValue() { return value; }
    4.29 +        @Override
    4.30 +        public void visit(Visitor v) { v.visitNumber(this); }
    4.31 +    }
    4.32 +    /** @since 2.0 */
    4.33 +    public static final class Minus extends Expression {
    4.34 +        private final Expression first;
    4.35 +        private final Expression second;
    4.36 +        
    4.37 +        public Minus(Expression first, Expression second) {
    4.38 +            this.first = first;
    4.39 +            this.second = second;
    4.40 +        }
    4.41 +        public Expression getFirst() { return first; }
    4.42 +        public Expression getSecond() { return second; }
    4.43 +        
    4.44 +        // BEGIN: visitor.cleanversion.dispatch2
    4.45 +        public void visit(Visitor v) { 
    4.46 +            if (v instanceof Visitor2_0) {
    4.47 +                ((Visitor2_0)v).visitMinus(this);
    4.48 +            } else {
    4.49 +                v.visitUnknown(this);
    4.50 +            }
    4.51 +        }
    4.52 +        // END: visitor.cleanversion.dispatch2
    4.53 +    }
    4.54 +
    4.55 +    // BEGIN: visitor.cleanversion.v2
    4.56 +    public interface Visitor {
    4.57 +        public void visitUnknown(Expression e);
    4.58 +        public void visitPlus(Plus s);
    4.59 +        public void visitNumber(Number n);
    4.60 +    }
    4.61 +    /** @since 2.0 */
    4.62 +    public interface Visitor2_0 extends Visitor {
    4.63 +        public void visitMinus(Minus s);
    4.64 +    }
    4.65 +    // END: visitor.cleanversion.v2
    4.66 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/samples/visitor/11-cleanversion/src-test/org/apidesign/test/visitor/PrintTest.java	Sat Jun 14 09:58:52 2008 +0200
     5.3 @@ -0,0 +1,53 @@
     5.4 +package org.apidesign.test.visitor;
     5.5 +
     5.6 +import static junit.framework.Assert.*;
     5.7 +import org.apidesign.visitor.Language.Expression;
     5.8 +import org.apidesign.visitor.Language.Number;
     5.9 +import org.apidesign.visitor.Language.Plus;
    5.10 +import org.apidesign.visitor.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 visitUnknown(Expression exp) {
    5.20 +            sb.append("unknown");
    5.21 +        }
    5.22 +        
    5.23 +        public void visitPlus(Plus s) {
    5.24 +            s.getFirst().visit(this);
    5.25 +            sb.append(" + ");
    5.26 +            s.getSecond().visit(this);
    5.27 +        }
    5.28 +
    5.29 +        public void visitNumber(Number n) {
    5.30 +            sb.append (n.getValue());
    5.31 +        }
    5.32 +    }
    5.33 +    
    5.34 +    @Test public void printOnePlusOne() {
    5.35 +        Number one = new Number(1);
    5.36 +        Expression plus = new Plus(one, one);
    5.37 +        
    5.38 +        PrintVisitor print = new PrintVisitor();
    5.39 +        plus.visit(print);
    5.40 +        
    5.41 +        assertEquals("1 + 1", print.sb.toString());
    5.42 +    }
    5.43 +    // END: visitor.notevolutionready.print
    5.44 +
    5.45 +    @Test public void printOnePlusTwoPlusThree() {
    5.46 +        Number one = new Number(1);
    5.47 +        Number two = new Number(2);
    5.48 +        Number three = new Number(3);
    5.49 +        Expression plus = new Plus(one, new Plus(two, three));
    5.50 +        
    5.51 +        PrintVisitor print = new PrintVisitor();
    5.52 +        plus.visit(print);
    5.53 +        
    5.54 +        assertEquals("1 + 2 + 3", print.sb.toString());
    5.55 +    }
    5.56 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/samples/visitor/11-cleanversion/src-test2.0/org/apidesign/test/visitor/PrintOfMinusStructureTest.java	Sat Jun 14 09:58:52 2008 +0200
     6.3 @@ -0,0 +1,61 @@
     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.Language.Expression;
     6.9 +import org.apidesign.visitor.Language.Minus;
    6.10 +import org.apidesign.visitor.Language.Number;
    6.11 +import org.apidesign.visitor.Language.Plus;
    6.12 +import org.apidesign.visitor.Language.Visitor;
    6.13 +import org.apidesign.visitor.Language.Visitor2_0;
    6.14 +import org.junit.Test;
    6.15 +
    6.16 +public class PrintOfMinusStructureTest {
    6.17 +    @Test public void printOneMinusTwo() {
    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);
    6.24 +        
    6.25 +        assertEquals("1 - 2", print.sb.toString());
    6.26 +    }
    6.27 +    
    6.28 +    @Test public void visitorReadyForVersion2_0() {
    6.29 +        // BEGIN: visitor.cleanversion.print2
    6.30 +        class PrintVisitor2_0 implements Visitor2_0 {
    6.31 +            StringBuffer sb = new StringBuffer();
    6.32 +
    6.33 +            public void visitUnknown(Expression exp) {
    6.34 +                sb.append("unknown");
    6.35 +            }
    6.36 +
    6.37 +            public void visitPlus(Plus s) {
    6.38 +                s.getFirst().visit(this);
    6.39 +                sb.append(" + ");
    6.40 +                s.getSecond().visit(this);
    6.41 +            }
    6.42 +
    6.43 +            public void visitNumber(Number n) {
    6.44 +                sb.append (n.getValue());
    6.45 +            }
    6.46 +            public void visitMinus(Minus m) {
    6.47 +                m.getFirst().visit(this);
    6.48 +                sb.append(" - ");
    6.49 +                m.getSecond().visit(this);
    6.50 +            }
    6.51 +        }
    6.52 +        
    6.53 +        Number one = new Number(1);
    6.54 +        Number two = new Number(2);
    6.55 +        Expression plus = new Minus(one, two);
    6.56 +        
    6.57 +        PrintVisitor2_0 print = new PrintVisitor2_0();
    6.58 +        plus.visit(print);
    6.59 +        
    6.60 +        assertEquals("1 - 2", print.sb.toString());
    6.61 +        // END: visitor.cleanversion.print2
    6.62 +        
    6.63 +    }
    6.64 +}