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.HashSet;
  27 import java.util.Objects;
  28 import java.util.Set;
  29 /**
  30  * @test
  31  * @bug 8046565
  32  * @summary Tests System.Logger.Level names and severity.
  33  * @author danielfuchs
  34  */
  35 public class LoggerLevelTest {
  36     public static void main(String[] args) {
  37         Set<Level> untested = EnumSet.allOf(Level.class);
  38         testLevel(untested, Level.ALL, java.util.logging.Level.ALL);
  39         testLevel(untested, Level.TRACE, java.util.logging.Level.FINER);
  40         testLevel(untested, Level.DEBUG, java.util.logging.Level.FINE);
  41         testLevel(untested, Level.INFO, java.util.logging.Level.INFO);
  42         testLevel(untested, Level.WARNING, java.util.logging.Level.WARNING);
  43         testLevel(untested, Level.ERROR, java.util.logging.Level.SEVERE);
  44         testLevel(untested, Level.OFF, java.util.logging.Level.OFF);
  45         if (!untested.isEmpty()) {
  46             throw new RuntimeException("Some level values were not tested: " + untested);
  47         }
  48     }
  49 
  50     private static void testLevel(Set<Level> untested, Level systemLevel, java.util.logging.Level julLevel) {
  51         untested.remove(systemLevel);
  52         assertEquals(systemLevel.getName(), systemLevel.name(),
  53                 "System.Logger.Level." + systemLevel.name() + ".getName()");
  54         assertEquals(systemLevel.getSeverity(), julLevel.intValue(),
  55                 "System.Logger.Level." + systemLevel.name() + ".getSeverity");
  56     }
  57 
  58     private static void assertEquals(Object actual, Object expected, String what) {
  59         if (!Objects.equals(actual, expected)) {
  60             throw new RuntimeException("Bad value for " + what
  61                     + "\n\t expected: " + expected
  62                     + "\n\t   actual: " + actual);
  63         } else {
  64             System.out.println("Got expected value for " + what + ": " + actual);
  65         }
  66     }
  67 
  68     private static void assertEquals(int actual, int expected, String what) {
  69         if (!Objects.equals(actual, expected)) {
  70             throw new RuntimeException("Bad value for " + what
  71                     + "\n\t expected: " + toString(expected)
  72                     + "\n\t   actual: " + toString(actual));
  73         } else {
  74             System.out.println("Got expected value for " + what + ": " + toString(actual));
  75         }
  76     }
  77 
  78     private static String toString(int value) {
  79         switch (value) {
  80             case Integer.MAX_VALUE: return "Integer.MAX_VALUE";
  81             case Integer.MIN_VALUE: return "Integer.MIN_VALUE";
  82             default:
  83                 return Integer.toString(value);
  84         }
  85     }
  86 
  87 }