|
@@ -20,20 +20,13 @@ package org.apache.hadoop.metrics2.sink;
|
|
|
|
|
|
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
|
|
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
|
|
import org.apache.commons.configuration.SubsetConfiguration;
|
|
import org.apache.commons.configuration.SubsetConfiguration;
|
|
-import org.apache.hadoop.metrics2.Metric;
|
|
|
|
-import org.apache.hadoop.metrics2.MetricsRecord;
|
|
|
|
-import org.apache.hadoop.metrics2.MetricsTag;
|
|
|
|
import org.junit.Assert;
|
|
import org.junit.Assert;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
|
|
|
|
import java.sql.CallableStatement;
|
|
import java.sql.CallableStatement;
|
|
import java.sql.Connection;
|
|
import java.sql.Connection;
|
|
-import java.util.Collections;
|
|
|
|
-import java.util.HashSet;
|
|
|
|
|
|
|
|
-import static org.easymock.EasyMock.anyObject;
|
|
|
|
import static org.easymock.EasyMock.createNiceMock;
|
|
import static org.easymock.EasyMock.createNiceMock;
|
|
-import static org.easymock.EasyMock.eq;
|
|
|
|
import static org.easymock.EasyMock.expect;
|
|
import static org.easymock.EasyMock.expect;
|
|
import static org.easymock.EasyMock.replay;
|
|
import static org.easymock.EasyMock.replay;
|
|
import static org.easymock.EasyMock.verify;
|
|
import static org.easymock.EasyMock.verify;
|
|
@@ -41,7 +34,10 @@ import static org.easymock.EasyMock.verify;
|
|
/**
|
|
/**
|
|
* SqlServerSink Tests.
|
|
* SqlServerSink Tests.
|
|
*/
|
|
*/
|
|
-public class SqlServerSinkTest {
|
|
|
|
|
|
+public abstract class SqlServerSinkTest<T extends SqlServerSink> {
|
|
|
|
+
|
|
|
|
+ public abstract T createInstance() throws InstantiationException, IllegalAccessException;
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void testInit() throws Exception {
|
|
public void testInit() throws Exception {
|
|
|
|
|
|
@@ -55,7 +51,7 @@ public class SqlServerSinkTest {
|
|
// replay
|
|
// replay
|
|
replay(configuration);
|
|
replay(configuration);
|
|
|
|
|
|
- SqlServerSink sink = new SqlServerSink();
|
|
|
|
|
|
+ SqlServerSink sink = createInstance();
|
|
|
|
|
|
sink.init(configuration);
|
|
sink.init(configuration);
|
|
|
|
|
|
@@ -76,11 +72,11 @@ public class SqlServerSinkTest {
|
|
// replay
|
|
// replay
|
|
replay(configuration, connection);
|
|
replay(configuration, connection);
|
|
|
|
|
|
- SqlServerSink sink = new SqlServerSink();
|
|
|
|
|
|
+ SqlServerSink sink = createInstance();
|
|
|
|
|
|
sink.init(configuration);
|
|
sink.init(configuration);
|
|
|
|
|
|
- Assert.assertFalse(sink.ensureConnection());
|
|
|
|
|
|
+ Assert.assertTrue(sink.ensureConnection());
|
|
|
|
|
|
SQLServerDriver.setConnection(connection);
|
|
SQLServerDriver.setConnection(connection);
|
|
Assert.assertTrue(sink.ensureConnection());
|
|
Assert.assertTrue(sink.ensureConnection());
|
|
@@ -103,7 +99,7 @@ public class SqlServerSinkTest {
|
|
// replay
|
|
// replay
|
|
replay(configuration, connection);
|
|
replay(configuration, connection);
|
|
|
|
|
|
- SqlServerSink sink = new SqlServerSink();
|
|
|
|
|
|
+ SqlServerSink sink = createInstance();
|
|
|
|
|
|
sink.init(configuration);
|
|
sink.init(configuration);
|
|
|
|
|
|
@@ -142,7 +138,7 @@ public class SqlServerSinkTest {
|
|
// replay
|
|
// replay
|
|
replay(configuration, connection, cstmt);
|
|
replay(configuration, connection, cstmt);
|
|
|
|
|
|
- SqlServerSink sink = new SqlServerSink();
|
|
|
|
|
|
+ SqlServerSink sink = createInstance();
|
|
|
|
|
|
sink.init(configuration);
|
|
sink.init(configuration);
|
|
|
|
|
|
@@ -182,7 +178,7 @@ public class SqlServerSinkTest {
|
|
// replay
|
|
// replay
|
|
replay(configuration, connection, cstmt);
|
|
replay(configuration, connection, cstmt);
|
|
|
|
|
|
- SqlServerSink sink = new SqlServerSink();
|
|
|
|
|
|
+ SqlServerSink sink = createInstance();
|
|
|
|
|
|
sink.init(configuration);
|
|
sink.init(configuration);
|
|
|
|
|
|
@@ -214,7 +210,7 @@ public class SqlServerSinkTest {
|
|
// replay
|
|
// replay
|
|
replay(configuration, connection, cstmt);
|
|
replay(configuration, connection, cstmt);
|
|
|
|
|
|
- SqlServerSink sink = new SqlServerSink();
|
|
|
|
|
|
+ SqlServerSink sink = createInstance();
|
|
|
|
|
|
sink.init(configuration);
|
|
sink.init(configuration);
|
|
|
|
|
|
@@ -225,60 +221,5 @@ public class SqlServerSinkTest {
|
|
verify(configuration, connection, cstmt);
|
|
verify(configuration, connection, cstmt);
|
|
}
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
|
- public void testPutMetrics() throws Exception {
|
|
|
|
- SubsetConfiguration configuration = createNiceMock(SubsetConfiguration.class);
|
|
|
|
- Connection connection = createNiceMock(Connection.class);
|
|
|
|
- CallableStatement cstmt = createNiceMock(CallableStatement.class);
|
|
|
|
- MetricsRecord record = createNiceMock(MetricsRecord.class);
|
|
|
|
- Metric metric = createNiceMock(Metric.class);
|
|
|
|
-
|
|
|
|
- // set expectations
|
|
|
|
- expect(configuration.getParent()).andReturn(null);
|
|
|
|
- expect(configuration.getPrefix()).andReturn("prefix");
|
|
|
|
- expect(configuration.getString("databaseUrl")).andReturn("url");
|
|
|
|
-
|
|
|
|
- expect(record.context()).andReturn("context");
|
|
|
|
- expect(record.name()).andReturn("typeName");
|
|
|
|
- expect(record.tags()).andReturn(new HashSet<MetricsTag>());
|
|
|
|
- expect(record.timestamp()).andReturn(9999L);
|
|
|
|
-
|
|
|
|
- expect(record.metrics()).andReturn(Collections.singleton(metric));
|
|
|
|
-
|
|
|
|
- expect(metric.name()).andReturn("name").anyTimes();
|
|
|
|
- expect(metric.value()).andReturn(1234);
|
|
|
|
-
|
|
|
|
- expect(connection.prepareCall("{call dbo.uspGetMetricRecord(?, ?, ?, ?, ?, ?, ?, ?, ?)}")).andReturn(cstmt);
|
|
|
|
- cstmt.setNString(1, "context");
|
|
|
|
- cstmt.setNString(2, "typeName");
|
|
|
|
- cstmt.setNString(eq(3), (String) anyObject());
|
|
|
|
- cstmt.setNString(eq(4), (String) anyObject());
|
|
|
|
- cstmt.setNString(eq(5), (String) anyObject());
|
|
|
|
- cstmt.setNString(6, "prefix");
|
|
|
|
- cstmt.setNString(7, "sourceName:prefix");
|
|
|
|
- cstmt.setLong(8, 9999L);
|
|
|
|
- cstmt.registerOutParameter(9, java.sql.Types.BIGINT);
|
|
|
|
- expect(cstmt.execute()).andReturn(true);
|
|
|
|
- expect(cstmt.getLong(9)).andReturn(99L);
|
|
|
|
- expect(cstmt.wasNull()).andReturn(false);
|
|
|
|
-
|
|
|
|
- expect(connection.prepareCall("{call dbo.uspInsertMetricValue(?, ?, ?)}")).andReturn(cstmt);
|
|
|
|
- cstmt.setLong(1, 99L);
|
|
|
|
- cstmt.setNString(2, "name");
|
|
|
|
- cstmt.setNString(3, "1234");
|
|
|
|
- expect(cstmt.execute()).andReturn(true);
|
|
|
|
-
|
|
|
|
- // replay
|
|
|
|
- replay(configuration, connection, cstmt, record, metric);
|
|
|
|
-
|
|
|
|
- SqlServerSink sink = new SqlServerSink();
|
|
|
|
-
|
|
|
|
- sink.init(configuration);
|
|
|
|
-
|
|
|
|
- SQLServerDriver.setConnection(connection);
|
|
|
|
-
|
|
|
|
- sink.putMetrics(record);
|
|
|
|
-
|
|
|
|
- verify(configuration, connection, cstmt, record, metric);
|
|
|
|
- }
|
|
|
|
|
|
+ public abstract void testPutMetrics() throws Exception;
|
|
}
|
|
}
|