/* ****************************************
 * $Id$
 * SF bug Bug2177967:
 *   Missed open stream warning
 *
 * JVM:  1.5.0_16 (OS X, PPC)
 * FBv:  1.3.7-dev-20081215
 *
 * Test case based on example code from bug report
 *
 * Test class has inner class an return open stream, which is never closed.
 * An open stream warning seems like it would be appropriate in this case,
 * since the test method leaves the returned FileInputStream open when
 * it returns.
 *
 * **************************************** */

package sfBugs;

import java.io.FileInputStream;
import java.io.IOException;

import edu.umd.cs.findbugs.annotations.ExpectWarning;

public class Bug2177967 {

    static class TestReader {
        String fileName;

        public TestReader(String newFileName) {
            fileName = newFileName;
        }

        /* creates and immediately returns open resource */
        public FileInputStream getStream() {
            try {
                return new FileInputStream(fileName);
            } catch (IOException ex) {
                System.out.println("Got IO Execption");
            }
            return null;
        }
    }

    /* ********************
     * Behavior at filing:
     *   no warning thrown
     *
     * Expected:
     *  Open stream error for 'in', which is never closed
     *    (might alternatively/additionally be an OBL warning)
     * ******************** */
    @ExpectWarning("OS")
    public void testMethod(String fileName) {
        TestReader reader = new TestReader(fileName);
        byte[] data = new byte[100];

        // test code adapted from bug report

        // accepts open file stream from inner class
        FileInputStream in = reader.getStream();
        if(in == null) {
            System.out.println("input stream is null!");
        } else {
            try{
                // do something with the stream we just received
                int read_bytes = in.read(data);
                if(read_bytes == -1) {
                    System.out.println("No bytes read");
                }
            } catch (IOException e) {
                System.out.println("Got IO Exception");
            }
        }
        // 'in' is not closed before return
        return;
    }
}

@@ -581,16 +581,73 @@
 ception;
+%0Aimport java.io.InputStream;%0Aimport java.util.Properties;
 %0A%0Aimport
@@ -726,1066 +726,515 @@
  %7B%0A%0A
-    static class TestReader %7B%0A        String fileName;%0A%0A        public TestRe
+%09Properties properties = new Properties();%0A%0A%09public void init(Class lo
 ader
-(
+, 
 String 
-newFileName) %7B%0A            fileName = newFileName;%0A        %7D%0A%0A        /* creates and immediately returns open resource */%0A        public FileInputStream getStream() %7B%0A            try %7B%0A                return new FileInputStream(fileName);%0A            %7D catch (IOException ex) %7B%0A                System.out.println(%22Got IO Execption%22);%0A            %7D%0A            return null;%0A        %7D%0A    %7D%0A%0A    /* ********************%0A     * Behavior at filing:%0A     *   no warning thrown%0A     *%0A     * Expected:%0A     *  Open stream error for 'in', which is never closed%0A     *    (might alternatively/additionally be an OBL warning)%0A     * ******************** */%0A    @ExpectWarning(%22OS%22)%0A    public void testMethod(String fileName) %7B%0A        TestReader reader = new TestReader(fileName);%0A        byte%5B%5D data = new byte%5B100%5D;%0A%0A        // test code adapted from bug report%0A%0A        // accepts open file stream from inner class%0A        FileInputStream in = reader.getStream();%0A        
+propertiesFile) throws IOException %7B%0A%0A%09%09// Load the properties from the file%0A%09%09InputStream in = loader.getResourceAsStream(propertiesFile);%0A%09%09if (in == null) %7B%0A%09%09%09throw new RuntimeException(%22Cound not locate %22 + propertiesFile);%0A%09%09%7D%0A%09%09properties.load(in);%0A%0A%09%7D%0A%0A%09public void init(String propertiesFile) throws IOException %7B%0A%0A%09%09// Load the properties from the file%0A%09%09InputStream in = new FileInputStream(propertiesFile);%0A%09%09
 if
+ 
 (in 
@@ -1248,508 +1248,107 @@
 ) %7B%0A
-            System.out.println(%22input stream is null!%22);%0A        %7D else %7B%0A            try%7B%0A                // do something with the stream we just received%0A                int read_bytes = in.read(data);%0A                if(read_bytes == -1) %7B%0A                    System.out.println(%22No bytes read%22);%0A                %7D%0A            %7D catch (IOException e) %7B%0A                System.out.println(%22Got IO Exception%22);%0A            %7D%0A        %7D%0A        // 'in' is not closed before return%0A        return;%0A    
+%09%09%09throw new RuntimeException(%22Cound not locate %22 + propertiesFile);%0A%09%09%7D%0A%09%09properties.load(in);%0A%0A%09
 %7D%0A
+%09
 %7D%0A

Fix test case to reflect bug report