1 package org.apache.torque.engine.database.model; 2 3 18 19 import java.util.ArrayList ; 20 import java.util.Arrays ; 21 import java.util.List ; 22 23 import org.apache.torque.engine.EngineException; 24 25 import junit.framework.TestCase; 26 27 42 public class NameFactoryTest extends TestCase 43 { 44 45 46 private static final String DATABASE_TYPE = "mysql"; 47 48 53 private static final String [] ALGORITHMS = 54 { NameFactory.CONSTRAINT_GENERATOR, NameFactory.JAVA_GENERATOR }; 55 56 59 private static final Object [][][] INPUTS = 60 { { { makeString(61), "I", new Integer (1)}, { 61 makeString(61), "I", new Integer (2) 62 }, { 63 makeString(65), "I", new Integer (3) 64 }, { 65 makeString(4), "FK", new Integer (1) 66 }, { 67 makeString(5), "FK", new Integer (2) 68 } 69 }, { 70 { 71 "MY_USER", 72 NameGenerator.CONV_METHOD_UNDERSCORE }, { 73 "MY_USER", 74 NameGenerator.CONV_METHOD_UNDERSCORE_OMIT_SCHEMA }, { 75 "MY_USER", 76 NameGenerator.CONV_METHOD_JAVANAME }, { 77 "MY_USER", 78 NameGenerator.CONV_METHOD_NOCHANGE }, { 79 "MY_SCHEMA.MY_USER", 80 NameGenerator.CONV_METHOD_UNDERSCORE }, { 81 "MY_SCHEMA.MY_USER", 82 NameGenerator.CONV_METHOD_UNDERSCORE_OMIT_SCHEMA }, { 83 "MY_SCHEMA.MY_USER", 84 NameGenerator.CONV_METHOD_JAVANAME } , { 85 "MY_SCHEMA.MY_USER", 86 NameGenerator.CONV_METHOD_NOCHANGE } 87 } 88 }; 89 90 93 private static final String [][] OUTPUTS = 94 { 95 { 96 makeString(60) + "_I_1", 97 makeString(60) + "_I_2", 98 makeString(60) + "_I_3", 99 makeString(4) + "_FK_1", 100 makeString(5) + "_FK_2" }, 101 { 102 "MyUser", 103 "MyUser", 104 "MYUSER", 105 "MY_USER", 106 "MySchemaMyUser", 107 "MyUser", 108 "MYSCHEMAMYUSER", 109 "MY_SCHEMA.MY_USER" 110 } 111 }; 112 113 116 private Database database; 117 118 123 public NameFactoryTest(String name) 124 { 125 super(name); 126 } 127 128 136 private static final String makeString(int len) 137 { 138 StringBuffer buf = new StringBuffer (); 139 for (int i = 0; i < len; i++) 140 { 141 buf.append('A'); 142 } 143 return buf.toString(); 144 } 145 146 147 public void setUp() 148 { 149 database = new Database(DATABASE_TYPE); 150 database.setDatabaseType(DATABASE_TYPE); 151 } 152 153 156 public void testNames() throws Exception 157 { 158 for (int algoIndex = 0; algoIndex < ALGORITHMS.length; algoIndex++) 159 { 160 String algo = ALGORITHMS[algoIndex]; 161 Object [][] algoInputs = INPUTS[algoIndex]; 162 for (int i = 0; i < algoInputs.length; i++) 163 { 164 List inputs = makeInputs(algo, algoInputs[i]); 165 String generated = NameFactory.generateName(algo, inputs); 166 String expected = OUTPUTS[algoIndex][i]; 167 assertEquals( 168 "Algorithm " + algo + " failed to generate an unique name", 169 generated, 170 expected); 171 } 172 } 173 } 174 175 178 public void testException() throws Exception 179 { 180 try 181 { 182 NameFactory.generateName("non.existing.class", new ArrayList ()); 183 assertTrue("Expected an EngineException", false); 184 } 185 catch (EngineException ex) 186 { 187 } 188 } 189 190 200 private final List makeInputs(String algo, Object [] inputs) 201 { 202 List list = null; 203 if (NameFactory.CONSTRAINT_GENERATOR.equals(algo)) 204 { 205 list = new ArrayList (inputs.length + 1); 206 list.add(0, database); 207 list.addAll(Arrays.asList(inputs)); 208 } 209 else if (NameFactory.JAVA_GENERATOR.equals(algo)) 210 { 211 list = Arrays.asList(inputs); 212 } 213 return list; 214 } 215 216 } 217 | Popular Tags |