View Javadoc
1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.jdbc.spring;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.junit.Test;
23  import org.kuali.common.jdbc.context.SqlExecutionContext;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  
30  public class SqlConfigUtilsTest {
31  
32      private static final Logger logger = LoggerFactory.getLogger(SqlConfigUtilsTest.class);
33  
34      /**
35       * All of these keys should fail to provide a valid SqlExecutionContext
36       */
37      protected static final List<String> INVALID_EXECUTION_CONTEXT_PROPERTY_KEYS = Arrays.asList("sql.foo.bar.nonconcurrent", "", "aSingleInvalidToken", "foo.bar.sequential");
38  
39      protected static final String SIMPLE_SCHEMA_KEY = "sql.schema";
40  
41      protected static final String SIMPLE_DATA_KEY = "sql.data";
42  
43      protected static final String COMPLEX_KEY = "sql.a.very.long.series.of.period.separated.words";
44  
45      protected static final List<String> EXPECTED_EXECUTION_CONTEXT_GROUP_NAMES = Arrays.asList(SIMPLE_SCHEMA_KEY, SIMPLE_DATA_KEY, COMPLEX_KEY);
46  
47      protected static final List<String> VALID_EXECUTION_CONTEXT_PROPERTY_KEYS = Arrays.asList(SIMPLE_SCHEMA_KEY + ".concurrent", SIMPLE_DATA_KEY + ".sequential", COMPLEX_KEY + ".concurrent");
48  
49      @Test
50      public void testGetSqlExecutionContexts() {
51  
52          // ensure that invalid keys fail
53          for (String invalidKey : INVALID_EXECUTION_CONTEXT_PROPERTY_KEYS) {
54              try {
55                  List<SqlExecutionContext> contexts = SqlConfigUtils.getSqlExecutionContexts(Arrays.asList(invalidKey));
56                  fail("Invalid key: " + invalidKey + " did not throw expected exception, returned contexts: " + contexts.toString());
57              }
58              catch(Exception e) {
59                  // exception is expected here
60              }
61          }
62  
63          // ensure that valid keys work
64          List<SqlExecutionContext> contexts = SqlConfigUtils.getSqlExecutionContexts(VALID_EXECUTION_CONTEXT_PROPERTY_KEYS);
65  
66          List<String> expectedEmptyList = new ArrayList<String>(EXPECTED_EXECUTION_CONTEXT_GROUP_NAMES);
67  
68          for (SqlExecutionContext context : contexts) {
69              logger.info("Context with full property key " + context.getKey() + " has group of " + context.getGroup());
70              expectedEmptyList.remove(context.getGroup());
71          }
72  
73          assertTrue("Did not find expected group names in contexts: " + expectedEmptyList, expectedEmptyList.isEmpty());
74  
75      }
76  
77  }