tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jboy...@apache.org
Subject svn commit: r374515 - in /incubator/tuscany/java/sdo/impl/src: main/java/org/apache/tuscany/sdo/ main/java/org/apache/tuscany/sdo/codegen/ test/java/org/apache/tuscany/sdo/codegen/
Date Thu, 02 Feb 2006 21:43:13 GMT
Author: jboynes
Date: Thu Feb  2 13:43:05 2006
New Revision: 374515

URL: http://svn.apache.org/viewcvs?rev=374515&view=rev
Log:
simple source generator for SDO interfaces

Added:
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOPropertyVisitor.java
  (with props)
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOTypeVisitor.java
  (with props)
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java
  (with props)
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java
  (with props)
    incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/NoJavaImplementationException.java
  (with props)
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar1.java
  (with props)
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar2.java
  (with props)
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java
  (with props)

Added: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOPropertyVisitor.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOPropertyVisitor.java?rev=374515&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOPropertyVisitor.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOPropertyVisitor.java
Thu Feb  2 13:43:05 2006
@@ -0,0 +1,76 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tuscany.sdo;
+
+import java.lang.reflect.Method;
+
+import commonj.sdo.Property;
+
+/**
+ * Visitor interface that allows applications to visit SDO Property metadata.
+ * @version $Rev$ $Date$
+ */
+public interface SDOPropertyVisitor {
+    /**
+     * Visit the default value for this property.
+     * @param defaultValue the default value for this property
+     * @return this visitor
+     */
+    SDOPropertyVisitor visitDefault(Object defaultValue);
+
+    /**
+     * Visit the declaration of an alias for this property.
+     *
+     * @param aliasName an alias for this property
+     * @return this visitor
+     */
+    SDOPropertyVisitor visitAlias(String aliasName);
+
+    /**
+     * Visit the property opposite of this property.
+     * @param opposite the opposing property
+     * @return this visitor
+     */
+    SDOPropertyVisitor visitOpposite(Property opposite);
+
+    /**
+     * Visit a Java method that can be used to read this property. A Java interface must
be
+     * associated with this property's containing type and must contain this method.
+     * The method signature must follow the JavaBean naming convention and its return type
+     * must be compatible with the Java type of this property.
+     *
+     * @param getter a method that can be used to read this property
+     * @return this visitor
+     */
+    SDOPropertyVisitor visitGetter(Method getter);
+
+    /**
+     * Visit a Java method that can be used to set this property. A Java interface must be
+     * associated with this property's containing type and must contain this method.
+     * The method signature must follow the JavaBean naming convention and its parameter
type
+     * must be compatible with the Java type of this property.
+     *
+     * @param setter a method that can be used to read this property
+     * @return this visitor
+     */
+    SDOPropertyVisitor visitSetter(Method setter);
+
+    /**
+     * Indicate that this visitor will not be called again.
+     */
+    void visitEnd();
+}

Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOPropertyVisitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOPropertyVisitor.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOTypeVisitor.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOTypeVisitor.java?rev=374515&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOTypeVisitor.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOTypeVisitor.java
Thu Feb  2 13:43:05 2006
@@ -0,0 +1,82 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tuscany.sdo;
+
+import commonj.sdo.Type;
+
+import org.apache.tuscany.sdo.codegen.GenerationException;
+
+/**
+ * Visitor interface that allows applications to process SDO type metadata.
+ *
+ * @version $Rev$ $Date$
+ */
+public interface SDOTypeVisitor {
+    /**
+     * Visit the type definition.
+     *
+     * @param namespace  the namespace that the type is declared in
+     * @param name       the name of the type
+     * @param open       true if the type is open
+     * @param sequenced  true if the type is sequenced
+     * @param isAbstract true if the type is abstract
+     * @return this visitor
+     */
+    SDOTypeVisitor visitType(String namespace, String name, boolean open, boolean sequenced,
boolean isAbstract);
+
+    /**
+     * Visit the declaration of a base type this type extends.
+     *
+     * @param baseType a base type this type extends
+     * @return this visitor
+     */
+    SDOTypeVisitor visitBaseType(Type baseType) throws GenerationException;
+
+    /**
+     * Visit the declaration of an alias for this type.
+     *
+     * @param aliasName an alias for this type
+     * @return this visitor
+     */
+    SDOTypeVisitor visitAlias(String aliasName);
+
+    /**
+     * Visit a property declared by this type. Property indices are determined by the order
in which
+     * properties are visited.
+     *
+     * @param name     the name of the property
+     * @param type     the SDO type of the property
+     * @param many     true if the property is multi-valued
+     * @param contains true if the property is a containing property
+     * @param readOnly true if the property only supports read access through the SDO API
+     * @return a property visitor that can be used to visit the property
+     */
+    SDOPropertyVisitor visitProperty(String name, Type type, boolean many, boolean contains,
boolean readOnly) throws GenerationException;
+
+    /**
+     * Visit the Java type associated with this SDO type.
+     *
+     * @param javaType the Java type for an interface associated with this SDO type
+     * @return this visitor
+     */
+    SDOTypeVisitor visitJavaType(java.lang.reflect.Type javaType);
+
+    /**
+     * Indicate that this visitor will not be called again.
+     */
+    void visitEnd();
+}

Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOTypeVisitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/SDOTypeVisitor.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java?rev=374515&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java
Thu Feb  2 13:43:05 2006
@@ -0,0 +1,39 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tuscany.sdo.codegen;
+
+/**
+ * Exception indicating there was a problem with code generation.
+ *
+ * @version $Rev$ $Date$
+ */
+public abstract class GenerationException extends Exception {
+    public GenerationException() {
+    }
+
+    public GenerationException(String message) {
+        super(message);
+    }
+
+    public GenerationException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public GenerationException(Throwable cause) {
+        super(cause);
+    }
+}

Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/GenerationException.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java?rev=374515&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java
Thu Feb  2 13:43:05 2006
@@ -0,0 +1,180 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tuscany.sdo.codegen;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.List;
+import java.lang.reflect.Method;
+
+import commonj.sdo.Type;
+import commonj.sdo.Property;
+
+import org.apache.tuscany.sdo.SDOPropertyVisitor;
+import org.apache.tuscany.sdo.SDOTypeVisitor;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JavaInterfaceGenerator implements SDOTypeVisitor {
+    private String packageName;
+    private String interfaceName;
+    private List<Type> baseTypes = new ArrayList<Type>();
+    private List<PropVisitor> props = new ArrayList<PropVisitor>();
+
+    public SDOTypeVisitor visitType(String namespace, String name, boolean open, boolean
sequenced, boolean isAbstract) {
+        int lastDot = name.lastIndexOf('.');
+        if (lastDot == -1) {
+            packageName = null;
+            this.interfaceName = name;
+        } else {
+            packageName = name.substring(0, lastDot);
+            interfaceName = name.substring(lastDot + 1);
+        }
+        return this;
+    }
+
+    public SDOTypeVisitor visitBaseType(Type baseType) throws NoJavaImplementationException
{
+        if (baseType.getInstanceClass() == null) {
+            throw new NoJavaImplementationException(baseType);
+        }
+        baseTypes.add(baseType);
+        return this;
+    }
+
+    public SDOTypeVisitor visitAlias(String aliasName) {
+        return this;
+    }
+
+    public SDOPropertyVisitor visitProperty(String name, Type type, boolean many, boolean
contains, boolean readOnly) throws NoJavaImplementationException {
+        if (type.getInstanceClass() == null) {
+            throw new NoJavaImplementationException(type);
+        }
+        PropVisitor propVisitor = new PropVisitor(name, type, many, readOnly);
+        props.add(propVisitor);
+        return propVisitor;
+    }
+
+    public SDOTypeVisitor visitJavaType(java.lang.reflect.Type javaType) {
+        if (javaType instanceof Class<?>) {
+            Class<?> javaClass = (Class<?>) javaType;
+            packageName = javaClass.getPackage().getName();
+            interfaceName = javaClass.getSimpleName();
+        } else {
+            throw new UnsupportedOperationException();
+        }
+        return this;
+    }
+
+    public void visitEnd() {
+    }
+
+    public void writeTo(PrintWriter writer) {
+        if (packageName != null) {
+            writer.print("package ");
+            writer.print(packageName);
+            writer.println(';');
+            writer.println();
+        }
+        writer.print("public interface ");
+        writer.print(interfaceName);
+        for (int i = 0; i < baseTypes.size(); i++) {
+            Type type = baseTypes.get(i);
+            if (i == 0) {
+                writer.print(" extends ");
+            } else {
+                writer.print(", ");
+            }
+            writer.print(type.getInstanceClass().getName());
+        }
+        writer.println(" {");
+        for (PropVisitor prop : props) {
+            prop.writeTo(writer);
+        }
+        writer.println('}');
+    }
+
+    public String toString() {
+        StringWriter out = new StringWriter();
+        PrintWriter writer = new PrintWriter(out);
+        writeTo(writer);
+        writer.flush();
+        return out.toString();
+    }
+
+    private static class PropVisitor implements SDOPropertyVisitor {
+        private final String name;
+        private final Type type;
+        private final boolean many;
+        private final boolean readOnly;
+
+        public PropVisitor(String name, Type type, boolean many, boolean readOnly) {
+            this.name = name;
+            this.type = type;
+            this.many = many;
+            this.readOnly = readOnly;
+        }
+
+        public SDOPropertyVisitor visitDefault(Object defaultValue) {
+            return this;
+        }
+
+        public SDOPropertyVisitor visitAlias(String aliasName) {
+            throw new UnsupportedOperationException();
+        }
+
+        public SDOPropertyVisitor visitOpposite(Property opposite) {
+            return this;
+        }
+
+        public SDOPropertyVisitor visitGetter(Method getter) {
+            throw new UnsupportedOperationException();
+        }
+
+        public SDOPropertyVisitor visitSetter(Method setter) {
+            throw new UnsupportedOperationException();
+        }
+
+        public void visitEnd() {
+        }
+
+        public void writeTo(PrintWriter writer) {
+            String propertyName = Character.toUpperCase(name.charAt(0)) + name.substring(1);
+            String javaType = type.getInstanceClass().getCanonicalName();
+
+            if (!many) {
+                writer.print("    ");
+                writer.print(javaType);
+                writer.print("boolean".equals(javaType) ? " is" : " get");
+                writer.print(propertyName);
+                writer.println("();");
+                if (!readOnly) {
+                    writer.print("    void set");
+                    writer.print(propertyName);
+                    writer.print('(');
+                    writer.print(javaType);
+                    writer.println(" value);");
+                }
+            } else {
+                writer.print("    java.util.List get");
+                writer.print(propertyName);
+                writer.println("();");
+            }
+        }
+    }
+}

Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGenerator.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/NoJavaImplementationException.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/NoJavaImplementationException.java?rev=374515&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/NoJavaImplementationException.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/NoJavaImplementationException.java
Thu Feb  2 13:43:05 2006
@@ -0,0 +1,58 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tuscany.sdo.codegen;
+
+import commonj.sdo.Type;
+
+/**
+ * Exception that indicates there is no Java mapping for an SDO type.
+ *
+ * @version $Rev$ $Date$
+ */
+public class NoJavaImplementationException extends GenerationException {
+    private final Type type;
+
+    /**
+     * Constructor supplying the Type that did not have a Java implementation.
+     * A default message is provided of the form "${URI}#${Name}"
+     *
+     * @param type the type that did not have a Java implementation.
+     */
+    public NoJavaImplementationException(Type type) {
+        super(type == null ? null : type.getURI() + "#" + type.getName());
+        this.type = type;
+    }
+
+    /**
+     * Constructor supplying a message and the Type that did not have a Java implementation.
+     *
+     * @param message the message
+     * @param type the type that did not have a Java implementation.
+     */
+    public NoJavaImplementationException(String message, Type type) {
+        super(message);
+        this.type = type;
+    }
+
+    /**
+     * Return the type that did not have a Java implementation.
+     * @return the type that did not have a Java implementation
+     */
+    public Type getType() {
+        return type;
+    }
+}

Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/NoJavaImplementationException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/codegen/NoJavaImplementationException.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar1.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar1.java?rev=374515&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar1.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar1.java
Thu Feb  2 13:43:05 2006
@@ -0,0 +1,23 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tuscany.sdo.codegen;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public interface Bar1 {
+}

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar1.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar1.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar2.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar2.java?rev=374515&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar2.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar2.java
Thu Feb  2 13:43:05 2006
@@ -0,0 +1,23 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tuscany.sdo.codegen;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public interface Bar2 {
+}

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar2.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java?rev=374515&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java
(added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java
Thu Feb  2 13:43:05 2006
@@ -0,0 +1,189 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tuscany.sdo.codegen;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+import commonj.sdo.Type;
+import commonj.sdo.Property;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JavaInterfaceGeneratorTestCase extends TestCase {
+    private static final String SEP = System.getProperty("line.separator");
+
+    private JavaInterfaceGenerator gen;
+
+    public void testHeaderNoBaseClass() {
+        gen.visitType(null, "Foo", false, false, false);
+        gen.visitEnd();
+        assertEquals(localize("public interface Foo {\n}\n"), gen.toString());
+    }
+
+    public void testHeaderWithPackage() {
+        gen.visitType(null, "org.apache.Foo", false, false, false);
+        gen.visitEnd();
+        assertEquals(localize("package org.apache;\n\npublic interface Foo {\n}\n"), gen.toString());
+    }
+
+    public void testJavaTypeOverridesSDO() {
+        gen.visitType(null, "org.apache.Foo", false, false, false);
+        gen.visitJavaType(Bar1.class);
+        gen.visitEnd();
+        assertEquals(localize("package org.apache.tuscany.sdo.codegen;\n\npublic interface
Bar1 {\n}\n"), gen.toString());
+    }
+
+    public void testHeaderOneBaseClass() throws NoJavaImplementationException {
+        gen.visitType(null, "Foo", false, false, false);
+        gen.visitBaseType(new MockType(Bar1.class));
+        gen.visitEnd();
+        assertEquals(localize("public interface Foo extends org.apache.tuscany.sdo.codegen.Bar1
{\n}\n"), gen.toString());
+    }
+
+    public void testHeaderMultipleBaseClass() throws NoJavaImplementationException {
+        gen.visitType(null, "Foo", false, false, false);
+        gen.visitBaseType(new MockType(Bar1.class));
+        gen.visitBaseType(new MockType(Bar2.class));
+        gen.visitEnd();
+        assertEquals(localize("public interface Foo extends org.apache.tuscany.sdo.codegen.Bar1,
org.apache.tuscany.sdo.codegen.Bar2 {\n}\n"), gen.toString());
+    }
+
+    public void testBooleanProperty() throws NoJavaImplementationException {
+        gen.visitType(null, "Foo", false, false, false);
+        gen.visitProperty("true", new MockType(Boolean.TYPE), false, false, false);
+        gen.visitEnd();
+        assertEquals(localize("public interface Foo {\n\tboolean isTrue();\n\tvoid setTrue(boolean
value);\n}\n"), gen.toString());
+    }
+
+    public void testByteArrayProperty() throws NoJavaImplementationException {
+        gen.visitType(null, "Foo", false, false, false);
+        gen.visitProperty("bytes", new MockType(byte[].class), false, false, false);
+        gen.visitEnd();
+        assertEquals(localize("public interface Foo {\n\tbyte[] getBytes();\n\tvoid setBytes(byte[]
value);\n}\n"), gen.toString());
+    }
+
+    public void testObjectProperty() throws NoJavaImplementationException {
+        gen.visitType(null, "Foo", false, false, false);
+        gen.visitProperty("bar", new MockType(Bar1.class), false, false, false);
+        gen.visitEnd();
+        assertEquals(localize("public interface Foo {\n\torg.apache.tuscany.sdo.codegen.Bar1
getBar();\n\tvoid setBar(org.apache.tuscany.sdo.codegen.Bar1 value);\n}\n"), gen.toString());
+    }
+
+    public void testReadOnlyProperty() throws NoJavaImplementationException {
+        gen.visitType(null, "Foo", false, false, false);
+        gen.visitProperty("int", new MockType(Integer.TYPE), false, false, true);
+        gen.visitEnd();
+        assertEquals(localize("public interface Foo {\n\tint getInt();\n}\n"), gen.toString());
+    }
+
+    public void testManyProperty() throws NoJavaImplementationException {
+        gen.visitType(null, "Foo", false, false, false);
+        gen.visitProperty("list", new MockType(Integer.TYPE), true, false, false);
+        gen.visitEnd();
+        assertEquals(localize("public interface Foo {\n\tjava.util.List getList();\n}\n"),
gen.toString());
+    }
+
+    public void testTwoProperties() throws NoJavaImplementationException {
+        gen.visitType(null, "Foo", false, false, false);
+        gen.visitProperty("true", new MockType(Boolean.TYPE), false, false, false);
+        gen.visitProperty("int", new MockType(Integer.TYPE), false, false, false);
+        gen.visitEnd();
+        assertEquals(localize("public interface Foo {\n\tboolean isTrue();\n\tvoid setTrue(boolean
value);\n\tint getInt();\n\tvoid setInt(int value);\n}\n"), gen.toString());
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        gen = new JavaInterfaceGenerator();
+    }
+
+    private String localize(String s) {
+        StringBuilder b = new StringBuilder(s.length() + s.length()/10);
+        for (int i = 0; i < s.length(); i++) {
+            char ch = s.charAt(i);
+            if (ch == '\t') {
+                b.append("    ");
+            } else if (ch == '\n') {
+                b.append(SEP);
+            } else {
+                b.append(ch);
+            }
+        }
+        return b.toString();
+    }
+
+    private static class MockType implements Type {
+        private final Class javaClass;
+
+        public MockType(Class javaClass) {
+            this.javaClass = javaClass;
+        }
+
+        public String getName() {
+            return javaClass.toString();
+        }
+
+        public String getURI() {
+            throw new UnsupportedOperationException();
+        }
+
+        public Class getInstanceClass() {
+            return javaClass;
+        }
+
+        public boolean isInstance(Object object) {
+            throw new UnsupportedOperationException();
+        }
+
+        public List /*Property*/ getProperties() {
+            throw new UnsupportedOperationException();
+        }
+
+        public Property getProperty(String propertyName) {
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean isDataType() {
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean isOpen() {
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean isSequenced() {
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean isAbstract() {
+            throw new UnsupportedOperationException();
+        }
+
+        public List /*Type*/ getBaseTypes() {
+            throw new UnsupportedOperationException();
+        }
+
+        public List /*Property*/ getDeclaredProperties() {
+            throw new UnsupportedOperationException();
+        }
+
+        public List /*String*/ getAliasNames() {
+            throw new UnsupportedOperationException();
+        }
+    }
+}

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date



Mime
View raw message