View Javadoc

1   package org.apache.torque.generator.source.jdbc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.fail;
25  
26  import java.io.BufferedReader;
27  import java.io.InputStream;
28  import java.io.InputStreamReader;
29  import java.sql.Connection;
30  import java.sql.DriverManager;
31  import java.sql.SQLException;
32  import java.sql.Statement;
33  import java.util.List;
34  import java.util.StringTokenizer;
35  
36  import org.apache.log4j.BasicConfigurator;
37  import org.apache.log4j.Logger;
38  import org.apache.torque.generator.source.SourceElement;
39  import org.junit.Before;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  
43  public class JdbcMetadataSourceTest
44  {
45      private static Logger logger
46              = Logger.getLogger(JdbcMetadataSourceTest.class);
47  
48      private static String SQL_FILENAME = "jdbcMetadataSourceTest.sql";
49  
50      private static String SQL_CHARSET = "iso-8859-1";
51  
52      private static String URL = "jdbc:derby:memory:myDb;create=true";
53  
54      private static String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
55  
56      @BeforeClass
57      public static void beforeClass() throws Exception
58      {
59          Class.forName(DRIVER).newInstance();
60      }
61  
62      @Before
63      public void setUp() throws Exception
64      {
65          BasicConfigurator.configure();
66          Connection connection = DriverManager.getConnection(URL);
67          InputStream inputStream = getClass().getResourceAsStream(SQL_FILENAME);
68          BufferedReader bufferedReader = new BufferedReader(
69                  new InputStreamReader(inputStream, SQL_CHARSET));
70          String line;
71          StringBuilder queryBuffer = new StringBuilder();
72          while ((line = bufferedReader.readLine()) != null)
73          {
74              if (line.trim().length() == 0 || line.trim().startsWith("--"))
75              {
76                  continue;
77              }
78              queryBuffer.append(line);
79          }
80          StringTokenizer tokenizer
81              = new StringTokenizer(queryBuffer.toString(), ";", false);
82          while (tokenizer.hasMoreTokens())
83          {
84            String sqlQuery = tokenizer.nextToken();
85            Statement statement = connection.createStatement();
86            try
87            {
88              statement.execute(sqlQuery);
89            }
90            catch (SQLException e)
91            {
92              logger.info(e.getMessage());
93            }
94            statement.close();
95          }
96          connection.close();
97      }
98  
99      @Test
100     public void testExecute() throws Exception
101     {
102         JdbcMetadataSource source
103                 = new JdbcMetadataSource(DRIVER, URL, null, null, null);
104         SourceElement rootElement = source.createRootElement();
105         assertEquals("database", rootElement.getName());
106         List<SourceElement> tableElements = rootElement.getChildren();
107         assertEquals(2, tableElements.size());
108         for (SourceElement tableElement : tableElements)
109         {
110             assertEquals("table", tableElement.getName());
111             Object tableName = tableElement.getAttribute("name");
112             if ("AUTHOR".equals(tableName))
113             {
114                 List<SourceElement> columnElements
115                         = tableElement.getChildren();
116                 assertEquals(2, columnElements.size());
117                 for (SourceElement columnElement : columnElements)
118                 {
119                     assertEquals("column", columnElement.getName());
120                     Object columnName = columnElement.getAttribute("name");
121                     if ("AUTHOR_ID".equals(columnName))
122                     {
123                         assertEquals(
124                                 "true",
125                                 columnElement.getAttribute("primaryKey"));
126                         assertNull(columnElement.getAttribute("required"));
127                         assertEquals(
128                                 "INTEGER",
129                                 columnElement.getAttribute("type"));
130                         assertNull(columnElement.getAttribute("size"));
131                         assertNull(columnElement.getAttribute("scale"));
132                         // do not check default value as it is currently
133                         // not generated correctly
134                         // assertNull(columnElement.getAttribute("default"));
135                     }
136                     else if ("NAME".equals(columnName))
137                     {
138                         assertNull(columnElement.getAttribute("primaryKey"));
139                         assertEquals(
140                                 "true",
141                                 columnElement.getAttribute("required"));
142                         assertEquals(
143                                 "VARCHAR",
144                                 columnElement.getAttribute("type"));
145                         assertEquals(
146                                 "50",
147                                 columnElement.getAttribute("size"));
148                         assertNull(columnElement.getAttribute("scale"));
149                         assertNull(columnElement.getAttribute("default"));
150                     }
151                     else
152                     {
153                         fail("Unknown column : " + columnName);
154                     }
155                 }
156             }
157             else if ("BOOK".equals(tableName))
158             {
159                 assertEquals(5, tableElement.getChildren().size());
160                 List<SourceElement> columnElements
161                         = tableElement.getChildren("column");
162                 assertEquals(4, columnElements.size());
163                 for (SourceElement columnElement : columnElements)
164                 {
165                     assertEquals("column", columnElement.getName());
166                     Object columnName = columnElement.getAttribute("name");
167                     if ("BOOK_ID".equals(columnName))
168                     {
169                         assertEquals(
170                                 "true",
171                                 columnElement.getAttribute("primaryKey"));
172                         assertNull(columnElement.getAttribute("required"));
173                         assertEquals(
174                                 "INTEGER",
175                                 columnElement.getAttribute("type"));
176                         assertNull(columnElement.getAttribute("size"));
177                         assertNull(columnElement.getAttribute("scale"));
178                         // do not check default value as it is currently
179                         // not generated correctly
180                         // assertNull(columnElement.getAttribute("default"));
181                     }
182                     else if ("ISBN".equals(columnName))
183                     {
184                         assertNull(columnElement.getAttribute("primaryKey"));
185                         assertNull(columnElement.getAttribute("required"));
186                         assertEquals(
187                                 "VARCHAR",
188                                 columnElement.getAttribute("type"));
189                         assertEquals(
190                                 "15",
191                                 columnElement.getAttribute("size"));
192                         assertNull(columnElement.getAttribute("scale"));
193                         assertNull(columnElement.getAttribute("default"));
194                     }
195                     else if ("AUTHOR_ID".equals(columnName))
196                     {
197                         assertNull(columnElement.getAttribute("primaryKey"));
198                         assertEquals(
199                                 "true",
200                                 columnElement.getAttribute("required"));
201                         assertEquals(
202                                 "INTEGER",
203                                 columnElement.getAttribute("type"));
204                         assertNull(columnElement.getAttribute("size"));
205                         assertNull(columnElement.getAttribute("scale"));
206                         assertNull(columnElement.getAttribute("default"));
207                     }
208                     else if ("TITLE".equals(columnName))
209                     {
210                         assertNull(columnElement.getAttribute("primaryKey"));
211                         assertEquals(
212                                 "true",
213                                 columnElement.getAttribute("required"));
214                         assertEquals(
215                                 "VARCHAR",
216                                 columnElement.getAttribute("type"));
217                         assertEquals(
218                                 "255",
219                                 columnElement.getAttribute("size"));
220                         assertNull(columnElement.getAttribute("scale"));
221                         assertEquals(
222                                 "no title",
223                                 columnElement.getAttribute("default"));
224                     }
225                     else
226                     {
227                         fail("Unknown column : " + columnName);
228                     }
229                 }
230                 List<SourceElement> foreignKeyElements
231                         = tableElement.getChildren("foreign-key");
232                 assertEquals(1, foreignKeyElements.size());
233                 SourceElement foreignKeyElement = foreignKeyElements.get(0);
234                 assertEquals(
235                         "AUTHOR",
236                         foreignKeyElement.getAttribute("foreignTable"));
237             }
238             else
239             {
240                 fail("Unknown table : " + tableName);
241             }
242         }
243 //
244 //        String xml = new SourceToXml().toXml(rootElement, false);
245 //        System.out.println(xml);
246     }
247 }