1   package org.apache.torque.engine.database.model;
2   
3   /*
4    * Copyright 2001-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  import junit.framework.TestCase;
23  
24  /***
25   * <p>Unit tests for class <code>NameFactory</code> and known
26   * <code>NameGenerator</code> implementations.</p>
27   *
28   * <p>To add more tests, add entries to the <code>ALGORITHMS</code>,
29   * <code>INPUTS</code>, and <code>OUTPUTS</code> arrays, and code to
30   * the <code>makeInputs()</code> method.</p>
31   *
32   * <p>This test assumes that it's being run using the MySQL database
33   * adapter, <code>DBMM</code>.  MySQL has a column length limit of 64
34   * characters.</p>
35   *
36   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
37   * @version $Id: NameFactoryTest.java,v 1.1.2.2 2004/05/20 04:35:16 seade Exp $
38   */
39  public class NameFactoryTest extends TestCase
40  {
41  
42      /*** The database to mimic in generating the SQL. */
43      private static final String DATABASE_TYPE = "mysql";
44  
45      /***
46       * The list of known name generation algorithms, specified as the
47       * fully qualified class names to <code>NameGenerator</code>
48       * implementations.
49       */
50      private static final String[] ALGORITHMS =
51          { NameFactory.CONSTRAINT_GENERATOR, NameFactory.JAVA_GENERATOR };
52  
53      /***
54       * Two dimensional arrays of inputs for each algorithm.
55       */
56      private static final Object[][][] INPUTS =
57          { { { makeString(61), "I", new Integer(1)}, {
58                  makeString(61), "I", new Integer(2)
59                  }, {
60                  makeString(65), "I", new Integer(3)
61                  }, {
62                  makeString(4), "FK", new Integer(1)
63                  }, {
64                  makeString(5), "FK", new Integer(2)
65                  }
66          }, {
67              {
68                  "MY_USER", NameGenerator.CONV_METHOD_UNDERSCORE }, {
69                  "MY_USER", NameGenerator.CONV_METHOD_JAVANAME }, {
70                  "MY_USER", NameGenerator.CONV_METHOD_NOCHANGE }
71          }
72      };
73  
74      /***
75       * Given the known inputs, the expected name outputs.
76       */
77      private static final String[][] OUTPUTS =
78          {
79              {
80                  makeString(60) + "_I_1",
81                  makeString(60) + "_I_2",
82                  makeString(60) + "_I_3",
83                  makeString(4) + "_FK_1",
84                  makeString(5) + "_FK_2" },
85              {
86              "MyUser", "MYUSER", "MY_USER" }
87      };
88  
89      /***
90       * Used as an input.
91       */
92      private Database database;
93  
94      /***
95       * Creates a new instance.
96       *
97       * @param name the name of the test to run
98       */
99      public NameFactoryTest(String name)
100     {
101         super(name);
102     }
103 
104     /***
105      * Creates a string of the specified length consisting entirely of
106      * the character <code>A</code>.  Useful for simulating table
107      * names, etc.
108      *
109      * @param len the number of characters to include in the string
110      * @return a string of length <code>len</code> with every character an 'A'
111      */
112     private static final String makeString(int len)
113     {
114         StringBuffer buf = new StringBuffer();
115         for (int i = 0; i < len; i++)
116         {
117             buf.append('A');
118         }
119         return buf.toString();
120     }
121 
122     /*** Sets up the Torque model. */
123     public void setUp()
124     {
125         AppData appData = new AppData(DATABASE_TYPE, "src/templates/sql/base/");
126         database = new Database();
127         database.setDatabaseType(DATABASE_TYPE);
128         appData.addDatabase(database);
129     }
130 
131     /***
132      * @throws Exception on fail
133      */
134     public void testNames() throws Exception
135     {
136         for (int algoIndex = 0; algoIndex < ALGORITHMS.length; algoIndex++)
137         {
138             String algo = ALGORITHMS[algoIndex];
139             Object[][] algoInputs = INPUTS[algoIndex];
140             for (int i = 0; i < algoInputs.length; i++)
141             {
142                 List inputs = makeInputs(algo, algoInputs[i]);
143                 String generated = NameFactory.generateName(algo, inputs);
144                 String expected = OUTPUTS[algoIndex][i];
145                 assertEquals(
146                     "Algorithm " + algo + " failed to generate an unique name",
147                     generated,
148                     expected);
149             }
150         }
151     }
152 
153     /***
154      * Creates the list of arguments to pass to the specified type of
155      * <code>NameGenerator</code> implementation.
156      *
157      * @param algo The class name of the <code>NameGenerator</code> to
158      * create an argument list for.
159      * @param inputs The (possibly partial) list inputs from which to
160      * generate the final list.
161      * @return the list of arguments to pass to the <code>NameGenerator</code>
162      */
163     private final List makeInputs(String algo, Object[] inputs)
164     {
165         List list = null;
166         if (NameFactory.CONSTRAINT_GENERATOR.equals(algo))
167         {
168             list = new ArrayList(inputs.length + 1);
169             list.add(0, database);
170             list.addAll(Arrays.asList(inputs));
171         }
172         else if (NameFactory.JAVA_GENERATOR.equals(algo))
173         {
174             list = Arrays.asList(inputs);
175         }
176         return list;
177     }
178 
179 }