<?xml version="1.0" encoding="UTF-8"?>
<test-data xmlns="http://pmd.sourceforge.net/rule-tests"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
   <test-code>
      <description>Returning null array</description>
      <expected-problems>1</expected-problems>
      <expected-linenumbers>6</expected-linenumbers>
      <code><![CDATA[
public class Foo {
    // Not a good idea...
    public int []bar()
    {
        // ...
        return null;
    }
}
        ]]></code>
   </test-code>
   <test-code>
      <description>Nonnull empty array</description>
      <expected-problems>0</expected-problems>
      <code><![CDATA[
public class Foo {
    // Good behavior
    public String[] bar()
    {
        //...
        return new String[0];
    }
}
        ]]></code>
   </test-code>
   <test-code>
      <description>Returning null instead of collection (List)</description>
      <expected-problems>1</expected-problems>
      <expected-linenumbers>8</expected-linenumbers>
      <code><![CDATA[
import java.util.List;

public class Foo {
    // Not a good idea...
    public List<String> bar()
    {
        // ...
        return null;
    }
}
        ]]></code>
   </test-code>
   <test-code>
      <description>Returning proper empty collection</description>
      <expected-problems>0</expected-problems>
      <code><![CDATA[
import java.util.List;
import java.util.Collections;

public class Foo {
    // Not a good idea...
    public List<String> bar()
    {
        // ...
        return Collections.emptyList();
    }
}
        ]]></code>
   </test-code>
   <test-code>
      <description>Returning null instead of collection (Set)</description>
      <expected-problems>1</expected-problems>
      <expected-linenumbers>8</expected-linenumbers>
      <code><![CDATA[
import java.util.Set;

public class Foo {
    // Not a good idea...
    public Set<String> bar()
    {
        // ...
        return null;
    }
}
        ]]></code>
   </test-code>
   <test-code>
      <description>Returning null instead of collection (Map)</description>
      <expected-problems>1</expected-problems>
      <expected-linenumbers>8</expected-linenumbers>
      <code><![CDATA[
import java.util.Map;

public class Foo {
    // Not a good idea...
    public Map<String, String> bar()
    {
        // ...
        return null;
    }
}
        ]]></code>
   </test-code>

  <test-code>
    <description>False negative case #3686</description>
    <expected-problems>2</expected-problems>
    <expected-linenumbers>4, 6</expected-linenumbers>
    <code><![CDATA[
public class Foo {
    public int[] foo() {
     if(true) {
         return null;   //violation
     } else {
         return null;   //violation
     }
   }
}
    ]]></code>
  </test-code>

  <test-code>
    <description>Null is returned from an internal statement, not from the method</description>
    <expected-problems>0</expected-problems>
    <code><![CDATA[
public class Foo {

    private int[] foo() {
        bar(new SomeInterface() {
            @Override
            public Object run() {
                baz();
                return null;
            }
        });

        return new int[0];
    }
}

interface SomeInterface {
    Object run();
}
    ]]></code>
  </test-code>

  <test-code>
    <description>Null is returned from an internal statement, not from the method</description>
    <expected-problems>0</expected-problems>
    <code><![CDATA[
public class Foo {

    private int[] foo() {
        bar(() -> {
            baz();
            return null;
        });

        return new int[0];
    }
}
    ]]></code>
  </test-code>

  <test-code>
    <description>Null is returned from a loop</description>
    <expected-problems>1</expected-problems>
    <expected-linenumbers>7</expected-linenumbers>
    <code><![CDATA[
  import java.util.List;

  public class Foo {
      private List<Integer> foo(List<Integer> list) {
          for(Integer i : list) {
              if(condition) {
                  return null;
              }
          }
          return list;
      }
  }
    ]]></code>
  </test-code>

  <test-code>
    <description>[java] ReturnEmptyCollectionRatherThanNull - false positive with stream and lambda #3721</description>
    <expected-problems>1</expected-problems>
    <expected-linenumbers>39</expected-linenumbers>
    <code><![CDATA[
import java.util.*;
import java.util.stream.*;
import java.util.concurrent.*;
import java.nio.file.Path;

public class Foo {
    private Map<String, List<String>> joinFutures(Map<String, List<Future<String>>> map) {
        Map<String, List<String>> joined = new HashMap<>();

        for (String p : map.keySet()) {
            List<String> evaluatedResult = map.get(p).stream()
                                              .map(f -> {
                                                  try {
                                                      return f.get();
                                                  } catch (InterruptedException | ExecutionException e) {
                                                      e.printStackTrace();
                                                      return null; // <----- false positive here
                                                  }
                                              })
                                              .filter(Objects::nonNull)
                                              .sorted(Comparator.naturalOrder())
                                              .collect(Collectors.toList());

            if (!evaluatedResult.isEmpty()) {
                joined.put(p, evaluatedResult);
            }
        }

        // checking basic lambdas and anonymous classes as well
        Callable<Object> c = () -> { return null; }; // <----- false positive here
        Callable<Object> c2 = new Callable<Object>() {
            public Object call() {
                return null; // <----- false positive here
            }
        };

        Callable<List<String>> c3 = new Callable<List<String>>() {
            public List<String> call() {
                return null;
            }
        };

        return joined;
    }
}
    ]]></code>
  </test-code>
</test-data>
