You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by to...@apache.org on 2004/11/17 22:18:16 UTC
cvs commit: db-ojb/src/xdoclet/test/xdoclet/modules/ojb/tests AnonymousFieldTagTests.java
tomdz 2004/11/17 13:18:16
Modified: src/xdoclet/java/src/xdoclet/modules/ojb/constraints Tag:
OJB_1_0_RELEASE FieldDescriptorConstraints.java
lib Tag: OJB_1_0_RELEASE xdoclet-ojb-module-1.2.1.jar
src/xdoclet/test/xdoclet/modules/ojb/tests Tag:
OJB_1_0_RELEASE AnonymousFieldTagTests.java
Log:
Added check for the name-attribute of anonymous fields
Revision Changes Path
No revision
No revision
1.8.2.1 +56 -31 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.8.2.1
diff -u -r1.8 -r1.8.2.1
--- FieldDescriptorConstraints.java 10 Jun 2004 22:34:23 -0000 1.8
+++ FieldDescriptorConstraints.java 17 Nov 2004 21:18:16 -0000 1.8.2.1
@@ -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);
+ }
}
/**
@@ -236,9 +246,9 @@
if (defaultLength != null)
{
LogHelper.warn(true,
- FieldDescriptorConstraints.class,
- "ensureLength",
- "The field "+fieldDef.getName()+" in class "+fieldDef.getOwner().getName()+" has no length setting though its jdbc type requires it (in most databases); using default length of "+defaultLength);
+ FieldDescriptorConstraints.class,
+ "ensureLength",
+ "The field "+fieldDef.getName()+" in class "+fieldDef.getOwner().getName()+" has no length setting though its jdbc type requires it (in most databases); using default length of "+defaultLength);
fieldDef.setProperty(PropertyHelper.OJB_PROPERTY_LENGTH, defaultLength);
}
}
@@ -261,9 +271,9 @@
if (defaultPrecision != null)
{
LogHelper.warn(true,
- FieldDescriptorConstraints.class,
- "ensureLength",
- "The field "+fieldDef.getName()+" in class "+fieldDef.getOwner().getName()+" has no precision setting though its jdbc type requires it (in most databases); using default precision of "+defaultPrecision);
+ FieldDescriptorConstraints.class,
+ "ensureLength",
+ "The field "+fieldDef.getName()+" in class "+fieldDef.getOwner().getName()+" has no precision setting though its jdbc type requires it (in most databases); using default precision of "+defaultPrecision);
fieldDef.setProperty(PropertyHelper.OJB_PROPERTY_DEFAULT_PRECISION, defaultPrecision);
}
else if (fieldDef.hasProperty(PropertyHelper.OJB_PROPERTY_SCALE))
@@ -278,9 +288,9 @@
if (defaultScale != null)
{
LogHelper.warn(true,
- FieldDescriptorConstraints.class,
- "ensureLength",
- "The field "+fieldDef.getName()+" in class "+fieldDef.getOwner().getName()+" has no scale setting though its jdbc type requires it (in most databases); using default scale of "+defaultScale);
+ FieldDescriptorConstraints.class,
+ "ensureLength",
+ "The field "+fieldDef.getName()+" in class "+fieldDef.getOwner().getName()+" has no scale setting though its jdbc type requires it (in most databases); using default scale of "+defaultScale);
fieldDef.setProperty(PropertyHelper.OJB_PROPERTY_DEFAULT_SCALE, defaultScale);
}
else if (fieldDef.hasProperty(PropertyHelper.OJB_PROPERTY_PRECISION) || fieldDef.hasProperty(PropertyHelper.OJB_PROPERTY_DEFAULT_PRECISION))
@@ -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");
}
}
}
No revision
No revision
1.3.2.2 +77 -81 db-ojb/lib/xdoclet-ojb-module-1.2.1.jar
<<Binary file>>
No revision
No revision
1.4.2.1 +82 -24 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.4.2.1
diff -u -r1.4 -r1.4.2.1
--- AnonymousFieldTagTests.java 5 Apr 2004 12:16:16 -0000 1.4
+++ AnonymousFieldTagTests.java 17 Nov 2004 21:18:16 -0000 1.4.2.1
@@ -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
{
@@ -37,7 +37,8 @@
"package test;\n"+
"/** @ojb.class\n" +
" * @ojb.field name=\"attr\"\n"+
- " * jdbc-type=\"INTEGER\""+
" */\n"+
+ " * jdbc-type=\"INTEGER\""+
+ " */\n"+
"public class A {}\n");
assertEqualsOjbDescriptorFile(
@@ -192,29 +193,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 +243,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"));
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org