-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TRUNK-5134 - Replace path constructions using separator char #2135
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank @MalshaL !
one main note: the goal of this ticket is to get rid of all the manual path constructions in the code using File.separator
or System.getProperty("file.separator")
and File.separatorChar
you havent replaced all of them.
check out the different constructors available https://docs.oracle.com/javase/7/docs/api/java/io/File.html
you can use a File as parent to construct a new File from it appending a child string.
@@ -126,7 +126,7 @@ public static boolean startModule(Module mod, ServletContext servletContext, boo | |||
realPath = System.getProperty("user.dir"); | |||
} | |||
|
|||
File webInf = new File(realPath + "/WEB-INF".replace("/", File.separator)); | |||
File webInf = new File(realPath, "WEB-INF".replace("/", File.separator)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MalshaL you need to remove the string replace call, see that before the "/" was replaced with the File.separator to make it cross-platform. thats not needed when letting File construct the path.
@@ -240,7 +240,7 @@ public static boolean startModule(Module mod, ServletContext servletContext, boo | |||
if (root.getElementsByTagName("dwr").getLength() > 0) { | |||
|
|||
// get the dwr-module.xml file that we're appending our code to | |||
File f = new File(realPath + "/WEB-INF/dwr-modules.xml".replace("/", File.separator)); | |||
File f = new File(realPath, "WEB-INF/dwr-modules.xml".replace("/", File.separator)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be replaced with File(webInf, "dwr-modules.xml")
check out the different constructors available https://docs.oracle.com/javase/7/docs/api/java/io/File.html
you can use a File as parent to construct a new File from it appending a child string.
@@ -775,7 +775,7 @@ public static void stopModule(Module mod, ServletContext servletContext, boolean | |||
if (root.getElementsByTagName("dwr").getLength() > 0) { | |||
|
|||
// get the dwr-module.xml file that we're appending our code to | |||
File f = new File(realPath + "/WEB-INF/dwr-modules.xml".replace("/", File.separator)); | |||
File f = new File(realPath, "WEB-INF/dwr-modules.xml".replace("/", File.separator)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above, the goal of this ticket is to get rid of all the manual path constructions in the code using File.separator
or System.getProperty("file.separator")
and File.separatorChar
StreamResult result = new StreamResult(new File(realPath | ||
+ "/WEB-INF/dwr-modules.xml".replace("/", File.separator))); | ||
StreamResult result = new StreamResult(new File(realPath, | ||
"WEB-INF/dwr-modules.xml".replace("/", File.separator))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
@@ -24,11 +25,11 @@ | |||
import org.openmrs.api.APIException; | |||
|
|||
public class UpgradeUtil { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MalshaL can you please double check your formatter settings am wondering why there are so many whitespace formatting changes in this file, I dont get them with my eclipse config (https://wiki.openmrs.org/display/docs/How-To+Setup+And+Use+Your+IDE).
@MalshaL are you still on this? would like to merge and not keep the PR open for so long |
@MalshaL hope you are well! can you please tell us if you are still going to work on this, we need a few simple changes from you to get this merged. having to recheck with contributors on their PRs over and over again takes time of the few reviewers. please respond. thanks a lot! |
@teleivo my sincere apologies for not being able to work on this due to an unavoidable circumstance. :( I will make the necessary changes and commit as soon as possible. |
@MalshaL did you see the merge conflicts on this? 😊 |
Ignore my previous comment about using SystemUtils |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please address comments.
generally make sure to use try-with-resource as per comment and
File outFile = new File(webInf, "view", ...);
try (FileInputStream fis = new FileInputStream(filePath)) { | ||
|
||
try { | ||
FileInputStream fis = new FileInputStream( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MalshaL please also use the try-with-resource statement
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
instead of calling the close() yourself.
this has just been fixed in https://issues.openmrs.org/browse/TRUNK-5031
try (PreparedStatement select = connection.prepareStatement("select uuid from concept where concept_id = ?")) { | ||
public static String getConceptUuid(Connection connection, int conceptId) throws SQLException { | ||
PreparedStatement select = connection.prepareStatement("select uuid from concept where concept_id = ?"); | ||
try { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not change this here, this has been fixed in
https://issues.openmrs.org/browse/TRUNK-5031
you are currently overriding his fix.
ResultSet resultSet = select.executeQuery(); | ||
if (resultSet.next()) { | ||
return resultSet.getString(1); | ||
} else { | ||
throw new IllegalArgumentException("Concept not found " + conceptId); | ||
} | ||
} | ||
finally { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need to discard your changes.
you can see the changes that introduced the try-with-resource at
b99cdc8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this relates to the entire UpgradeUtil.java
String folderPath = realPath + "/WEB-INF/view/module/" + mod.getModuleIdAsPath(); | ||
File outFile = new File(folderPath.replace("/", File.separator)); | ||
//String folderPath = realPath + "/WEB-INF/view/module/" + mod.getModuleIdAsPath(); | ||
File outFile = Paths.get(realPath, "WEB-INF", "view", "module", mod.getModuleIdAsPath()).toFile(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MalshaL cant you just use
File outFile = new File(webInf, "view", ...);
instead of using the Paths.get() ?
Also please do not comment out code! Always remove it. We use git, if we need it back we can easily get it.
String absPath = realPath + "/WEB-INF/view/module/" + moduleId; | ||
File moduleWebFolder = new File(absPath.replace("/", File.separator)); | ||
//String absPath = realPath + "/WEB-INF/view/module/" + moduleId; | ||
File moduleWebFolder = Paths.get(realPath, "WEB-INF", "view", "module", moduleId).toFile(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as my last comment above this one
@@ -775,7 +775,7 @@ public static void stopModule(Module mod, ServletContext servletContext, boolean | |||
if (root.getElementsByTagName("dwr").getLength() > 0) { | |||
|
|||
// get the dwr-module.xml file that we're appending our code to | |||
File f = new File(realPath + "/WEB-INF/dwr-modules.xml".replace("/", File.separator)); | |||
File f = Paths.get(realPath, "WEB-INF", "dwr-modules.xml").toFile(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same, use new File()
@@ -961,8 +961,7 @@ public static void createDwrModulesXml(String realPath) { | |||
TransformerFactory transformerFactory = TransformerFactory.newInstance(); | |||
Transformer transformer = transformerFactory.newTransformer(); | |||
DOMSource source = new DOMSource(doc); | |||
StreamResult result = new StreamResult(new File(realPath | |||
+ "/WEB-INF/dwr-modules.xml".replace("/", File.separator))); | |||
StreamResult result = new StreamResult(Paths.get(realPath, "WEB-INF", "dwr-modules.xml").toFile()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
@@ -67,8 +68,7 @@ protected static boolean addTestData(String host, int port, String databaseName, | |||
|
|||
//For stand-alone, use explicit path to the mysql executable. | |||
String runDirectory = System.getProperties().getProperty("user.dir"); | |||
File file = new File(runDirectory + File.separatorChar + "database" + File.separatorChar + "bin" | |||
+ File.separatorChar + "mysql"); | |||
File file = Paths.get(runDirectory, "database", "bin", "mysql").toFile(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
@@ -41,8 +42,7 @@ public void createWebInfFolderIfNotExist() { | |||
//when run from the IDE and this folder does not exist, some tests fail with | |||
//org.openmrs.module.ModuleException: Unable to load module messages from file: | |||
// /Projects/openmrs/core/web/target/test-classes/WEB-INF/module_messages_fr.properties | |||
|
|||
File folder = new File("target" + File.separatorChar + "test-classes" + File.separatorChar + "WEB-INF"); | |||
File folder = Paths.get("target", "test-classes", "WEB-INF").toFile(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
@MalshaL am closing this for now. Will reopen when you are ready to reclaim the ticket. 😊 |
Description of what I changed
Issue I worked on
see https://issues.openmrs.org/browse/TRUNK-5134
Checklist: I completed these to help reviewers :)
My pull request only contains ONE single commit
(the number above, next to the 'Commits' tab is 1).
No? -> read here on how to squash multiple commits into one
My IDE is configured to follow the code style of this project.
No? Unsure? -> configure your IDE, format the code and add the changes with
git add . && git commit --amend
I have added tests to cover my changes. (If you refactored
existing code that was well tested you do not have to add tests)
No? -> write tests and add them to this commit
git add . && git commit --amend
I ran
mvn clean package
right before creating this pull request andadded all formatting changes to my commit.
No? -> execute above command
All new and existing tests passed.
No? -> figure out why and add the fix to your commit. It is your responsibility to make sure your code works.
My pull request is based on the latest changes of the master branch.
No? Unsure? -> execute command
git pull --rebase upstream master