View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.dbutils;
18  
19  import static org.junit.Assert.assertTrue;
20  
21  import java.util.ServiceLoader;
22  
23  import org.apache.commons.dbutils.handlers.columns.TestColumnHandler;
24  import org.apache.commons.dbutils.handlers.properties.TestPropertyHandler;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  public class ServiceLoaderTest {
29      private ServiceLoader<ColumnHandler> columns;
30      private ServiceLoader<PropertyHandler> properties;
31  
32      @Before
33      public void setUp() {
34          columns = ServiceLoader.load(ColumnHandler.class);
35          properties = ServiceLoader.load(PropertyHandler.class);
36      }
37  
38      /**
39       * Verifying 'more than 1' shows that we found more than we loaded locally which assumes the core handlers
40       * were loaded, too.
41       */
42      @Test
43      public void testFindMoreThanLocalColumns() {
44          int count = 0;
45          for (final ColumnHandler<?> handler : columns) {
46              count++;
47          }
48  
49          assertTrue(count > 1);
50      }
51  
52      /**
53       * Verifying 'more than 1' shows that we found more than we loaded locally which assumes the core handlers
54       * were loaded, too.
55       */
56      @Test
57      public void testFindMoreThanLocalProperties() {
58          int count = 0;
59          for (final PropertyHandler handler : properties) {
60              count++;
61          }
62  
63          assertTrue(count > 1);
64      }
65  
66      @Test
67      public void testFindsLocalColumnHandler() {
68          boolean found = false;
69          for (final ColumnHandler<?> handler : columns) {
70              // this class is defined outside of the main classes in dbutils
71              if (handler instanceof TestColumnHandler) {
72                  found = true;
73              }
74          }
75  
76          assertTrue(found);
77      }
78  
79      @Test
80      public void testFindsLocalPropertyHandler() {
81          boolean found = false;
82          for (final PropertyHandler handler : properties) {
83              // this class is defined outside of the main classes in dbutils
84              if (handler instanceof TestPropertyHandler) {
85                  found = true;
86              }
87          }
88  
89          assertTrue(found);
90      }
91  
92  }