Skip to content

Commit

Permalink
SDJ-351 and SDK-352 and SDK-353 - Tech debt code cleanup (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
mseaton authored Dec 11, 2024
1 parent e2e0d1b commit 7ab917e
Show file tree
Hide file tree
Showing 24 changed files with 117 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -387,13 +386,17 @@ protected static void addMockDbSettings(Verifier verifier) {
}

protected static Model getTestPom(File directory) throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
return reader.read(new FileInputStream(new File(directory, "pom.xml")));
try (InputStream in = Files.newInputStream(new File(directory, "pom.xml").toPath())) {
MavenXpp3Reader reader = new MavenXpp3Reader();
return reader.read(in);
}
}

protected static void saveTestPom(File directory, Model model) throws IOException {
MavenXpp3Writer writer = new MavenXpp3Writer();
writer.write(new FileOutputStream(new File(directory, "pom.xml")), model);
try (OutputStream out = Files.newOutputStream(new File(directory, "pom.xml").toPath())) {
MavenXpp3Writer writer = new MavenXpp3Writer();
writer.write(out, model);
}
}

protected void addAnswer(String answer){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public void clone_shouldCloneRepository() throws Exception {

// Check if upstream was added
String absolutePath = new File(testDirectory.getAbsolutePath(), "openmrs-module-appui").getAbsolutePath();
Repository repository = new RepositoryBuilder().findGitDir(new File(absolutePath)).build();
Config storedConfig = repository.getConfig();
Set<String> remotes = storedConfig.getSubsections("remote");

assertThat(remotes, hasItem("upstream"));
try (Repository repository = new RepositoryBuilder().findGitDir(new File(absolutePath)).build()) {
Config storedConfig = repository.getConfig();
Set<String> remotes = storedConfig.getSubsections("remote");
assertThat(remotes, hasItem("upstream"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.openmrs.maven.plugins.utility.SDKConstants;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
Expand Down Expand Up @@ -558,11 +558,10 @@ public void testSetupWithMissingContentDependencies() throws Exception {
}

private String readValueFromPropertyKey(File propertiesFile, String key) throws Exception {

InputStream in = new FileInputStream(propertiesFile);
Properties sdkProperties = new Properties();
sdkProperties.load(in);

try (InputStream in = Files.newInputStream(propertiesFile.toPath())) {
sdkProperties.load(in);
}
return sdkProperties.getProperty(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var getConfig = function () {
} finally {
return config;
}
;

}
var config = getConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void executeTask() throws MojoExecutionException, MojoFailureException {
case SPA_OPTION:
moduleName = wizard.promptForValueIfMissing(moduleName, "frontend module name");
if (StringUtils.isBlank(version)) {
List<String> versions = new NpmVersionHelper().getPackageVersions(moduleName, 6);;
List<String> versions = new NpmVersionHelper().getPackageVersions(moduleName, 6);
version = wizard.promptForMissingValueWithOptions("Enter the module version", null, null, versions,
"Please specify the SPA version", null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -493,13 +493,13 @@ private InputStream getSqlDumpStream(String sqlScriptPath, File targetDirectory,
if (distroArtifact != null && distroArtifact.isValid()) {
File extractedSqlFile = distroHelper
.extractFileFromDistro(targetDirectory, distroArtifact, sqlScript);
stream = new FileInputStream(extractedSqlFile);
stream = Files.newInputStream(extractedSqlFile.toPath());
}
}
} else {
File scriptFile = new File(sqlScriptPath);
if (scriptFile.exists()) {
stream = new FileInputStream(scriptFile);
stream = Files.newInputStream(scriptFile.toPath());
} else {
throw new MojoExecutionException("Specified script \"" + scriptFile.getAbsolutePath() + "\" does not exist.");
}
Expand Down
17 changes: 11 additions & 6 deletions maven-plugin/src/main/java/org/openmrs/maven/plugins/Clone.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@
import java.util.ArrayList;
import java.util.List;

import static org.twdata.maven.mojoexecutor.MojoExecutor.*;
import static org.twdata.maven.mojoexecutor.MojoExecutor.Element;
import static org.twdata.maven.mojoexecutor.MojoExecutor.artifactId;
import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration;
import static org.twdata.maven.mojoexecutor.MojoExecutor.element;
import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo;
import static org.twdata.maven.mojoexecutor.MojoExecutor.executionEnvironment;
import static org.twdata.maven.mojoexecutor.MojoExecutor.goal;
import static org.twdata.maven.mojoexecutor.MojoExecutor.groupId;
import static org.twdata.maven.mojoexecutor.MojoExecutor.plugin;
import static org.twdata.maven.mojoexecutor.MojoExecutor.version;

/**
* Clone any OpenMRS module repository
Expand Down Expand Up @@ -154,11 +163,7 @@ private void cloneRepo(String repoUrl) throws MojoExecutionException {
throw new MojoExecutionException("Destination path \"" + localPath.getAbsolutePath() + "\" already exists.");
}

try {
Git.cloneRepository()
.setURI(GITHUB_BASE_URL + originUrl)
.setDirectory(localPath)
.call();
try (Git call = Git.cloneRepository().setURI(GITHUB_BASE_URL + originUrl).setDirectory(localPath).call()) {
Git git = new Git(gitHelper.getLocalRepository(localPath.getAbsolutePath()));
gitHelper.addRemoteUpstream(git, localPath.getAbsolutePath());
}
Expand Down
4 changes: 2 additions & 2 deletions maven-plugin/src/main/java/org/openmrs/maven/plugins/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ private void runInFork(Server server) throws MojoExecutionException, MojoFailure

if (server.hasWatchedProjects()) {
Path serversPath = Server.getServersPath();
File springloadedJar = serversPath.resolve("springloaded.jar").toFile();;
if (!springloadedJar.exists()) {
File springloadedJar = serversPath.resolve("springloaded.jar").toFile();
if (!springloadedJar.exists()) {
Artifact artifact = new Artifact("springloaded", "1.2.5.RELEASE", "org.springframework", "jar");
artifact.setDestFileName("springloaded.jar");
executeMojo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.apache.maven.shared.utils.StringUtils;
import org.openmrs.maven.plugins.model.Artifact;
import org.openmrs.maven.plugins.model.BaseSdkProperties;
import org.openmrs.maven.plugins.model.ContentPackage;
import org.openmrs.maven.plugins.model.Distribution;
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.model.Server;
Expand All @@ -16,7 +15,6 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down
11 changes: 5 additions & 6 deletions maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
import org.openmrs.maven.plugins.utility.ServerHelper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
Expand Down Expand Up @@ -516,18 +515,18 @@ private void importDb(Server server, String sqlScriptPath) throws MojoExecutionE
.extractFileFromDistro(server.getServerDirectory(), distroArtifact, sqlScript);
extractedSqlFile.deleteOnExit();
try {
sqlStream = new FileInputStream(extractedSqlFile);
sqlStream = Files.newInputStream(extractedSqlFile.toPath());
}
catch (FileNotFoundException e) {
catch (Exception e) {
throw new MojoExecutionException("Error during opening sql dump script file", e);
}
}
} else {
File scriptFile = new File(sqlScriptPath);
try {
sqlStream = new FileInputStream(scriptFile);
sqlStream = Files.newInputStream(scriptFile.toPath());
}
catch (FileNotFoundException e) {
catch (Exception e) {
throw new MojoExecutionException("SQL import script could not be found at \"" +
scriptFile.getAbsolutePath() + "\" " + e.getMessage() , e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import org.openmrs.maven.plugins.utility.SettingsManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

Expand Down Expand Up @@ -51,11 +51,13 @@ public void executeTask() throws MojoExecutionException {
File mavenSettings = new File(mavenHome, SDKConstants.MAVEN_SETTINGS);
try {
SettingsManager settings = new SettingsManager(mavenSession);
InputStream settingsStream = getClass().getClassLoader().getResourceAsStream(SDKConstants.MAVEN_SETTINGS);
SettingsManager defaultSettings = new SettingsManager(settingsStream);
settings.updateSettings(defaultSettings.getSettings());
OutputStream out = new FileOutputStream(mavenSettings);
settings.apply(out);
try (InputStream settingsStream = getClass().getClassLoader().getResourceAsStream(SDKConstants.MAVEN_SETTINGS)) {
SettingsManager defaultSettings = new SettingsManager(settingsStream);
settings.updateSettings(defaultSettings.getSettings());
}
try (OutputStream out = Files.newOutputStream(mavenSettings.toPath())) {
settings.apply(out);
}
getLog().info(String.format(SUCCESS_TEMPLATE, mavenSettings.getPath()));
getLog().info(SDK_INFO);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void executeTask() throws MojoExecutionException {
serverConfig.save();
wizard.showMessage("Stopped watching " + project.getPath() + " for changes.");
} else {
wizard.showMessage((groupId != null) ? (groupId + ":") : "" + artifactId + " has not been watched.");
wizard.showMessage((groupId != null) ? (groupId + ":") : artifactId + " has not been watched.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.openmrs.maven.plugins.AbstractTask;
import org.openmrs.maven.plugins.Pull;
import org.openmrs.maven.plugins.utility.CompositeException;
import org.openmrs.maven.plugins.model.Project;
import org.openmrs.maven.plugins.utility.CompositeException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -536,12 +537,12 @@ private boolean isUpstreamRepoCreated(Git git, String path) throws Exception {
*/
private String getRemoteRepoUrlFromPom(String path) throws MojoExecutionException {
Model model;
try {
File pomFile = new File(path);
if (pomFile.isDirectory()) {
pomFile = new File(pomFile, "pom.xml");
}
model = new MavenXpp3Reader().read(new FileInputStream(pomFile));
File pomFile = new File(path);
if (pomFile.isDirectory()) {
pomFile = new File(pomFile, "pom.xml");
}
try (InputStream in = Files.newInputStream(pomFile.toPath())) {
model = new MavenXpp3Reader().read(in);
}
catch (IOException e) {
throw new MojoExecutionException("Failed to access file " + path, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import org.apache.maven.plugin.MojoExecutionException;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

Expand All @@ -24,8 +25,12 @@ public Issue getIssue(String issueId) throws MojoExecutionException {
throw new MojoExecutionException(OPENMRS_ISSUES_URI + " is not a valid URI " + e.getMessage(), e);
}

JiraRestClient client = factory.create(jiraServerUri, new AnonymousAuthenticationHandler());
return client.getIssueClient().getIssue(issueId).claim();
try (JiraRestClient client = factory.create(jiraServerUri, new AnonymousAuthenticationHandler())) {
return client.getIssueClient().getIssue(issueId).claim();
}
catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}

@Override
Expand Down
Loading

0 comments on commit 7ab917e

Please sign in to comment.