test/hotspot/jtreg/compiler/jvmci/jdk.vm.ci.hotspot.test/src/jdk/vm/ci/hotspot/test/TestServices.java
changeset 54732 2d012a75d35c
child 58974 2b0f2fe82735
equal deleted inserted replaced
54731:81de17a33575 54732:2d012a75d35c
       
     1 /*
       
     2  * Copyright (c) 2019, 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 package jdk.vm.ci.hotspot.test;
       
    24 
       
    25 import java.lang.reflect.Field;
       
    26 import java.lang.reflect.Method;
       
    27 import java.util.HashMap;
       
    28 import java.util.Map;
       
    29 
       
    30 import org.junit.Assert;
       
    31 import org.junit.Test;
       
    32 
       
    33 import jdk.vm.ci.services.Services;
       
    34 
       
    35 public class TestServices {
       
    36 
       
    37     @SuppressWarnings("unchecked")
       
    38     @Test
       
    39     public void serializeSavedPropertiesTest() throws Exception {
       
    40 
       
    41         Field f = Services.class.getDeclaredField("MAX_UTF8_PROPERTY_STRING_LENGTH");
       
    42         f.setAccessible(true);
       
    43         int maxUtf8PropertyStringLength = (int) f.get(null);
       
    44 
       
    45         Method serializeProperties = Services.class.getDeclaredMethod("serializeProperties", Map.class);
       
    46         Method deserializeProperties = Services.class.getDeclaredMethod("deserializeProperties", byte[].class);
       
    47         serializeProperties.setAccessible(true);
       
    48         deserializeProperties.setAccessible(true);
       
    49 
       
    50         Map<String, String> props = new HashMap<>(Services.getSavedProperties());
       
    51         String[] names = {
       
    52                         new String(new char[maxUtf8PropertyStringLength - 100]).replace('\0', 'x'),
       
    53                         new String(new char[maxUtf8PropertyStringLength - 1]).replace('\0', 'x'),
       
    54                         new String(new char[maxUtf8PropertyStringLength]).replace('\0', 'y'),
       
    55                         new String(new char[maxUtf8PropertyStringLength + 1]).replace('\0', 'z'),
       
    56                         new String(new char[maxUtf8PropertyStringLength + 100]).replace('\0', 'z')
       
    57         };
       
    58         String[] values = {
       
    59                         new String(new char[maxUtf8PropertyStringLength - 100]).replace('\0', '1'),
       
    60                         new String(new char[maxUtf8PropertyStringLength - 1]).replace('\0', '1'),
       
    61                         new String(new char[maxUtf8PropertyStringLength]).replace('\0', '2'),
       
    62                         new String(new char[maxUtf8PropertyStringLength + 1]).replace('\0', '1'),
       
    63                         new String(new char[maxUtf8PropertyStringLength + 100]).replace('\0', '3')
       
    64         };
       
    65         for (String name : names) {
       
    66             for (String value : values) {
       
    67                 props.put(name, value);
       
    68             }
       
    69         }
       
    70 
       
    71         byte[] data = (byte[]) serializeProperties.invoke(null, props);
       
    72 
       
    73         Map<String, String> newProps = (Map<String, String>) deserializeProperties.invoke(null, data);
       
    74 
       
    75         Assert.assertEquals(props.size(), newProps.size());
       
    76         for (String name : props.keySet()) {
       
    77             String expect = props.get(name);
       
    78             String actual = newProps.get(name);
       
    79             Assert.assertEquals(expect, actual);
       
    80         }
       
    81     }
       
    82 }