Skip to content

Commit 869f8dc

Browse files
committed
Test that exception is thrown when encountering non-static main
1 parent 4a8d4b9 commit 869f8dc

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.util;
19+
20+
public class NonStaticMain {
21+
public void main(String[] args) {
22+
}
23+
}

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestRunJar.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,35 @@ public void testUnJar2() throws IOException {
297297
"would create file outside of", e);
298298
}
299299
}
300-
}
300+
301+
/**
302+
* Tests that RunJar errors appropriately for classes with non-static main
303+
* methods.
304+
*/
305+
public void testRunNonStaticMain() throws Throwable {
306+
RunJar runJar = spy(new RunJar());
307+
// enable the client classloader
308+
when(runJar.useClientClassLoader()).thenReturn(true);
309+
// set the system classes and blacklist the test main class and the test
310+
// third class so they can be loaded by the application classloader
311+
String mainCls = NonStaticMain.class.getName();
312+
String systemClasses = "-" + mainCls + "," + ApplicationClassLoader.SYSTEM_CLASSES_DEFAULT;
313+
when(runJar.getSystemClasses()).thenReturn(systemClasses);
314+
315+
// create the test jar
316+
File testJar = JarFinder.makeClassLoaderTestJar(this.getClass(), TEST_ROOT_DIR, TEST_JAR_2_NAME, BUFF_SIZE,
317+
mainCls);
318+
// form the args
319+
String[] args = new String[3];
320+
args[0] = testJar.getAbsolutePath();
321+
args[1] = mainCls;
322+
323+
// run RunJar
324+
try {
325+
runJar.run(args);
326+
fail("run should throw IOException.");
327+
} catch (IOException e) {
328+
GenericTestUtils.assertExceptionContains("Method main must be static", e);
329+
}
330+
}
331+
}

0 commit comments

Comments
 (0)