db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject cvs commit: db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/constraints FieldDescriptorConstraints.java
Date Wed, 17 Nov 2004 21:20:36 GMT
tomdz       2004/11/17 13:20:36

  Modified:    src/xdoclet/test/xdoclet/modules/ojb/tests
                        AnonymousFieldTagTests.java
               lib      xdoclet-ojb-module-1.2.1.jar
               src/xdoclet/java/src/xdoclet/modules/ojb/constraints
                        FieldDescriptorConstraints.java
  Log:
  Added check for the name-attribute of anonymous fields
  
  Revision  Changes    Path
  1.5       +80 -23    db-ojb/src/xdoclet/test/xdoclet/modules/ojb/tests/AnonymousFieldTagTests.java
  
  Index: AnonymousFieldTagTests.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/xdoclet/test/xdoclet/modules/ojb/tests/AnonymousFieldTagTests.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- AnonymousFieldTagTests.java	5 Apr 2004 12:16:16 -0000	1.4
  +++ AnonymousFieldTagTests.java	17 Nov 2004 21:20:36 -0000	1.5
  @@ -18,7 +18,7 @@
   /**
    * Tests for the ojb.field tag placed in the class javadoc comment.
    *
  - * @author <a href="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
  + * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
    */
   public class AnonymousFieldTagTests extends OjbTestBase
   {
  @@ -192,29 +192,29 @@
               "public class A {}\n");
   
           assertEqualsOjbDescriptorFile(
  -                "<class-descriptor\n"+
  -                "    class=\"test.A\"\n"+
  -                "    table=\"A\"\n"+
  -                ">\n"+
  -                "    <field-descriptor\n"+
  -                "        name=\"attr\"\n"+
  -                "        column=\"attr\"\n"+
  -                "        jdbc-type=\"INTEGER\"\n"+
  -                "        access=\"anonymous\"\n"+
  -                "    >\n"+
  -                "    </field-descriptor>\n"+
  -                "</class-descriptor>",
  -                runOjbXDoclet(OJB_DEST_FILE));
  +            "<class-descriptor\n"+
  +            "    class=\"test.A\"\n"+
  +            "    table=\"A\"\n"+
  +            ">\n"+
  +            "    <field-descriptor\n"+
  +            "        name=\"attr\"\n"+
  +            "        column=\"attr\"\n"+
  +            "        jdbc-type=\"INTEGER\"\n"+
  +            "        access=\"anonymous\"\n"+
  +            "    >\n"+
  +            "    </field-descriptor>\n"+
  +            "</class-descriptor>",
  +            runOjbXDoclet(OJB_DEST_FILE));
           assertEqualsTorqueSchemaFile(
  -                "<database name=\"ojbtest\">\n"+
  -                "    <table name=\"A\">\n"+
  -                "        <column name=\"attr\"\n"+
  -                "                javaName=\"attr\"\n"+
  -                "                type=\"INTEGER\"\n"+
  -                "        />\n"+
  -                "    </table>\n"+
  -                "</database>",
  -                runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
  +            "<database name=\"ojbtest\">\n"+
  +            "    <table name=\"A\">\n"+
  +            "        <column name=\"attr\"\n"+
  +            "                javaName=\"attr\"\n"+
  +            "                type=\"INTEGER\"\n"+
  +            "        />\n"+
  +            "    </table>\n"+
  +            "</database>",
  +            runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
       }
   
       /**
  @@ -242,6 +242,63 @@
               "  *            jdbc-type=\"VARCHAR\""+
               "  */\n"+
               "public class C extends B {}\n");
  +
  +        assertNull(runOjbXDoclet(OJB_DEST_FILE));
  +        assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
  +    }
  +
  +    /**
  +     * Test: missing name attribute
  +     */
  +    public void testSimple7()
  +    {
  +        addClass(
  +            "test.A",
  +            "package test;\n"+
  +            "/** @ojb.class\n" +
  +            "  * @ojb.field column=\"attr\"\n"+
  +            "  *            jdbc-type=\"INTEGER\""+
  +            "  */\n"+
  +            "public class A {}\n");
  +
  +        assertNull(runOjbXDoclet(OJB_DEST_FILE));
  +        assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
  +    }
  +
  +    /**
  +     * Test: empty name attribute
  +     */
  +    public void testSimple8()
  +    {
  +        addClass(
  +            "test.A",
  +            "package test;\n"+
  +            "/** @ojb.class\n" +
  +            "  * @ojb.field name=\"\"\n"+
  +            "  *            jdbc-type=\"INTEGER\""+
  +            "  */\n"+
  +            "public class A {}\n");
  +
  +        assertNull(runOjbXDoclet(OJB_DEST_FILE));
  +        assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
  +    }
  +
  +    /**
  +     * Test: there exists a persistent field with same name
  +     */
  +    public void testSimple9()
  +    {
  +        addClass(
  +            "test.A",
  +            "package test;\n"+
  +            "/** @ojb.class\n" +
  +            "  * @ojb.field name=\"attr\"\n"+
  +            "  *            jdbc-type=\"INTEGER\""+
  +            "  */\n"+
  +            "public class A {\n"+
  +            " /** @ojb.field */\n"+
  +            "  private int attr;\n"+
  +            "}\n");
   
           assertNull(runOjbXDoclet(OJB_DEST_FILE));
           assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
  
  
  
  1.6       +77 -81    db-ojb/lib/xdoclet-ojb-module-1.2.1.jar
  
  	<<Binary file>>
  
  
  1.9       +47 -22    db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/FieldDescriptorConstraints.java
  
  Index: FieldDescriptorConstraints.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/FieldDescriptorConstraints.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- FieldDescriptorConstraints.java	10 Jun 2004 22:34:23 -0000	1.8
  +++ FieldDescriptorConstraints.java	17 Nov 2004 21:20:36 -0000	1.9
  @@ -24,16 +24,19 @@
   /**
    * Checks constraints for field descriptors. Note that constraints may modify the field
descriptor.
    *
  - * @author <a href="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
  + * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
    */
   public class FieldDescriptorConstraints extends FeatureDescriptorConstraints
   {
       /** The interface that conversion classes must implement */
       private final static String CONVERSION_INTERFACE = "org.apache.ojb.broker.accesslayer.conversions.FieldConversion";
       /** The allowed jdbc types */
  -    private static HashMap _jdbcTypes = new HashMap();
  +    private HashMap _jdbcTypes = new HashMap();
   
  -    static
  +    /**
  +     * Creates a new field descriptor constraints object.
  +     */
  +    public FieldDescriptorConstraints()
       {
           _jdbcTypes.put("BIT", null);
           _jdbcTypes.put("TINYINT", null);
  @@ -80,7 +83,14 @@
           checkLocking(fieldDef, checkLevel);
           checkSequenceName(fieldDef, checkLevel);
           checkId(fieldDef, checkLevel);
  -        checkAccess(fieldDef, checkLevel);
  +        if (fieldDef.isAnonymous())
  +        {
  +            checkAnonymous(fieldDef, checkLevel);
  +        }
  +        else
  +        {
  +            checkReadonlyAccessForNativePKs(fieldDef, checkLevel);
  +        }
       }
   
       /**
  @@ -375,13 +385,39 @@
       }
   
       /**
  -     * Checks the access value for anonymous fields.
  +     * Checks that native primarykey fields have readonly access, and warns if not.
  +     * 
  +     * @param fieldDef The field descriptor
  +     * @param checkLevel The current check level (this constraint is checked in basic and
strict)
  +     */
  +    private void checkReadonlyAccessForNativePKs(FieldDescriptorDef fieldDef, String checkLevel)
  +    {
  +        if (CHECKLEVEL_NONE.equals(checkLevel))
  +        {
  +            return;
  +        }
  +
  +        String access  = fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_ACCESS);
  +        String autoInc = fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_AUTOINCREMENT);
  +
  +        if ("database".equals(autoInc) && !"readonly".equals(access))
  +        {
  +            LogHelper.warn(true,
  +            		       FieldDescriptorConstraints.class,
  +						   "checkAccess",
  +						   "The field "+fieldDef.getName()+" in class "+fieldDef.getOwner().getName()+" is
set to database auto-increment. Therefore the field's access is set to 'readonly'.");
  +            fieldDef.setProperty(PropertyHelper.OJB_PROPERTY_ACCESS, "readonly");
  +        }
  +    }
  +
  +    /**
  +     * Checks anonymous fields.
        * 
        * @param fieldDef The field descriptor
        * @param checkLevel The current check level (this constraint is checked in basic and
strict)
        * @exception ConstraintException If the constraint has been violated
        */
  -    private void checkAccess(FieldDescriptorDef fieldDef, String checkLevel) throws ConstraintException
  +    private void checkAnonymous(FieldDescriptorDef fieldDef, String checkLevel) throws
ConstraintException
       {
           if (CHECKLEVEL_NONE.equals(checkLevel))
           {
  @@ -390,25 +426,14 @@
   
           String access = fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_ACCESS);
   
  -        if (fieldDef.isAnonymous())
  +        if (!"anonymous".equals(access))
           {
  -            if (!"anonymous".equals(access))
  -            {
  -            	throw new ConstraintException("The access property of the field "+fieldDef.getName()+"
inherited in class "+fieldDef.getOwner().getName()+" cannot be changed");
  -            }
  +            throw new ConstraintException("The access property of the field "+fieldDef.getName()+"
defined in class "+fieldDef.getOwner().getName()+" cannot be changed");
           }
  -        else
  -        {
  -            String autoInc = fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_AUTOINCREMENT);
   
  -            if ("database".equals(autoInc) && !"readonly".equals(access))
  -            {
  -                LogHelper.warn(true,
  -                		       FieldDescriptorConstraints.class,
  -							   "checkAccess",
  -							   "The field "+fieldDef.getName()+" in class "+fieldDef.getOwner().getName()+"
is set to database auto-increment. Therefore the field's access is set to 'readonly'.");
  -                fieldDef.setProperty(PropertyHelper.OJB_PROPERTY_ACCESS, "readonly");
  -            }
  +        if ((fieldDef.getName() == null) || (fieldDef.getName().length() == 0))
  +        {
  +            throw new ConstraintException("An anonymous field defined in class "+fieldDef.getOwner().getName()+"
has no name");
           }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message