MAPREDUCE-2539. Fixed NPE in getMapTaskReports in JobClient. Contributed by Robert Evans.


git-svn-id: https://svn.apache.org/repos/asf/hadoop/mapreduce/trunk@1130994 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index 45caa30..493d130 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -163,6 +163,9 @@
 
   BUG FIXES
 
+    MAPREDUCE-2539. Fixed NPE in getMapTaskReports in JobClient. (Robert Evans via
+    acmurthy) 
+
     MAPREDUCE-2531. Fixed jobcontrol to downgrade JobID. (Robert Evans via
     acmurthy) 
 
diff --git a/src/java/org/apache/hadoop/mapred/JobClient.java b/src/java/org/apache/hadoop/mapred/JobClient.java
index ec1c731..245e362 100644
--- a/src/java/org/apache/hadoop/mapred/JobClient.java
+++ b/src/java/org/apache/hadoop/mapred/JobClient.java
@@ -576,6 +576,8 @@
     return getJob(JobID.forName(jobid));
   }
   
+  private static final TaskReport[] EMPTY_TASK_REPORTS = new TaskReport[0];
+  
   /**
    * Get the information of the current state of the map tasks of a job.
    * 
@@ -584,9 +586,16 @@
    * @throws IOException
    */
   public TaskReport[] getMapTaskReports(JobID jobId) throws IOException {
+    return getTaskReports(jobId, TaskType.MAP);
+  }
+  
+  private TaskReport[] getTaskReports(JobID jobId, TaskType type) throws IOException {
     try {
-      return TaskReport.downgradeArray(
-        cluster.getJob(jobId).getTaskReports(TaskType.MAP));
+      Job j = cluster.getJob(jobId);
+      if(j == null) {
+        return EMPTY_TASK_REPORTS;
+      }
+      return TaskReport.downgradeArray(j.getTaskReports(type));
     } catch (InterruptedException ie) {
       throw new IOException(ie);
     }
@@ -606,12 +615,7 @@
    * @throws IOException
    */    
   public TaskReport[] getReduceTaskReports(JobID jobId) throws IOException {
-    try {
-      return TaskReport.downgradeArray(
-        cluster.getJob(jobId).getTaskReports(TaskType.REDUCE));
-    } catch (InterruptedException ie) {
-      throw new IOException(ie);
-    }
+    return getTaskReports(jobId, TaskType.REDUCE);
   }
 
   /**
@@ -622,12 +626,7 @@
    * @throws IOException
    */    
   public TaskReport[] getCleanupTaskReports(JobID jobId) throws IOException {
-    try {
-      return TaskReport.downgradeArray(
-        cluster.getJob(jobId).getTaskReports(TaskType.JOB_CLEANUP));
-    } catch (InterruptedException ie) {
-      throw new IOException(ie);
-    }
+    return getTaskReports(jobId, TaskType.JOB_CLEANUP);
   }
 
   /**
@@ -638,12 +637,7 @@
    * @throws IOException
    */    
   public TaskReport[] getSetupTaskReports(JobID jobId) throws IOException {
-    try {
-      return TaskReport.downgradeArray(
-        cluster.getJob(jobId).getTaskReports(TaskType.JOB_SETUP));
-    } catch (InterruptedException ie) {
-      throw new IOException(ie);
-    }
+    return getTaskReports(jobId, TaskType.JOB_SETUP);
   }
 
   
diff --git a/src/test/mapred/org/apache/hadoop/mapred/JobClientUnitTest.java b/src/test/mapred/org/apache/hadoop/mapred/JobClientUnitTest.java
new file mode 100644
index 0000000..11873c1
--- /dev/null
+++ b/src/test/mapred/org/apache/hadoop/mapred/JobClientUnitTest.java
@@ -0,0 +1,94 @@
+/**
+ * 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.mapred;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.apache.hadoop.mapreduce.Cluster;
+import org.junit.Test;
+
+public class JobClientUnitTest {
+  
+  @SuppressWarnings("deprecation")
+  @Test
+  public void testMapTaskReportsWithNullJob() throws Exception {
+    JobClient client = new JobClient();
+    Cluster mockCluster = mock(Cluster.class);
+    client.cluster = mockCluster;
+    JobID id = new JobID("test",0);
+    
+    when(mockCluster.getJob(id)).thenReturn(null);
+    
+    TaskReport[] result = client.getMapTaskReports(id);
+    assertEquals(0, result.length);
+    
+    verify(mockCluster).getJob(id);
+  }
+  
+  @SuppressWarnings("deprecation")
+  @Test
+  public void testReduceTaskReportsWithNullJob() throws Exception {
+    JobClient client = new JobClient();
+    Cluster mockCluster = mock(Cluster.class);
+    client.cluster = mockCluster;
+    JobID id = new JobID("test",0);
+    
+    when(mockCluster.getJob(id)).thenReturn(null);
+    
+    TaskReport[] result = client.getReduceTaskReports(id);
+    assertEquals(0, result.length);
+    
+    verify(mockCluster).getJob(id);
+  }
+  
+  @SuppressWarnings("deprecation")
+  @Test
+  public void testSetupTaskReportsWithNullJob() throws Exception {
+    JobClient client = new JobClient();
+    Cluster mockCluster = mock(Cluster.class);
+    client.cluster = mockCluster;
+    JobID id = new JobID("test",0);
+    
+    when(mockCluster.getJob(id)).thenReturn(null);
+    
+    TaskReport[] result = client.getSetupTaskReports(id);
+    assertEquals(0, result.length);
+    
+    verify(mockCluster).getJob(id);
+  }
+  
+  @SuppressWarnings("deprecation")
+  @Test
+  public void testCleanupTaskReportsWithNullJob() throws Exception {
+    JobClient client = new JobClient();
+    Cluster mockCluster = mock(Cluster.class);
+    client.cluster = mockCluster;
+    JobID id = new JobID("test",0);
+    
+    when(mockCluster.getJob(id)).thenReturn(null);
+    
+    TaskReport[] result = client.getCleanupTaskReports(id);
+    assertEquals(0, result.length);
+    
+    verify(mockCluster).getJob(id);
+  }
+}