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

Logging APIs: StreamHandler constructor ignores encoding property

    XMLWordPrintable

Details

    • beta
    • generic
    • generic
    • Verified

    Description



      Name: elR10090 Date: 12/22/2000



      Logging APIs implementation fails the test
      nsk/logging/StreamHandler/StreamHandler/strmhnd003 from testbase_nsk.

      The test checks if StreamHandler created with the constructor
      StreamHandler() is configured correctly. That is handler level, filter,
      formatter and encoding are initialized using the specified default values
      when their properties are not defined and the expected values when their
      properties are defined.

      The test reveals that the constructor StreamHandler does not set
      character encoding to a value difined by the property
      java.util.logging.StreamHandler.encoding.

      By the way, the Logging APIs spec (draft 0.55) doesn't explicitly state
      the constructor to use LogManager properties (or their defaults) as it does
      for ConsoleHandler, FileHandler, MemoryHandler and SocketHandler constructors.
      I think the spec should document this by the same way.

      Log and the test source follow:

      $ java -version
      java version "1.4.0beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0beta-b45)
      Java HotSpot(TM) Client VM (build B45, mixed mode)

      $ java logging.StreamHandler.StreamHandler.strmhnd003
      # Wrong ".level" value "INFO", expected "ALL"
      # Wrong ".encoding" value "null", expected "US-ASCII"
      # TEST FAILED.

      ------------------------------------------------------ strmhnd003.java

      package logging.StreamHandler.StreamHandler;

      import java.util.logging.*;
      import java.io.*;

      public class strmhnd003 {

          final static int PASSED = 0;
          final static int FAILED = 2;
          final static int JCK_STATUS_BASE = 95;
          final static String failMessage = "# TEST FAILED.";

          final static String prefixString = "java.util.logging.StreamHandler";
          final static int PREFIX_LEN = prefixString.length();
          final static String properties[][] = {
              { prefixString + ".level", "ALL", "123456" },
              { prefixString + ".filter", "null", TestFilter.class.getName() },
              { prefixString + ".formatter", SimpleFormatter.class.getName(),
                TestFormatter.class.getName() },
              { prefixString + ".encoding", "null", "US-ASCII" }
          };

          private static boolean testFailed = false;
          private static boolean verbose = false;

          public static int run(String args[], PrintStream out) {
              verbose = (args.length > 0) && args[0].equals("-v");

              String config = "";
              LogManager manager = LogManager.getLogManager();
              StreamHandler handler = null;

              // check on default values when properties are not defined
              try {
                  InputStream in = new ByteArrayInputStream(config.getBytes());
                  manager.readConfiguration(in);
                  handler = new StreamHandler();
              } catch (Exception ex) {
                  out.println("# Unexpected exception while reading configuration:");
                  out.print("# ");
                  ex.printStackTrace(out);
                  testFailed = true;
              }

              if (handler != null) {
                  for (int i = 0; i < properties.length; i++) {
                      String propertyName = properties[i][0].substring(PREFIX_LEN);
                      String knownValue = properties[i][1];
                      String value = null;
                      switch (i) {
                      case 0:
                          value = "" + handler.getLevel();
                          break;
                      case 1:
                          Filter filter = handler.getFilter();
                          value = (filter == null)
                                  ? "null"
                                  : filter.getClass().getName();
                          break;
                      case 2:
                          Formatter formatter = handler.getFormatter();
                          value = (formatter == null)
                                  ? "null"
                                  : formatter.getClass().getName();
                          break;
                      case 3:
                          value = "" + handler.getEncoding();
                          break;
                      default:
                          // it never should be here
                          break;
                      }
                      if (!value.equals(knownValue)) {
                          out.println("# Wrong \"" + propertyName
                              + "\" value \"" + value
                              + "\", expected \"" + knownValue + "\"");
                          testFailed = true;
                      }
                      if (verbose) {
                          out.println(propertyName + " = " + value);
                      }
                  }
              }

              // check on expected values when properties are defined
              for (int i = 0; i < properties.length; i++) {
                  config = config + properties[i][0]
                      + " = " + properties[i][2] + "\n";
              }
              handler = null;
              try {
                  InputStream in = new ByteArrayInputStream(config.getBytes());
                  manager.readConfiguration(in);
                  handler = new StreamHandler();
              } catch (Exception ex) {
                  out.println("# Unexpected exception while reading configuration:");
                  out.print("# ");
                  ex.printStackTrace(out);
                  testFailed = true;
              }

              if (handler != null) {
                  for (int i = 0; i < properties.length; i++) {
                      String propertyName = properties[i][0].substring(PREFIX_LEN);
                      String knownValue = properties[i][2];
                      String value = null;
                      switch (i) {
                      case 0:
                          value = "" + handler.getLevel();
                          break;
                      case 1:
                          Filter filter = handler.getFilter();
                          value = (filter == null)
                                  ? "null"
                                  : filter.getClass().getName();
                          break;
                      case 2:
                          Formatter formatter = handler.getFormatter();
                          value = (formatter == null)
                                  ? "null"
                                  : formatter.getClass().getName();
                          break;
                      case 3:
                          value = "" + handler.getEncoding();
                          break;
                      default:
                          // it never should be here
                          break;
                      }
                      if (!value.equals(knownValue)) {
                          out.println("# Wrong \"" + propertyName
                              + "\" value \"" + value
                              + "\", expected \"" + knownValue + "\"");
                          testFailed = true;
                      }
                      if (verbose) {
                          out.println(propertyName + " = " + value);
                      }
                  }
              }

              if (testFailed) {
                  out.println(failMessage);
                  return FAILED;
              } else {
                  return PASSED;
              }
          }

          public static class TestFilter implements Filter {
              public boolean isLoggable(LogRecord record) {
                  return true;
              }
              public String toString() {
                  return "TestFilter";
              }
          }

          public static class TestFormatter extends Formatter {
              public String format (LogRecord record) {
                  return null;
              }
              public String toString() {
                  return "TestFormatter";
              }
          }

          public static void main(String args[]) {
              // produce JCK-like exit status.
              System.exit(run(args, System.out) + JCK_STATUS_BASE);
          }
      }
       
      ======================================================================

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: