From 4e81ee5fdf2db69773bbcf55a3898b4aed01f7f9 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 26 Aug 2017 15:04:48 +0100 Subject: [PATCH] Verify support for H2 database aliases in SQL scripts This commit introduces a test to demonstrate how to define an alias in an SQL script executed against an embedded H2 database. Issue: SPR-15896 --- .../init/AbstractDatabasePopulatorTests.java | 4 ++-- .../init/H2DatabasePopulatorTests.java | 24 ++++++++++++++++++- .../datasource/init/db-test-data-h2-alias.sql | 10 ++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 spring-jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-test-data-h2-alias.sql diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/AbstractDatabasePopulatorTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/AbstractDatabasePopulatorTests.java index 38bd4a7cee0..4a48efffc4a 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/AbstractDatabasePopulatorTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/AbstractDatabasePopulatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,7 @@ import static org.mockito.Mockito.*; */ public abstract class AbstractDatabasePopulatorTests extends AbstractDatabaseInitializationTests { - private final ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); + protected final ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); @Test diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/H2DatabasePopulatorTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/H2DatabasePopulatorTests.java index 699ea75b1a9..fa6883bccba 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/H2DatabasePopulatorTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/H2DatabasePopulatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,12 @@ package org.springframework.jdbc.datasource.init; +import org.junit.Test; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + /** * @author Sam Brannen * @since 4.0.3 @@ -28,4 +32,22 @@ public class H2DatabasePopulatorTests extends AbstractDatabasePopulatorTests { return EmbeddedDatabaseType.H2; } + /** + * https://jira.spring.io/browse/SPR-15896 + * + * @since 5.0 + */ + @Test + public void scriptWithH2Alias() throws Exception { + databasePopulator.addScript(usersSchema()); + databasePopulator.addScript(resource("db-test-data-h2-alias.sql")); + // Set statement separator to double newline so that ";" is not + // considered a statement separator within the source code of the + // aliased function 'REVERSE'. + databasePopulator.setSeparator("\n\n"); + DatabasePopulatorUtils.execute(databasePopulator, db); + String sql = "select REVERSE(first_name) from users where last_name='Brannen'"; + assertThat(jdbcTemplate.queryForObject(sql, String.class), equalTo("maS")); + } + } diff --git a/spring-jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-test-data-h2-alias.sql b/spring-jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-test-data-h2-alias.sql new file mode 100644 index 00000000000..5eb502aeae3 --- /dev/null +++ b/spring-jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-test-data-h2-alias.sql @@ -0,0 +1,10 @@ +DROP ALIAS IF EXISTS REVERSE; + +-- REVERSE function borrowed from http://www.h2database.com/html/grammar.html#create_alias +CREATE ALIAS REVERSE AS $$ + String reverse(String s) { + return new StringBuilder(s).reverse().toString(); + } +$$; + +INSERT INTO users(first_name, last_name) values('Sam', 'Brannen');