View Javadoc
1   /*
2    * Copyright (c) 2012-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.jdbc;
31  
32  import java.io.PrintWriter;
33  import java.sql.Connection;
34  import java.sql.Driver;
35  import java.sql.SQLException;
36  import java.util.Properties;
37  import java.util.logging.Logger;
38  import javax.sql.DataSource;
39  import lombok.EqualsAndHashCode;
40  import lombok.ToString;
41  
42  /**
43   * H2 data source, for unit testing.
44   *
45   * @since 0.13
46   */
47  @ToString
48  @EqualsAndHashCode(of = "name")
49  final class H2Source implements DataSource {
50  
51      /**
52       * H2 driver.
53       */
54      private static final Driver DRIVER = new org.h2.Driver();
55  
56      /**
57       * Unique name of the DB.
58       */
59      private final transient String name;
60  
61      /**
62       * Public ctor.
63       * @param dbname DB name
64       */
65      H2Source(final String dbname) {
66          this.name = dbname;
67      }
68  
69      @Override
70      public Connection getConnection() throws SQLException {
71          return H2Source.DRIVER.connect(
72              String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1", this.name),
73              new Properties()
74          );
75      }
76  
77      @Override
78      public Connection getConnection(final String username,
79          final String password) {
80          throw new UnsupportedOperationException("#getConnection()");
81      }
82  
83      @Override
84      public PrintWriter getLogWriter() {
85          throw new UnsupportedOperationException("#getLogWriter()");
86      }
87  
88      @Override
89      public void setLogWriter(final PrintWriter writer) {
90          throw new UnsupportedOperationException("#setLogWriter()");
91      }
92  
93      @Override
94      public void setLoginTimeout(final int seconds) {
95          throw new UnsupportedOperationException("#setLoginTimeout()");
96      }
97  
98      @Override
99      public int getLoginTimeout() {
100         throw new UnsupportedOperationException("#getLoginTimeout()");
101     }
102 
103     @Override
104     public Logger getParentLogger() {
105         throw new UnsupportedOperationException("#getParentLogger()");
106     }
107 
108     @Override
109     public <T> T unwrap(final Class<T> iface) {
110         throw new UnsupportedOperationException("#unwrap()");
111     }
112 
113     @Override
114     public boolean isWrapperFor(final Class<?> iface) {
115         throw new UnsupportedOperationException("#isWrapperFor()");
116     }
117 
118 }