Java Code Examples for org.apache.flink.table.client.cli.utils.TerminalUtils

The following examples show how to use org.apache.flink.table.client.cli.utils.TerminalUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Flink-CEPplus   Source File: CliClientTest.java    License: Apache License 2.0 6 votes vote down vote up
private void verifyUpdateSubmission(String statement, boolean failExecution, boolean testFailure) {
	final SessionContext context = new SessionContext("test-session", new Environment());

	final MockExecutor mockExecutor = new MockExecutor();
	mockExecutor.failExecution = failExecution;

	CliClient cli = null;
	try {
		cli = new CliClient(TerminalUtils.createDummyTerminal(), context, mockExecutor);
		if (testFailure) {
			assertFalse(cli.submitUpdate(statement));
		} else {
			assertTrue(cli.submitUpdate(statement));
			assertEquals(statement, mockExecutor.receivedStatement);
			assertEquals(context, mockExecutor.receivedContext);
		}
	} finally {
		if (cli != null) {
			cli.close();
		}
	}
}
 
Example 2
Source Project: flink   Source File: CliClientTest.java    License: Apache License 2.0 6 votes vote down vote up
private void verifyUpdateSubmission(String statement, boolean failExecution, boolean testFailure) {
	final SessionContext context = new SessionContext("test-session", new Environment());

	final MockExecutor mockExecutor = new MockExecutor();
	mockExecutor.failExecution = failExecution;

	CliClient cli = null;
	try {
		cli = new CliClient(TerminalUtils.createDummyTerminal(), context, mockExecutor);
		if (testFailure) {
			assertFalse(cli.submitUpdate(statement));
		} else {
			assertTrue(cli.submitUpdate(statement));
			assertEquals(statement, mockExecutor.receivedStatement);
			assertEquals(context, mockExecutor.receivedContext);
		}
	} finally {
		if (cli != null) {
			cli.close();
		}
	}
}
 
Example 3
Source Project: Flink-CEPplus   Source File: CliResultViewTest.java    License: Apache License 2.0 5 votes vote down vote up
private void testResultViewClearResult(TypedResult<?> typedResult, boolean isTableMode, int expectedCancellationCount) throws Exception {
	final CountDownLatch cancellationCounterLatch = new CountDownLatch(expectedCancellationCount);
	final SessionContext session = new SessionContext("test-session", new Environment());
	final MockExecutor executor = new MockExecutor(typedResult, cancellationCounterLatch);
	final ResultDescriptor descriptor = new ResultDescriptor(
		"result-id",
		TableSchema.builder().field("Null Field", Types.STRING()).build(),
		false);

	Thread resultViewRunner = null;
	CliClient cli = null;
	try {
		cli = new CliClient(TerminalUtils.createDummyTerminal(), session, executor);
		resultViewRunner = new Thread(new TestingCliResultView(cli, descriptor, isTableMode));
		resultViewRunner.start();
	} finally {
		if (resultViewRunner != null && !resultViewRunner.isInterrupted()) {
			resultViewRunner.interrupt();
		}
		if (cli != null) {
			cli.close();
		}
	}

	assertTrue(
		"Invalid number of cancellations.",
		cancellationCounterLatch.await(10, TimeUnit.SECONDS));
}
 
Example 4
Source Project: Flink-CEPplus   Source File: CliClientTest.java    License: Apache License 2.0 5 votes vote down vote up
private void verifySqlCompletion(String statement, int position, List<String> expectedHints, List<String> notExpectedHints) throws IOException {
	final SessionContext context = new SessionContext("test-session", new Environment());
	final MockExecutor mockExecutor = new MockExecutor();

	final SqlCompleter completer = new SqlCompleter(context, mockExecutor);
	final SqlMultiLineParser parser = new SqlMultiLineParser();

	try (Terminal terminal = TerminalUtils.createDummyTerminal()) {
		final LineReader reader = LineReaderBuilder.builder().terminal(terminal).build();

		final ParsedLine parsedLine = parser.parse(statement, position, Parser.ParseContext.COMPLETE);
		final List<Candidate> candidates = new ArrayList<>();
		final List<String> results = new ArrayList<>();
		completer.complete(reader, parsedLine, candidates);
		candidates.forEach(item -> results.add(item.value()));

		assertTrue(results.containsAll(expectedHints));

		assertEquals(statement, mockExecutor.receivedStatement);
		assertEquals(context, mockExecutor.receivedContext);
		assertEquals(position, mockExecutor.receivedPosition);
		assertTrue(results.contains("HintA"));
		assertTrue(results.contains("Hint B"));

		results.retainAll(notExpectedHints);
		assertEquals(0, results.size());
	}
}
 
Example 5
Source Project: flink   Source File: CliResultViewTest.java    License: Apache License 2.0 5 votes vote down vote up
private void testResultViewClearResult(TypedResult<?> typedResult, boolean isTableMode, int expectedCancellationCount) throws Exception {
	final CountDownLatch cancellationCounterLatch = new CountDownLatch(expectedCancellationCount);
	final SessionContext session = new SessionContext("test-session", new Environment());
	final MockExecutor executor = new MockExecutor(typedResult, cancellationCounterLatch);
	final ResultDescriptor descriptor = new ResultDescriptor(
		"result-id",
		TableSchema.builder().field("Null Field", Types.STRING()).build(),
		false);

	Thread resultViewRunner = null;
	CliClient cli = null;
	try {
		cli = new CliClient(TerminalUtils.createDummyTerminal(), session, executor);
		resultViewRunner = new Thread(new TestingCliResultView(cli, descriptor, isTableMode));
		resultViewRunner.start();
	} finally {
		if (resultViewRunner != null && !resultViewRunner.isInterrupted()) {
			resultViewRunner.interrupt();
		}
		if (cli != null) {
			cli.close();
		}
	}

	assertTrue(
		"Invalid number of cancellations.",
		cancellationCounterLatch.await(10, TimeUnit.SECONDS));
}
 
Example 6
Source Project: flink   Source File: CliClientTest.java    License: Apache License 2.0 5 votes vote down vote up
private void verifySqlCompletion(String statement, int position, List<String> expectedHints, List<String> notExpectedHints) throws IOException {
	final SessionContext context = new SessionContext("test-session", new Environment());
	final MockExecutor mockExecutor = new MockExecutor();

	final SqlCompleter completer = new SqlCompleter(context, mockExecutor);
	final SqlMultiLineParser parser = new SqlMultiLineParser();

	try (Terminal terminal = TerminalUtils.createDummyTerminal()) {
		final LineReader reader = LineReaderBuilder.builder().terminal(terminal).build();

		final ParsedLine parsedLine = parser.parse(statement, position, Parser.ParseContext.COMPLETE);
		final List<Candidate> candidates = new ArrayList<>();
		final List<String> results = new ArrayList<>();
		completer.complete(reader, parsedLine, candidates);
		candidates.forEach(item -> results.add(item.value()));

		assertTrue(results.containsAll(expectedHints));

		assertEquals(statement, mockExecutor.receivedStatement);
		assertEquals(context, mockExecutor.receivedContext);
		assertEquals(position, mockExecutor.receivedPosition);
		assertTrue(results.contains("HintA"));
		assertTrue(results.contains("Hint B"));

		results.retainAll(notExpectedHints);
		assertEquals(0, results.size());
	}
}
 
Example 7
Source Project: flink   Source File: CliResultViewTest.java    License: Apache License 2.0 5 votes vote down vote up
private void testResultViewClearResult(TypedResult<?> typedResult, boolean isTableMode, int expectedCancellationCount) throws Exception {
	final CountDownLatch cancellationCounterLatch = new CountDownLatch(expectedCancellationCount);
	final SessionContext session = new SessionContext("test-session", new Environment());
	final MockExecutor executor = new MockExecutor(typedResult, cancellationCounterLatch);
	String sessionId = executor.openSession(session);
	final ResultDescriptor descriptor = new ResultDescriptor(
			"result-id",
			TableSchema.builder().field("Null Field", Types.STRING()).build(),
			false,
			false);

	Thread resultViewRunner = null;
	CliClient cli = null;
	try {
		cli = new CliClient(
				TerminalUtils.createDummyTerminal(),
				sessionId,
				executor,
				File.createTempFile("history", "tmp").toPath());
		resultViewRunner = new Thread(new TestingCliResultView(cli, descriptor, isTableMode));
		resultViewRunner.start();
	} finally {
		if (resultViewRunner != null && !resultViewRunner.isInterrupted()) {
			resultViewRunner.interrupt();
		}
		if (cli != null) {
			cli.close();
		}
	}

	assertTrue(
		"Invalid number of cancellations.",
		cancellationCounterLatch.await(10, TimeUnit.SECONDS));
}
 
Example 8
Source Project: flink   Source File: CliClientTest.java    License: Apache License 2.0 5 votes vote down vote up
private void verifyUpdateSubmission(String statement, boolean failExecution, boolean testFailure) throws Exception {
	final SessionContext context = new SessionContext("test-session", new Environment());

	final MockExecutor mockExecutor = new MockExecutor();
	String sessionId = mockExecutor.openSession(context);
	mockExecutor.failExecution = failExecution;

	CliClient cli = null;
	try {
		cli = new CliClient(
				TerminalUtils.createDummyTerminal(),
				sessionId,
				mockExecutor,
				File.createTempFile("history", "tmp").toPath());
		if (testFailure) {
			assertFalse(cli.submitUpdate(statement));
		} else {
			assertTrue(cli.submitUpdate(statement));
			assertEquals(statement, mockExecutor.receivedStatement);
			assertEquals(context, mockExecutor.receivedContext);
		}
	} finally {
		if (cli != null) {
			cli.close();
		}
	}
}
 
Example 9
Source Project: flink   Source File: CliClientTest.java    License: Apache License 2.0 5 votes vote down vote up
private void verifySqlCompletion(String statement, int position, List<String> expectedHints, List<String> notExpectedHints) throws IOException {
	final SessionContext context = new SessionContext("test-session", new Environment());
	final MockExecutor mockExecutor = new MockExecutor();
	String sessionId = mockExecutor.openSession(context);

	final SqlCompleter completer = new SqlCompleter(sessionId, mockExecutor);
	final SqlMultiLineParser parser = new SqlMultiLineParser();

	try (Terminal terminal = TerminalUtils.createDummyTerminal()) {
		final LineReader reader = LineReaderBuilder.builder().terminal(terminal).build();

		final ParsedLine parsedLine = parser.parse(statement, position, Parser.ParseContext.COMPLETE);
		final List<Candidate> candidates = new ArrayList<>();
		final List<String> results = new ArrayList<>();
		completer.complete(reader, parsedLine, candidates);
		candidates.forEach(item -> results.add(item.value()));

		assertTrue(results.containsAll(expectedHints));

		assertEquals(statement, mockExecutor.receivedStatement);
		assertEquals(context, mockExecutor.receivedContext);
		assertEquals(position, mockExecutor.receivedPosition);
		assertTrue(results.contains("HintA"));
		assertTrue(results.contains("Hint B"));

		results.retainAll(notExpectedHints);
		assertEquals(0, results.size());
	}
}
 
Example 10
Source Project: flink   Source File: CliTableauResultViewTest.java    License: Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
	terminalOutput = new ByteArrayOutputStream();
	terminal = TerminalUtils.createDummyTerminal(terminalOutput);

	schema = TableSchema.builder()
			.field("boolean", DataTypes.BOOLEAN())
			.field("int", DataTypes.INT())
			.field("bigint", DataTypes.BIGINT())
			.field("varchar", DataTypes.STRING())
			.field("decimal(10, 5)", DataTypes.DECIMAL(10, 5))
			.field("timestamp", DataTypes.TIMESTAMP(6))
			.build();

	data = new ArrayList<>();
	data.add(
			Row.of(
					null,
					1,
					2,
					"abc",
					BigDecimal.valueOf(1.23),
					Timestamp.valueOf("2020-03-01 18:39:14"))
	);
	data.add(
			Row.of(
					false,
					null,
					0,
					"",
					BigDecimal.valueOf(1),
					Timestamp.valueOf("2020-03-01 18:39:14.1"))
	);
	data.add(
			Row.of(
					true,
					Integer.MAX_VALUE,
					null,
					"abcdefg",
					BigDecimal.valueOf(1234567890),
					Timestamp.valueOf("2020-03-01 18:39:14.12"))
	);
	data.add(
			Row.of(
					false,
					Integer.MIN_VALUE,
					Long.MAX_VALUE,
					null,
					BigDecimal.valueOf(12345.06789),
					Timestamp.valueOf("2020-03-01 18:39:14.123"))
	);
	data.add(
			Row.of(
					true,
					100,
					Long.MIN_VALUE,
					"abcdefg111",
					null,
					Timestamp.valueOf("2020-03-01 18:39:14.123456"))
	);
	data.add(
			Row.of(
					null,
					-1,
					-1,
					"abcdefghijklmnopqrstuvwxyz",
					BigDecimal.valueOf(-12345.06789),
					null)
	);

	data.add(
		Row.of(
			null,
			-1,
			-1,
			"这是一段中文",
			BigDecimal.valueOf(-12345.06789),
			Timestamp.valueOf("2020-03-04 18:39:14"))
	);

	data.add(
		Row.of(
			null,
			-1,
			-1,
			"これは日本語をテストするための文です",
			BigDecimal.valueOf(-12345.06789),
			Timestamp.valueOf("2020-03-04 18:39:14"))
	);

	streamingData = new ArrayList<>();
	for (int i = 0; i < data.size(); ++i) {
		streamingData.add(new Tuple2<>(i % 2 == 0, data.get(i)));
	}
}