@ -17,10 +17,13 @@
@@ -17,10 +17,13 @@
package org.apache.kafka.connect.runtime.isolation ;
import org.apache.kafka.connect.connector.Connector ;
import org.apache.kafka.common.config.provider.ConfigProvider ;
import org.apache.kafka.common.config.provider.FileConfigProvider ;
import org.apache.kafka.connect.json.JsonConverter ;
import org.apache.kafka.connect.sink.SinkConnector ;
import org.apache.kafka.connect.source.SourceConnector ;
import org.apache.kafka.connect.storage.Converter ;
import org.apache.kafka.connect.storage.HeaderConverter ;
import org.apache.kafka.connect.transforms.Transformation ;
import org.apache.kafka.connect.transforms.predicates.Predicate ;
import org.junit.Before ;
@ -31,7 +34,10 @@ import java.nio.file.Paths;
@@ -31,7 +34,10 @@ import java.nio.file.Paths;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertNotEquals ;
import static org.junit.Assert.assertThrows ;
import static org.junit.Assert.assertTrue ;
import static org.mockito.Mockito.mock ;
import static org.mockito.Mockito.when ;
public class PluginDescTest {
private final ClassLoader systemLoader = ClassLoader . getSystemClassLoader ( ) ;
@ -40,41 +46,47 @@ public class PluginDescTest {
@@ -40,41 +46,47 @@ public class PluginDescTest {
private final String snapshotVersion = "1.0.0-SNAPSHOT" ;
private final String noVersion = "undefined" ;
private PluginClassLoader pluginLoader ;
private PluginClassLoader otherPluginLoader ;
@Before
public void setUp ( ) throws Exception {
// Fairly simple use case, thus no need to create a random directory here yet.
URL location = Paths . get ( "/tmp" ) . toUri ( ) . toURL ( ) ;
URL otherLocation = Paths . get ( "/tmp-other" ) . toUri ( ) . toURL ( ) ;
// Normally parent will be a DelegatingClassLoader.
pluginLoader = new PluginClassLoader ( location , new URL [ 0 ] , systemLoader ) ;
otherPluginLoader = new PluginClassLoader ( otherLocation , new URL [ 0 ] , systemLoader ) ;
}
@SuppressWarnings ( "rawtypes" )
@Test
public void testRegularPluginDesc ( ) {
PluginDesc < Connector > connectorDesc = new PluginDesc < > (
Connector . class ,
PluginDesc < Sink Connector> connectorDesc = new PluginDesc < > (
Sink Connector. class ,
regularVersion ,
PluginType . SINK ,
pluginLoader
) ;
assertPluginDesc ( connectorDesc , Connector . class , regularVersion , pluginLoader . location ( ) ) ;
assertPluginDesc ( connectorDesc , Sink Connector. class , regularVersion , PluginType . SINK , pluginLoader . location ( ) ) ;
PluginDesc < Converter > converterDesc = new PluginDesc < > (
Converter . class ,
snapshotVersion ,
PluginType . CONVERTER ,
pluginLoader
) ;
assertPluginDesc ( converterDesc , Converter . class , snapshotVersion , pluginLoader . location ( ) ) ;
assertPluginDesc ( converterDesc , Converter . class , snapshotVersion , PluginType . CONVERTER , pluginLoader . location ( ) ) ;
PluginDesc < Transformation > transformDesc = new PluginDesc < > (
Transformation . class ,
noVersion ,
PluginType . TRANSFORMATION ,
pluginLoader
) ;
assertPluginDesc ( transformDesc , Transformation . class , noVersion , pluginLoader . location ( ) ) ;
assertPluginDesc ( transformDesc , Transformation . class , noVersion , PluginType . TRANSFORMATION , pluginLoader . location ( ) ) ;
}
@SuppressWarnings ( "rawtypes" )
@ -84,26 +96,29 @@ public class PluginDescTest {
@@ -84,26 +96,29 @@ public class PluginDescTest {
PluginDesc < SinkConnector > connectorDesc = new PluginDesc < > (
SinkConnector . class ,
regularVersion ,
PluginType . SINK ,
systemLoader
) ;
assertPluginDesc ( connectorDesc , SinkConnector . class , regularVersion , location ) ;
assertPluginDesc ( connectorDesc , SinkConnector . class , regularVersion , PluginType . SINK , location ) ;
PluginDesc < Converter > converterDesc = new PluginDesc < > (
Converter . class ,
snapshotVersion ,
PluginType . CONVERTER ,
systemLoader
) ;
assertPluginDesc ( converterDesc , Converter . class , snapshotVersion , location ) ;
assertPluginDesc ( converterDesc , Converter . class , snapshotVersion , PluginType . CONVERTER , location ) ;
PluginDesc < Transformation > transformDesc = new PluginDesc < > (
Transformation . class ,
noVersion ,
PluginType . TRANSFORMATION ,
systemLoader
) ;
assertPluginDesc ( transformDesc , Transformation . class , noVersion , location ) ;
assertPluginDesc ( transformDesc , Transformation . class , noVersion , PluginType . TRANSFORMATION , location ) ;
}
@Test
@ -112,6 +127,7 @@ public class PluginDescTest {
@@ -112,6 +127,7 @@ public class PluginDescTest {
PluginDesc < SourceConnector > connectorDesc = new PluginDesc < > (
SourceConnector . class ,
null ,
PluginType . SOURCE ,
pluginLoader
) ;
@ -119,6 +135,7 @@ public class PluginDescTest {
@@ -119,6 +135,7 @@ public class PluginDescTest {
connectorDesc ,
SourceConnector . class ,
nullVersion ,
PluginType . SOURCE ,
pluginLoader . location ( )
) ;
@ -126,24 +143,27 @@ public class PluginDescTest {
@@ -126,24 +143,27 @@ public class PluginDescTest {
PluginDesc < Converter > converterDesc = new PluginDesc < > (
Converter . class ,
null ,
PluginType . CONVERTER ,
systemLoader
) ;
assertPluginDesc ( converterDesc , Converter . class , nullVersion , location ) ;
assertPluginDesc ( converterDesc , Converter . class , nullVersion , PluginType . CONVERTER , location ) ;
}
@SuppressWarnings ( "rawtypes" )
@Test
public void testPluginDescEquality ( ) {
PluginDesc < Connector > connectorDescPluginPath = new PluginDesc < > (
Connector . class ,
PluginDesc < Sink Connector> connectorDescPluginPath = new PluginDesc < > (
Sink Connector. class ,
snapshotVersion ,
PluginType . SINK ,
pluginLoader
) ;
PluginDesc < Connector > connectorDescClasspath = new PluginDesc < > (
Connector . class ,
PluginDesc < Sink Connector> connectorDescClasspath = new PluginDesc < > (
Sink Connector. class ,
snapshotVersion ,
PluginType . SINK ,
systemLoader
) ;
@ -153,12 +173,14 @@ public class PluginDescTest {
@@ -153,12 +173,14 @@ public class PluginDescTest {
PluginDesc < Converter > converterDescPluginPath = new PluginDesc < > (
Converter . class ,
noVersion ,
PluginType . CONVERTER ,
pluginLoader
) ;
PluginDesc < Converter > converterDescClasspath = new PluginDesc < > (
Converter . class ,
noVersion ,
PluginType . CONVERTER ,
systemLoader
) ;
@ -168,30 +190,34 @@ public class PluginDescTest {
@@ -168,30 +190,34 @@ public class PluginDescTest {
PluginDesc < Transformation > transformDescPluginPath = new PluginDesc < > (
Transformation . class ,
null ,
PluginType . TRANSFORMATION ,
pluginLoader
) ;
PluginDesc < Transformation > transformDescClasspath = new PluginDesc < > (
Transformation . class ,
noVersion ,
PluginType . TRANSFORMATION ,
pluginLoader
) ;
assertNotEquals ( transformDescPluginPath , transformDescClasspath ) ;
}
@SuppressWarnings ( "rawtypes" )
@SuppressWarnings ( { "rawtypes" , "unchecked" } )
@Test
public void testPluginDescComparison ( ) {
PluginDesc < Connector > connectorDescPluginPath = new PluginDesc < > (
Connector . class ,
PluginDesc < Sink Connector> connectorDescPluginPath = new PluginDesc < > (
Sink Connector. class ,
regularVersion ,
PluginType . SINK ,
pluginLoader
) ;
PluginDesc < Connector > connectorDescClasspath = new PluginDesc < > (
Connector . class ,
PluginDesc < Sink Connector> connectorDescClasspath = new PluginDesc < > (
Sink Connector. class ,
newerVersion ,
PluginType . SINK ,
systemLoader
) ;
@ -200,12 +226,14 @@ public class PluginDescTest {
@@ -200,12 +226,14 @@ public class PluginDescTest {
PluginDesc < Converter > converterDescPluginPath = new PluginDesc < > (
Converter . class ,
noVersion ,
PluginType . CONVERTER ,
pluginLoader
) ;
PluginDesc < Converter > converterDescClasspath = new PluginDesc < > (
Converter . class ,
snapshotVersion ,
PluginType . CONVERTER ,
systemLoader
) ;
@ -214,12 +242,14 @@ public class PluginDescTest {
@@ -214,12 +242,14 @@ public class PluginDescTest {
PluginDesc < Transformation > transformDescPluginPath = new PluginDesc < > (
Transformation . class ,
null ,
PluginType . TRANSFORMATION ,
pluginLoader
) ;
PluginDesc < Transformation > transformDescClasspath = new PluginDesc < > (
Transformation . class ,
regularVersion ,
PluginType . TRANSFORMATION ,
systemLoader
) ;
@ -228,33 +258,87 @@ public class PluginDescTest {
@@ -228,33 +258,87 @@ public class PluginDescTest {
PluginDesc < Predicate > predicateDescPluginPath = new PluginDesc < > (
Predicate . class ,
regularVersion ,
PluginType . PREDICATE ,
pluginLoader
) ;
PluginDesc < Predicate > predicateDescClasspath = new PluginDesc < > (
Predicate . class ,
regularVersion ,
PluginType . PREDICATE ,
systemLoader
) ;
assertNewer ( predicateDescPluginPath , predicateDescClasspath ) ;
PluginDesc < ConfigProvider > configProviderDescPluginPath = new PluginDesc < > (
FileConfigProvider . class ,
regularVersion ,
PluginType . CONFIGPROVIDER ,
pluginLoader
) ;
PluginDesc < ConfigProvider > configProviderDescOtherPluginLoader = new PluginDesc < > (
FileConfigProvider . class ,
regularVersion ,
PluginType . CONFIGPROVIDER ,
otherPluginLoader
) ;
assertTrue ( "Different plugin loaders should have an ordering" ,
configProviderDescPluginPath . compareTo ( configProviderDescOtherPluginLoader ) ! = 0 ) ;
PluginDesc < Converter > jsonConverterPlugin = new PluginDesc < > (
JsonConverter . class ,
regularVersion ,
PluginType . CONVERTER ,
systemLoader
) ;
PluginDesc < HeaderConverter > jsonHeaderConverterPlugin = new PluginDesc < > (
JsonConverter . class ,
regularVersion ,
PluginType . HEADER_CONVERTER ,
systemLoader
) ;
assertNewer ( jsonConverterPlugin , jsonHeaderConverterPlugin ) ;
}
@Test
public void testNullArguments ( ) {
// Null version is acceptable
PluginDesc < SinkConnector > sink = new PluginDesc < > ( SinkConnector . class , null , PluginType . SINK , systemLoader ) ;
assertEquals ( "null" , sink . version ( ) ) ;
// Direct nulls are not acceptable for other arguments
assertThrows ( NullPointerException . class , ( ) - > new PluginDesc < > ( null , regularVersion , PluginType . SINK , systemLoader ) ) ;
assertThrows ( NullPointerException . class , ( ) - > new PluginDesc < > ( SinkConnector . class , regularVersion , null , systemLoader ) ) ;
assertThrows ( NullPointerException . class , ( ) - > new PluginDesc < > ( SinkConnector . class , regularVersion , PluginType . SINK , null ) ) ;
// PluginClassLoaders must have non-null locations
PluginClassLoader nullLocationLoader = mock ( PluginClassLoader . class ) ;
when ( nullLocationLoader . location ( ) ) . thenReturn ( null ) ;
assertThrows ( NullPointerException . class , ( ) - > new PluginDesc < > ( SinkConnector . class , regularVersion , PluginType . SINK , nullLocationLoader ) ) ;
}
private static < T > void assertPluginDesc (
PluginDesc < T > desc ,
Class < ? extends T > klass ,
String version ,
PluginType type ,
String location
) {
assertEquals ( desc . pluginClass ( ) , klass ) ;
assertEquals ( desc . className ( ) , klass . getName ( ) ) ;
assertEquals ( desc . version ( ) , version ) ;
assertEquals ( desc . type ( ) , PluginType . from ( klass ) ) ;
assertEquals ( desc . typeName ( ) , PluginType . from ( klass ) . toString ( ) ) ;
assertEquals ( desc . type ( ) , type ) ;
assertEquals ( desc . typeName ( ) , type . toString ( ) ) ;
assertEquals ( desc . location ( ) , location ) ;
}
private static < T > void assertNewer ( PluginDesc < T > older , PluginDesc < T > newer ) {
private static void assertNewer ( PluginDesc < ? > older , PluginDesc < ? > newer ) {
assertTrue ( newer + " should be newer than " + older , older . compareTo ( newer ) < 0 ) ;
}
}