1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/visitor/traversal/build.xml Sat Jun 14 09:58:35 2008 +0200
1.3 @@ -0,0 +1,94 @@
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 + <antcall target="-build-one">
1.25 + <param name="version" value="test"/>
1.26 + <param name="out" value="will-not-compile"/>
1.27 + <param name="cp" value="build/api2.0/classes:${junit.jar}"/>
1.28 + </antcall>
1.29 +
1.30 + <antcall target="-build-one">
1.31 + <param name="version" value="test2.0"/>
1.32 + <param name="cp" value="build/api2.0/classes:build/test/classes:${junit.jar}"/>
1.33 + </antcall>
1.34 + </target>
1.35 +
1.36 + <target name="test" depends="build">
1.37 + <echo level="info" message="PrintVisitor on old API. This should succeeds."/>
1.38 + <antcall target="-run-one">
1.39 + <param name="version" value="api1.0"/>
1.40 + </antcall>
1.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."/>
1.42 + <antcall target="-run-one">
1.43 + <param name="version" value="api2.0"/>
1.44 + </antcall>
1.45 + <echo level="info" message="PrintVisitor on new API. This yields runtime error."/>
1.46 + <antcall target="-run-one">
1.47 + <param name="test" value="test2.0"/>
1.48 + <param name="extra.cp" value="build/test/classes"/>
1.49 + <param name="version" value="api2.0"/>
1.50 + </antcall>
1.51 + </target>
1.52 +
1.53 + <!-- support methods -->
1.54 +
1.55 + <target name="-libraries">
1.56 + <ant dir="../../libs/"/>
1.57 +
1.58 + <property name="junit.jar" location="../../libs/dist/junit-4.4.jar"/>
1.59 + </target>
1.60 +
1.61 + <target name="-run-one">
1.62 + <fail message="You need to specify API version number" unless="version"/>
1.63 + <property name="test" value="test"/>
1.64 + <mkdir dir="build/${test}results${version}"/>
1.65 + <junit dir="build/test/classes" errorproperty="tests.failed" failureproperty="tests.failed" fork="true" showoutput="true">
1.66 + <batchtest todir="build/${test}results${version}">
1.67 + <fileset dir="build/${test}/classes">
1.68 + <filename name="**/*Test.class"/>
1.69 + </fileset>
1.70 + </batchtest>
1.71 + <classpath>
1.72 + <path location="build/${version}/classes"/>
1.73 + <path location="build/${test}/classes"/>
1.74 + <path location="${junit.jar}"/>
1.75 + <path path="${extra.cp}"/>
1.76 + </classpath>
1.77 + <formatter type="brief" usefile="false"/>
1.78 + <formatter type="xml"/>
1.79 + </junit>
1.80 + </target>
1.81 +
1.82 + <target name="-build-one">
1.83 + <fail message="You need to specify version number" unless="version"/>
1.84 +
1.85 + <property name="cp" value=""/>
1.86 + <property name="out" value="${version}"/>
1.87 + <property name="failonerror" value="true"/>
1.88 + <mkdir dir="build/${out}/classes"/>
1.89 + <javac
1.90 + srcdir="src-${version}"
1.91 + destdir="build/${out}/classes"
1.92 + source="1.5" target="1.5"
1.93 + classpath="${cp}"
1.94 + failonerror="${failonerror}"
1.95 + />
1.96 + </target>
1.97 +</project>
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/samples/visitor/traversal/nbproject/project.xml Sat Jun 14 09:58:35 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>traversal</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>traversal</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/traversal/src-api1.0/org/apidesign/visitor/Language.java Sat Jun 14 09:58:35 2008 +0200
3.3 @@ -0,0 +1,46 @@
3.4 +package org.apidesign.visitor;
3.5 +
3.6 +public final class Language {
3.7 + private Language() { }
3.8 +
3.9 + public static abstract class Expression {
3.10 + public abstract void visit(Visitor v);
3.11 + }
3.12 + public static final class Plus extends Expression {
3.13 + private final Expression first;
3.14 + private final Expression second;
3.15 +
3.16 + public Plus(Expression first, Expression second) {
3.17 + this.first = first;
3.18 + this.second = second;
3.19 + }
3.20 + public Expression getFirst() { return first; }
3.21 + public Expression getSecond() { return second; }
3.22 + @Override
3.23 + public void visit(Visitor v) { v.visitPlus(this); }
3.24 + }
3.25 + public static final class Number extends Expression {
3.26 + private final int value;
3.27 + public Number(int value) { this.value = value; }
3.28 + public int getValue() { return value; }
3.29 + @Override
3.30 + public void visit(Visitor v) { v.visitNumber(this); }
3.31 + }
3.32 +
3.33 + // BEGIN: visitor.traversal.v1
3.34 + public static abstract class Visitor/*1.0*/ {
3.35 + public boolean visitUnknown(Expression e) {
3.36 + throw new IllegalStateException("Unknown element faced: " + e);
3.37 + }
3.38 + public void visitPlus(Plus s) {
3.39 + if (visitUnknown(s)) {
3.40 + s.getFirst().visit(this);
3.41 + s.getSecond().visit(this);
3.42 + }
3.43 + }
3.44 + public void visitNumber(Number n) {
3.45 + visitUnknown(n);
3.46 + }
3.47 + }
3.48 + // END: visitor.traversal.v1
3.49 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/samples/visitor/traversal/src-api2.0/org/apidesign/visitor/Language.java Sat Jun 14 09:58:35 2008 +0200
4.3 @@ -0,0 +1,67 @@
4.4 +package org.apidesign.visitor;
4.5 +
4.6 +public final class Language {
4.7 + private Language() { }
4.8 +
4.9 + public static abstract class Expression {
4.10 + public abstract void visit(Visitor v);
4.11 + }
4.12 + public static final class Plus extends Expression {
4.13 + private final Expression first;
4.14 + private final Expression second;
4.15 +
4.16 + public Plus(Expression first, Expression second) {
4.17 + this.first = first;
4.18 + this.second = second;
4.19 + }
4.20 + public Expression getFirst() { return first; }
4.21 + public Expression getSecond() { return second; }
4.22 + @Override
4.23 + public void visit(Visitor v) { v.visitPlus(this); }
4.24 + }
4.25 + public static final class Number extends Expression {
4.26 + private final int value;
4.27 + public Number(int value) { this.value = value; }
4.28 + public int getValue() { return value; }
4.29 + @Override
4.30 + public void visit(Visitor v) { v.visitNumber(this); }
4.31 + }
4.32 + /** @since 2.0 */
4.33 + public static final class Minus extends Expression {
4.34 + private final Expression first;
4.35 + private final Expression second;
4.36 +
4.37 + public Minus(Expression first, Expression second) {
4.38 + this.first = first;
4.39 + this.second = second;
4.40 + }
4.41 + public Expression getFirst() { return first; }
4.42 + public Expression getSecond() { return second; }
4.43 + public void visit(Visitor v) {
4.44 + v.visitMinus(this);
4.45 + }
4.46 + }
4.47 +
4.48 + // BEGIN: visitor.traversal.v2
4.49 + public static abstract class Visitor/*2.0*/ {
4.50 + public boolean visitUnknown(Expression e) {
4.51 + throw new IllegalStateException("Unknown element faced: " + e);
4.52 + }
4.53 + public void visitPlus(Plus s) {
4.54 + if (visitUnknown(s)) {
4.55 + s.getFirst().visit(this);
4.56 + s.getSecond().visit(this);
4.57 + }
4.58 + }
4.59 + public void visitMinus(Minus s) {
4.60 + if (visitUnknown(s)) {
4.61 + s.getFirst().visit(this);
4.62 + s.getSecond().visit(this);
4.63 + }
4.64 + }
4.65 + public void visitNumber(Number n) {
4.66 + visitUnknown(n);
4.67 + }
4.68 + }
4.69 + // END: visitor.traversal.v2
4.70 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/samples/visitor/traversal/src-test/org/apidesign/test/visitor/CountNumbersTest.java Sat Jun 14 09:58:35 2008 +0200
5.3 @@ -0,0 +1,51 @@
5.4 +package org.apidesign.test.visitor;
5.5 +
5.6 +import static junit.framework.Assert.*;
5.7 +import org.apidesign.visitor.Language.Expression;
5.8 +import org.apidesign.visitor.Language.Number;
5.9 +import org.apidesign.visitor.Language.Plus;
5.10 +import org.apidesign.visitor.Language.Visitor;
5.11 +import org.junit.Test;
5.12 +
5.13 +public class CountNumbersTest {
5.14 +
5.15 + // BEGIN: visitor.count.numbers.visitunknown
5.16 + private static class CountNumbers extends Visitor/*version1.0*/ {
5.17 + int cnt;
5.18 +
5.19 + @Override
5.20 + public boolean visitUnknown(Expression exp) {
5.21 + return true;
5.22 + }
5.23 + public void visitPlus(Plus s) {
5.24 + s.getFirst().visit(this);
5.25 + s.getSecond().visit(this);
5.26 + }
5.27 + public void visitNumber(Number n) {
5.28 + cnt++;
5.29 + }
5.30 + }
5.31 +
5.32 + public static int countNumbers(Expression expression) {
5.33 + CountNumbers counter = new CountNumbers();
5.34 + expression.visit(counter);
5.35 + return counter.cnt;
5.36 + }
5.37 + // END: visitor.count.numbers.visitunknown
5.38 +
5.39 + @Test public void printOnePlusOne() {
5.40 + Number one = new Number(1);
5.41 + Expression expression = new Plus(one, one);
5.42 +
5.43 + assertEquals("Two 1's", 2, countNumbers(expression));
5.44 + }
5.45 +
5.46 + @Test public void printOnePlusTwoPlusThree() {
5.47 + Number one = new Number(1);
5.48 + Number two = new Number(2);
5.49 + Number three = new Number(3);
5.50 + Expression plus = new Plus(one, new Plus(two, three));
5.51 +
5.52 + assertEquals("Three", 3, countNumbers(plus));
5.53 + }
5.54 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/samples/visitor/traversal/src-test/org/apidesign/test/visitor/LanguageCheckTest.java Sat Jun 14 09:58:35 2008 +0200
6.3 @@ -0,0 +1,51 @@
6.4 +package org.apidesign.test.visitor;
6.5 +
6.6 +import static junit.framework.Assert.*;
6.7 +import org.apidesign.visitor.Language.Expression;
6.8 +import org.apidesign.visitor.Language.Number;
6.9 +import org.apidesign.visitor.Language.Plus;
6.10 +import org.apidesign.visitor.Language.Visitor;
6.11 +import org.junit.Test;
6.12 +
6.13 +public class LanguageCheckTest {
6.14 +
6.15 + // BEGIN: visitor.language.check.visitunknown
6.16 + private static class Valid1_0Language extends Visitor/*version1.0*/ {
6.17 + boolean invalid;
6.18 +
6.19 + @Override
6.20 + public boolean visitUnknown(Expression exp) {
6.21 + invalid = true;
6.22 + return false;
6.23 + }
6.24 + public void visitPlus(Plus s) {
6.25 + s.getFirst().visit(this);
6.26 + s.getSecond().visit(this);
6.27 + }
6.28 + public void visitNumber(Number n) {
6.29 + }
6.30 + }
6.31 +
6.32 + public static boolean isValid1_0Language(Expression expression) {
6.33 + Valid1_0Language valid = new Valid1_0Language();
6.34 + expression.visit(valid);
6.35 + return !valid.invalid;
6.36 + }
6.37 + // END: visitor.language.check.visitunknown
6.38 +
6.39 + @Test public void printOnePlusOne() {
6.40 + Number one = new Number(1);
6.41 + Expression expression = new Plus(one, one);
6.42 +
6.43 + assertTrue("Valid language", isValid1_0Language(expression));
6.44 + }
6.45 +
6.46 + @Test public void printOnePlusTwoPlusThree() {
6.47 + Number one = new Number(1);
6.48 + Number two = new Number(2);
6.49 + Number three = new Number(3);
6.50 + Expression plus = new Plus(one, new Plus(two, three));
6.51 +
6.52 + assertTrue("Valid language", isValid1_0Language(plus));
6.53 + }
6.54 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/samples/visitor/traversal/src-test/org/apidesign/test/visitor/PrintTest.java Sat Jun 14 09:58:35 2008 +0200
7.3 @@ -0,0 +1,51 @@
7.4 +package org.apidesign.test.visitor;
7.5 +
7.6 +import static junit.framework.Assert.*;
7.7 +import org.apidesign.visitor.Language.Expression;
7.8 +import org.apidesign.visitor.Language.Number;
7.9 +import org.apidesign.visitor.Language.Plus;
7.10 +import org.apidesign.visitor.Language.Visitor;
7.11 +import org.junit.Test;
7.12 +
7.13 +public class PrintTest {
7.14 + public static class PrintVisitor extends Visitor {
7.15 + StringBuffer sb = new StringBuffer();
7.16 +
7.17 + @Override
7.18 + public boolean visitUnknown(Expression exp) {
7.19 + return true;
7.20 + }
7.21 +
7.22 + public void visitPlus(Plus s) {
7.23 + s.getFirst().visit(this);
7.24 + sb.append(" + ");
7.25 + s.getSecond().visit(this);
7.26 + }
7.27 +
7.28 + public void visitNumber(Number n) {
7.29 + sb.append (n.getValue());
7.30 + }
7.31 + }
7.32 +
7.33 + @Test public void printOnePlusOne() {
7.34 + Number one = new Number(1);
7.35 + Expression plus = new Plus(one, one);
7.36 +
7.37 + PrintVisitor print = new PrintVisitor();
7.38 + plus.visit(print);
7.39 +
7.40 + assertEquals("1 + 1", print.sb.toString());
7.41 + }
7.42 +
7.43 + @Test public void printOnePlusTwoPlusThree() {
7.44 + Number one = new Number(1);
7.45 + Number two = new Number(2);
7.46 + Number three = new Number(3);
7.47 + Expression plus = new Plus(one, new Plus(two, three));
7.48 +
7.49 + PrintVisitor print = new PrintVisitor();
7.50 + plus.visit(print);
7.51 +
7.52 + assertEquals("1 + 2 + 3", print.sb.toString());
7.53 + }
7.54 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/samples/visitor/traversal/src-test2.0/org/apidesign/test/visitor/InvalidCountNumbersTest.java Sat Jun 14 09:58:35 2008 +0200
8.3 @@ -0,0 +1,25 @@
8.4 +package org.apidesign.test.visitor;
8.5 +
8.6 +import static junit.framework.Assert.*;
8.7 +import org.apidesign.visitor.Language.Expression;
8.8 +import org.apidesign.visitor.Language.Minus;
8.9 +import org.apidesign.visitor.Language.Number;
8.10 +import org.apidesign.visitor.Language.Plus;
8.11 +import org.junit.Test;
8.12 +
8.13 +public class InvalidCountNumbersTest {
8.14 + @Test public void printOneMinusTwo() {
8.15 + // BEGIN: visitor.visitunknown.traversal
8.16 + Number one = new Number(1);
8.17 + Number three = new Number(3);
8.18 + Number four = new Number(4);
8.19 + Expression minus = new Plus(one, new Minus(three, four));
8.20 +
8.21 + assertEquals(
8.22 + "Should have three numbers, but visitor does not " +
8.23 + "know how to go through minus",
8.24 + 3, CountNumbersTest.countNumbers(minus)
8.25 + );
8.26 + // END: visitor.visitunknown.traversal
8.27 + }
8.28 +}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/samples/visitor/traversal/src-test2.0/org/apidesign/test/visitor/InvalidLanguageCheckTest.java Sat Jun 14 09:58:35 2008 +0200
9.3 @@ -0,0 +1,17 @@
9.4 +package org.apidesign.test.visitor;
9.5 +
9.6 +import static junit.framework.Assert.*;
9.7 +import org.apidesign.visitor.Language.Expression;
9.8 +import org.apidesign.visitor.Language.Minus;
9.9 +import org.apidesign.visitor.Language.Number;
9.10 +import org.junit.Test;
9.11 +
9.12 +public class InvalidLanguageCheckTest {
9.13 + @Test public void printOneMinusTwo() {
9.14 + Number one = new Number(1);
9.15 + Number two = new Number(2);
9.16 + Expression minus = new Minus(one, two);
9.17 +
9.18 + assertFalse("Recognized as invalid 1.0 version of the language", LanguageCheckTest.isValid1_0Language(minus));
9.19 + }
9.20 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/samples/visitor/traversal/src-test2.0/org/apidesign/test/visitor/PrintOfMinusStructureTest.java Sat Jun 14 09:58:35 2008 +0200
10.3 @@ -0,0 +1,23 @@
10.4 +package org.apidesign.test.visitor;
10.5 +
10.6 +import org.apidesign.test.visitor.PrintTest.PrintVisitor;
10.7 +import static junit.framework.Assert.*;
10.8 +import org.apidesign.visitor.Language.Expression;
10.9 +import org.apidesign.visitor.Language.Minus;
10.10 +import org.apidesign.visitor.Language.Number;
10.11 +import org.apidesign.visitor.Language.Plus;
10.12 +import org.apidesign.visitor.Language.Visitor;
10.13 +import org.junit.Test;
10.14 +
10.15 +public class PrintOfMinusStructureTest {
10.16 + @Test public void printOneMinusTwo() {
10.17 + Number one = new Number(1);
10.18 + Number two = new Number(2);
10.19 + Expression minus = new Minus(one, two);
10.20 +
10.21 + PrintVisitor print = new PrintVisitor();
10.22 + minus.visit(print); // fails with IllegalStateException
10.23 +
10.24 + assertEquals("1 - 2", print.sb.toString());
10.25 + }
10.26 +}