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 package jdk.test.failurehandler.value;
  25 
  26 import org.junit.Assert;
  27 import org.junit.Test;
  28 
  29 import java.lang.reflect.Field;
  30 import java.util.Properties;
  31 
  32 public class ValueHandlerTest {
  33     @Test
  34     public void testApplyAnonymousPrivateFinalInt() throws Exception {
  35         Properties p = new Properties();
  36         p.put("int", "010");
  37         Object o = new Object() {
  38             @Value (name = "int")
  39             private final int i1 = -1;
  40         };
  41         Field f = o.getClass().getDeclaredField("i1");
  42         f.setAccessible(true);
  43         int value = f.getInt(o);
  44         Assert.assertEquals(value, -1);
  45         f.setAccessible(false);
  46         ValueHandler.apply(o, p, null);
  47         f.setAccessible(true);
  48         value = f.getInt(o);
  49         Assert.assertEquals(value, 8);
  50         f.setAccessible(false);
  51     }
  52 
  53     @Test
  54     public void testApplyPublicStaticWithDefault() throws Exception {
  55         Assert.assertEquals(StaticDefaultCase.s, null);
  56         Properties p = new Properties();
  57         StaticDefaultCase o = new StaticDefaultCase();
  58         ValueHandler.apply(o, p, "prefix");
  59         Assert.assertEquals(StaticDefaultCase.s, "default");
  60         p.put("s", "new2");
  61         ValueHandler.apply(o, p, "prefix");
  62         Assert.assertEquals(StaticDefaultCase.s, "new2");
  63         p.put("prefix.s", "new");
  64         ValueHandler.apply(o, p, "prefix");
  65         Assert.assertEquals(StaticDefaultCase.s, "new");
  66         ValueHandler.apply(o, p, null);
  67         Assert.assertEquals(StaticDefaultCase.s, "new2");
  68     }
  69 
  70     protected class InnerClass1 {
  71         @Value (name = "innerClass")
  72         String[] arr = null;
  73     }
  74 
  75     public class InnerClass2 extends InnerClass1 {
  76         @Value (name = "float")
  77         float f = 0.0f;
  78 
  79         @SubValues (prefix = "inner")
  80         InnerClass1 inner1 = new InnerClass1();
  81 
  82         @SubValues (prefix = "")
  83         InnerClass1 inner2 = new InnerClass1();
  84     }
  85 
  86     @Test
  87     public void testApplySub() throws Exception {
  88         InnerClass2 o = new InnerClass2();
  89         Assert.assertArrayEquals(o.arr, null);
  90         Assert.assertArrayEquals(o.inner1.arr, null);
  91         Assert.assertArrayEquals(o.inner2.arr, null);
  92         Assert.assertEquals(o.f, 0.0f, Float.MIN_VALUE);
  93 
  94         Properties p = new Properties();
  95         p.put("float", "1.f");
  96         p.put("innerClass", "a b");
  97         p.put("inner.innerClass", "a b c");
  98         ValueHandler.apply(o, p, "");
  99         Assert.assertArrayEquals(o.arr, new String[]{"a", "b"});
 100         Assert.assertArrayEquals(o.inner1.arr, new String[]{"a", "b", "c"});
 101         Assert.assertArrayEquals(o.inner2.arr, new String[]{"a", "b"});
 102         Assert.assertEquals(o.f, 1.0f, Float.MIN_VALUE);
 103     }
 104 }
 105 
 106 class StaticDefaultCase {
 107     @Value (name = "s")
 108     @DefaultValue (value = "default")
 109     public static String s;
 110 }