Skip to content

Commit 93d6c5d

Browse files
committed
Added code samples under test/
1 parent e291c33 commit 93d6c5d

File tree

1 file changed

+267
-0
lines changed

1 file changed

+267
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
/*
2+
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 com.oracle.adbaoverjdbc.test;
17+
18+
import jdk.incubator.sql2.AdbaType;
19+
import jdk.incubator.sql2.Connection;
20+
import jdk.incubator.sql2.DataSourceFactory;
21+
import jdk.incubator.sql2.DataSource;
22+
import jdk.incubator.sql2.Result;
23+
import jdk.incubator.sql2.SqlException;
24+
import jdk.incubator.sql2.Transaction;
25+
import java.util.Properties;
26+
import java.util.concurrent.CompletionStage;
27+
import java.util.concurrent.ForkJoinPool;
28+
import java.util.concurrent.TimeUnit;
29+
import java.util.stream.Collector;
30+
import org.junit.After;
31+
import org.junit.AfterClass;
32+
import org.junit.Before;
33+
import org.junit.BeforeClass;
34+
import org.junit.Test;
35+
import static org.junit.Assert.*;
36+
import static com.oracle.adbaoverjdbc.JdbcConnectionProperties.JDBC_CONNECTION_PROPERTIES;
37+
38+
/**
39+
* This is a quick and dirty test to check if anything at all is working.
40+
*
41+
* Depends on the scott/tiger schema availble here:
42+
* https://github.com/oracle/dotnet-db-samples/blob/master/schemas/scott.sql
43+
*/
44+
public class FirstLight {
45+
46+
//
47+
// EDIT THESE
48+
//
49+
// Define these three constants with the appropriate values for the database
50+
// and JDBC driver you want to use. Should work with ANY reasonably standard
51+
// JDBC driver. These values are passed to DriverManager.getConnection.
52+
public static final String URL = "jdbc:oracle:thin:@//den03cll.us.oracle.com:5521/main2.regress.rdbms.dev.us.oracle.com"; //"<JDBC driver connect string>";
53+
public static final String USER = "scott"; //<database user name>";
54+
public static final String PASSWORD = "tiger"; //<database user password>";
55+
// Define this to be the most trivial SELECT possible
56+
public static final String TRIVIAL = "SELECT 1 FROM DUAL";
57+
58+
59+
public static final String FACTORY_NAME = "com.oracle.adbaoverjdbc.DataSourceFactory";
60+
61+
public FirstLight() {
62+
}
63+
64+
@BeforeClass
65+
public static void setUpClass() {
66+
}
67+
68+
@AfterClass
69+
public static void tearDownClass() {
70+
}
71+
72+
@Before
73+
public void setUp() {
74+
}
75+
76+
@After
77+
public void tearDown() {
78+
}
79+
80+
/**
81+
* Verify that DataSourceFactory.forName works. Can't do anything without that.
82+
*/
83+
@Test
84+
public void firstLight() {
85+
assertEquals("com.oracle.adbaoverjdbc.DataSourceFactory",
86+
DataSourceFactory.forName(FACTORY_NAME).getClass().getName());
87+
}
88+
89+
/**
90+
* Verify that can create a DataSource, though not a Connection. Should work
91+
* even if there is no database.
92+
*/
93+
@Test
94+
public void createDataSource() {
95+
DataSourceFactory factory = DataSourceFactory.forName(FACTORY_NAME);
96+
DataSource ds = factory.builder()
97+
.url(URL)
98+
.username(USER)
99+
.password(PASSWORD)
100+
.build();
101+
assertNotNull(ds);
102+
}
103+
104+
/**
105+
* create a Connection and send a SQL to the database
106+
*/
107+
@Test
108+
public void sqlOperation() {
109+
Properties props = new Properties();
110+
props.setProperty("oracle.jdbc.implicitStatementCacheSize", "10");
111+
DataSourceFactory factory = DataSourceFactory.forName(FACTORY_NAME);
112+
DataSource ds = factory.builder()
113+
.url(URL)
114+
.username(USER)
115+
.password(PASSWORD)
116+
.connectionProperty(JDBC_CONNECTION_PROPERTIES, props)
117+
.build();
118+
Connection conn = ds.getConnection(t -> System.out.println("ERROR: " + t.getMessage()));
119+
try (conn) {
120+
assertNotNull(conn);
121+
conn.operation(TRIVIAL).submit();
122+
}
123+
ForkJoinPool.commonPool().awaitQuiescence(1, TimeUnit.MINUTES);
124+
}
125+
126+
/**
127+
* Execute a few trivial queries.
128+
*/
129+
@Test
130+
public void rowOperation() {
131+
Properties props = new Properties();
132+
props.setProperty("oracle.jdbc.implicitStatementCacheSize", "10");
133+
DataSourceFactory factory = DataSourceFactory.forName(FACTORY_NAME);
134+
try (DataSource ds = factory.builder()
135+
.url(URL)
136+
.username(USER)
137+
.password(PASSWORD)
138+
.connectionProperty(JDBC_CONNECTION_PROPERTIES, props)
139+
.build();
140+
Connection conn = ds.getConnection(t -> System.out.println("ERROR: " + t.getMessage()))) {
141+
assertNotNull(conn);
142+
conn.<Void>rowOperation(TRIVIAL)
143+
.collect(Collector.of(() -> null,
144+
(a, r) -> {
145+
System.out.println("Trivial: " + r.get("1", String.class));
146+
},
147+
(x, y) -> null))
148+
.submit();
149+
conn.<Integer>rowOperation("select * from emp")
150+
.collect(Collector.<Result.Row, int[], Integer>of(
151+
() -> new int[1],
152+
(int[] a, Result.Row r) -> {
153+
a[0] = a[0]+r.get("sal", Integer.class);
154+
},
155+
(l, r) -> l,
156+
a -> (Integer)a[0]))
157+
.submit()
158+
.getCompletionStage()
159+
.thenAccept( n -> {System.out.println("labor cost: " + n);})
160+
.toCompletableFuture();
161+
conn.<Integer>rowOperation("select * from emp where empno = ?")
162+
.set("1", 7782)
163+
.collect(Collector.of(
164+
() -> null,
165+
(a, r) -> {
166+
System.out.println("salary: $" + r.get("sal", Integer.class));
167+
},
168+
(l, r) -> null))
169+
.submit();
170+
}
171+
ForkJoinPool.commonPool().awaitQuiescence(1, TimeUnit.MINUTES);
172+
}
173+
174+
/**
175+
* check does error handling do anything
176+
*/
177+
@Test
178+
public void errorHandling() {
179+
Properties props = new Properties();
180+
props.setProperty("oracle.jdbc.implicitStatementCacheSize", "10");
181+
DataSourceFactory factory = DataSourceFactory.forName(FACTORY_NAME);
182+
try (DataSource ds = factory.builder()
183+
.url(URL)
184+
.username(USER)
185+
.password("invalid password")
186+
.connectionProperty(JDBC_CONNECTION_PROPERTIES, props)
187+
.build();
188+
Connection conn = ds.getConnection(t -> System.out.println("ERROR: " + t.toString()))) {
189+
conn.<Void>rowOperation(TRIVIAL)
190+
.collect(Collector.of(() -> null,
191+
(a, r) -> {
192+
System.out.println("Trivial: " + r.get("1", String.class));
193+
},
194+
(x, y) -> null))
195+
.onError( t -> { System.out.println(t.toString()); })
196+
.submit();
197+
}
198+
199+
try (DataSource ds = factory.builder()
200+
.url(URL)
201+
.username(USER)
202+
.password(PASSWORD)
203+
.connectionProperty(JDBC_CONNECTION_PROPERTIES, props)
204+
.build();
205+
Connection conn = ds.getConnection(t -> System.out.println("ERROR: " + t.toString()))) {
206+
conn.<Integer>rowOperation("select * from emp where empno = ?")
207+
.set("1", 7782)
208+
.collect(Collector.of(
209+
() -> null,
210+
(a, r) -> {
211+
System.out.println("salary: $" + r.get("sal", Integer.class));
212+
},
213+
(l, r) -> null))
214+
.onError( t -> { System.out.println(t.getMessage()); } )
215+
.submit();
216+
}
217+
ForkJoinPool.commonPool().awaitQuiescence(1, TimeUnit.MINUTES);
218+
}
219+
220+
/**
221+
* Do something that approximates real work. Do a transaction. Uses
222+
* Transaction, CompletionStage args, and catch Operation.
223+
*/
224+
@Test
225+
public void transaction() {
226+
Properties props = new Properties();
227+
props.setProperty("oracle.jdbc.implicitStatementCacheSize", "10");
228+
DataSourceFactory factory = DataSourceFactory.forName(FACTORY_NAME);
229+
try (DataSource ds = factory.builder()
230+
.url(URL)
231+
.username(USER)
232+
.password(PASSWORD)
233+
.connectionProperty(JDBC_CONNECTION_PROPERTIES, props)
234+
.build();
235+
Connection conn = ds.getConnection(t -> System.out.println("ERROR: " + t.toString()))) {
236+
Transaction trans = conn.transaction();
237+
CompletionStage<Integer> idF = conn.<Integer>rowOperation("select empno, ename from emp where ename = ? for update")
238+
.set("1", "CLARK", AdbaType.VARCHAR)
239+
.collect(Collector.of(
240+
() -> new int[1],
241+
(a, r) -> {a[0] = r.get("empno", Integer.class); },
242+
(l, r) -> null,
243+
a -> a[0])
244+
)
245+
.submit()
246+
.getCompletionStage();
247+
idF.thenAccept( id -> { System.out.println("id: " + id); } );
248+
conn.<Long>countOperation("update emp set deptno = ? where empno = ?")
249+
.set("1", 50, AdbaType.INTEGER)
250+
.set("2", idF, AdbaType.INTEGER)
251+
.apply(c -> {
252+
if (c.getCount() != 1L) {
253+
trans.setRollbackOnly();
254+
throw new SqlException("updated wrong number of rows", null, null, -1, null, -1);
255+
}
256+
return c.getCount();
257+
})
258+
.onError(t -> t.printStackTrace())
259+
.submit()
260+
.getCompletionStage()
261+
.thenAccept( c -> { System.out.println("updated rows: " + c); } );
262+
conn.catchErrors();
263+
conn.commitMaybeRollback(trans);
264+
}
265+
ForkJoinPool.commonPool().awaitQuiescence(1, TimeUnit.MINUTES);
266+
}
267+
}

0 commit comments

Comments
 (0)