1 package org.apache.ojb.broker.metadata.torque; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import junit.textui.TestRunner; 7 import org.apache.ojb.broker.metadata.FieldDescriptor; 8 import org.apache.ojb.broker.metadata.torque.TorqueFieldGenerator; 9 10 public class TorqueFieldGeneratorTest extends TestCase { 11 12 private static final String EXPECTED_STANDARD_FIELDS = 13 " <column name=\"JUnit column 1\" required=\"false\" autoIncrement=\"false\" primaryKey=\"false\" type=\"VARCHAR\"/>\n" + 14 " <column name=\"JUnit column 2\" required=\"true\" autoIncrement=\"true\" primaryKey=\"true\" type=\"DOUBLE\"/>\n"; 15 private static final String EXPECTED_MAPPING_FIELDS = 16 " <column name=\"JUnit column 1\" required=\"true\" autoIncrement=\"false\" primaryKey=\"true\" type=\"VARCHAR\"/>\n" + 17 " <column name=\"JUnit column 2\" required=\"true\" autoIncrement=\"false\" primaryKey=\"true\" type=\"DOUBLE\"/>\n"; 18 private static final String EXPECTED_STANDARD_FIELDS_NO_AUTO = 19 " <column name=\"JUnit column 1\" required=\"false\" primaryKey=\"false\" type=\"VARCHAR\"/>\n" + 20 " <column name=\"JUnit column 2\" required=\"true\" primaryKey=\"true\" type=\"DOUBLE\"/>\n"; 21 private static final String EXPECTED_MAPPING_FIELDS_NO_AUTO = 22 " <column name=\"JUnit column 1\" required=\"true\" primaryKey=\"true\" type=\"VARCHAR\"/>\n" + 23 " <column name=\"JUnit column 2\" required=\"true\" primaryKey=\"true\" type=\"DOUBLE\"/>\n"; 24 25 public TorqueFieldGeneratorTest(String name) { 26 super(name); 27 } 28 29 public void testGenerateFieldDescriptors() { 30 TorqueFieldGenerator fieldGenerator = new TorqueFieldGenerator(false); 31 StringBuffer buffer = new StringBuffer (); 32 33 FieldDescriptor[] descriptors = new FieldDescriptor[2]; 34 descriptors[0] = new FieldDescriptor(null, 1); 35 descriptors[0].setColumnName("JUnit column 1"); 36 descriptors[0].setColumnType("VARCHAR"); 37 38 descriptors[1] = new FieldDescriptor(null, 2); 39 descriptors[1].setColumnName("JUnit column 2"); 40 descriptors[1].setColumnType("DOUBLE"); 41 descriptors[1].setRequired(true); 42 descriptors[1].setAutoIncrement(true); 43 descriptors[1].setPrimaryKey(true); 44 45 fieldGenerator.generateFieldDescriptors(descriptors, buffer); 46 47 assertEquals("The fields were all hosed to pieces", EXPECTED_STANDARD_FIELDS, buffer.toString()); 48 49 fieldGenerator = new TorqueFieldGenerator(true); 50 buffer = new StringBuffer (); 51 52 descriptors = new FieldDescriptor[2]; 53 descriptors[0] = new FieldDescriptor(null, 1); 54 descriptors[0].setColumnName("JUnit column 1"); 55 descriptors[0].setColumnType("VARCHAR"); 56 57 descriptors[1] = new FieldDescriptor(null, 2); 58 descriptors[1].setColumnName("JUnit column 2"); 59 descriptors[1].setColumnType("DOUBLE"); 60 descriptors[1].setRequired(true); 61 descriptors[1].setAutoIncrement(true); 62 descriptors[1].setPrimaryKey(true); 63 64 fieldGenerator.generateFieldDescriptors(descriptors, buffer); 65 66 assertEquals("The fields were all hosed to pieces", EXPECTED_STANDARD_FIELDS_NO_AUTO, buffer.toString()); 67 } 68 69 public void testGenerateMappingFieldDescriptors() { 70 TorqueFieldGenerator fieldGenerator = new TorqueFieldGenerator(false); 71 StringBuffer buffer = new StringBuffer (); 72 73 FieldDescriptor[] descriptors = new FieldDescriptor[2]; 74 descriptors[0] = new FieldDescriptor(null, 1); 75 descriptors[0].setColumnName("JUnit column 1"); 76 descriptors[0].setColumnType("VARCHAR"); 77 78 descriptors[1] = new FieldDescriptor(null, 2); 79 descriptors[1].setColumnName("JUnit column 2"); 80 descriptors[1].setColumnType("DOUBLE"); 81 descriptors[1].setRequired(true); 82 descriptors[1].setAutoIncrement(true); 83 descriptors[1].setPrimaryKey(true); 84 85 fieldGenerator.generateMappingFieldDescriptors(descriptors, buffer); 86 87 assertEquals("The fields were all hosed to pieces", EXPECTED_MAPPING_FIELDS, buffer.toString()); 88 89 fieldGenerator = new TorqueFieldGenerator(true); 90 buffer = new StringBuffer (); 91 92 descriptors = new FieldDescriptor[2]; 93 descriptors[0] = new FieldDescriptor(null,1); 94 descriptors[0].setColumnName("JUnit column 1"); 95 descriptors[0].setColumnType("VARCHAR"); 96 97 descriptors[1] = new FieldDescriptor(null, 2); 98 descriptors[1].setColumnName("JUnit column 2"); 99 descriptors[1].setColumnType("DOUBLE"); 100 descriptors[1].setRequired(true); 101 descriptors[1].setAutoIncrement(true); 102 descriptors[1].setPrimaryKey(true); 103 104 fieldGenerator.generateMappingFieldDescriptors(descriptors, buffer); 105 106 assertEquals("The fields were all hosed to pieces", EXPECTED_MAPPING_FIELDS_NO_AUTO, buffer.toString()); 107 } 108 109 public static void main(String [] args) { 110 TestRunner.run(suite()); 111 } 112 113 public static Test suite() { 114 return new TestSuite(TorqueFieldGeneratorTest.class); 115 } 116 117 } 118 | Popular Tags |