In order to provide better navigation in the Visitor examples, I will prefix them with a number
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/visitor/01-notevolutionready/build.xml Sat Jun 14 09:58:47 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/01-notevolutionready/nbproject/project.xml Sat Jun 14 09:58:47 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>notevolutionready</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>notevolutionready</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/01-notevolutionready/src-api1.0/org/apidesign/visitor/notevolutionready/Language.java Sat Jun 14 09:58:47 2008 +0200
3.3 @@ -0,0 +1,36 @@
3.4 +package org.apidesign.visitor.notevolutionready;
3.5 +
3.6 +public final class Language {
3.7 + private Language() { }
3.8 +
3.9 + // BEGIN: visitor.notevolutionready.v1
3.10 + public static abstract class 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 visitPlus(Plus s);
3.36 + public void visitNumber(Number n);
3.37 + }
3.38 + // END: visitor.notevolutionready.v1
3.39 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/samples/visitor/01-notevolutionready/src-api2.0/org/apidesign/visitor/notevolutionready/Language.java Sat Jun 14 09:58:47 2008 +0200
4.3 @@ -0,0 +1,53 @@
4.4 +package org.apidesign.visitor.notevolutionready;
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 + // BEGIN: visitor.notevolutionready.v2
4.33 + /** @since 2.0 */
4.34 + public static final class Minus extends Expression {
4.35 + private final Expression first;
4.36 + private final Expression second;
4.37 +
4.38 + public Minus(Expression first, Expression second) {
4.39 + this.first = first;
4.40 + this.second = second;
4.41 + }
4.42 + public Expression getFirst() { return first; }
4.43 + public Expression getSecond() { return second; }
4.44 + public void visit(Visitor v) {
4.45 + /* now what? add new method to an interface!? */
4.46 + v.visitMinus(this);
4.47 + }
4.48 + }
4.49 + // END: visitor.notevolutionready.v2
4.50 +
4.51 + public interface Visitor {
4.52 + public void visitPlus(Plus s);
4.53 + public void visitMinus(Minus s);
4.54 + public void visitNumber(Number n);
4.55 + }
4.56 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/samples/visitor/01-notevolutionready/src-test/org/apidesign/test/visitor/PrintTest.java Sat Jun 14 09:58:47 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/01-notevolutionready/src-test2.0/org/apidesign/test/visitor/PrintOfMinusStructureTest.java Sat Jun 14 09:58:47 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 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/samples/visitor/02-abstractclass/build.xml Sat Jun 14 09:58:47 2008 +0200
7.3 @@ -0,0 +1,94 @@
7.4 +<?xml version="1.0" encoding="UTF-8"?>
7.5 +<project name="Build Script" default="test" basedir=".">
7.6 + <target name="clean">
7.7 + <delete dir="build"/>
7.8 + </target>
7.9 +
7.10 + <target name="compile" depends="build"/>
7.11 + <target name="build" depends="-libraries">
7.12 + <antcall target="-build-one">
7.13 + <param name="version" value="api1.0"/>
7.14 + </antcall>
7.15 + <antcall target="-build-one">
7.16 + <param name="version" value="api2.0"/>
7.17 + </antcall>
7.18 +
7.19 + <antcall target="-build-one">
7.20 + <param name="version" value="test"/>
7.21 + <param name="cp" value="build/api1.0/classes:${junit.jar}"/>
7.22 + </antcall>
7.23 +
7.24 + <antcall target="-build-one">
7.25 + <param name="version" value="test"/>
7.26 + <param name="out" value="will-not-compile"/>
7.27 + <param name="cp" value="build/api2.0/classes:${junit.jar}"/>
7.28 + </antcall>
7.29 +
7.30 + <antcall target="-build-one">
7.31 + <param name="version" value="test2.0"/>
7.32 + <param name="cp" value="build/api2.0/classes:build/test/classes:${junit.jar}"/>
7.33 + </antcall>
7.34 + </target>
7.35 +
7.36 + <target name="test" depends="build">
7.37 + <echo level="info" message="PrintVisitor on old API. This should succeeds."/>
7.38 + <antcall target="-run-one">
7.39 + <param name="version" value="api1.0"/>
7.40 + </antcall>
7.41 + <echo level="info" message="PrintVisitor on old API. This would not compile, but it runs as it does not deal with minus at all."/>
7.42 + <antcall target="-run-one">
7.43 + <param name="version" value="api2.0"/>
7.44 + </antcall>
7.45 + <echo level="info" message="PrintVisitor on new API. This yields runtime error."/>
7.46 + <antcall target="-run-one">
7.47 + <param name="test" value="test2.0"/>
7.48 + <param name="extra.cp" value="build/test/classes"/>
7.49 + <param name="version" value="api2.0"/>
7.50 + </antcall>
7.51 + </target>
7.52 +
7.53 + <!-- support methods -->
7.54 +
7.55 + <target name="-libraries">
7.56 + <ant dir="../../libs/"/>
7.57 +
7.58 + <property name="junit.jar" location="../../libs/dist/junit-4.4.jar"/>
7.59 + </target>
7.60 +
7.61 + <target name="-run-one">
7.62 + <fail message="You need to specify API version number" unless="version"/>
7.63 + <property name="test" value="test"/>
7.64 + <mkdir dir="build/${test}results${version}"/>
7.65 + <junit dir="build/test/classes" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" showoutput="true">
7.66 + <batchtest todir="build/${test}results${version}">
7.67 + <fileset dir="build/${test}/classes">
7.68 + <filename name="**/*Test.class"/>
7.69 + </fileset>
7.70 + </batchtest>
7.71 + <classpath>
7.72 + <path location="build/${version}/classes"/>
7.73 + <path location="build/${test}/classes"/>
7.74 + <path location="${junit.jar}"/>
7.75 + <path path="${extra.cp}"/>
7.76 + </classpath>
7.77 + <formatter type="brief" usefile="false"/>
7.78 + <formatter type="xml"/>
7.79 + </junit>
7.80 + </target>
7.81 +
7.82 + <target name="-build-one">
7.83 + <fail message="You need to specify version number" unless="version"/>
7.84 +
7.85 + <property name="cp" value=""/>
7.86 + <property name="out" value="${version}"/>
7.87 + <property name="failonerror" value="true"/>
7.88 + <mkdir dir="build/${out}/classes"/>
7.89 + <javac
7.90 + srcdir="src-${version}"
7.91 + destdir="build/${out}/classes"
7.92 + source="1.5" target="1.5"
7.93 + classpath="${cp}"
7.94 + failonerror="${failonerror}"
7.95 + />
7.96 + </target>
7.97 +</project>
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/samples/visitor/02-abstractclass/nbproject/project.xml Sat Jun 14 09:58:47 2008 +0200
8.3 @@ -0,0 +1,107 @@
8.4 +<?xml version="1.0" encoding="UTF-8"?>
8.5 +<project xmlns="http://www.netbeans.org/ns/project/1" xmlns:ns4="null">
8.6 + <type>org.netbeans.modules.ant.freeform</type>
8.7 + <configuration>
8.8 + <general-data xmlns="http://www.netbeans.org/ns/freeform-project/1">
8.9 + <name>abstractclass</name>
8.10 + </general-data>
8.11 + <general-data xmlns="http://www.netbeans.org/ns/freeform-project/2">
8.12 + <!-- Do not use Project Properties customizer when editing this file manually. -->
8.13 + <name>abstractclass</name>
8.14 + <properties/>
8.15 + <folders>
8.16 + <source-folder>
8.17 + <label>src-api1.0</label>
8.18 + <type>java</type>
8.19 + <location>src-api1.0</location>
8.20 + <encoding>UTF-8</encoding>
8.21 + </source-folder>
8.22 + <source-folder>
8.23 + <label>src-api2.0</label>
8.24 + <type>java</type>
8.25 + <location>src-api2.0</location>
8.26 + <encoding>UTF-8</encoding>
8.27 + </source-folder>
8.28 + <source-folder>
8.29 + <label>test</label>
8.30 + <type>java</type>
8.31 + <location>src-test</location>
8.32 + <encoding>UTF-8</encoding>
8.33 + </source-folder>
8.34 + <source-folder>
8.35 + <label>test-2.0</label>
8.36 + <type>java</type>
8.37 + <location>src-test2.0</location>
8.38 + <encoding>UTF-8</encoding>
8.39 + </source-folder>
8.40 + </folders>
8.41 + <ide-actions>
8.42 + <action name="build">
8.43 + <target>build</target>
8.44 + </action>
8.45 + <action name="clean">
8.46 + <target>clean</target>
8.47 + </action>
8.48 + <action name="test">
8.49 + <target>test</target>
8.50 + </action>
8.51 + <action name="run">
8.52 + <target>test</target>
8.53 + </action>
8.54 + <action name="rebuild">
8.55 + <target>clean</target>
8.56 + <target>build</target>
8.57 + </action>
8.58 + </ide-actions>
8.59 + <view>
8.60 + <items>
8.61 + <source-folder style="packages">
8.62 + <label>API Version 1.0</label>
8.63 + <location>src-api1.0</location>
8.64 + </source-folder>
8.65 + <source-folder style="packages">
8.66 + <label>API Version 2.0</label>
8.67 + <location>src-api2.0</location>
8.68 + </source-folder>
8.69 + <source-folder style="packages">
8.70 + <label>Usage of the API</label>
8.71 + <location>src-test</location>
8.72 + </source-folder>
8.73 + <source-folder style="packages">
8.74 + <label>Usage of new structures with old visitor</label>
8.75 + <location>src-test2.0</location>
8.76 + </source-folder>
8.77 + <source-file>
8.78 + <location>build.xml</location>
8.79 + </source-file>
8.80 + </items>
8.81 + <context-menu>
8.82 + <ide-action name="build"/>
8.83 + <ide-action name="rebuild"/>
8.84 + <ide-action name="clean"/>
8.85 + <ide-action name="test"/>
8.86 + </context-menu>
8.87 + </view>
8.88 + </general-data>
8.89 + <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/1">
8.90 + <compilation-unit>
8.91 + <package-root>src-api1.0</package-root>
8.92 + <source-level>1.5</source-level>
8.93 + </compilation-unit>
8.94 + <compilation-unit>
8.95 + <package-root>src-api2.0</package-root>
8.96 + <source-level>1.5</source-level>
8.97 + </compilation-unit>
8.98 + <compilation-unit>
8.99 + <package-root>src-test</package-root>
8.100 + <classpath mode="compile">src-api1.0:../../libs/dist/junit-4.4.jar</classpath>
8.101 + <source-level>1.5</source-level>
8.102 + </compilation-unit>
8.103 + <compilation-unit>
8.104 + <package-root>src-test2.0</package-root>
8.105 + <classpath mode="compile">src-api2.0:src-test:../../libs/dist/junit-4.4.jar</classpath>
8.106 + <source-level>1.5</source-level>
8.107 + </compilation-unit>
8.108 + </java-data>
8.109 + </configuration>
8.110 +</project>
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/samples/visitor/02-abstractclass/src-api1.0/org/apidesign/visitor/Language.java Sat Jun 14 09:58:47 2008 +0200
9.3 @@ -0,0 +1,34 @@
9.4 +package org.apidesign.visitor;
9.5 +
9.6 +public final class Language {
9.7 + private Language() { }
9.8 +
9.9 + public static abstract class Expression {
9.10 + public abstract void visit(Visitor v);
9.11 + }
9.12 + public static final class Plus extends Expression {
9.13 + private final Expression first;
9.14 + private final Expression second;
9.15 +
9.16 + public Plus(Expression first, Expression second) {
9.17 + this.first = first;
9.18 + this.second = second;
9.19 + }
9.20 + public Expression getFirst() { return first; }
9.21 + public Expression getSecond() { return second; }
9.22 + @Override
9.23 + public void visit(Visitor v) { v.visitPlus(this); }
9.24 + }
9.25 + public static final class Number extends Expression {
9.26 + private final int value;
9.27 + public Number(int value) { this.value = value; }
9.28 + public int getValue() { return value; }
9.29 + @Override
9.30 + public void visit(Visitor v) { v.visitNumber(this); }
9.31 + }
9.32 +
9.33 + public static abstract class Visitor {
9.34 + public abstract void visitPlus(Plus s);
9.35 + public abstract void visitNumber(Number n);
9.36 + }
9.37 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/samples/visitor/02-abstractclass/src-api2.0/org/apidesign/visitor/Language.java Sat Jun 14 09:58:47 2008 +0200
10.3 @@ -0,0 +1,58 @@
10.4 +package org.apidesign.visitor;
10.5 +
10.6 +public final class Language {
10.7 + private Language() { }
10.8 +
10.9 + public static abstract class Expression {
10.10 + public abstract void visit(Visitor v);
10.11 + }
10.12 + public static final class Plus extends Expression {
10.13 + private final Expression first;
10.14 + private final Expression second;
10.15 +
10.16 + public Plus(Expression first, Expression second) {
10.17 + this.first = first;
10.18 + this.second = second;
10.19 + }
10.20 + public Expression getFirst() { return first; }
10.21 + public Expression getSecond() { return second; }
10.22 + @Override
10.23 + public void visit(Visitor v) { v.visitPlus(this); }
10.24 + }
10.25 + public static final class Number extends Expression {
10.26 + private final int value;
10.27 + public Number(int value) { this.value = value; }
10.28 + public int getValue() { return value; }
10.29 + @Override
10.30 + public void visit(Visitor v) { v.visitNumber(this); }
10.31 + }
10.32 + // BEGIN: visitor.abstractclass.v2
10.33 + /** @since 2.0 */
10.34 + public static final class Minus extends Expression {
10.35 + private final Expression first;
10.36 + private final Expression second;
10.37 +
10.38 + public Minus(Expression first, Expression second) {
10.39 + this.first = first;
10.40 + this.second = second;
10.41 + }
10.42 + public Expression getFirst() { return first; }
10.43 + public Expression getSecond() { return second; }
10.44 + public void visit(Visitor v) {
10.45 + v.visitMinus(this);
10.46 + }
10.47 + }
10.48 +
10.49 + public static abstract class Visitor {
10.50 + public abstract void visitPlus(Plus s);
10.51 + public abstract void visitNumber(Number n);
10.52 + /** @since 2.0 */
10.53 + public void visitMinus(Minus s) {
10.54 + throw new IllegalStateException(
10.55 + "Old visitor used on new exceptions"
10.56 + );
10.57 + }
10.58 + }
10.59 + // END: visitor.abstractclass.v2
10.60 +
10.61 +}
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/samples/visitor/02-abstractclass/src-test/org/apidesign/test/visitor/LanguageCheckTest.java Sat Jun 14 09:58:47 2008 +0200
11.3 @@ -0,0 +1,48 @@
11.4 +package org.apidesign.test.visitor;
11.5 +
11.6 +import static junit.framework.Assert.*;
11.7 +import org.apidesign.visitor.Language.Expression;
11.8 +import org.apidesign.visitor.Language.Number;
11.9 +import org.apidesign.visitor.Language.Plus;
11.10 +import org.apidesign.visitor.Language.Visitor;
11.11 +import org.junit.Test;
11.12 +
11.13 +public class LanguageCheckTest {
11.14 +
11.15 + // BEGIN: visitor.language.check.exception
11.16 + private static class Valid1_0Language extends Visitor/*version1.0*/ {
11.17 + public void visitPlus(Plus s) {
11.18 + s.getFirst().visit(this);
11.19 + s.getSecond().visit(this);
11.20 + }
11.21 + public void visitNumber(Number n) {
11.22 + }
11.23 + }
11.24 +
11.25 + public static boolean isValid1_0Language(Expression expression) {
11.26 + Valid1_0Language valid = new Valid1_0Language();
11.27 + try {
11.28 + expression.visit(valid);
11.29 + return true; // yes, no unknown elements
11.30 + } catch (IllegalStateException ex) {
11.31 + return false; // no, probably from visitMinus of Visitor/*2.0*/
11.32 + }
11.33 + }
11.34 + // END: visitor.language.check.exception
11.35 +
11.36 + @Test public void printOnePlusOne() {
11.37 + Number one = new Number(1);
11.38 + Expression expression = new Plus(one, one);
11.39 +
11.40 + assertTrue("Valid language", isValid1_0Language(expression));
11.41 + }
11.42 +
11.43 + @Test public void printOnePlusTwoPlusThree() {
11.44 + Number one = new Number(1);
11.45 + Number two = new Number(2);
11.46 + Number three = new Number(3);
11.47 + Expression plus = new Plus(one, new Plus(two, three));
11.48 +
11.49 + assertTrue("Valid language", isValid1_0Language(plus));
11.50 + }
11.51 +}
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
12.2 +++ b/samples/visitor/02-abstractclass/src-test/org/apidesign/test/visitor/PrintTest.java Sat Jun 14 09:58:47 2008 +0200
12.3 @@ -0,0 +1,46 @@
12.4 +package org.apidesign.test.visitor;
12.5 +
12.6 +import static junit.framework.Assert.*;
12.7 +import org.apidesign.visitor.Language.Expression;
12.8 +import org.apidesign.visitor.Language.Number;
12.9 +import org.apidesign.visitor.Language.Plus;
12.10 +import org.apidesign.visitor.Language.Visitor;
12.11 +import org.junit.Test;
12.12 +
12.13 +public class PrintTest {
12.14 + public static class PrintVisitor extends Visitor {
12.15 + StringBuffer sb = new StringBuffer();
12.16 +
12.17 + public void visitPlus(Plus s) {
12.18 + s.getFirst().visit(this);
12.19 + sb.append(" + ");
12.20 + s.getSecond().visit(this);
12.21 + }
12.22 +
12.23 + public void visitNumber(Number n) {
12.24 + sb.append (n.getValue());
12.25 + }
12.26 + }
12.27 +
12.28 + @Test public void printOnePlusOne() {
12.29 + Number one = new Number(1);
12.30 + Expression plus = new Plus(one, one);
12.31 +
12.32 + PrintVisitor print = new PrintVisitor();
12.33 + plus.visit(print);
12.34 +
12.35 + assertEquals("1 + 1", print.sb.toString());
12.36 + }
12.37 +
12.38 + @Test public void printOnePlusTwoPlusThree() {
12.39 + Number one = new Number(1);
12.40 + Number two = new Number(2);
12.41 + Number three = new Number(3);
12.42 + Expression plus = new Plus(one, new Plus(two, three));
12.43 +
12.44 + PrintVisitor print = new PrintVisitor();
12.45 + plus.visit(print);
12.46 +
12.47 + assertEquals("1 + 2 + 3", print.sb.toString());
12.48 + }
12.49 +}
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
13.2 +++ b/samples/visitor/02-abstractclass/src-test2.0/org/apidesign/test/visitor/InvalidLanguageCheckTest.java Sat Jun 14 09:58:47 2008 +0200
13.3 @@ -0,0 +1,17 @@
13.4 +package org.apidesign.test.visitor;
13.5 +
13.6 +import static junit.framework.Assert.*;
13.7 +import org.apidesign.visitor.Language.Expression;
13.8 +import org.apidesign.visitor.Language.Minus;
13.9 +import org.apidesign.visitor.Language.Number;
13.10 +import org.junit.Test;
13.11 +
13.12 +public class InvalidLanguageCheckTest {
13.13 + @Test public void printOneMinusTwo() {
13.14 + Number one = new Number(1);
13.15 + Number two = new Number(2);
13.16 + Expression minus = new Minus(one, two);
13.17 +
13.18 + assertFalse("Recognized as invalid 1.0 version of the language", LanguageCheckTest.isValid1_0Language(minus));
13.19 + }
13.20 +}
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
14.2 +++ b/samples/visitor/02-abstractclass/src-test2.0/org/apidesign/test/visitor/PrintOfMinusStructureTest.java Sat Jun 14 09:58:47 2008 +0200
14.3 @@ -0,0 +1,23 @@
14.4 +package org.apidesign.test.visitor;
14.5 +
14.6 +import org.apidesign.test.visitor.PrintTest.PrintVisitor;
14.7 +import static junit.framework.Assert.*;
14.8 +import org.apidesign.visitor.Language.Expression;
14.9 +import org.apidesign.visitor.Language.Minus;
14.10 +import org.apidesign.visitor.Language.Number;
14.11 +import org.apidesign.visitor.Language.Plus;
14.12 +import org.apidesign.visitor.Language.Visitor;
14.13 +import org.junit.Test;
14.14 +
14.15 +public class PrintOfMinusStructureTest {
14.16 + @Test public void printOneMinusTwo() {
14.17 + Number one = new Number(1);
14.18 + Number two = new Number(2);
14.19 + Expression minus = new Minus(one, two);
14.20 +
14.21 + PrintVisitor print = new PrintVisitor();
14.22 + minus.visit(print); // fails with IllegalStateException
14.23 +
14.24 + assertEquals("1 - 2", print.sb.toString());
14.25 + }
14.26 +}
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
15.2 +++ b/samples/visitor/03-visitunknown/build.xml Sat Jun 14 09:58:47 2008 +0200
15.3 @@ -0,0 +1,94 @@
15.4 +<?xml version="1.0" encoding="UTF-8"?>
15.5 +<project name="Build Script" default="test" basedir=".">
15.6 + <target name="clean">
15.7 + <delete dir="build"/>
15.8 + </target>
15.9 +
15.10 + <target name="compile" depends="build"/>
15.11 + <target name="build" depends="-libraries">
15.12 + <antcall target="-build-one">
15.13 + <param name="version" value="api1.0"/>
15.14 + </antcall>
15.15 + <antcall target="-build-one">
15.16 + <param name="version" value="api2.0"/>
15.17 + </antcall>
15.18 +
15.19 + <antcall target="-build-one">
15.20 + <param name="version" value="test"/>
15.21 + <param name="cp" value="build/api1.0/classes:${junit.jar}"/>
15.22 + </antcall>
15.23 +
15.24 + <antcall target="-build-one">
15.25 + <param name="version" value="test"/>
15.26 + <param name="out" value="will-not-compile"/>
15.27 + <param name="cp" value="build/api2.0/classes:${junit.jar}"/>
15.28 + </antcall>
15.29 +
15.30 + <antcall target="-build-one">
15.31 + <param name="version" value="test2.0"/>
15.32 + <param name="cp" value="build/api2.0/classes:build/test/classes:${junit.jar}"/>
15.33 + </antcall>
15.34 + </target>
15.35 +
15.36 + <target name="test" depends="build">
15.37 + <echo level="info" message="PrintVisitor on old API. This should succeeds."/>
15.38 + <antcall target="-run-one">
15.39 + <param name="version" value="api1.0"/>
15.40 + </antcall>
15.41 + <echo level="info" message="PrintVisitor on old API. This would not compile, but it runs as it does not deal with minus at all."/>
15.42 + <antcall target="-run-one">
15.43 + <param name="version" value="api2.0"/>
15.44 + </antcall>
15.45 + <echo level="info" message="PrintVisitor on new API. This yields runtime error."/>
15.46 + <antcall target="-run-one">
15.47 + <param name="test" value="test2.0"/>
15.48 + <param name="extra.cp" value="build/test/classes"/>
15.49 + <param name="version" value="api2.0"/>
15.50 + </antcall>
15.51 + </target>
15.52 +
15.53 + <!-- support methods -->
15.54 +
15.55 + <target name="-libraries">
15.56 + <ant dir="../../libs/"/>
15.57 +
15.58 + <property name="junit.jar" location="../../libs/dist/junit-4.4.jar"/>
15.59 + </target>
15.60 +
15.61 + <target name="-run-one">
15.62 + <fail message="You need to specify API version number" unless="version"/>
15.63 + <property name="test" value="test"/>
15.64 + <mkdir dir="build/${test}results${version}"/>
15.65 + <junit dir="build/test/classes" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" showoutput="true">
15.66 + <batchtest todir="build/${test}results${version}">
15.67 + <fileset dir="build/${test}/classes">
15.68 + <filename name="**/*Test.class"/>
15.69 + </fileset>
15.70 + </batchtest>
15.71 + <classpath>
15.72 + <path location="build/${version}/classes"/>
15.73 + <path location="build/${test}/classes"/>
15.74 + <path location="${junit.jar}"/>
15.75 + <path path="${extra.cp}"/>
15.76 + </classpath>
15.77 + <formatter type="brief" usefile="false"/>
15.78 + <formatter type="xml"/>
15.79 + </junit>
15.80 + </target>
15.81 +
15.82 + <target name="-build-one">
15.83 + <fail message="You need to specify version number" unless="version"/>
15.84 +
15.85 + <property name="cp" value=""/>
15.86 + <property name="out" value="${version}"/>
15.87 + <property name="failonerror" value="true"/>
15.88 + <mkdir dir="build/${out}/classes"/>
15.89 + <javac
15.90 + srcdir="src-${version}"
15.91 + destdir="build/${out}/classes"
15.92 + source="1.5" target="1.5"
15.93 + classpath="${cp}"
15.94 + failonerror="${failonerror}"
15.95 + />
15.96 + </target>
15.97 +</project>
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
16.2 +++ b/samples/visitor/03-visitunknown/nbproject/project.xml Sat Jun 14 09:58:47 2008 +0200
16.3 @@ -0,0 +1,107 @@
16.4 +<?xml version="1.0" encoding="UTF-8"?>
16.5 +<project xmlns="http://www.netbeans.org/ns/project/1" xmlns:ns4="null">
16.6 + <type>org.netbeans.modules.ant.freeform</type>
16.7 + <configuration>
16.8 + <general-data xmlns="http://www.netbeans.org/ns/freeform-project/1">
16.9 + <name>visitunknown</name>
16.10 + </general-data>
16.11 + <general-data xmlns="http://www.netbeans.org/ns/freeform-project/2">
16.12 + <!-- Do not use Project Properties customizer when editing this file manually. -->
16.13 + <name>visitunknown</name>
16.14 + <properties/>
16.15 + <folders>
16.16 + <source-folder>
16.17 + <label>src-api1.0</label>
16.18 + <type>java</type>
16.19 + <location>src-api1.0</location>
16.20 + <encoding>UTF-8</encoding>
16.21 + </source-folder>
16.22 + <source-folder>
16.23 + <label>src-api2.0</label>
16.24 + <type>java</type>
16.25 + <location>src-api2.0</location>
16.26 + <encoding>UTF-8</encoding>
16.27 + </source-folder>
16.28 + <source-folder>
16.29 + <label>test</label>
16.30 + <type>java</type>
16.31 + <location>src-test</location>
16.32 + <encoding>UTF-8</encoding>
16.33 + </source-folder>
16.34 + <source-folder>
16.35 + <label>test-2.0</label>
16.36 + <type>java</type>
16.37 + <location>src-test2.0</location>
16.38 + <encoding>UTF-8</encoding>
16.39 + </source-folder>
16.40 + </folders>
16.41 + <ide-actions>
16.42 + <action name="build">
16.43 + <target>build</target>
16.44 + </action>
16.45 + <action name="clean">
16.46 + <target>clean</target>
16.47 + </action>
16.48 + <action name="test">
16.49 + <target>test</target>
16.50 + </action>
16.51 + <action name="run">
16.52 + <target>test</target>
16.53 + </action>
16.54 + <action name="rebuild">
16.55 + <target>clean</target>
16.56 + <target>build</target>
16.57 + </action>
16.58 + </ide-actions>
16.59 + <view>
16.60 + <items>
16.61 + <source-folder style="packages">
16.62 + <label>API Version 1.0</label>
16.63 + <location>src-api1.0</location>
16.64 + </source-folder>
16.65 + <source-folder style="packages">
16.66 + <label>API Version 2.0</label>
16.67 + <location>src-api2.0</location>
16.68 + </source-folder>
16.69 + <source-folder style="packages">
16.70 + <label>Usage of the API</label>
16.71 + <location>src-test</location>
16.72 + </source-folder>
16.73 + <source-folder style="packages">
16.74 + <label>Usage of new structures with old visitor</label>
16.75 + <location>src-test2.0</location>
16.76 + </source-folder>
16.77 + <source-file>
16.78 + <location>build.xml</location>
16.79 + </source-file>
16.80 + </items>
16.81 + <context-menu>
16.82 + <ide-action name="build"/>
16.83 + <ide-action name="rebuild"/>
16.84 + <ide-action name="clean"/>
16.85 + <ide-action name="test"/>
16.86 + </context-menu>
16.87 + </view>
16.88 + </general-data>
16.89 + <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/1">
16.90 + <compilation-unit>
16.91 + <package-root>src-api1.0</package-root>
16.92 + <source-level>1.5</source-level>
16.93 + </compilation-unit>
16.94 + <compilation-unit>
16.95 + <package-root>src-api2.0</package-root>
16.96 + <source-level>1.5</source-level>
16.97 + </compilation-unit>
16.98 + <compilation-unit>
16.99 + <package-root>src-test</package-root>
16.100 + <classpath mode="compile">src-api1.0:../../libs/dist/junit-4.4.jar</classpath>
16.101 + <source-level>1.5</source-level>
16.102 + </compilation-unit>
16.103 + <compilation-unit>
16.104 + <package-root>src-test2.0</package-root>
16.105 + <classpath mode="compile">src-api2.0:src-test:../../libs/dist/junit-4.4.jar</classpath>
16.106 + <source-level>1.5</source-level>
16.107 + </compilation-unit>
16.108 + </java-data>
16.109 + </configuration>
16.110 +</project>
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
17.2 +++ b/samples/visitor/03-visitunknown/src-api1.0/org/apidesign/visitor/Language.java Sat Jun 14 09:58:47 2008 +0200
17.3 @@ -0,0 +1,39 @@
17.4 +package org.apidesign.visitor;
17.5 +
17.6 +public final class Language {
17.7 + private Language() { }
17.8 +
17.9 + public static abstract class Expression {
17.10 + public abstract void visit(Visitor v);
17.11 + }
17.12 + public static final class Plus extends Expression {
17.13 + private final Expression first;
17.14 + private final Expression second;
17.15 +
17.16 + public Plus(Expression first, Expression second) {
17.17 + this.first = first;
17.18 + this.second = second;
17.19 + }
17.20 + public Expression getFirst() { return first; }
17.21 + public Expression getSecond() { return second; }
17.22 + @Override
17.23 + public void visit(Visitor v) { v.visitPlus(this); }
17.24 + }
17.25 + public static final class Number extends Expression {
17.26 + private final int value;
17.27 + public Number(int value) { this.value = value; }
17.28 + public int getValue() { return value; }
17.29 + @Override
17.30 + public void visit(Visitor v) { v.visitNumber(this); }
17.31 + }
17.32 +
17.33 + // BEGIN: visitor.visitunknown.v1
17.34 + public static abstract class Visitor/*1.0*/ {
17.35 + public void visitUnknown(Expression exp) {
17.36 + throw new IllegalStateException("Unknown element faced: " + exp);
17.37 + }
17.38 + public abstract void visitPlus(Plus s);
17.39 + public abstract void visitNumber(Number n);
17.40 + }
17.41 + // END: visitor.visitunknown.v1
17.42 +}
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
18.2 +++ b/samples/visitor/03-visitunknown/src-api2.0/org/apidesign/visitor/Language.java Sat Jun 14 09:58:47 2008 +0200
18.3 @@ -0,0 +1,57 @@
18.4 +package org.apidesign.visitor;
18.5 +
18.6 +public final class Language {
18.7 + private Language() { }
18.8 +
18.9 + public static abstract class Expression {
18.10 + public abstract void visit(Visitor v);
18.11 + }
18.12 + public static final class Plus extends Expression {
18.13 + private final Expression first;
18.14 + private final Expression second;
18.15 +
18.16 + public Plus(Expression first, Expression second) {
18.17 + this.first = first;
18.18 + this.second = second;
18.19 + }
18.20 + public Expression getFirst() { return first; }
18.21 + public Expression getSecond() { return second; }
18.22 + @Override
18.23 + public void visit(Visitor v) { v.visitPlus(this); }
18.24 + }
18.25 + public static final class Number extends Expression {
18.26 + private final int value;
18.27 + public Number(int value) { this.value = value; }
18.28 + public int getValue() { return value; }
18.29 + @Override
18.30 + public void visit(Visitor v) { v.visitNumber(this); }
18.31 + }
18.32 + /** @since 2.0 */
18.33 + public static final class Minus extends Expression {
18.34 + private final Expression first;
18.35 + private final Expression second;
18.36 +
18.37 + public Minus(Expression first, Expression second) {
18.38 + this.first = first;
18.39 + this.second = second;
18.40 + }
18.41 + public Expression getFirst() { return first; }
18.42 + public Expression getSecond() { return second; }
18.43 + public void visit(Visitor v) {
18.44 + v.visitMinus(this);
18.45 + }
18.46 + }
18.47 + // BEGIN: visitor.visitunknown.v2
18.48 + public static abstract class Visitor/*2.0*/ {
18.49 + public void visitUnknown(Expression exp) {
18.50 + throw new IllegalStateException("Unknown element faced: " + exp);
18.51 + }
18.52 + public abstract void visitPlus(Plus s);
18.53 + public abstract void visitNumber(Number n);
18.54 + /** @since 2.0 */
18.55 + public void visitMinus(Minus s) {
18.56 + visitUnknown(s);
18.57 + }
18.58 + }
18.59 + // END: visitor.visitunknown.v2
18.60 +}
19.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
19.2 +++ b/samples/visitor/03-visitunknown/src-test/org/apidesign/test/visitor/CountNumbersTest.java Sat Jun 14 09:58:47 2008 +0200
19.3 @@ -0,0 +1,51 @@
19.4 +package org.apidesign.test.visitor;
19.5 +
19.6 +import static junit.framework.Assert.*;
19.7 +import org.apidesign.visitor.Language.Expression;
19.8 +import org.apidesign.visitor.Language.Number;
19.9 +import org.apidesign.visitor.Language.Plus;
19.10 +import org.apidesign.visitor.Language.Visitor;
19.11 +import org.junit.Test;
19.12 +
19.13 +public class CountNumbersTest {
19.14 +
19.15 + // BEGIN: visitor.count.numbers.visitunknown
19.16 + private static class CountNumbers extends Visitor/*version1.0*/ {
19.17 + int cnt;
19.18 +
19.19 + @Override
19.20 + public void visitUnknown(Expression exp) {
19.21 + // not a number
19.22 + }
19.23 + public void visitPlus(Plus s) {
19.24 + s.getFirst().visit(this);
19.25 + s.getSecond().visit(this);
19.26 + }
19.27 + public void visitNumber(Number n) {
19.28 + cnt++;
19.29 + }
19.30 + }
19.31 +
19.32 + public static int countNumbers(Expression expression) {
19.33 + CountNumbers counter = new CountNumbers();
19.34 + expression.visit(counter);
19.35 + return counter.cnt;
19.36 + }
19.37 + // END: visitor.count.numbers.visitunknown
19.38 +
19.39 + @Test public void printOnePlusOne() {
19.40 + Number one = new Number(1);
19.41 + Expression expression = new Plus(one, one);
19.42 +
19.43 + assertEquals("Two 1's", 2, countNumbers(expression));
19.44 + }
19.45 +
19.46 + @Test public void printOnePlusTwoPlusThree() {
19.47 + Number one = new Number(1);
19.48 + Number two = new Number(2);
19.49 + Number three = new Number(3);
19.50 + Expression plus = new Plus(one, new Plus(two, three));
19.51 +
19.52 + assertEquals("Three", 3, countNumbers(plus));
19.53 + }
19.54 +}
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
20.2 +++ b/samples/visitor/03-visitunknown/src-test/org/apidesign/test/visitor/LanguageCheckTest.java Sat Jun 14 09:58:47 2008 +0200
20.3 @@ -0,0 +1,50 @@
20.4 +package org.apidesign.test.visitor;
20.5 +
20.6 +import static junit.framework.Assert.*;
20.7 +import org.apidesign.visitor.Language.Expression;
20.8 +import org.apidesign.visitor.Language.Number;
20.9 +import org.apidesign.visitor.Language.Plus;
20.10 +import org.apidesign.visitor.Language.Visitor;
20.11 +import org.junit.Test;
20.12 +
20.13 +public class LanguageCheckTest {
20.14 +
20.15 + // BEGIN: visitor.language.check.visitunknown
20.16 + private static class Valid1_0Language extends Visitor/*version1.0*/ {
20.17 + boolean invalid;
20.18 +
20.19 + @Override
20.20 + public void visitUnknown(Expression exp) {
20.21 + invalid = true;
20.22 + }
20.23 + public void visitPlus(Plus s) {
20.24 + s.getFirst().visit(this);
20.25 + s.getSecond().visit(this);
20.26 + }
20.27 + public void visitNumber(Number n) {
20.28 + }
20.29 + }
20.30 +
20.31 + public static boolean isValid1_0Language(Expression expression) {
20.32 + Valid1_0Language valid = new Valid1_0Language();
20.33 + expression.visit(valid);
20.34 + return !valid.invalid;
20.35 + }
20.36 + // END: visitor.language.check.visitunknown
20.37 +
20.38 + @Test public void printOnePlusOne() {
20.39 + Number one = new Number(1);
20.40 + Expression expression = new Plus(one, one);
20.41 +
20.42 + assertTrue("Valid language", isValid1_0Language(expression));
20.43 + }
20.44 +
20.45 + @Test public void printOnePlusTwoPlusThree() {
20.46 + Number one = new Number(1);
20.47 + Number two = new Number(2);
20.48 + Number three = new Number(3);
20.49 + Expression plus = new Plus(one, new Plus(two, three));
20.50 +
20.51 + assertTrue("Valid language", isValid1_0Language(plus));
20.52 + }
20.53 +}
21.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
21.2 +++ b/samples/visitor/03-visitunknown/src-test/org/apidesign/test/visitor/PrintTest.java Sat Jun 14 09:58:47 2008 +0200
21.3 @@ -0,0 +1,51 @@
21.4 +package org.apidesign.test.visitor;
21.5 +
21.6 +import static junit.framework.Assert.*;
21.7 +import org.apidesign.visitor.Language.Expression;
21.8 +import org.apidesign.visitor.Language.Number;
21.9 +import org.apidesign.visitor.Language.Plus;
21.10 +import org.apidesign.visitor.Language.Visitor;
21.11 +import org.junit.Test;
21.12 +
21.13 +public class PrintTest {
21.14 + public static class PrintVisitor extends Visitor {
21.15 + StringBuffer sb = new StringBuffer();
21.16 +
21.17 + @Override
21.18 + public void visitUnknown(Expression exp) {
21.19 + sb.append("unknown");
21.20 + }
21.21 +
21.22 + public void visitPlus(Plus s) {
21.23 + s.getFirst().visit(this);
21.24 + sb.append(" + ");
21.25 + s.getSecond().visit(this);
21.26 + }
21.27 +
21.28 + public void visitNumber(Number n) {
21.29 + sb.append (n.getValue());
21.30 + }
21.31 + }
21.32 +
21.33 + @Test public void printOnePlusOne() {
21.34 + Number one = new Number(1);
21.35 + Expression plus = new Plus(one, one);
21.36 +
21.37 + PrintVisitor print = new PrintVisitor();
21.38 + plus.visit(print);
21.39 +
21.40 + assertEquals("1 + 1", print.sb.toString());
21.41 + }
21.42 +
21.43 + @Test public void printOnePlusTwoPlusThree() {
21.44 + Number one = new Number(1);
21.45 + Number two = new Number(2);
21.46 + Number three = new Number(3);
21.47 + Expression plus = new Plus(one, new Plus(two, three));
21.48 +
21.49 + PrintVisitor print = new PrintVisitor();
21.50 + plus.visit(print);
21.51 +
21.52 + assertEquals("1 + 2 + 3", print.sb.toString());
21.53 + }
21.54 +}
22.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
22.2 +++ b/samples/visitor/03-visitunknown/src-test2.0/org/apidesign/test/visitor/InvalidCountNumbersTest.java Sat Jun 14 09:58:47 2008 +0200
22.3 @@ -0,0 +1,25 @@
22.4 +package org.apidesign.test.visitor;
22.5 +
22.6 +import static junit.framework.Assert.*;
22.7 +import org.apidesign.visitor.Language.Expression;
22.8 +import org.apidesign.visitor.Language.Minus;
22.9 +import org.apidesign.visitor.Language.Number;
22.10 +import org.apidesign.visitor.Language.Plus;
22.11 +import org.junit.Test;
22.12 +
22.13 +public class InvalidCountNumbersTest {
22.14 + @Test public void printOneMinusTwo() {
22.15 + // BEGIN: visitor.visitunknown.traversal
22.16 + Number one = new Number(1);
22.17 + Number three = new Number(3);
22.18 + Number four = new Number(4);
22.19 + Expression minus = new Plus(one, new Minus(three, four));
22.20 +
22.21 + assertEquals(
22.22 + "Should have three numbers, but visitor does not " +
22.23 + "know how to go through minus",
22.24 + 3, CountNumbersTest.countNumbers(minus)
22.25 + );
22.26 + // END: visitor.visitunknown.traversal
22.27 + }
22.28 +}
23.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
23.2 +++ b/samples/visitor/03-visitunknown/src-test2.0/org/apidesign/test/visitor/InvalidLanguageCheckTest.java Sat Jun 14 09:58:47 2008 +0200
23.3 @@ -0,0 +1,17 @@
23.4 +package org.apidesign.test.visitor;
23.5 +
23.6 +import static junit.framework.Assert.*;
23.7 +import org.apidesign.visitor.Language.Expression;
23.8 +import org.apidesign.visitor.Language.Minus;
23.9 +import org.apidesign.visitor.Language.Number;
23.10 +import org.junit.Test;
23.11 +
23.12 +public class InvalidLanguageCheckTest {
23.13 + @Test public void printOneMinusTwo() {
23.14 + Number one = new Number(1);
23.15 + Number two = new Number(2);
23.16 + Expression minus = new Minus(one, two);
23.17 +
23.18 + assertFalse("Recognized as invalid 1.0 version of the language", LanguageCheckTest.isValid1_0Language(minus));
23.19 + }
23.20 +}
24.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
24.2 +++ b/samples/visitor/03-visitunknown/src-test2.0/org/apidesign/test/visitor/PrintOfMinusStructureTest.java Sat Jun 14 09:58:47 2008 +0200
24.3 @@ -0,0 +1,23 @@
24.4 +package org.apidesign.test.visitor;
24.5 +
24.6 +import org.apidesign.test.visitor.PrintTest.PrintVisitor;
24.7 +import static junit.framework.Assert.*;
24.8 +import org.apidesign.visitor.Language.Expression;
24.9 +import org.apidesign.visitor.Language.Minus;
24.10 +import org.apidesign.visitor.Language.Number;
24.11 +import org.apidesign.visitor.Language.Plus;
24.12 +import org.apidesign.visitor.Language.Visitor;
24.13 +import org.junit.Test;
24.14 +
24.15 +public class PrintOfMinusStructureTest {
24.16 + @Test public void printOneMinusTwo() {
24.17 + Number one = new Number(1);
24.18 + Number two = new Number(2);
24.19 + Expression minus = new Minus(one, two);
24.20 +
24.21 + PrintVisitor print = new PrintVisitor();
24.22 + minus.visit(print); // fails with IllegalStateException
24.23 +
24.24 + assertEquals("1 - 2", print.sb.toString());
24.25 + }
24.26 +}
25.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
25.2 +++ b/samples/visitor/04-traversal/build.xml Sat Jun 14 09:58:47 2008 +0200
25.3 @@ -0,0 +1,94 @@
25.4 +<?xml version="1.0" encoding="UTF-8"?>
25.5 +<project name="Build Script" default="test" basedir=".">
25.6 + <target name="clean">
25.7 + <delete dir="build"/>
25.8 + </target>
25.9 +
25.10 + <target name="compile" depends="build"/>
25.11 + <target name="build" depends="-libraries">
25.12 + <antcall target="-build-one">
25.13 + <param name="version" value="api1.0"/>
25.14 + </antcall>
25.15 + <antcall target="-build-one">
25.16 + <param name="version" value="api2.0"/>
25.17 + </antcall>
25.18 +
25.19 + <antcall target="-build-one">
25.20 + <param name="version" value="test"/>
25.21 + <param name="cp" value="build/api1.0/classes:${junit.jar}"/>
25.22 + </antcall>
25.23 +
25.24 + <antcall target="-build-one">
25.25 + <param name="version" value="test"/>
25.26 + <param name="out" value="will-not-compile"/>
25.27 + <param name="cp" value="build/api2.0/classes:${junit.jar}"/>
25.28 + </antcall>
25.29 +
25.30 + <antcall target="-build-one">
25.31 + <param name="version" value="test2.0"/>
25.32 + <param name="cp" value="build/api2.0/classes:build/test/classes:${junit.jar}"/>
25.33 + </antcall>
25.34 + </target>
25.35 +
25.36 + <target name="test" depends="build">
25.37 + <echo level="info" message="PrintVisitor on old API. This should succeeds."/>
25.38 + <antcall target="-run-one">
25.39 + <param name="version" value="api1.0"/>
25.40 + </antcall>
25.41 + <echo level="info" message="PrintVisitor on old API. This would not compile, but it runs as it does not deal with minus at all."/>
25.42 + <antcall target="-run-one">
25.43 + <param name="version" value="api2.0"/>
25.44 + </antcall>
25.45 + <echo level="info" message="PrintVisitor on new API. This yields runtime error."/>
25.46 + <antcall target="-run-one">
25.47 + <param name="test" value="test2.0"/>
25.48 + <param name="extra.cp" value="build/test/classes"/>
25.49 + <param name="version" value="api2.0"/>
25.50 + </antcall>
25.51 + </target>
25.52 +
25.53 + <!-- support methods -->
25.54 +
25.55 + <target name="-libraries">
25.56 + <ant dir="../../libs/"/>
25.57 +
25.58 + <property name="junit.jar" location="../../libs/dist/junit-4.4.jar"/>
25.59 + </target>
25.60 +
25.61 + <target name="-run-one">
25.62 + <fail message="You need to specify API version number" unless="version"/>
25.63 + <property name="test" value="test"/>
25.64 + <mkdir dir="build/${test}results${version}"/>
25.65 + <junit dir="build/test/classes" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" showoutput="true">
25.66 + <batchtest todir="build/${test}results${version}">
25.67 + <fileset dir="build/${test}/classes">
25.68 + <filename name="**/*Test.class"/>
25.69 + </fileset>
25.70 + </batchtest>
25.71 + <classpath>
25.72 + <path location="build/${version}/classes"/>
25.73 + <path location="build/${test}/classes"/>
25.74 + <path location="${junit.jar}"/>
25.75 + <path path="${extra.cp}"/>
25.76 + </classpath>
25.77 + <formatter type="brief" usefile="false"/>
25.78 + <formatter type="xml"/>
25.79 + </junit>
25.80 + </target>
25.81 +
25.82 + <target name="-build-one">
25.83 + <fail message="You need to specify version number" unless="version"/>
25.84 +
25.85 + <property name="cp" value=""/>
25.86 + <property name="out" value="${version}"/>
25.87 + <property name="failonerror" value="true"/>
25.88 + <mkdir dir="build/${out}/classes"/>
25.89 + <javac
25.90 + srcdir="src-${version}"
25.91 + destdir="build/${out}/classes"
25.92 + source="1.5" target="1.5"
25.93 + classpath="${cp}"
25.94 + failonerror="${failonerror}"
25.95 + />
25.96 + </target>
25.97 +</project>
26.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
26.2 +++ b/samples/visitor/04-traversal/nbproject/project.xml Sat Jun 14 09:58:47 2008 +0200
26.3 @@ -0,0 +1,107 @@
26.4 +<?xml version="1.0" encoding="UTF-8"?>
26.5 +<project xmlns="http://www.netbeans.org/ns/project/1" xmlns:ns4="null">
26.6 + <type>org.netbeans.modules.ant.freeform</type>
26.7 + <configuration>
26.8 + <general-data xmlns="http://www.netbeans.org/ns/freeform-project/1">
26.9 + <name>traversal</name>
26.10 + </general-data>
26.11 + <general-data xmlns="http://www.netbeans.org/ns/freeform-project/2">
26.12 + <!-- Do not use Project Properties customizer when editing this file manually. -->
26.13 + <name>traversal</name>
26.14 + <properties/>
26.15 + <folders>
26.16 + <source-folder>
26.17 + <label>src-api1.0</label>
26.18 + <type>java</type>
26.19 + <location>src-api1.0</location>
26.20 + <encoding>UTF-8</encoding>
26.21 + </source-folder>
26.22 + <source-folder>
26.23 + <label>src-api2.0</label>
26.24 + <type>java</type>
26.25 + <location>src-api2.0</location>
26.26 + <encoding>UTF-8</encoding>
26.27 + </source-folder>
26.28 + <source-folder>
26.29 + <label>test</label>
26.30 + <type>java</type>
26.31 + <location>src-test</location>
26.32 + <encoding>UTF-8</encoding>
26.33 + </source-folder>
26.34 + <source-folder>
26.35 + <label>test-2.0</label>
26.36 + <type>java</type>
26.37 + <location>src-test2.0</location>
26.38 + <encoding>UTF-8</encoding>
26.39 + </source-folder>
26.40 + </folders>
26.41 + <ide-actions>
26.42 + <action name="build">
26.43 + <target>build</target>
26.44 + </action>
26.45 + <action name="clean">
26.46 + <target>clean</target>
26.47 + </action>
26.48 + <action name="test">
26.49 + <target>test</target>
26.50 + </action>
26.51 + <action name="run">
26.52 + <target>test</target>
26.53 + </action>
26.54 + <action name="rebuild">
26.55 + <target>clean</target>
26.56 + <target>build</target>
26.57 + </action>
26.58 + </ide-actions>
26.59 + <view>
26.60 + <items>
26.61 + <source-folder style="packages">
26.62 + <label>API Version 1.0</label>
26.63 + <location>src-api1.0</location>
26.64 + </source-folder>
26.65 + <source-folder style="packages">
26.66 + <label>API Version 2.0</label>
26.67 + <location>src-api2.0</location>
26.68 + </source-folder>
26.69 + <source-folder style="packages">
26.70 + <label>Usage of the API</label>
26.71 + <location>src-test</location>
26.72 + </source-folder>
26.73 + <source-folder style="packages">
26.74 + <label>Usage of new structures with old visitor</label>
26.75 + <location>src-test2.0</location>
26.76 + </source-folder>
26.77 + <source-file>
26.78 + <location>build.xml</location>
26.79 + </source-file>
26.80 + </items>
26.81 + <context-menu>
26.82 + <ide-action name="build"/>
26.83 + <ide-action name="rebuild"/>
26.84 + <ide-action name="clean"/>
26.85 + <ide-action name="test"/>
26.86 + </context-menu>
26.87 + </view>
26.88 + </general-data>
26.89 + <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/1">
26.90 + <compilation-unit>
26.91 + <package-root>src-api1.0</package-root>
26.92 + <source-level>1.5</source-level>
26.93 + </compilation-unit>
26.94 + <compilation-unit>
26.95 + <package-root>src-api2.0</package-root>
26.96 + <source-level>1.5</source-level>
26.97 + </compilation-unit>
26.98 + <compilation-unit>
26.99 + <package-root>src-test</package-root>
26.100 + <classpath mode="compile">src-api1.0:../../libs/dist/junit-4.4.jar</classpath>
26.101 + <source-level>1.5</source-level>
26.102 + </compilation-unit>
26.103 + <compilation-unit>
26.104 + <package-root>src-test2.0</package-root>
26.105 + <classpath mode="compile">src-api2.0:src-test:../../libs/dist/junit-4.4.jar</classpath>
26.106 + <source-level>1.5</source-level>
26.107 + </compilation-unit>
26.108 + </java-data>
26.109 + </configuration>
26.110 +</project>
27.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
27.2 +++ b/samples/visitor/04-traversal/src-api1.0/org/apidesign/visitor/Language.java Sat Jun 14 09:58:47 2008 +0200
27.3 @@ -0,0 +1,46 @@
27.4 +package org.apidesign.visitor;
27.5 +
27.6 +public final class Language {
27.7 + private Language() { }
27.8 +
27.9 + public static abstract class Expression {
27.10 + public abstract void visit(Visitor v);
27.11 + }
27.12 + public static final class Plus extends Expression {
27.13 + private final Expression first;
27.14 + private final Expression second;
27.15 +
27.16 + public Plus(Expression first, Expression second) {
27.17 + this.first = first;
27.18 + this.second = second;
27.19 + }
27.20 + public Expression getFirst() { return first; }
27.21 + public Expression getSecond() { return second; }
27.22 + @Override
27.23 + public void visit(Visitor v) { v.visitPlus(this); }
27.24 + }
27.25 + public static final class Number extends Expression {
27.26 + private final int value;
27.27 + public Number(int value) { this.value = value; }
27.28 + public int getValue() { return value; }
27.29 + @Override
27.30 + public void visit(Visitor v) { v.visitNumber(this); }
27.31 + }
27.32 +
27.33 + // BEGIN: visitor.traversal.v1
27.34 + public static abstract class Visitor/*1.0*/ {
27.35 + public boolean visitUnknown(Expression e) {
27.36 + throw new IllegalStateException("Unknown element faced: " + e);
27.37 + }
27.38 + public void visitPlus(Plus s) {
27.39 + if (visitUnknown(s)) {
27.40 + s.getFirst().visit(this);
27.41 + s.getSecond().visit(this);
27.42 + }
27.43 + }
27.44 + public void visitNumber(Number n) {
27.45 + visitUnknown(n);
27.46 + }
27.47 + }
27.48 + // END: visitor.traversal.v1
27.49 +}
28.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
28.2 +++ b/samples/visitor/04-traversal/src-api2.0/org/apidesign/visitor/Language.java Sat Jun 14 09:58:47 2008 +0200
28.3 @@ -0,0 +1,67 @@
28.4 +package org.apidesign.visitor;
28.5 +
28.6 +public final class Language {
28.7 + private Language() { }
28.8 +
28.9 + public static abstract class Expression {
28.10 + public abstract void visit(Visitor v);
28.11 + }
28.12 + public static final class Plus extends Expression {
28.13 + private final Expression first;
28.14 + private final Expression second;
28.15 +
28.16 + public Plus(Expression first, Expression second) {
28.17 + this.first = first;
28.18 + this.second = second;
28.19 + }
28.20 + public Expression getFirst() { return first; }
28.21 + public Expression getSecond() { return second; }
28.22 + @Override
28.23 + public void visit(Visitor v) { v.visitPlus(this); }
28.24 + }
28.25 + public static final class Number extends Expression {
28.26 + private final int value;
28.27 + public Number(int value) { this.value = value; }
28.28 + public int getValue() { return value; }
28.29 + @Override
28.30 + public void visit(Visitor v) { v.visitNumber(this); }
28.31 + }
28.32 + /** @since 2.0 */
28.33 + public static final class Minus extends Expression {
28.34 + private final Expression first;
28.35 + private final Expression second;
28.36 +
28.37 + public Minus(Expression first, Expression second) {
28.38 + this.first = first;
28.39 + this.second = second;
28.40 + }
28.41 + public Expression getFirst() { return first; }
28.42 + public Expression getSecond() { return second; }
28.43 + public void visit(Visitor v) {
28.44 + v.visitMinus(this);
28.45 + }
28.46 + }
28.47 +
28.48 + // BEGIN: visitor.traversal.v2
28.49 + public static abstract class Visitor/*2.0*/ {
28.50 + public boolean visitUnknown(Expression e) {
28.51 + throw new IllegalStateException("Unknown element faced: " + e);
28.52 + }
28.53 + public void visitPlus(Plus s) {
28.54 + if (visitUnknown(s)) {
28.55 + s.getFirst().visit(this);
28.56 + s.getSecond().visit(this);
28.57 + }
28.58 + }
28.59 + public void visitMinus(Minus s) {
28.60 + if (visitUnknown(s)) {
28.61 + s.getFirst().visit(this);
28.62 + s.getSecond().visit(this);
28.63 + }
28.64 + }
28.65 + public void visitNumber(Number n) {
28.66 + visitUnknown(n);
28.67 + }
28.68 + }
28.69 + // END: visitor.traversal.v2
28.70 +}
29.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
29.2 +++ b/samples/visitor/04-traversal/src-test/org/apidesign/test/visitor/CountNumbersTest.java Sat Jun 14 09:58:47 2008 +0200
29.3 @@ -0,0 +1,51 @@
29.4 +package org.apidesign.test.visitor;
29.5 +
29.6 +import static junit.framework.Assert.*;
29.7 +import org.apidesign.visitor.Language.Expression;
29.8 +import org.apidesign.visitor.Language.Number;
29.9 +import org.apidesign.visitor.Language.Plus;
29.10 +import org.apidesign.visitor.Language.Visitor;
29.11 +import org.junit.Test;
29.12 +
29.13 +public class CountNumbersTest {
29.14 +
29.15 + // BEGIN: visitor.count.numbers.traversal
29.16 + private static class CountNumbers extends Visitor/*version1.0*/ {
29.17 + int cnt;
29.18 +
29.19 + @Override
29.20 + public boolean visitUnknown(Expression exp) {
29.21 + return true;
29.22 + }
29.23 + public void visitPlus(Plus s) {
29.24 + s.getFirst().visit(this);
29.25 + s.getSecond().visit(this);
29.26 + }
29.27 + public void visitNumber(Number n) {
29.28 + cnt++;
29.29 + }
29.30 + }
29.31 +
29.32 + public static int countNumbers(Expression expression) {
29.33 + CountNumbers counter = new CountNumbers();
29.34 + expression.visit(counter);
29.35 + return counter.cnt;
29.36 + }
29.37 + // END: visitor.count.numbers.traversal
29.38 +
29.39 + @Test public void printOnePlusOne() {
29.40 + Number one = new Number(1);
29.41 + Expression expression = new Plus(one, one);
29.42 +
29.43 + assertEquals("Two 1's", 2, countNumbers(expression));
29.44 + }
29.45 +
29.46 + @Test public void printOnePlusTwoPlusThree() {
29.47 + Number one = new Number(1);
29.48 + Number two = new Number(2);
29.49 + Number three = new Number(3);
29.50 + Expression plus = new Plus(one, new Plus(two, three));
29.51 +
29.52 + assertEquals("Three", 3, countNumbers(plus));
29.53 + }
29.54 +}
30.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
30.2 +++ b/samples/visitor/04-traversal/src-test/org/apidesign/test/visitor/LanguageCheckTest.java Sat Jun 14 09:58:47 2008 +0200
30.3 @@ -0,0 +1,49 @@
30.4 +package org.apidesign.test.visitor;
30.5 +
30.6 +import static junit.framework.Assert.*;
30.7 +import org.apidesign.visitor.Language.Expression;
30.8 +import org.apidesign.visitor.Language.Number;
30.9 +import org.apidesign.visitor.Language.Plus;
30.10 +import org.apidesign.visitor.Language.Visitor;
30.11 +import org.junit.Test;
30.12 +
30.13 +public class LanguageCheckTest {
30.14 +
30.15 + private static class Valid1_0Language extends Visitor/*version1.0*/ {
30.16 + boolean invalid;
30.17 +
30.18 + @Override
30.19 + public boolean visitUnknown(Expression exp) {
30.20 + invalid = true;
30.21 + return false;
30.22 + }
30.23 + public void visitPlus(Plus s) {
30.24 + s.getFirst().visit(this);
30.25 + s.getSecond().visit(this);
30.26 + }
30.27 + public void visitNumber(Number n) {
30.28 + }
30.29 + }
30.30 +
30.31 + public static boolean isValid1_0Language(Expression expression) {
30.32 + Valid1_0Language valid = new Valid1_0Language();
30.33 + expression.visit(valid);
30.34 + return !valid.invalid;
30.35 + }
30.36 +
30.37 + @Test public void printOnePlusOne() {
30.38 + Number one = new Number(1);
30.39 + Expression expression = new Plus(one, one);
30.40 +
30.41 + assertTrue("Valid language", isValid1_0Language(expression));
30.42 + }
30.43 +
30.44 + @Test public void printOnePlusTwoPlusThree() {
30.45 + Number one = new Number(1);
30.46 + Number two = new Number(2);
30.47 + Number three = new Number(3);
30.48 + Expression plus = new Plus(one, new Plus(two, three));
30.49 +
30.50 + assertTrue("Valid language", isValid1_0Language(plus));
30.51 + }
30.52 +}
31.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
31.2 +++ b/samples/visitor/04-traversal/src-test/org/apidesign/test/visitor/PrintTest.java Sat Jun 14 09:58:47 2008 +0200
31.3 @@ -0,0 +1,51 @@
31.4 +package org.apidesign.test.visitor;
31.5 +
31.6 +import static junit.framework.Assert.*;
31.7 +import org.apidesign.visitor.Language.Expression;
31.8 +import org.apidesign.visitor.Language.Number;
31.9 +import org.apidesign.visitor.Language.Plus;
31.10 +import org.apidesign.visitor.Language.Visitor;
31.11 +import org.junit.Test;
31.12 +
31.13 +public class PrintTest {
31.14 + public static class PrintVisitor extends Visitor {
31.15 + StringBuffer sb = new StringBuffer();
31.16 +
31.17 + @Override
31.18 + public boolean visitUnknown(Expression exp) {
31.19 + return true;
31.20 + }
31.21 +
31.22 + public void visitPlus(Plus s) {
31.23 + s.getFirst().visit(this);
31.24 + sb.append(" + ");
31.25 + s.getSecond().visit(this);
31.26 + }
31.27 +
31.28 + public void visitNumber(Number n) {
31.29 + sb.append (n.getValue());
31.30 + }
31.31 + }
31.32 +
31.33 + @Test public void printOnePlusOne() {
31.34 + Number one = new Number(1);
31.35 + Expression plus = new Plus(one, one);
31.36 +
31.37 + PrintVisitor print = new PrintVisitor();
31.38 + plus.visit(print);
31.39 +
31.40 + assertEquals("1 + 1", print.sb.toString());
31.41 + }
31.42 +
31.43 + @Test public void printOnePlusTwoPlusThree() {
31.44 + Number one = new Number(1);
31.45 + Number two = new Number(2);
31.46 + Number three = new Number(3);
31.47 + Expression plus = new Plus(one, new Plus(two, three));
31.48 +
31.49 + PrintVisitor print = new PrintVisitor();
31.50 + plus.visit(print);
31.51 +
31.52 + assertEquals("1 + 2 + 3", print.sb.toString());
31.53 + }
31.54 +}
32.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
32.2 +++ b/samples/visitor/04-traversal/src-test2.0/org/apidesign/test/visitor/InvalidCountNumbersTest.java Sat Jun 14 09:58:47 2008 +0200
32.3 @@ -0,0 +1,23 @@
32.4 +package org.apidesign.test.visitor;
32.5 +
32.6 +import static junit.framework.Assert.*;
32.7 +import org.apidesign.visitor.Language.Expression;
32.8 +import org.apidesign.visitor.Language.Minus;
32.9 +import org.apidesign.visitor.Language.Number;
32.10 +import org.apidesign.visitor.Language.Plus;
32.11 +import org.junit.Test;
32.12 +
32.13 +public class InvalidCountNumbersTest {
32.14 + @Test public void printOneMinusTwo() {
32.15 + Number one = new Number(1);
32.16 + Number three = new Number(3);
32.17 + Number four = new Number(4);
32.18 + Expression minus = new Plus(one, new Minus(three, four));
32.19 +
32.20 + assertEquals(
32.21 + "Should have three numbers, but visitor does not " +
32.22 + "know how to go through minus",
32.23 + 3, CountNumbersTest.countNumbers(minus)
32.24 + );
32.25 + }
32.26 +}
33.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
33.2 +++ b/samples/visitor/04-traversal/src-test2.0/org/apidesign/test/visitor/InvalidLanguageCheckTest.java Sat Jun 14 09:58:47 2008 +0200
33.3 @@ -0,0 +1,17 @@
33.4 +package org.apidesign.test.visitor;
33.5 +
33.6 +import static junit.framework.Assert.*;
33.7 +import org.apidesign.visitor.Language.Expression;
33.8 +import org.apidesign.visitor.Language.Minus;
33.9 +import org.apidesign.visitor.Language.Number;
33.10 +import org.junit.Test;
33.11 +
33.12 +public class InvalidLanguageCheckTest {
33.13 + @Test public void printOneMinusTwo() {
33.14 + Number one = new Number(1);
33.15 + Number two = new Number(2);
33.16 + Expression minus = new Minus(one, two);
33.17 +
33.18 + assertFalse("Recognized as invalid 1.0 version of the language", LanguageCheckTest.isValid1_0Language(minus));
33.19 + }
33.20 +}
34.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
34.2 +++ b/samples/visitor/04-traversal/src-test2.0/org/apidesign/test/visitor/PrintOfMinusStructureTest.java Sat Jun 14 09:58:47 2008 +0200
34.3 @@ -0,0 +1,23 @@
34.4 +package org.apidesign.test.visitor;
34.5 +
34.6 +import org.apidesign.test.visitor.PrintTest.PrintVisitor;
34.7 +import static junit.framework.Assert.*;
34.8 +import org.apidesign.visitor.Language.Expression;
34.9 +import org.apidesign.visitor.Language.Minus;
34.10 +import org.apidesign.visitor.Language.Number;
34.11 +import org.apidesign.visitor.Language.Plus;
34.12 +import org.apidesign.visitor.Language.Visitor;
34.13 +import org.junit.Test;
34.14 +
34.15 +public class PrintOfMinusStructureTest {
34.16 + @Test public void printOneMinusTwo() {
34.17 + Number one = new Number(1);
34.18 + Number two = new Number(2);
34.19 + Expression minus = new Minus(one, two);
34.20 +
34.21 + PrintVisitor print = new PrintVisitor();
34.22 + minus.visit(print); // fails with IllegalStateException
34.23 +
34.24 + assertEquals("1 - 2", print.sb.toString());
34.25 + }
34.26 +}