-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #331 from Bhashinee/main
Integrate bal persist code generation to the `bal build` command
- Loading branch information
Showing
654 changed files
with
3,227 additions
and
1,535 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
persist-cli-tests/src/test/java/io/ballerina/persist/tools/BuildCodeGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.ballerina.persist.tools; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* persist tool in `bal build` command tests. | ||
*/ | ||
public class BuildCodeGeneratorTest { | ||
public static final Path TARGET_DIR = Paths.get(System.getProperty("user.dir"), "build"); | ||
public static final Path TEST_DISTRIBUTION_PATH = TARGET_DIR.resolve("ballerina-distribution"); | ||
|
||
@Test(enabled = true) | ||
public void testBuildWithMysql() throws IOException, InterruptedException { | ||
String log = "Persist client and entity types generated successfully in " + | ||
"the persist_build_1 directory."; | ||
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_1"); | ||
assertLogs(log, project); | ||
} | ||
|
||
@Test(enabled = true) | ||
public void testBuildWithInvalidTargetModule() throws IOException, InterruptedException { | ||
String log = "ERROR: invalid module name : 'persist_add_1' :" + System.lineSeparator() + | ||
"module name should follow the template <package_name>.<module_name>"; | ||
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_2"); | ||
assertLogs(log, project); | ||
} | ||
|
||
@Test(enabled = true) | ||
public void testBuildWithInvalidCharachtersInTargetModule() throws IOException, InterruptedException { | ||
String log = "ERROR: invalid module name : '*****' :" + System.lineSeparator() + | ||
"module name can only contain alphanumerics, underscores and periods"; | ||
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_3"); | ||
assertLogs(log, project); | ||
} | ||
|
||
@Test(enabled = true) | ||
public void testBuildWithInvalidLengthOfTargetModule() throws IOException, InterruptedException { | ||
String log = "ERROR: invalid module name : 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + | ||
"aaaaaaaaaaaaaaaaaaaaaaaaddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" + | ||
"ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" + | ||
"dddddddddddddddddddddd' :" + System.lineSeparator() + | ||
"maximum length of module name is 256 characters"; | ||
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_4"); | ||
assertLogs(log, project); | ||
} | ||
|
||
@Test(enabled = true) | ||
public void testBuildWithInvalidDataSource() throws IOException, InterruptedException { | ||
String log = "ERROR: the persist layer supports one of data stores:"; | ||
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_5"); | ||
assertContainLogs(log, project); | ||
} | ||
|
||
@Test(enabled = true) | ||
public void testBuildWithoutEntities() throws IOException, InterruptedException { | ||
String log = "ERROR: the model definition file(model.bal) does not contain any entity definition."; | ||
Path project = TARGET_DIR.resolve("generated-sources/tool_test_build_6"); | ||
assertLogs(log, project); | ||
} | ||
|
||
private String collectLogOutput(Path project) throws IOException, InterruptedException { | ||
List<String> buildArgs = new LinkedList<>(); | ||
Process process = executeBuild(TEST_DISTRIBUTION_PATH.toString(), project, buildArgs); | ||
try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream(), | ||
StandardCharsets.UTF_8))) { | ||
Stream<String> logLines = br.lines(); | ||
String generatedLog = logLines.collect(Collectors.joining(System.lineSeparator())); | ||
logLines.close(); | ||
return generatedLog; | ||
} | ||
} | ||
|
||
private void assertLogs(String log, Path project) throws IOException, InterruptedException { | ||
String generatedLog = collectLogOutput(project); | ||
Assert.assertEquals(generatedLog, log); | ||
} | ||
|
||
private void assertContainLogs(String log, Path project) throws IOException, InterruptedException { | ||
String generatedLog = collectLogOutput(project); | ||
Assert.assertTrue(generatedLog.contains(log)); | ||
} | ||
|
||
public static Process executeBuild(String distributionName, Path sourceDirectory, | ||
List<String> args) throws IOException, InterruptedException { | ||
args.add(0, "build"); | ||
Process process = getProcessBuilderResults(distributionName, sourceDirectory, args); | ||
process.waitFor(); | ||
return process; | ||
} | ||
|
||
public static Process getProcessBuilderResults(String distributionName, Path sourceDirectory, List<String> args) | ||
throws IOException { | ||
String balFile = "bal"; | ||
|
||
if (System.getProperty("os.name").startsWith("Windows")) { | ||
balFile = "bal.bat"; | ||
} | ||
args.add(0, TEST_DISTRIBUTION_PATH.resolve(distributionName).resolve("bin").resolve(balFile).toString()); | ||
ProcessBuilder pb = new ProcessBuilder(args); | ||
pb.directory(sourceDirectory.toFile()); | ||
return pb.start(); | ||
} | ||
} |
Oops, something went wrong.