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

LOGGING APIs: an unsupported encoding is being announced as used

XMLWordPrintable

    • beta
    • sparc
    • solaris_7
    • Verified



      Name: szR10032 Date: 08/28/2000



      The testbase_nsk/src/nsk/logging/Handler/getEncoding/gtencd002
      test detects that Handler.setEncoding(...) method allows to set and announce an unsupported
      encoding as "used by this Handler" encoding after UnsupportedEncodingException rising.
      in that time when previouse encoding is actually used for publishing log records in the cases
      when UnsupportedEncodingException is rised.

      It seems that unsupported encoding should not be set as actual in such situation
      and setEncoding() source should be changed as suggested:

      FROM:

          public void setEncoding(String encoding)
      throws SecurityException, java.io.UnsupportedEncodingException {
      manager.checkAccess();
      this.encoding = encoding;
      if (encoding != null) {
      // Check the encoding exists.
      sun.io.CharToByteConverter.getConverter(encoding);
      }
          }

      TO:

          public void setEncoding(String encoding)
      throws SecurityException, java.io.UnsupportedEncodingException {
      manager.checkAccess();
      if (encoding != null) {
      // Check the encoding exists.
      sun.io.CharToByteConverter.getConverter(encoding);
      }
      this.encoding = encoding;
          }


      See log and source below.

      LOG:
      ====
      novo7% setenv CLASSPATH .:/export/ld24/java/hotspot/Solaris_JDK_1.3/lib/tools.jar:/export/home/_FOR_REMOVING/logging/classes
      novo7% /export/ld24/java/hotspot/Solaris_JDK_1.3/bin/javac -d . gtencd002.java
      novo7% /export/ld24/java/hotspot/Solaris_JDK_1.3/bin/java -Xbootclasspath/a:/export/home/_FOR_REMOVING/logging/classes -Xbootclasspath/a:. logging.Handler.getEncoding.gtencd002
      Encoding is: UTF-8
      java.io.UnsupportedEncodingException: UNEXISTING-ENCODING
      Encoding is: UNEXISTING-ENCODING
      ERROR: UTF-8-encoding is continuing to be used but unexisting UNEXISTING-ENCODING-encoding is being announced as actual for used handler.


      SOURCE:
      =======
      package logging.Handler.getEncoding;

      import java.util.logging.LogManager;
      import java.util.logging.StreamHandler;
      import java.util.logging.Logger;
      import java.util.logging.Level;
      import java.util.logging.Formatter;
      import java.util.logging.LogRecord;

      import java.io.PrintStream;
      import java.io.FileOutputStream;
      import java.io.FileInputStream;
      import java.io.OutputStream;



      /**
       * Test for the control of
       *
       * Interface: java.util.logging.Handler
       * Method: public java.lang.String getEncoding()
       * Assertions: "Return the character encoding for this Handler."
       *
       *
       * Description: The test creates StreamHandler with UTF-8 encoding,
       * loges 3 chars string (one of which has 2-bytes representation in UTF-8),
       * changes (incorrectly but specially) encoding on unexisting "UNEXISTING-ENCODING",
       * after UnsupportedEncodingException arising the test loges same string again
       * and checks that output includes 8 bytes (UTF-8) representation for 6 chars
       * and actual encoing is UTF-8.
       *
       *
       * Comments: If security manager exists and
       * access to filename file is forbidden and/or
       * lAPIs methods callers do not have LoggingPermission("control")
       * then test is passed but doesn't check demanded assertions.
       *
       * Warning:
       *
       *
       * @author Serguei S. Zapreyev
       */

      public class gtencd002 {
          static LogManager manager;
          static StreamHandler sh;
          static Formatter f;
          static Logger lggr;

          public static void main( String argv[] ) {
              System.exit(run(argv, System.out)+95); // JCK-compatible exit status
          }
          
          public static int run(String argv[],PrintStream out) {
              gtencd002 test = new gtencd002();

              try {
                  FileOutputStream fos = new FileOutputStream("gtencd002F");
                  f = new gtencd002f();
                  sh = new StreamHandler((OutputStream) fos, f);
                  sh.setLevel(Level.INFO);

                  manager = LogManager.getLogManager();
                  manager.removeAllGlobalHandlers();

                  lggr = Logger.getLogger("logging.Handler.getEncoding.gtencd002.one");
                  lggr.setLevel(Level.INFO);
                  lggr.addHandler(sh);

                  try {
                      sh.setEncoding("UTF-8");
                      out.println("Encoding is: "+sh.getEncoding());
                      lggr.info("\u0023\u00ff\u0023");
                      sh.flush();
                      sh.setEncoding("UNEXISTING-ENCODING");
                  } catch (java.io.UnsupportedEncodingException ex) {
                      out.println(ex);
                      out.println("Encoding is: "+sh.getEncoding());
                      lggr.info("\u0023\u00ff\u0023");
                      sh.flush();
                      sh.close();
                  }
                  sh.close();
                  FileInputStream fis = new FileInputStream("gtencd002F");
                  int ia[] = {0,0,0,0,0,0,0,0,0,0};
                  int i = 1;
                  for (;;i++) {
                      ia[i] = fis.read();
      //debug// System.out.println("|"+ ia[i] +"|");
                      if (ia[i] == -1) break;
                  }
                  if (i == 4+4+1 && ia[6] == 195 && ia[7] == 191 && sh.getEncoding().compareTo("UTF-8") != 0) {
                      out.println("ERROR: UTF-8-encoding is continuing to be used but unexisting "+sh.getEncoding()+"-encoding is being announced as actual for used handler.");
                      return 2;
                  }
              } catch (SecurityException ex) {
                  out.println(ex);
                  return 0; //TEST PASSED
              } catch (Exception ex) {
                  out.println(ex);
                  return 2; //TEST FAILED
              }

              return 0;
          }

      }

      class gtencd002f extends Formatter {
          public java.lang.String format(LogRecord record) {
              return record.message;
          }
      }




      ======================================================================

      Name: elR10090 Date: 02/01/2001


      This bug also affects the following testbase_nsk test:
          nsk/logging/StreamHandler/setOutputStream/setoutstrm003


      ======================================================================

            ghamiltosunw Graham Hamilton (Inactive)
            zsssunw Zss Zss (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: