3737 */
3838class SpringApplicationAotProcessorTests {
3939
40+ private static final ApplicationInvoker invoker = new ApplicationInvoker ();
41+
4042 @ BeforeEach
4143 void setup () {
42- SampleApplication .argsHolder = null ;
43- SampleApplication .postRunInvoked = false ;
44+ invoker .clean ();
4445 }
4546
4647 @ Test
47- void processApplicationInvokesRunMethod (@ TempDir Path directory ) {
48+ void processApplicationInvokesMainMethod (@ TempDir Path directory ) {
4849 String [] arguments = new String [] { "1" , "2" };
49- SpringApplicationAotProcessor processor = new SpringApplicationAotProcessor (SampleApplication .class ,
50+ SpringApplicationAotProcessor processor = new SpringApplicationAotProcessor (PublicMainMethod .class ,
5051 settings (directory ), arguments );
5152 processor .process ();
52- assertThat (SampleApplication .argsHolder ).isEqualTo (arguments );
53- assertThat (SampleApplication .postRunInvoked ).isFalse ();
53+ assertThat (ApplicationInvoker .argsHolder ).isEqualTo (arguments );
54+ assertThat (ApplicationInvoker .postRunInvoked ).isFalse ();
5455 }
5556
5657 @ Test
@@ -63,23 +64,53 @@ void processApplicationWithMainMethodThatDoesNotRun(@TempDir Path directory) {
6364 }
6465
6566 @ Test
66- void invokeMainParsesArgumentsAndInvokesRunMethod (@ TempDir Path directory ) throws Exception {
67- String [] mainArguments = new String [] { SampleApplication .class .getName (),
67+ void invokeMainParsesArgumentsAndInvokesMainMethod (@ TempDir Path directory ) throws Exception {
68+ String [] mainArguments = new String [] { PublicMainMethod .class .getName (),
69+ directory .resolve ("source" ).toString (), directory .resolve ("resource" ).toString (),
70+ directory .resolve ("class" ).toString (), "com.example" , "example" , "1" , "2" };
71+ SpringApplicationAotProcessor .main (mainArguments );
72+ assertThat (ApplicationInvoker .argsHolder ).containsExactly ("1" , "2" );
73+ assertThat (ApplicationInvoker .postRunInvoked ).isFalse ();
74+ }
75+
76+ @ Test
77+ void invokeMainParsesArgumentsAndInvokesPackagePrivateMainMethod (@ TempDir Path directory ) throws Exception {
78+ String [] mainArguments = new String [] { PackagePrivateMainMethod .class .getName (),
6879 directory .resolve ("source" ).toString (), directory .resolve ("resource" ).toString (),
6980 directory .resolve ("class" ).toString (), "com.example" , "example" , "1" , "2" };
7081 SpringApplicationAotProcessor .main (mainArguments );
71- assertThat (SampleApplication .argsHolder ).containsExactly ("1" , "2" );
72- assertThat (SampleApplication .postRunInvoked ).isFalse ();
82+ assertThat (ApplicationInvoker .argsHolder ).containsExactly ("1" , "2" );
83+ assertThat (ApplicationInvoker .postRunInvoked ).isFalse ();
84+ }
85+
86+ @ Test
87+ void invokeMainParsesArgumentsAndInvokesParameterLessMainMethod (@ TempDir Path directory ) throws Exception {
88+ String [] mainArguments = new String [] { PublicParameterlessMainMethod .class .getName (),
89+ directory .resolve ("source" ).toString (), directory .resolve ("resource" ).toString (),
90+ directory .resolve ("class" ).toString (), "com.example" , "example" , "1" , "2" };
91+ SpringApplicationAotProcessor .main (mainArguments );
92+ assertThat (ApplicationInvoker .argsHolder ).isNull ();
93+ assertThat (ApplicationInvoker .postRunInvoked ).isFalse ();
94+ }
95+
96+ @ Test
97+ void invokeMainParsesArgumentsAndInvokesPackagePrivateRunMethod (@ TempDir Path directory ) throws Exception {
98+ String [] mainArguments = new String [] { PackagePrivateParameterlessMainMethod .class .getName (),
99+ directory .resolve ("source" ).toString (), directory .resolve ("resource" ).toString (),
100+ directory .resolve ("class" ).toString (), "com.example" , "example" , "1" , "2" };
101+ SpringApplicationAotProcessor .main (mainArguments );
102+ assertThat (ApplicationInvoker .argsHolder ).isNull ();
103+ assertThat (ApplicationInvoker .postRunInvoked ).isFalse ();
73104 }
74105
75106 @ Test
76107 void invokeMainParsesArgumentsAndInvokesRunMethodWithoutGroupId (@ TempDir Path directory ) throws Exception {
77- String [] mainArguments = new String [] { SampleApplication .class .getName (),
108+ String [] mainArguments = new String [] { PublicMainMethod .class .getName (),
78109 directory .resolve ("source" ).toString (), directory .resolve ("resource" ).toString (),
79110 directory .resolve ("class" ).toString (), "" , "example" , "1" , "2" };
80111 SpringApplicationAotProcessor .main (mainArguments );
81- assertThat (SampleApplication .argsHolder ).containsExactly ("1" , "2" );
82- assertThat (SampleApplication .postRunInvoked ).isFalse ();
112+ assertThat (ApplicationInvoker .argsHolder ).containsExactly ("1" , "2" );
113+ assertThat (ApplicationInvoker .postRunInvoked ).isFalse ();
83114 }
84115
85116 @ Test
@@ -100,16 +131,37 @@ private Settings settings(Path directory) {
100131 }
101132
102133 @ Configuration (proxyBeanMethods = false )
103- public static class SampleApplication {
134+ public static class PublicMainMethod {
104135
105- public static String @ Nullable [] argsHolder ;
136+ public static void main (String [] args ) {
137+ invoker .invoke (args , () -> SpringApplication .run (PublicMainMethod .class , args ));
138+ }
106139
107- public static boolean postRunInvoked ;
140+ }
108141
109- public static void main (String [] args ) {
110- argsHolder = args ;
111- SpringApplication .run (SampleApplication .class , args );
112- postRunInvoked = true ;
142+ @ Configuration (proxyBeanMethods = false )
143+ public static class PackagePrivateMainMethod {
144+
145+ static void main (String [] args ) {
146+ invoker .invoke (args , () -> SpringApplication .run (PackagePrivateMainMethod .class , args ));
147+ }
148+
149+ }
150+
151+ @ Configuration (proxyBeanMethods = false )
152+ public static class PublicParameterlessMainMethod {
153+
154+ public static void main () {
155+ invoker .invoke (null , () -> SpringApplication .run (PublicParameterlessMainMethod .class ));
156+ }
157+
158+ }
159+
160+ @ Configuration (proxyBeanMethods = false )
161+ public static class PackagePrivateParameterlessMainMethod {
162+
163+ static void main () {
164+ invoker .invoke (null , () -> SpringApplication .run (PackagePrivateParameterlessMainMethod .class ));
113165 }
114166
115167 }
@@ -122,4 +174,23 @@ public static void main(String[] args) {
122174
123175 }
124176
177+ private static final class ApplicationInvoker {
178+
179+ public static String @ Nullable [] argsHolder ;
180+
181+ public static boolean postRunInvoked ;
182+
183+ void invoke (String @ Nullable [] args , Runnable applicationRun ) {
184+ argsHolder = args ;
185+ applicationRun .run ();
186+ postRunInvoked = true ;
187+ }
188+
189+ void clean () {
190+ argsHolder = null ;
191+ postRunInvoked = false ;
192+ }
193+
194+ }
195+
125196}
0 commit comments