Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4244815

JFrame setDefaultCloseOperation(int) doesn't handle invalid param value

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.2.0
    • client-libs
    • None
    • beta
    • sparc
    • solaris_2.6

      swing JFrame setDefaultCloseOperation(int operation) does not handle invalid
      param values properly. Values outside the valid range (0-2) are accepted. If
      you use getDefaultCloseOperation the same invalid value will be returned.
      If the param value is < 0 or > 2, then the default close operation should
      be set to the default value (1, HIDE_ON_CLOSE) or a IllegalArgumentException
      should be thrown.

      /*
       * @(#)SetDefaultCloseOperationTest.java
       * @author Mike Colburn 06/08/99
       * @version 1.0
       * @bug
       *
       * swing.JFrame.setDefaultCloseOperation tests
       */

      import java.io.*;
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.accessibility.*;

      public class SetDefaultCloseOperationTest {

        public static void main(String[] args) {

          boolean status[];
          int passcount = 0, failcount = 0;
          int numTests = 4;
          int i, j = 0;
          
          status = new boolean[numTests];

          for( i = 0; i < numTests; ++i ) {
      // Put all tests in try... catch block
             try {
      ++j;
      switch (j) {
      case 1:
      status[i] = Test01();
      break;
      case 2:
      status[i] = Test02();
      break;
      case 3:
      status[i] = Test03();
      break;
      case 4:
      status[i] = Test04();
      break;
      }

        } catch (Exception e) {
                   status[i] = false;
      System.out.println ("Exception in test "+j+": "+e.getMessage());
      e.printStackTrace();
              }
          } // end for

          // Get pass and fail totals
          for( i = 0; i < numTests; ++i ) {
             if (status[i] == true)
      passcount++;
      else
      failcount++;
          }
          
          System.out.println("Pass count: " + passcount); // debug
          System.out.println("Fail count: " + failcount); // debug

          // check if tests passed
          if ( failcount < 1 ) {
            System.out.println("Test for JFrame.setDefaultCloseOperation Passed");
            System.exit(0);
          } else {
            System.out.println("Test for JFrame.setDefaultCloseOperation Failed");
            System.exit(1);
          }
          
          
        } // end main
            
        /**
         * Test Test01: setDefaultCloseOperation param int operation
         * set to DO_NOTHING_ON_CLOSE
         */
        public static boolean Test01() {
          System.out.println("Test 1: DO_NOTHING_ON_CLOSE");
          boolean bReturn = false;
          String sText = "setDefaultCloseOperation test";
          String sParam;
          int iClose = WindowConstants.DO_NOTHING_ON_CLOSE;
                 
          try {
           JFrame jf = new JFrame(sText);
            jf.setDefaultCloseOperation(iClose);
           
            int dco = jf.getDefaultCloseOperation();
            System.out.println ("getDefaultCloseOperation return: " + dco);
           
           jf.setLocation(200,200);
           jf.pack();
           jf.setVisible(true);
              
              jf.addWindowListener(new WindowAdapter() {
                 public void windowClosing(WindowEvent e) {
               System.exit(0);
              }
              });

      if (dco == iClose) {
      bReturn = true;
           } else {
           bReturn = false;
           System.out.println("Expected "+iClose+", Returned "+dco);
           System.out.println("Test 1 Failed");
      }
      jf.dispose();
          } catch (Exception e) {
           bReturn = false;
              System.out.println("Unexpected Exception thrown " + e.getMessage());
             e.printStackTrace();
          }
          return bReturn;
        }
        
        /**
         * Test Test02: setDefaultCloseOperation param int operation
         * set to HIDE_ON_CLOSE
         */
        public static boolean Test02() {
          System.out.println("Test 2: HIDE_ON_CLOSE ");
          boolean bReturn = false;
          String sText = "setDefaultCloseOperation test";
          String sParam;
          int iClose = WindowConstants.HIDE_ON_CLOSE ;
                 
          try {
           JFrame jf = new JFrame(sText);
            jf.setDefaultCloseOperation(iClose);
           
            int dco = jf.getDefaultCloseOperation();
            System.out.println ("getDefaultCloseOperation return: " + dco);
           
           jf.setLocation(200,200);
           jf.pack();
           jf.setVisible(true);
              
              jf.addWindowListener(new WindowAdapter() {
                 public void windowClosing(WindowEvent e) {
               System.exit(0);
              }
              });

      if (dco == iClose) {
      bReturn = true;
           } else {
           bReturn = false;
           System.out.println("Expected "+iClose+", Returned "+dco);
           System.out.println("Test 2 Failed");
      }
      jf.dispose();
          } catch (Exception e) {
           bReturn = false;
              System.out.println("Unexpected Exception thrown " + e.getMessage());
             e.printStackTrace();
          }
          return bReturn;
        }
        
        /**
         * Test Test03: setDefaultCloseOperation param int operation
         * set to DISPOSE_ON_CLOSE
         */
        public static boolean Test03() {
          System.out.println("Test 3: DISPOSE_ON_CLOSE");
          boolean bReturn = false;
          String sText = "setDefaultCloseOperation test";
          String sParam;
          int iClose = WindowConstants.DISPOSE_ON_CLOSE;
                 
          try {
           JFrame jf = new JFrame(sText);
            jf.setDefaultCloseOperation(iClose);
           
            int dco = jf.getDefaultCloseOperation();
            System.out.println ("getDefaultCloseOperation return: " + dco);
           
           jf.setLocation(200,200);
           jf.pack();
           jf.setVisible(true);
              
              jf.addWindowListener(new WindowAdapter() {
                 public void windowClosing(WindowEvent e) {
               System.exit(0);
              }
              });

      if (dco == iClose) {
      bReturn = true;
           } else {
           bReturn = false;
           System.out.println("Expected "+iClose+", Returned "+dco);
           System.out.println("Test 3 Failed");
      }
      jf.dispose();
          } catch (Exception e) {
           bReturn = false;
              System.out.println("Unexpected Exception thrown " + e.getMessage());
             e.printStackTrace();
          }
          return bReturn;
        }
        
        /**
         * Test Test04: setDefaultCloseOperation param int operation
         * set to invalid value.
         */
        public static boolean Test04() {
          System.out.println("Test 4: param invalid");
          boolean bReturn = false;
          String sText = "setDefaultCloseOperation test";
          String sParam;
          int iClose = -1;
          int iExpect = 1; // default close operation HIDE_ON_CLOSE
                 
          try {
           JFrame jf = new JFrame(sText);
            jf.setDefaultCloseOperation(iClose);
           
            int dco = jf.getDefaultCloseOperation();
            System.out.println ("getDefaultCloseOperation return: " + dco);
           
           jf.setLocation(200,200);
           jf.pack();
           jf.setVisible(true);
              
              jf.addWindowListener(new WindowAdapter() {
                 public void windowClosing(WindowEvent e) {
               System.exit(0);
              }
              });

      if (dco == iExpect) {
      bReturn = true;
           } else {
           bReturn = false;
           System.out.println("Expected "+iExpect+", Returned "+dco);
           System.out.println("Test 4 Failed");
      }
      jf.dispose();
          } catch (Exception e) {
           bReturn = false;
              System.out.println("Unexpected Exception thrown " + e.getMessage());
             e.printStackTrace();
          }
          return bReturn;
        }
        
      }

            amfowler Anne Fowler (Inactive)
            mcolburnsunw Michael Colburn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: