Skip to content

Commit d0d9400

Browse files
committed
remove logic from main method
1 parent b787e05 commit d0d9400

File tree

3 files changed

+50
-47
lines changed

3 files changed

+50
-47
lines changed

EclipseFormatter/src/main/java/com/spidasoftware/EclipseFormatter/Formatter.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,21 @@
5757
public class Formatter {
5858
private static Logger log = Logger.getRootLogger();
5959
private static Options options = new Options();
60-
private static boolean instantiateLoggerOnlyOnce = true;
6160

6261
/**
63-
* This method will parse the command line arguments and format the file(s).
62+
* This method will pass the command line arguments to the runFormatter method.
6463
* @param args the command-line arguments
6564
*/
6665
public static void main(String[] args) {
66+
instantiateLogger();
67+
runFormatter(args);
68+
}
6769

68-
// This conditional is incase clients of this class call this object more than once, the logger
69-
// will only be instantiated once.
70-
if (instantiateLoggerOnlyOnce) {
71-
instantiateLogger();
72-
instantiateLoggerOnlyOnce = false;
73-
}
70+
/**
71+
* This method will perform the main logic of the program
72+
* @param args the command-line arguments
73+
*/
74+
public static void runFormatter(String[] args) {
7475
CommandLine cmd = getOptions(args, options);
7576
args = cmd.getArgs();
7677
if (cmd.hasOption("help")) {

EclipseFormatter/src/test/java/com/spidasoftware/EclipseFormatter/FormatterTest.java

+41-39
Original file line numberDiff line numberDiff line change
@@ -455,19 +455,19 @@ public void testisgroovyScriptFalse() {
455455
}
456456

457457
/**
458-
* test the main method on a java file
458+
* test the runFormatter method on a java file
459459
*/
460-
public void testmainOnAJavaFile() {
461-
log.info("Formatter.main on a java file");
460+
public void testrunFormatterOnAJavaFile() {
461+
log.info("Formatter.runFormatter on a java file");
462462
fileName = System.getProperty("user.dir") + File.separator + "javaEclipseFormatterTest13243546.java";
463463
absolutePathFile = new File(fileName);
464464
String before = " package groovyTest;\n public class genericJavaClass"
465-
+ "{\npublic static void main(String[] args) {\n// TODO Auto-generated method stub\n}\n}";
465+
+ "{\npublic static main(String[] args) {\n// TODO Auto-generated method stub\n}\n}";
466466
try {
467467
absolutePathFile.createNewFile();
468468
String[] args = new String[] { "-java", fileName };
469469
FileUtils.writeStringToFile(absolutePathFile, before);
470-
Formatter.main(args);
470+
Formatter.runFormatter(args);
471471
} catch (SecurityException e) {
472472
log.error(e, e);
473473
} catch (IOException e) {
@@ -478,10 +478,10 @@ public void testmainOnAJavaFile() {
478478
}
479479

480480
/**
481-
* test the main method on a groovy file
481+
* test the runFormatter method on a groovy file
482482
*/
483-
public void testmainOnAGroovyFile() {
484-
log.info("Formatter.main on a groovy file");
483+
public void testrunFormatterOnAGroovyFile() {
484+
log.info("Formatter.runFormatter on a groovy file");
485485
fileName = System.getProperty("user.dir") + File.separator + "javaEclipseFormatterTest13243546.groovy";
486486
absolutePathFile = new File(fileName);
487487
String before = " package groovyTest;\n public class genericJavaClass"
@@ -490,20 +490,21 @@ public void testmainOnAGroovyFile() {
490490
absolutePathFile.createNewFile();
491491
String[] args = new String[] { "-groovy", fileName };
492492
FileUtils.writeStringToFile(absolutePathFile, before);
493-
Formatter.main(args);
493+
Formatter.runFormatter(args);
494494
} catch (SecurityException e) {
495495
log.error(e, e);
496496
} catch (IOException e) {
497497
log.error(e, e);
498498
}
499-
assertTrue("Formatter.main on a groovy file that does exist", !before.equals(Formatter.readInFile(fileName)));
499+
assertTrue("Formatter.runFormatter on a groovy file that does exist",
500+
!before.equals(Formatter.readInFile(fileName)));
500501
}
501502

502503
/**
503-
* Test the main method on a directory
504+
* Test the runFormatter method on a directory
504505
*/
505-
public void testmainOnADirectory() {
506-
log.info("Formatter.main on a directory that does exist");
506+
public void testrunFormatterOnADirectory() {
507+
log.info("Formatter.runFormatter on a directory that does exist");
507508
String dirName = System.getProperty("user.dir") + File.separator + "test";
508509
String fileName1 = System.getProperty("user.dir") + File.separator + "test" + File.separator
509510
+ "javaEclipseFormatterTest13243546.java";
@@ -525,14 +526,14 @@ public void testmainOnADirectory() {
525526
FileUtils.writeStringToFile(absolutePathFile1, before1);
526527
FileUtils.writeStringToFile(absolutePathFile2, before2);
527528
String[] args = new String[] { dirName };
528-
Formatter.main(args);
529+
Formatter.runFormatter(args);
529530
} catch (SecurityException e) {
530531
log.error(e, e);
531532
} catch (IOException e) {
532533
log.error(e, e);
533534
}
534-
assertTrue("Formatter.main on a directory that does exist", !before1.equals(Formatter.readInFile(fileName1))
535-
&& !before2.equals(Formatter.readInFile(fileName2)));
535+
assertTrue("Formatter.runFormatter on a directory that does exist",
536+
!before1.equals(Formatter.readInFile(fileName1)) && !before2.equals(Formatter.readInFile(fileName2)));
536537
if (absolutePathFile1 != null) {
537538
if (absolutePathFile1.exists()) {
538539
absolutePathFile1.delete();
@@ -551,10 +552,10 @@ public void testmainOnADirectory() {
551552
}
552553

553554
/**
554-
* Test the main method on a directory with a groovy script
555+
* Test the runFormatter method on a directory with a groovy script
555556
*/
556-
public void testmainOnADirectoryGroovyScript() {
557-
log.info("Formatter.main on a directory that does exist with a groovy script");
557+
public void testrunFormatterOnADirectoryGroovyScript() {
558+
log.info("Formatter.runFormatter on a directory that does exist with a groovy script");
558559
String dirName = System.getProperty("user.dir") + File.separator + "test";
559560
String fileName1 = System.getProperty("user.dir") + File.separator + "test" + File.separator
560561
+ "javaEclipseFormatterTest13243546.java";
@@ -576,14 +577,14 @@ public void testmainOnADirectoryGroovyScript() {
576577
FileUtils.writeStringToFile(absolutePathFile1, before1);
577578
FileUtils.writeStringToFile(absolutePathFile2, before2);
578579
String[] args = new String[] { dirName };
579-
Formatter.main(args);
580+
Formatter.runFormatter(args);
580581
} catch (SecurityException e) {
581582
log.error(e, e);
582583
} catch (IOException e) {
583584
log.error(e, e);
584585
}
585-
assertTrue("Formatter.main on a directory that does exist", !before1.equals(Formatter.readInFile(fileName1))
586-
&& !before2.equals(Formatter.readInFile(fileName2)));
586+
assertTrue("Formatter.runFormatter on a directory that does exist",
587+
!before1.equals(Formatter.readInFile(fileName1)) && !before2.equals(Formatter.readInFile(fileName2)));
587588
if (absolutePathFile1 != null) {
588589
if (absolutePathFile1.exists()) {
589590
absolutePathFile1.delete();
@@ -602,10 +603,10 @@ public void testmainOnADirectoryGroovyScript() {
602603
}
603604

604605
/**
605-
* Test the main method on a directory with a groovy script and javaflag set
606+
* Test the runFormatter method on a directory with a groovy script and javaflag set
606607
*/
607-
public void testmainOnADirectoryGroovyScriptJavaFlagSet() {
608-
log.info("Formatter.main on a directory that does exist with a groovy script and javaflag set");
608+
public void testrunFormatterOnADirectoryGroovyScriptJavaFlagSet() {
609+
log.info("Formatter.runFormatter on a directory that does exist with a groovy script and javaflag set");
609610
String dirName = System.getProperty("user.dir") + File.separator + "test";
610611
String fileName1 = System.getProperty("user.dir") + File.separator + "test" + File.separator
611612
+ "javaEclipseFormatterTest13243546.java";
@@ -627,13 +628,13 @@ public void testmainOnADirectoryGroovyScriptJavaFlagSet() {
627628
FileUtils.writeStringToFile(absolutePathFile1, before1);
628629
FileUtils.writeStringToFile(absolutePathFile2, before2);
629630
String[] args = new String[] { "-java", dirName };
630-
Formatter.main(args);
631+
Formatter.runFormatter(args);
631632
} catch (SecurityException e) {
632633
log.error(e, e);
633634
} catch (IOException e) {
634635
log.error(e, e);
635636
}
636-
assertTrue("Formatter.main on a directory that does exist with a groovy script and javaflag set",
637+
assertTrue("Formatter.runFormatter on a directory that does exist with a groovy script and javaflag set",
637638
!before1.equals(Formatter.readInFile(fileName1)) && before2.equals(Formatter.readInFile(fileName2)));
638639
if (absolutePathFile1 != null) {
639640
if (absolutePathFile1.exists()) {
@@ -653,10 +654,10 @@ public void testmainOnADirectoryGroovyScriptJavaFlagSet() {
653654
}
654655

655656
/**
656-
* Test the main method on a directory with a groovy script and Groovy flag set
657+
* Test the runFormatter method on a directory with a groovy script and Groovy flag set
657658
*/
658-
public void testmainOnADirectoryGroovyScriptGroovyFlagSet() {
659-
log.info("Formatter.main on a directory that does exist with a groovy script and Groovy flag set");
659+
public void testrunFormatterOnADirectoryGroovyScriptGroovyFlagSet() {
660+
log.info("Formatter.runFormatter on a directory that does exist with a groovy script and Groovy flag set");
660661
String dirName = System.getProperty("user.dir") + File.separator + "test";
661662
String fileName1 = System.getProperty("user.dir") + File.separator + "test" + File.separator
662663
+ "javaEclipseFormatterTest13243546.java";
@@ -678,13 +679,13 @@ public void testmainOnADirectoryGroovyScriptGroovyFlagSet() {
678679
FileUtils.writeStringToFile(absolutePathFile1, before1);
679680
FileUtils.writeStringToFile(absolutePathFile2, before2);
680681
String[] args = new String[] { "-groovy", dirName };
681-
Formatter.main(args);
682+
Formatter.runFormatter(args);
682683
} catch (SecurityException e) {
683684
log.error(e, e);
684685
} catch (IOException e) {
685686
log.error(e, e);
686687
}
687-
assertTrue("Formatter.main on a directory that does exist with a groovy script and Groovy flag set",
688+
assertTrue("Formatter.runFormatter on a directory that does exist with a groovy script and Groovy flag set",
688689
before1.equals(Formatter.readInFile(fileName1)) && !before2.equals(Formatter.readInFile(fileName2)));
689690
if (absolutePathFile1 != null) {
690691
if (absolutePathFile1.exists()) {
@@ -704,25 +705,26 @@ public void testmainOnADirectoryGroovyScriptGroovyFlagSet() {
704705
}
705706

706707
/**
707-
* test the main method on a groovy file
708+
* test the runFormatter method on a groovy file that has unrecognizable syntax
708709
*/
709-
public void testmainOnAGroovyFileparseError() {
710-
log.info("Formatter.main on a groovy file");
710+
public void testrunFormatterOnAGroovyFileparseError() {
711+
log.info("Formatter.runFormatter on a groovy file that has unrecognizable syntax");
711712
fileName = System.getProperty("user.dir") + File.separator + "javaEclipseFormatterTest13243546.groovy";
712713
absolutePathFile = new File(fileName);
713-
String before = "\n@artifact.package@class @artifact.name@ { \n "
714-
+ "public class genericJavaClass" + "{\npublic static void main(String[] args) {\n// TODO Auto-generated method stub\n}\n}";
714+
String before = "\n@artifact.package@class @artifact.name@ { \n " + "public class genericJavaClass"
715+
+ "{\npublic static void main(String[] args) {\n// TODO Auto-generated method stub\n}\n}";
715716
try {
716717
absolutePathFile.createNewFile();
717718
String[] args = new String[] { "-groovy", fileName };
718719
FileUtils.writeStringToFile(absolutePathFile, before);
719-
Formatter.main(args);
720+
Formatter.runFormatter(args);
720721
} catch (SecurityException e) {
721722
log.error(e, e);
722723
} catch (IOException e) {
723724
log.error(e, e);
724725
}
725-
assertTrue("Formatter.main on a groovy file that does exist", before.equals(Formatter.readInFile(fileName)));
726+
assertTrue("Formatter.runFormatter on a groovy file that has unrecognizable syntax",
727+
before.equals(Formatter.readInFile(fileName)));
726728
}
727729
}
728730

bin/formatters/EclipseFormatter.jar

-13 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)