For a project I'm working on I'm trying to create a script that will go over all files with name *Test*.java (ex. AccountServiceTest.java) . Each file contains Java Test methods. During the years the naming conventions have been interpreted by each team member differently and we ended with a mess of Test method names. As a refactoring task I'm trying to rename all those method names correctly. Manually it will take too long, so I'm trying to do it with Bash scripts. This script should change the name of the method based on rules discussed in the team.
for example :
...
public void givensomethingWhensomethingElseThensomethingElse() {
...
should end up like :
...
public void given_something_when_somethingElse_then_somethingElse() {
...
These are the steps that I now think that should do the trick:
Pick line that contains "public void"
catch given and make sure it's in lowercase (GIVEN_something results in given_something)
catch when and make sure it's in lowercase
catch then and make sure it's in lowercase
catch string between given and when (or then as sometimes there is no when statement in method name)
- String found for example is _SomeRandomText - remove all _ --> SomeRandomText - change first character to lowercase --> someRandomText - add _ in front and in back --> _someRandomText_ - result is : ...given_someRandomText_when...
catch string between when and then
- String found for example is _SomeRandomText - remove all _ --> SomeRandomText - change first character to lowercase --> someRandomText - add _ in front and in back --> _someRandomText_ - result is : ...when_someRandomText_then...
catch string between then and the parenthesis
- String found for example is _SomeRandomText - remove all _ --> SomeRandomText - change first character to lowercase --> someRandomText - add _ in front --> _someRandomText - result is : ...then_someRandomText(...
I think sed will be the solution here, but I'm just not seeing how I can create it. Anyone an idea?