This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 2973e6c CAMEL-12272: PgEventEndpoint should handle wrapped connections 2973e6c is described below commit 2973e6c5190ad92687c6bd53101b0027bad70848 Author: James Netherton <jamesnether...@gmail.com> AuthorDate: Fri Feb 16 09:42:38 2018 +0000 CAMEL-12272: PgEventEndpoint should handle wrapped connections --- .../camel/component/pgevent/PgEventEndpoint.java | 2 +- .../camel/component/pgevent/PgEventHelper.java | 44 ++++++++++++++ .../apache/camel/pgevent/PgEventConsumerTest.java | 5 +- .../apache/camel/pgevent/PgEventHelperTest.java | 69 ++++++++++++++++++++++ .../apache/camel/pgevent/PgEventProducerTest.java | 12 ++-- 5 files changed, 122 insertions(+), 10 deletions(-) diff --git a/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventEndpoint.java b/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventEndpoint.java index 18c28a2..ae4d903 100644 --- a/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventEndpoint.java +++ b/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventEndpoint.java @@ -84,7 +84,7 @@ public class PgEventEndpoint extends DefaultEndpoint { public final PGConnection initJdbc() throws Exception { PGConnection conn; if (this.getDatasource() != null) { - conn = (PGConnection) this.getDatasource().getConnection(); + conn = PgEventHelper.toPGConnection(this.getDatasource().getConnection()); } else { // ensure we can load the class ClassResolver classResolver = getCamelContext().getClassResolver(); diff --git a/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventHelper.java b/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventHelper.java new file mode 100644 index 0000000..01a0b62 --- /dev/null +++ b/components/camel-pgevent/src/main/java/org/apache/camel/component/pgevent/PgEventHelper.java @@ -0,0 +1,44 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.pgevent; + +import java.sql.Connection; +import java.sql.SQLException; + +import com.impossibl.postgres.api.jdbc.PGConnection; + +public final class PgEventHelper { + + private PgEventHelper() { + } + + public static PGConnection toPGConnection(Connection connection) throws SQLException { + if (connection == null) { + throw new IllegalArgumentException("Connection must not be null"); + } + + PGConnection conn; + if (connection instanceof PGConnection) { + conn = (PGConnection) connection; + } else if (connection.isWrapperFor(PGConnection.class)) { + conn = connection.unwrap(PGConnection.class); + } else { + throw new IllegalStateException("Cannot obtain PGConnection"); + } + return conn; + } +} diff --git a/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventConsumerTest.java b/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventConsumerTest.java index f8e8912..c0f92d4 100644 --- a/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventConsumerTest.java +++ b/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventConsumerTest.java @@ -47,7 +47,7 @@ public class PgEventConsumerTest { Processor processor = mock(Processor.class); when(endpoint.getDatasource()).thenReturn(dataSource); - when(endpoint.initJdbc()).thenReturn(connection); + when(dataSource.getConnection()).thenReturn(connection); when(connection.prepareStatement("LISTEN camel")).thenReturn(statement); when(endpoint.getChannel()).thenReturn("camel"); @@ -67,7 +67,7 @@ public class PgEventConsumerTest { Processor processor = mock(Processor.class); when(endpoint.getDatasource()).thenReturn(dataSource); - when(endpoint.initJdbc()).thenReturn(connection); + when(dataSource.getConnection()).thenReturn(connection); when(connection.prepareStatement("LISTEN camel")).thenReturn(statement); when(endpoint.getChannel()).thenReturn("camel"); when(connection.prepareStatement("UNLISTEN camel")).thenReturn(statement); @@ -98,5 +98,4 @@ public class PgEventConsumerTest { verify(message).setBody("some event"); verify(processor).process(exchange); } - } diff --git a/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventHelperTest.java b/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventHelperTest.java new file mode 100644 index 0000000..dc2b6dd --- /dev/null +++ b/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventHelperTest.java @@ -0,0 +1,69 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.pgevent; + +import java.sql.Connection; + +import com.impossibl.postgres.api.jdbc.PGConnection; + +import org.apache.camel.component.pgevent.PgEventHelper; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +import static org.junit.Assert.assertSame; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class PgEventHelperTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void testToPGConnectionWithNullConnection() throws Exception { + expectedException.expect(IllegalArgumentException.class); + PgEventHelper.toPGConnection(null); + } + + @Test + public void testToPGConnectionWithNonWrappedConnection() throws Exception { + Connection originalConnection = mock(PGConnection.class); + PGConnection actualConnection = PgEventHelper.toPGConnection(originalConnection); + assertSame(originalConnection, actualConnection); + } + + @Test + public void testToPGConnectionWithWrappedConnection() throws Exception { + Connection wrapperConnection = mock(Connection.class); + PGConnection unwrappedConnection = mock(PGConnection.class); + when(wrapperConnection.isWrapperFor(PGConnection.class)).thenReturn(true); + when(wrapperConnection.unwrap(PGConnection.class)).thenReturn(unwrappedConnection); + PGConnection actualConnection = PgEventHelper.toPGConnection(wrapperConnection); + assertSame(unwrappedConnection, actualConnection); + } + + @Test + public void testToPGConnectionWithInvalidWrappedConnection() throws Exception { + expectedException.expect(IllegalStateException.class); + Connection wrapperConnection = mock(Connection.class); + when(wrapperConnection.isWrapperFor(PGConnection.class)).thenReturn(false); + PgEventHelper.toPGConnection(wrapperConnection); + } +} diff --git a/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventProducerTest.java b/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventProducerTest.java index a2904bb..6e5df9b 100644 --- a/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventProducerTest.java +++ b/components/camel-pgevent/src/test/java/org/apache/camel/pgevent/PgEventProducerTest.java @@ -51,7 +51,7 @@ public class PgEventProducerTest { @Test public void testPgEventProducerStart() throws Exception { when(endpoint.getDatasource()).thenReturn(dataSource); - when(endpoint.initJdbc()).thenReturn(connection); + when(dataSource.getConnection()).thenReturn(connection); PgEventProducer producer = new PgEventProducer(endpoint); producer.start(); @@ -62,7 +62,7 @@ public class PgEventProducerTest { @Test public void testPgEventProducerStop() throws Exception { when(endpoint.getDatasource()).thenReturn(dataSource); - when(endpoint.initJdbc()).thenReturn(connection); + when(dataSource.getConnection()).thenReturn(connection); PgEventProducer producer = new PgEventProducer(endpoint); producer.start(); @@ -75,7 +75,7 @@ public class PgEventProducerTest { @Test(expected = InvalidStateException.class) public void testPgEventProducerProcessDbThrowsInvalidStateException() throws Exception { when(endpoint.getDatasource()).thenReturn(dataSource); - when(endpoint.initJdbc()).thenReturn(connection); + when(dataSource.getConnection()).thenReturn(connection); when(connection.isClosed()).thenThrow(new SQLException("DB problem occurred")); PgEventProducer producer = new PgEventProducer(endpoint); @@ -88,7 +88,7 @@ public class PgEventProducerTest { PGConnection connectionNew = mock(PGConnection.class); when(endpoint.getDatasource()).thenReturn(dataSource); - when(endpoint.initJdbc()).thenReturn(connection, connectionNew); + when(dataSource.getConnection()).thenReturn(connection, connectionNew); when(connection.isClosed()).thenReturn(true); when(exchange.getIn()).thenReturn(message); when(message.getBody(String.class)).thenReturn("pgevent"); @@ -108,7 +108,7 @@ public class PgEventProducerTest { when(endpoint.getDatasource()).thenReturn(dataSource); when(connection.isClosed()).thenReturn(false); - when(endpoint.initJdbc()).thenReturn(connection); + when(dataSource.getConnection()).thenReturn(connection); when(exchange.getIn()).thenReturn(message); when(message.getBody(String.class)).thenReturn("pgevent"); when(endpoint.getChannel()).thenReturn("camel"); @@ -126,7 +126,7 @@ public class PgEventProducerTest { public void testPgEventProducerProcessServerMinimumVersionNotMatched() throws Exception { when(endpoint.getDatasource()).thenReturn(dataSource); when(connection.isClosed()).thenReturn(false); - when(endpoint.initJdbc()).thenReturn(connection); + when(dataSource.getConnection()).thenReturn(connection); when(exchange.getIn()).thenReturn(message); when(message.getBody(String.class)).thenReturn("pgevent"); when(endpoint.getChannel()).thenReturn("camel"); -- To stop receiving notification emails like this one, please contact jamesnether...@apache.org.