Path: blob/master/test/jdk/javax/management/ObjectName/CompressedStorageTest.java
41149 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 804156526* @summary Tests the limits imposed on the domain name part of an27* ObjectName instance28* @author Jaroslav Bachorik29* @modules java.management/javax.management:open30* @run main CompressedStorageTest31*/3233import java.lang.reflect.Field;34import java.lang.reflect.InvocationTargetException;35import java.lang.reflect.Method;36import java.util.function.Consumer;37import javax.management.MalformedObjectNameException;38import javax.management.ObjectName;3940public class CompressedStorageTest {41private static Method setDomainLengthM;42private static Field compressedStorageFld;4344private static int DOMAIN_PATTERN;45private static int PROPLIST_PATTERN;46private static int PROPVAL_PATTERN;4748private static Method setDomainPattern;49private static Method setPropertyListPattern;50private static Method setPropertyValuePattern;515253static {54try {55Class<?> clz = ObjectName.class;56setDomainLengthM = clz.getDeclaredMethod("setDomainLength", int.class);57setDomainLengthM.setAccessible(true);5859compressedStorageFld = clz.getDeclaredField("_compressed_storage");60compressedStorageFld.setAccessible(true);6162setDomainPattern = clz.getDeclaredMethod("setDomainPattern", boolean.class);63setDomainPattern.setAccessible(true);64setPropertyListPattern = clz.getDeclaredMethod("setPropertyListPattern", boolean.class);65setPropertyListPattern.setAccessible(true);66setPropertyValuePattern = clz.getDeclaredMethod("setPropertyValuePattern", boolean.class);67setPropertyValuePattern.setAccessible(true);6869DOMAIN_PATTERN = getStaticIntFld("DOMAIN_PATTERN");70PROPLIST_PATTERN = getStaticIntFld("PROPLIST_PATTERN");71PROPVAL_PATTERN = getStaticIntFld("PROPVAL_PATTERN");7273} catch (Exception e) {74throw new Error(e);75}76}7778public static void main(String[] args) throws Exception {79testZeroLength();80testNegativeLength();81testMaxLength();8283testSetDomainPattern();84testSetPropertyListPattern();85testSetPropertyValuePattern();86}8788private static ObjectName getObjectName()89throws MalformedObjectNameException {90return new ObjectName("domain", "key", "value");91}9293/**94* Test for accepting 0 being passed as argument to95* {@linkplain ObjectName#setDomainLength(int)}.96*97*/98private static void testZeroLength() throws Exception {99setDomainNameLength(0);100}101102/**103* Test for rejecting negative value being passed as argument to104* {@linkplain ObjectName#setDomainLength(int)}.105*/106private static void testNegativeLength() throws Exception {107try {108setDomainNameLength(-1);109} catch (MalformedObjectNameException e) {110return;111}112fail("Allowing negative domain name length");113}114115/**116* Test for rejecting value exceeding the maximum allowed length117* being passed as argument to {@linkplain ObjectName#setDomainLength(int)}.118*/119private static void testMaxLength() throws Exception {120try {121setDomainNameLength(Integer.MAX_VALUE / 4 + 1);122} catch (MalformedObjectNameException e) {123return;124}125fail("Maximum domain name length is not respected");126}127128/**129* Tests that calling {@linkplain ObjectName#setDomainPattern(boolean)}130* results in setting correct bits in {@linkplain ObjectName#_compressed_storage}.131*/132private static void testSetDomainPattern() throws Exception {133ObjectName on = getObjectName();134135checkMask(DOMAIN_PATTERN, setDomainPattern, on);136}137138/**139* Tests that calling {@linkplain ObjectName#setPropertyListPattern(boolean)}140* results in setting correct bits in {@linkplain ObjectName#_compressed_storage}.141*/142private static void testSetPropertyListPattern() throws Exception {143ObjectName on = getObjectName();144145checkMask(PROPLIST_PATTERN, setPropertyListPattern, on);146}147148/**149* Tests that calling {@linkplain ObjectName#setPropertyValuePattern(boolean)}150* results in setting correct bits in {@linkplain ObjectName#_compressed_storage}.151*/152private static void testSetPropertyValuePattern() throws Exception {153ObjectName on = getObjectName();154155checkMask(PROPVAL_PATTERN, setPropertyValuePattern, on);156}157158/**159* Helper method to call {@linkplain ObjectName#setDomainLength(int)}160* method via reflection.161* @param len The domain name length162* @throws MalformedObjectNameException Propagated from163* {@linkplain ObjectName#setDomainLength(int)} invocation.164*/165private static void setDomainNameLength(int len)166throws MalformedObjectNameException {167try {168setDomainLengthM.invoke(getObjectName(), len);169} catch (InvocationTargetException e) {170Throwable cause = e.getCause();171if (cause instanceof MalformedObjectNameException) {172throw (MalformedObjectNameException)cause;173}174throw new Error(cause);175} catch (IllegalAccessException | IllegalArgumentException e) {176throw new Error(e);177}178}179180/**181* Helper method to assert that a particular boolean setter affects only182* a particular bit in the {@linkplain ObjectName#_compressed_storage} field.183* @param mask bitmask for storing the boolean value184* @param setter setter method reference185* @param on {@linkplain ObjectName} instance186*/187private static void checkMask(int mask, Method setter, ObjectName on)188throws Exception {189int valBefore = compressedStorageFld.getInt(on);190setter.invoke(on, true);191int valAfter = compressedStorageFld.getInt(on);192193checkMask(mask, valAfter ^ valBefore);194195valBefore = valAfter;196setter.invoke(on, false);197valAfter = compressedStorageFld.getInt(on);198199checkMask(mask, valAfter ^ valBefore);200}201202/**203* Compare the changed bits with the given mask.204* @param mask bitmask205* @param val the changed bits; may be 0 if there was no change206*/207private static void checkMask(int mask, int val) {208if (val != 0 && val != mask) {209fail("Invalid mask: expecting '" +210Integer.toBinaryString(mask) + "' , received '" +211Integer.toBinaryString(val) + "'");212}213}214215/**216* Helper method to obtain the value of a static field via reflection.217* @param name static field name218* @return static field value219*/220private static int getStaticIntFld(String name) throws Exception {221Field fld = ObjectName.class.getDeclaredField(name);222fld.setAccessible(true);223224return fld.getInt(null);225}226227private static void fail(String msg) {228throw new Error(msg);229}230}231232233