-
Notifications
You must be signed in to change notification settings - Fork 9.1k
MAPREDUCE-7251: Present friendlier error for non-static main #1751
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
4a8d4b9
869f8dc
fa56d95
2ab04ae
5616672
93b909e
415418f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.util; | ||
|
||
/** | ||
* NonStaticMain is used to test classes which provide a main which | ||
* isn't static. | ||
*/ | ||
public class NonStaticMain { | ||
public void main(String[] args) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ public class TestRunJar { | |
private static final int BUFF_SIZE = 2048; | ||
private File TEST_ROOT_DIR; | ||
|
||
private static final String TEST_JAR_NAME="test-runjar.jar"; | ||
private static final String TEST_JAR_NAME = "test-runjar.jar"; | ||
private static final String TEST_JAR_2_NAME = "test-runjar2.jar"; | ||
private static final long MOCKED_NOW = 1_460_389_972_000L; | ||
private static final long MOCKED_NOW_PLUS_TWO_SEC = MOCKED_NOW + 2_000; | ||
|
@@ -297,4 +297,35 @@ public void testUnJar2() throws IOException { | |
"would create file outside of", e); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Tests that RunJar errors appropriately for classes with non-static main | ||
* methods. | ||
*/ | ||
@Test | ||
public void testRunNonStaticMain() throws Throwable { | ||
RunJar runJar = spy(new RunJar()); | ||
// enable the client classloader | ||
when(runJar.useClientClassLoader()).thenReturn(true); | ||
// set the system classes and blacklist the test main class so it | ||
// can be loaded by the application classloader | ||
String mainCls = NonStaticMain.class.getName(); | ||
String systemClasses = "-" + mainCls + "," | ||
+ ApplicationClassLoader.SYSTEM_CLASSES_DEFAULT; | ||
when(runJar.getSystemClasses()).thenReturn(systemClasses); | ||
|
||
// create the test jar | ||
File testJar = JarFinder.makeClassLoaderTestJar(this.getClass(), | ||
TEST_ROOT_DIR, TEST_JAR_2_NAME, BUFF_SIZE, mainCls); | ||
// form the args | ||
String[] args = new String[] {testJar.getAbsolutePath(), mainCls}; | ||
|
||
// run RunJar | ||
try { | ||
runJar.run(args); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use LambaTestUtils#intercept There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, this doesn't work because |
||
fail("run should throw IOException."); | ||
} catch (IOException e) { | ||
GenericTestUtils.assertExceptionContains("Method main must be static", e); | ||
} | ||
} | ||
} |
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.
Can we add a unit test to make sure we trigger this instead of NPE?
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've added a test, but I couldn't get Hadoop to run locally—seeing this error:
Guidance from the Dev Environment Setup was no help unfortunately. The suggested docker script also fails to build.