Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #176 from Trivadis/feature/issue-175-merge-test
Browse files Browse the repository at this point in the history
Feature/issue 175 Merge tests project into standalone project
  • Loading branch information
PhilippSalvisberg authored Dec 21, 2021
2 parents bfe8c84 + 90c0066 commit 2be8977
Show file tree
Hide file tree
Showing 184 changed files with 122 additions and 306 deletions.
17 changes: 13 additions & 4 deletions standalone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ This Maven project produces a standalone command line executable `tvdformat.jar`

The startup time of standalone JAR file and the native image are identical since the image still requires a JDK to execute. However, it is faster than running `format.js` from SQLcl.

This project contains JUnit tests for

- the SQLDev/SQLcl formatter settings `trivadis_advanced_format.xml` and `trivadis_custom_format.arbori`
- the SQLcl script `format.js`
- the SQLcl command `tvdformat`
- the standalone executable `tvdformat`

The project requires a JDK 17, but it produces a Java 8 JAR file. A GraalVM JDK is required only if you want to produce a native image.

## How to Build

1. [Download](https://www.oracle.com/tools/downloads/sqlcl-downloads.html) and install SQLcl 21.3.0
2. [Download](https://github.com/graalvm/graalvm-ce-builds/releases) and install the GraalVM JDK 11 21.2.0
3. Go to the bin directory of the GraalVM JDK and run `gu install native-image`, if you want to produce a native image
4. [Download](https://maven.apache.org/download.cgi) and install Apache Maven 3.8.3
1. [Download](https://www.oracle.com/tools/downloads/sqlcl-downloads.html) and install SQLcl 21.4.0
2. [Download](https://github.com/graalvm/graalvm-ce-builds/releases) and install the GraalVM JDK 17 21.3.0
3. Go to the `bin` directory of the GraalVM JDK and run `./gu install native-image`, if you want to produce a native image
4. [Download](https://maven.apache.org/download.cgi) and install Apache Maven 3.8.4
5. [Download](https://git-scm.com/downloads) and install a git command line client
6. Clone the plsql-formatter-settings repository
7. Open a terminal window in the plsql-formatter-settings root folder and type
Expand Down
50 changes: 49 additions & 1 deletion standalone/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,50 @@
<version>${graalvm.version}</version>
<scope>compile</scope>
</dependency>
<!-- SQLcl dependencies not available in public Maven repositories -->
<!-- SQLcl dependencies available in public Maven repositories -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
<scope>test</scope>
</dependency>
<!-- SQLcl dependencies not available in public Maven repositories (compile) -->
<dependency>
<groupId>oracle.dbtools</groupId>
<artifactId>dbtools-common</artifactId>
<version>${sqlcl.version}</version>
<scope>system</scope>
<systemPath>${sqlcl.libdir}/dbtools-common.jar</systemPath>
</dependency>
<!-- SQLcl dependencies not available in public Maven repositories (test) -->
<dependency>
<groupId>oracle.dbtools</groupId>
<artifactId>dbtools-sqlcl</artifactId>
<version>${sqlcl.version}</version>
<scope>system</scope>
<systemPath>${sqlcl.libdir}/dbtools-sqlcl.jar</systemPath>
</dependency>
<dependency>
<groupId>oracle.xml</groupId>
<artifactId>xmlparserv2-sans-jaxp-services</artifactId>
<version>${sqlcl.version}</version>
<scope>system</scope>
<systemPath>${sqlcl.libdir}/xmlparserv2_sans_jaxp_services.jar</systemPath>
</dependency>
<dependency>
<groupId>oracle.i18n</groupId>
<artifactId>orai18n</artifactId>
<version>${sqlcl.version}</version>
<scope>system</scope>
<systemPath>${sqlcl.libdir}/orai18n.jar</systemPath>
</dependency>
<dependency>
<groupId>oracle.soda</groupId>
<artifactId>orajsoda</artifactId>
<version>${sqlcl.version}</version>
<scope>system</scope>
<systemPath>${sqlcl.libdir}/orajsoda.jar</systemPath>
</dependency>
<!-- GraalVM native image and Context -->
<dependency>
<groupId>org.graalvm.sdk</groupId>
Expand All @@ -56,6 +92,17 @@
</dependency>
</dependencies>

<!-- Reporting -->
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</reporting>

<!-- Build Settings -->
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
Expand Down Expand Up @@ -192,6 +239,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<workingDirectory>${project.basedir}</workingDirectory>
<includes>
<include>**/*.java</include>
</includes>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package com.trivadis.plsql.formatter;
package com.trivadis.plsql.formatter.standalone.tests;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.stream.Collectors;

public abstract class AbstractTvdFormatTest {
static final PrintStream originalPrintStream = System.out;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);
Path tempDir;

@BeforeEach
public void setup() {
try {
tempDir = Files.createTempDirectory("tvdformat-test-");
var url = Thread.currentThread().getContextClassLoader().getResource("input");
var url = Thread.currentThread().getContextClassLoader().getResource("unformatted");
assert url != null;
var resourceDir = Paths.get(url.getPath());
var sources = Files.walk(resourceDir)
Expand All @@ -26,11 +33,20 @@ public void setup() {
var target = Paths.get(tempDir.toString() + File.separator + source.getFileName());
Files.copy(source, target);
}
System.setOut(printStream);
outputStream.reset();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@AfterEach
public void teardown() {
System.clearProperty("tvdformat.standalone");
System.clearProperty("polyglot.engine.WarnInterpreterOnly");
System.setOut(originalPrintStream);
}

private String getFileContent(Path file) {
try {
return new String(Files.readAllBytes(file));
Expand All @@ -44,9 +60,7 @@ public String getFormattedContent(String fileName) {
return getFileContent(file);
}

public String getExpectedContent(String fileName) {
var url = Thread.currentThread().getContextClassLoader().getResource("expected");
assert url != null;
return getFileContent(Paths.get(url.getPath() + File.separator + fileName));
public String getConsoleOutput() {
return outputStream.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.trivadis.plsql.formatter;
package com.trivadis.plsql.formatter.standalone.tests;

import com.trivadis.plsql.formatter.TvdFormat;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -8,9 +9,31 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;

public class TvdFormatTest extends AbstractTvdFormatTest {

private final String EXPECTED_QUERY_SQL = """
select d.department_name,
v.employee_id,
v.last_name
from departments d
cross apply (
select *
from employees e
where e.department_id = d.department_id
) v
where d.department_name in ('Marketing', 'Operations', 'Public Relations')
order by d.department_name, v.employee_id;
""";

private final String XML = Objects.requireNonNull(
Thread.currentThread().getContextClassLoader().getResource("trivadis_advanced_format.xml")).getPath();

private final String ARBORI = Objects.requireNonNull(
Thread.currentThread().getContextClassLoader().getResource("trivadis_custom_format.arbori")).getPath();


@Test
public void jsonArrayDirTest() throws ScriptException, IOException {
var configFileContent = """
Expand All @@ -20,13 +43,11 @@ public void jsonArrayDirTest() throws ScriptException, IOException {
""".replace("#TEMP_DIR#", tempDir.toString());
var configFile = Paths.get(tempDir + File.separator + "config.json");
Files.write(configFile, configFileContent.getBytes());
var args = new String[]{tempDir + File.separator + "config.json",
"xml=" + tempDir + File.separator + "trivadis_advanced_format.xml",
"arbori=" + tempDir + File.separator + "trivadis_custom_format.arbori"};
var args = new String[]{tempDir + File.separator + "config.json", "xml=" + XML, "arbori=" + ARBORI};
TvdFormat.main(args);
var expected = getExpectedContent("query.sql");
var actual = getFormattedContent("query.sql");
Assertions.assertEquals(expected, actual);
Assertions.assertEquals(EXPECTED_QUERY_SQL, actual);
Assertions.assertTrue(getConsoleOutput().contains("1 of 4"));
}

@Test
Expand All @@ -38,32 +59,34 @@ public void jsonArrayFileTest() throws ScriptException, IOException {
""".replace("#TEMP_DIR#", tempDir.toString()).replace("#FILE_SEP#", File.separator);
var configFile = Paths.get(tempDir + File.separator + "config.json");
Files.write(configFile, configFileContent.getBytes());
var args = new String[]{tempDir + File.separator + "config.json",
"xml=" + tempDir + File.separator + "trivadis_advanced_format.xml",
"arbori=" + tempDir + File.separator + "trivadis_custom_format.arbori"};
var args = new String[]{tempDir + File.separator + "config.json", "xml=" + XML, "arbori=" + ARBORI};
TvdFormat.main(args);
var expected = getExpectedContent("query.sql");
var actual = getFormattedContent("query.sql");
Assertions.assertEquals(expected, actual);
Assertions.assertEquals(EXPECTED_QUERY_SQL, actual);
Assertions.assertTrue(getConsoleOutput().contains("1 of 1"));
}

@Test
public void jsonObjectFileTest() throws ScriptException, IOException {
var configFileContent = """
{
"xml": "#TEMP_DIR##FILE_SEP#trivadis_advanced_format.xml",
"arbori": "#TEMP_DIR##FILE_SEP#trivadis_custom_format.arbori",
"xml": "#XML#",
"arbori": "#ARBORI#",
"files": [
"#TEMP_DIR##FILE_SEP#query.sql"
"#TEMP_DIR##FILE_SEP#query.sql",
"#TEMP_DIR##FILE_SEP#package_body.pkb"
]
}
""".replace("#TEMP_DIR#", tempDir.toString()).replace("#FILE_SEP#", File.separator);
""".replace("#XML#", XML)
.replace("#ARBORI#", ARBORI)
.replace("#TEMP_DIR#", tempDir.toString())
.replace("#FILE_SEP#", File.separator);
var configFile = Paths.get(tempDir + File.separator + "config.json");
Files.write(configFile, configFileContent.getBytes());
var args = new String[]{tempDir + File.separator + "config.json"};
TvdFormat.main(args);
var expected = getExpectedContent("query.sql");
var actual = getFormattedContent("query.sql");
Assertions.assertEquals(expected, actual);
Assertions.assertEquals(EXPECTED_QUERY_SQL, actual);
Assertions.assertTrue(getConsoleOutput().contains("1 of 2"));
}
}
11 changes: 0 additions & 11 deletions standalone/src/test/resources/expected/query.sql

This file was deleted.

This file was deleted.

This file was deleted.

47 changes: 0 additions & 47 deletions tests/.gitignore

This file was deleted.

52 changes: 0 additions & 52 deletions tests/README.md

This file was deleted.

Loading

0 comments on commit 2be8977

Please sign in to comment.