1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.lang.System.Logger.Level;
  25 import java.util.EnumSet;
  26 import java.util.Objects;
  27 import java.util.Set;
  28 /**
  29  * @test
  30  * @bug 8140364
  31  * @summary Tests System.Logger.Level names and severity.
  32  * @author danielfuchs
  33  */
  34 public class LoggerLevelTest {
  35     public static void main(String[] args) {
  36         Set<Level> untested = EnumSet.allOf(Level.class);
  37         testLevel(untested, Level.ALL, java.util.logging.Level.ALL);
  38         testLevel(untested, Level.TRACE, java.util.logging.Level.FINER);
  39         testLevel(untested, Level.DEBUG, java.util.logging.Level.FINE);
  40         testLevel(untested, Level.INFO, java.util.logging.Level.INFO);
  41         testLevel(untested, Level.WARNING, java.util.logging.Level.WARNING);
  42         testLevel(untested, Level.ERROR, java.util.logging.Level.SEVERE);
  43         testLevel(untested, Level.OFF, java.util.logging.Level.OFF);
  44         if (!untested.isEmpty()) {
  45             throw new RuntimeException("Some level values were not tested: " + untested);
  46         }
  47     }
  48 
  49     private static void testLevel(Set<Level> untested, Level systemLevel, java.util.logging.Level julLevel) {
  50         untested.remove(systemLevel);
  51         assertEquals(systemLevel.getName(), systemLevel.name(),
  52                 "System.Logger.Level." + systemLevel.name() + ".getName()");
  53         assertEquals(systemLevel.getSeverity(), julLevel.intValue(),
  54                 "System.Logger.Level." + systemLevel.name() + ".getSeverity");
  55     }
  56 
  57     private static void assertEquals(Object actual, Object expected, String what) {
  58         if (!Objects.equals(actual, expected)) {
  59             throw new RuntimeException("Bad value for " + what
  60                     + "\n\t expected: " + expected
  61                     + "\n\t   actual: " + actual);
  62         } else {
  63             System.out.println("Got expected value for " + what + ": " + actual);
  64         }
  65     }
  66 
  67     private static void assertEquals(int actual, int expected, String what) {
  68         if (!Objects.equals(actual, expected)) {
  69             throw new RuntimeException("Bad value for " + what
  70                     + "\n\t expected: " + toString(expected)
  71                     + "\n\t   actual: " + toString(actual));
  72         } else {
  73             System.out.println("Got expected value for " + what + ": " + toString(actual));
  74         }
  75     }
  76 
  77     private static String toString(int value) {
  78         switch (value) {
  79             case Integer.MAX_VALUE: return "Integer.MAX_VALUE";
  80             case Integer.MIN_VALUE: return "Integer.MIN_VALUE";
  81             default:
  82                 return Integer.toString(value);
  83         }
  84     }
  85 
  86 }