1 package org.apache.torque.engine.database.model; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>. 55 */ 56 57 import java.util.ArrayList; 58 import java.util.Arrays; 59 import java.util.List; 60 import junit.framework.TestCase; 61 62 /*** 63 * <p>Unit tests for class <code>NameFactory</code> and known 64 * <code>NameGenerator</code> implementations.</p> 65 * 66 * <p>To add more tests, add entries to the <code>ALGORITHMS</code>, 67 * <code>INPUTS</code>, and <code>OUTPUTS</code> arrays, and code to 68 * the <code>makeInputs()</code> method.</p> 69 * 70 * <p>This test assumes that it's being run using the MySQL database 71 * adapter, <code>DBMM</code>. MySQL has a column length limit of 64 72 * characters.</p> 73 * 74 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a> 75 * @version $Id: NameFactoryTest.java,v 1.1 2003/02/10 13:21:36 mpoeschl Exp $ 76 */ 77 public class NameFactoryTest extends TestCase 78 { 79 80 /*** The database to mimic in generating the SQL. */ 81 private static final String DATABASE_TYPE = "mysql"; 82 83 /*** 84 * The list of known name generation algorithms, specified as the 85 * fully qualified class names to <code>NameGenerator</code> 86 * implementations. 87 */ 88 private static final String[] ALGORITHMS = 89 { NameFactory.CONSTRAINT_GENERATOR, NameFactory.JAVA_GENERATOR }; 90 91 /*** 92 * Two dimensional arrays of inputs for each algorithm. 93 */ 94 private static final Object[][][] INPUTS = 95 { { { makeString(61), "I", new Integer(1)}, { 96 makeString(61), "I", new Integer(2) 97 }, { 98 makeString(65), "I", new Integer(3) 99 }, { 100 makeString(4), "FK", new Integer(1) 101 }, { 102 makeString(5), "FK", new Integer(2) 103 } 104 }, { 105 { 106 "MY_USER", NameGenerator.CONV_METHOD_UNDERSCORE }, { 107 "MY_USER", NameGenerator.CONV_METHOD_JAVANAME }, { 108 "MY_USER", NameGenerator.CONV_METHOD_NOCHANGE } 109 } 110 }; 111 112 /*** 113 * Given the known inputs, the expected name outputs. 114 */ 115 private static final String[][] OUTPUTS = 116 { 117 { 118 makeString(60) + "_I_1", 119 makeString(60) + "_I_2", 120 makeString(60) + "_I_3", 121 makeString(4) + "_FK_1", 122 makeString(5) + "_FK_2" }, 123 { 124 "MyUser", "MYUSER", "MY_USER" } 125 }; 126 127 /*** 128 * Used as an input. 129 */ 130 private Database database; 131 132 /*** 133 * Creates a new instance. 134 * 135 * @param name the name of the test to run 136 */ 137 public NameFactoryTest(String name) 138 { 139 super(name); 140 } 141 142 /*** 143 * Creates a string of the specified length consisting entirely of 144 * the character <code>A</code>. Useful for simulating table 145 * names, etc. 146 * 147 * @param len the number of characters to include in the string 148 * @return a string of length <code>len</code> with every character an 'A' 149 */ 150 private static final String makeString(int len) 151 { 152 StringBuffer buf = new StringBuffer(); 153 for (int i = 0; i < len; i++) 154 { 155 buf.append('A'); 156 } 157 return buf.toString(); 158 } 159 160 /*** Sets up the Torque model. */ 161 public void setUp() 162 { 163 AppData appData = new AppData(DATABASE_TYPE, "src/templates/sql/base/"); 164 database = new Database(); 165 database.setDatabaseType(DATABASE_TYPE); 166 appData.addDatabase(database); 167 } 168 169 /*** 170 * @throws Exception on fail 171 */ 172 public void testNames() throws Exception 173 { 174 for (int algoIndex = 0; algoIndex < ALGORITHMS.length; algoIndex++) 175 { 176 String algo = ALGORITHMS[algoIndex]; 177 Object[][] algoInputs = INPUTS[algoIndex]; 178 for (int i = 0; i < algoInputs.length; i++) 179 { 180 List inputs = makeInputs(algo, algoInputs[i]); 181 String generated = NameFactory.generateName(algo, inputs); 182 String expected = OUTPUTS[algoIndex][i]; 183 assertEquals( 184 "Algorithm " + algo + " failed to generate an unique name", 185 generated, 186 expected); 187 } 188 } 189 } 190 191 /*** 192 * Creates the list of arguments to pass to the specified type of 193 * <code>NameGenerator</code> implementation. 194 * 195 * @param algo The class name of the <code>NameGenerator</code> to 196 * create an argument list for. 197 * @param inputs The (possibly partial) list inputs from which to 198 * generate the final list. 199 * @return the list of arguments to pass to the <code>NameGenerator</code> 200 */ 201 private final List makeInputs(String algo, Object[] inputs) 202 { 203 List list = null; 204 if (NameFactory.CONSTRAINT_GENERATOR.equals(algo)) 205 { 206 list = new ArrayList(inputs.length + 1); 207 list.add(0, database); 208 list.addAll(Arrays.asList(inputs)); 209 } 210 else if (NameFactory.JAVA_GENERATOR.equals(algo)) 211 { 212 list = Arrays.asList(inputs); 213 } 214 return list; 215 } 216 217 }

This page was automatically generated by Maven