1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/visitor/22-self/build.xml Sat Jun 14 09:59:32 2008 +0200
1.3 @@ -0,0 +1,101 @@
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 + <antcall target="-build-one">
1.19 + <param name="version" value="api3.0"/>
1.20 + </antcall>
1.21 +
1.22 + <antcall target="-build-one">
1.23 + <param name="version" value="test"/>
1.24 + <param name="cp" value="build/api1.0/classes:${junit.jar}"/>
1.25 + </antcall>
1.26 +
1.27 + <antcall target="-build-one">
1.28 + <param name="version" value="test2.0"/>
1.29 + <param name="cp" value="build/api2.0/classes:build/test/classes:${junit.jar}"/>
1.30 + </antcall>
1.31 + <antcall target="-build-one">
1.32 + <param name="version" value="test3.0"/>
1.33 + <param name="cp" value="build/api3.0/classes:build/test/classes:build/test2.0/classes:${junit.jar}"/>
1.34 + </antcall>
1.35 + </target>
1.36 +
1.37 + <target name="test" depends="build">
1.38 + <echo level="info" message="PrintVisitor on old API. This should succeeds."/>
1.39 + <antcall target="-run-one">
1.40 + <param name="version" value="api1.0"/>
1.41 + </antcall>
1.42 + <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.43 + <antcall target="-run-one">
1.44 + <param name="version" value="api2.0"/>
1.45 + </antcall>
1.46 + <echo level="info" message="PrintVisitor on new API. This yields runtime error."/>
1.47 + <antcall target="-run-one">
1.48 + <param name="test" value="test2.0"/>
1.49 + <param name="extra.cp" value="build/test/classes"/>
1.50 + <param name="version" value="api2.0"/>
1.51 + </antcall>
1.52 + <echo level="info" message="Using old Number with new visitors"/>
1.53 + <antcall target="-run-one">
1.54 + <param name="test" value="test3.0"/>
1.55 + <param name="extra.cp" value="build/test/classes:build/test2.0/classes:"/>
1.56 + <param name="version" value="api3.0"/>
1.57 + </antcall>
1.58 + </target>
1.59 +
1.60 + <!-- support methods -->
1.61 +
1.62 + <target name="-libraries">
1.63 + <ant dir="../../libs/"/>
1.64 +
1.65 + <property name="junit.jar" location="../../libs/dist/junit-4.4.jar"/>
1.66 + </target>
1.67 +
1.68 + <target name="-run-one">
1.69 + <fail message="You need to specify API version number" unless="version"/>
1.70 + <property name="test" value="test"/>
1.71 + <mkdir dir="build/${test}results${version}"/>
1.72 + <junit dir="build/test/classes" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" showoutput="true">
1.73 + <batchtest todir="build/${test}results${version}">
1.74 + <fileset dir="build/${test}/classes">
1.75 + <filename name="**/*Test.class"/>
1.76 + </fileset>
1.77 + </batchtest>
1.78 + <classpath>
1.79 + <path location="build/${version}/classes"/>
1.80 + <path location="build/${test}/classes"/>
1.81 + <path location="${junit.jar}"/>
1.82 + <path path="${extra.cp}"/>
1.83 + </classpath>
1.84 + <formatter type="brief" usefile="false"/>
1.85 + <formatter type="xml"/>
1.86 + </junit>
1.87 + </target>
1.88 +
1.89 + <target name="-build-one">
1.90 + <fail message="You need to specify version number" unless="version"/>
1.91 +
1.92 + <property name="cp" value=""/>
1.93 + <property name="out" value="${version}"/>
1.94 + <property name="failonerror" value="true"/>
1.95 + <mkdir dir="build/${out}/classes"/>
1.96 + <javac
1.97 + srcdir="src-${version}"
1.98 + destdir="build/${out}/classes"
1.99 + source="1.5" target="1.5"
1.100 + classpath="${cp}"
1.101 + failonerror="${failonerror}"
1.102 + />
1.103 + </target>
1.104 +</project>
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/samples/visitor/22-self/nbproject/project.xml Sat Jun 14 09:59:32 2008 +0200
2.3 @@ -0,0 +1,136 @@
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>self</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>self</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>src-api3.0</label>
2.30 + <type>java</type>
2.31 + <location>src-api3.0</location>
2.32 + <encoding>UTF-8</encoding>
2.33 + </source-folder>
2.34 + <source-folder>
2.35 + <label>test</label>
2.36 + <type>java</type>
2.37 + <location>src-test</location>
2.38 + <encoding>UTF-8</encoding>
2.39 + </source-folder>
2.40 + <source-folder>
2.41 + <label>test-2.0</label>
2.42 + <type>java</type>
2.43 + <location>src-test2.0</location>
2.44 + <encoding>UTF-8</encoding>
2.45 + </source-folder>
2.46 + <source-folder>
2.47 + <label>test-3.0</label>
2.48 + <type>java</type>
2.49 + <location>src-test3.0</location>
2.50 + <encoding>UTF-8</encoding>
2.51 + </source-folder>
2.52 + </folders>
2.53 + <ide-actions>
2.54 + <action name="build">
2.55 + <target>build</target>
2.56 + </action>
2.57 + <action name="clean">
2.58 + <target>clean</target>
2.59 + </action>
2.60 + <action name="test">
2.61 + <target>test</target>
2.62 + </action>
2.63 + <action name="run">
2.64 + <target>test</target>
2.65 + </action>
2.66 + <action name="rebuild">
2.67 + <target>clean</target>
2.68 + <target>build</target>
2.69 + </action>
2.70 + </ide-actions>
2.71 + <view>
2.72 + <items>
2.73 + <source-folder style="packages">
2.74 + <label>API Version 1.0</label>
2.75 + <location>src-api1.0</location>
2.76 + </source-folder>
2.77 + <source-folder style="packages">
2.78 + <label>Usage of the API</label>
2.79 + <location>src-test</location>
2.80 + </source-folder>
2.81 + <source-folder style="packages">
2.82 + <label>API Version 2.0</label>
2.83 + <location>src-api2.0</location>
2.84 + </source-folder>
2.85 + <source-folder style="packages">
2.86 + <label>Usage of 2.0 structures with 1.0 visitor</label>
2.87 + <location>src-test2.0</location>
2.88 + </source-folder>
2.89 + <source-folder style="packages">
2.90 + <label>API Version 3.0</label>
2.91 + <location>src-api3.0</location>
2.92 + </source-folder>
2.93 + <source-folder style="packages">
2.94 + <label>Usage of 3.0 visitor</label>
2.95 + <location>src-test3.0</location>
2.96 + </source-folder>
2.97 + <source-file>
2.98 + <location>build.xml</location>
2.99 + </source-file>
2.100 + </items>
2.101 + <context-menu>
2.102 + <ide-action name="build"/>
2.103 + <ide-action name="rebuild"/>
2.104 + <ide-action name="clean"/>
2.105 + <ide-action name="test"/>
2.106 + </context-menu>
2.107 + </view>
2.108 + </general-data>
2.109 + <java-data xmlns="http://www.netbeans.org/ns/freeform-project-java/1">
2.110 + <compilation-unit>
2.111 + <package-root>src-api1.0</package-root>
2.112 + <source-level>1.5</source-level>
2.113 + </compilation-unit>
2.114 + <compilation-unit>
2.115 + <package-root>src-api2.0</package-root>
2.116 + <source-level>1.5</source-level>
2.117 + </compilation-unit>
2.118 + <compilation-unit>
2.119 + <package-root>src-api3.0</package-root>
2.120 + <source-level>1.5</source-level>
2.121 + </compilation-unit>
2.122 + <compilation-unit>
2.123 + <package-root>src-test</package-root>
2.124 + <classpath mode="compile">src-api1.0:../../libs/dist/junit-4.4.jar</classpath>
2.125 + <source-level>1.5</source-level>
2.126 + </compilation-unit>
2.127 + <compilation-unit>
2.128 + <package-root>src-test2.0</package-root>
2.129 + <classpath mode="compile">src-api2.0:src-test:../../libs/dist/junit-4.4.jar</classpath>
2.130 + <source-level>1.5</source-level>
2.131 + </compilation-unit>
2.132 + <compilation-unit>
2.133 + <package-root>src-test3.0</package-root>
2.134 + <classpath mode="compile">src-api3.0:src-test:src-test2.0:../../libs/dist/junit-4.4.jar</classpath>
2.135 + <source-level>1.5</source-level>
2.136 + </compilation-unit>
2.137 + </java-data>
2.138 + </configuration>
2.139 +</project>
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/samples/visitor/22-self/src-api1.0/org/apidesign/visitor/Language.java Sat Jun 14 09:59:32 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 + public interface Expression {
3.10 + public abstract void visit(Visitor v);
3.11 + }
3.12 + public interface Plus extends Expression {
3.13 + public Expression getFirst();
3.14 + public Expression getSecond();
3.15 + }
3.16 + public interface Number extends Expression {
3.17 + public int getValue();
3.18 + }
3.19 +
3.20 + // BEGIN: visitor.clientprovider.self.v1
3.21 + public static abstract class Visitor {
3.22 + Visitor() {}
3.23 +
3.24 + public static 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, Visitor self);
3.30 + public void visitPlus(Plus s, Visitor self);
3.31 + public void visitNumber(Number n, Visitor self);
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.self.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, this);
3.44 + }
3.45 +
3.46 + @Override
3.47 + public void dispatchNumber(Number n) {
3.48 + v.visitNumber(n, this);
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/22-self/src-api2.0/org/apidesign/visitor/Language.java Sat Jun 14 09:59:32 2008 +0200
4.3 @@ -0,0 +1,91 @@
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 + /** @since 2.0 */
4.20 + public interface Minus extends Expression {
4.21 + public Expression getFirst();
4.22 + public Expression getSecond();
4.23 + }
4.24 +
4.25 + public static abstract class Visitor {
4.26 + Visitor() {}
4.27 + /** @since 2.0 */
4.28 + public static Visitor create(Version2_0 v) {
4.29 + return create2_0(v);
4.30 + }
4.31 +
4.32 + /** @since 2.0 */
4.33 + public interface Version2_0 extends Version1_0 {
4.34 + public void visitMinus(Minus m, Visitor self);
4.35 + }
4.36 +
4.37 +
4.38 + /** @since 2.0 */
4.39 + public abstract void dispatchNumber(Number n);
4.40 +
4.41 + public static Visitor create(Version1_0 v) {
4.42 + return create1_0(v);
4.43 + }
4.44 +
4.45 + public interface Version1_0 {
4.46 + public boolean visitUnknown(Expression e, Visitor self);
4.47 + public void visitPlus(Plus s, Visitor self);
4.48 + public void visitNumber(Number n, Visitor self);
4.49 + }
4.50 +
4.51 + public abstract void dispatchPlus(Plus p);
4.52 + public abstract void dispatchMinus(Minus m);
4.53 + }
4.54 +
4.55 + static Visitor create1_0(final Visitor.Version1_0 v) {
4.56 + return new Visitor() {
4.57 + @Override
4.58 + public void dispatchPlus(Plus p) {
4.59 + v.visitPlus(p, this);
4.60 + }
4.61 +
4.62 + @Override
4.63 + public void dispatchNumber(Number n) {
4.64 + v.visitNumber(n, this);
4.65 + }
4.66 +
4.67 + @Override
4.68 + public void dispatchMinus(Minus m) {
4.69 + if (v.visitUnknown(m, this)) {
4.70 + m.getFirst().visit(this);
4.71 + m.getSecond().visit(this);
4.72 + }
4.73 + }
4.74 + };
4.75 + }
4.76 + static Visitor create2_0(final Visitor.Version2_0 v) {
4.77 + return new Visitor() {
4.78 + @Override
4.79 + public void dispatchPlus(Plus p) {
4.80 + v.visitPlus(p, this);
4.81 + }
4.82 +
4.83 + @Override
4.84 + public void dispatchNumber(Number n) {
4.85 + v.visitNumber(n, this);
4.86 + }
4.87 +
4.88 + @Override
4.89 + public void dispatchMinus(Minus m) {
4.90 + v.visitMinus(m, this);
4.91 + }
4.92 + };
4.93 + }
4.94 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/samples/visitor/22-self/src-api3.0/org/apidesign/visitor/Language.java Sat Jun 14 09:59:32 2008 +0200
5.3 @@ -0,0 +1,154 @@
5.4 +package org.apidesign.visitor;
5.5 +
5.6 +public final class Language {
5.7 + private Language() { }
5.8 +
5.9 + public interface Expression {
5.10 + public abstract void visit(Visitor v);
5.11 + }
5.12 + public interface Plus extends Expression {
5.13 + public Expression getFirst();
5.14 + public Expression getSecond();
5.15 + }
5.16 + public interface Number extends Expression {
5.17 + public int getValue();
5.18 + }
5.19 + /** @since 2.0 */
5.20 + public interface Minus extends Expression {
5.21 + public Expression getFirst();
5.22 + public Expression getSecond();
5.23 + }
5.24 +
5.25 + /** @since 3.0 */
5.26 + public interface Real extends Expression {
5.27 + public double getValue();
5.28 + }
5.29 + public static abstract class Visitor {
5.30 + Visitor() {}
5.31 +
5.32 +
5.33 + /** @since 3.0 */
5.34 + public static Visitor create(Version3_0 v) {
5.35 + return create3_0(v);
5.36 + }
5.37 +
5.38 + /** @since 3.0 */
5.39 + public interface Version3_0 {
5.40 + public boolean visitUnknown(Expression e, Visitor self);
5.41 + public void visitPlus(Plus s, Visitor self);
5.42 + public void visitMinus(Minus s, Visitor self);
5.43 + public void visitReal(Real r, Visitor self);
5.44 + }
5.45 +
5.46 + /** @since 3.0 */
5.47 + public abstract void dispatchReal(Real r);
5.48 +
5.49 + /** @since 2.0 */
5.50 + public static Visitor create(Version2_0 v) {
5.51 + return create2_0(v);
5.52 + }
5.53 +
5.54 + /** @since 2.0 */
5.55 + public interface Version2_0 extends Version1_0 {
5.56 + public void visitMinus(Minus m, Visitor self);
5.57 + }
5.58 +
5.59 +
5.60 + /** @since 2.0 */
5.61 + public abstract void dispatchNumber(Number n);
5.62 +
5.63 + public static Visitor create(Version1_0 v) {
5.64 + return create1_0(v);
5.65 + }
5.66 +
5.67 + public interface Version1_0 {
5.68 + public boolean visitUnknown(Expression e, Visitor self);
5.69 + public void visitPlus(Plus s, Visitor self);
5.70 + public void visitNumber(Number n, Visitor self);
5.71 + }
5.72 +
5.73 + public abstract void dispatchPlus(Plus p);
5.74 + public abstract void dispatchMinus(Minus m);
5.75 + }
5.76 +
5.77 + static Visitor create1_0(final Visitor.Version1_0 v) {
5.78 + return new Visitor() {
5.79 + @Override
5.80 + public void dispatchPlus(Plus p) {
5.81 + v.visitPlus(p, this);
5.82 + }
5.83 +
5.84 + @Override
5.85 + public void dispatchNumber(Number n) {
5.86 + v.visitNumber(n, this);
5.87 + }
5.88 +
5.89 + @Override
5.90 + public void dispatchMinus(Minus m) {
5.91 + if (v.visitUnknown(m, this)) {
5.92 + m.getFirst().visit(this);
5.93 + m.getSecond().visit(this);
5.94 + }
5.95 + }
5.96 +
5.97 + @Override
5.98 + public void dispatchReal(Real r) {
5.99 + v.visitUnknown(r, this);
5.100 + }
5.101 + };
5.102 + }
5.103 + static Visitor create2_0(final Visitor.Version2_0 v) {
5.104 + return new Visitor() {
5.105 + @Override
5.106 + public void dispatchPlus(Plus p) {
5.107 + v.visitPlus(p, this);
5.108 + }
5.109 +
5.110 + @Override
5.111 + public void dispatchNumber(Number n) {
5.112 + v.visitNumber(n, this);
5.113 + }
5.114 +
5.115 + @Override
5.116 + public void dispatchMinus(Minus m) {
5.117 + v.visitMinus(m, this);
5.118 + }
5.119 +
5.120 + @Override
5.121 + public void dispatchReal(Real r) {
5.122 + v.visitUnknown(r, this);
5.123 + }
5.124 + };
5.125 + }
5.126 + static Visitor create3_0(final Visitor.Version3_0 v) {
5.127 + return new Visitor() {
5.128 + @Override
5.129 + public void dispatchReal(Real r) {
5.130 + v.visitReal(r, this);
5.131 + }
5.132 +
5.133 + @Override
5.134 + public void dispatchNumber(final Number n) {
5.135 + class RealWrapper implements Real {
5.136 + public double getValue() {
5.137 + return n.getValue();
5.138 + }
5.139 + public void visit(Visitor v) {
5.140 + n.visit(v);
5.141 + }
5.142 + }
5.143 + v.visitReal(new RealWrapper(), this);
5.144 + }
5.145 +
5.146 + @Override
5.147 + public void dispatchPlus(Plus p) {
5.148 + v.visitPlus(p, this);
5.149 + }
5.150 +
5.151 + @Override
5.152 + public void dispatchMinus(Minus m) {
5.153 + v.visitMinus(m, this);
5.154 + }
5.155 + };
5.156 + }
5.157 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/samples/visitor/22-self/src-test/org/apidesign/test/visitor/PrintTest.java Sat Jun 14 09:59:32 2008 +0200
6.3 @@ -0,0 +1,84 @@
6.4 +package org.apidesign.test.visitor;
6.5 +
6.6 +import org.apidesign.visitor.Language.Expression;
6.7 +import org.apidesign.visitor.Language.Number;
6.8 +import org.apidesign.visitor.Language.Plus;
6.9 +import org.apidesign.visitor.Language.Visitor;
6.10 +import org.apidesign.visitor.Language.Visitor;
6.11 +import static junit.framework.Assert.*;
6.12 +import org.junit.Test;
6.13 +
6.14 +public class PrintTest {
6.15 +
6.16 + public static Number newNumber(final int value) {
6.17 + return new Number() {
6.18 + public int getValue() {
6.19 + return value;
6.20 + }
6.21 +
6.22 + public void visit(Visitor v) {
6.23 + v.dispatchNumber(this);
6.24 + }
6.25 + };
6.26 + }
6.27 + public static Plus newPlus(
6.28 + final Expression first, final Expression second
6.29 + ) {
6.30 + return new Plus() {
6.31 + public Expression getFirst() {
6.32 + return first;
6.33 + }
6.34 +
6.35 + public Expression getSecond() {
6.36 + return second;
6.37 + }
6.38 +
6.39 + public void visit(Visitor v) {
6.40 + v.dispatchPlus(this);
6.41 + }
6.42 + };
6.43 + }
6.44 +
6.45 + // BEGIN: visitor.clientprovider.self.print
6.46 + public static class PrintVisitor implements Visitor.Version1_0 {
6.47 + StringBuffer sb = new StringBuffer();
6.48 +
6.49 + public void visitPlus(Plus s, Visitor self) {
6.50 + s.getFirst().visit(self);
6.51 + sb.append(" + ");
6.52 + s.getSecond().visit(self);
6.53 + }
6.54 +
6.55 + public void visitNumber(Number n, Visitor self) {
6.56 + sb.append (n.getValue());
6.57 + }
6.58 +
6.59 + public boolean visitUnknown(Expression e, Visitor self) {
6.60 + sb.append("unknown");
6.61 + return true;
6.62 + }
6.63 + }
6.64 +
6.65 + @Test public void printOnePlusOne() {
6.66 + Number one = newNumber(1);
6.67 + Expression plus = newPlus(one, one);
6.68 +
6.69 + PrintVisitor print = new PrintVisitor();
6.70 + plus.visit(Visitor.create(print));
6.71 +
6.72 + assertEquals("1 + 1", print.sb.toString());
6.73 + }
6.74 + // END: visitor.clientprovider.self.print
6.75 +
6.76 + @Test public void printOnePlusTwoPlusThree() {
6.77 + Number one = newNumber(1);
6.78 + Number two = newNumber(2);
6.79 + Number three = newNumber(3);
6.80 + Expression plus = newPlus(one, newPlus(two, three));
6.81 +
6.82 + PrintVisitor print = new PrintVisitor();
6.83 + plus.visit(Visitor.create(print));
6.84 +
6.85 + assertEquals("1 + 2 + 3", print.sb.toString());
6.86 + }
6.87 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/samples/visitor/22-self/src-test2.0/org/apidesign/test/visitor/PrintOfMinusStructureTest.java Sat Jun 14 09:59:32 2008 +0200
7.3 @@ -0,0 +1,41 @@
7.4 +package org.apidesign.test.visitor;
7.5 +
7.6 +import org.apidesign.test.visitor.PrintTest.PrintVisitor;
7.7 +import org.apidesign.visitor.Language.Expression;
7.8 +import org.apidesign.visitor.Language.Minus;
7.9 +import org.apidesign.visitor.Language.Number;
7.10 +import static junit.framework.Assert.*;
7.11 +import org.apidesign.visitor.Language.Visitor;
7.12 +import org.junit.Test;
7.13 +
7.14 +public class PrintOfMinusStructureTest {
7.15 + public static Minus newMinus(
7.16 + final Expression first, final Expression second
7.17 + ) {
7.18 + return new Minus() {
7.19 + public Expression getFirst() {
7.20 + return first;
7.21 + }
7.22 +
7.23 + public Expression getSecond() {
7.24 + return second;
7.25 + }
7.26 +
7.27 + public void visit(Visitor v) {
7.28 + v.dispatchMinus(this);
7.29 + }
7.30 + };
7.31 + }
7.32 +
7.33 +
7.34 + @Test public void printOneMinusTwo() {
7.35 + Number one = PrintTest.newNumber(1);
7.36 + Number two = PrintTest.newNumber(2);
7.37 + Expression plus = newMinus(one, two);
7.38 +
7.39 + PrintVisitor print = new PrintVisitor();
7.40 + plus.visit(Visitor.create(print));
7.41 +
7.42 + assertEquals("1 - 2", print.sb.toString());
7.43 + }
7.44 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/samples/visitor/22-self/src-test3.0/org/apidesign/test/visitor/PrintRealTest.java Sat Jun 14 09:59:32 2008 +0200
8.3 @@ -0,0 +1,82 @@
8.4 +package org.apidesign.test.visitor;
8.5 +
8.6 +import org.apidesign.test.visitor.PrintTest.PrintVisitor;
8.7 +import static junit.framework.Assert.*;
8.8 +import org.apidesign.visitor.Language.Expression;
8.9 +import org.apidesign.visitor.Language.Minus;
8.10 +import org.apidesign.visitor.Language.Number;
8.11 +import org.apidesign.visitor.Language.Plus;
8.12 +import org.apidesign.visitor.Language.Real;
8.13 +import org.apidesign.visitor.Language.Visitor;
8.14 +import org.junit.Test;
8.15 +
8.16 +public class PrintRealTest {
8.17 + @Test public void printOneMinusTwo() {
8.18 + Real one = newReal(1);
8.19 + Real two = newReal(2);
8.20 + Expression plus = PrintOfMinusStructureTest.newMinus(one, two);
8.21 +
8.22 + PrintVisitor print = new PrintVisitor();
8.23 + plus.visit(Visitor.create(print));
8.24 +
8.25 + assertEquals("1.0 - 2.0", print.sb.toString());
8.26 + }
8.27 +
8.28 + @Test public void visitorReadyForVersion3_0() {
8.29 + class PrintVisitor3_0 implements Visitor.Version3_0 {
8.30 + StringBuffer sb = new StringBuffer();
8.31 +
8.32 + public boolean visitUnknown(Expression exp, Visitor self) {
8.33 + sb.append("unknown");
8.34 + return true;
8.35 + }
8.36 +
8.37 + public void visitPlus(Plus s, Visitor self) {
8.38 + s.getFirst().visit(self);
8.39 + sb.append(" + ");
8.40 + s.getSecond().visit(self);
8.41 + }
8.42 +
8.43 + public void visitMinus(Minus m, Visitor self) {
8.44 + m.getFirst().visit(self);
8.45 + sb.append(" - ");
8.46 + m.getSecond().visit(self);
8.47 + }
8.48 +
8.49 + public void visitReal(Real r, Visitor self) {
8.50 + sb.append(r.getValue());
8.51 + }
8.52 + }
8.53 +
8.54 + Number one = PrintTest.newNumber(1);
8.55 + Number two = PrintTest.newNumber(2);
8.56 + Expression plus = PrintOfMinusStructureTest.newMinus(one, two);
8.57 +
8.58 + PrintVisitor3_0 print = new PrintVisitor3_0();
8.59 + plus.visit(Visitor.create(print));
8.60 +
8.61 + assertEquals("1.0 - 2.0", print.sb.toString());
8.62 +
8.63 + Real five = newReal(5);
8.64 + Real three = newReal(3);
8.65 + Expression realPlus = PrintOfMinusStructureTest.newMinus(five, three);
8.66 +
8.67 + PrintVisitor3_0 printReal = new PrintVisitor3_0();
8.68 + realPlus.visit(Visitor.create(printReal));
8.69 +
8.70 + assertEquals("5.0 - 3.0", printReal.sb.toString());
8.71 +
8.72 + }
8.73 +
8.74 + public static Real newReal(final double value) {
8.75 + return new Real() {
8.76 + public double getValue() {
8.77 + return value;
8.78 + }
8.79 +
8.80 + public void visit(Visitor v) {
8.81 + v.dispatchReal(this);
8.82 + }
8.83 + };
8.84 + }
8.85 +}