1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.rjmx.test.subscription;
  34 
  35 import static org.junit.Assert.assertEquals;
  36 import static org.junit.Assert.assertTrue;
  37 
  38 import java.util.HashSet;
  39 
  40 import javax.management.ObjectName;
  41 
  42 import org.junit.Test;
  43 
  44 import org.openjdk.jmc.rjmx.subscription.MRI;
  45 import org.openjdk.jmc.rjmx.subscription.MRI.Type;
  46 
  47 /**
  48  */
  49 public class AttributeDescriptorTest {
  50 
  51         @Test
  52         public void testHashCode() throws Exception {
  53                 MRI descriptor = createDescriptor1();
  54                 MRI anotherDescriptor = createDescriptor1();
  55                 assertTrue(descriptor.hashCode() == anotherDescriptor.hashCode());
  56 
  57                 HashSet<MRI> set = new HashSet<>();
  58                 set.add(descriptor);
  59                 set.add(anotherDescriptor);
  60                 assertTrue(set.size() == 1);
  61 
  62         }
  63 
  64         /*
  65          * Class under test for void ConsoleAttributeName(String)
  66          */
  67         @Test
  68         public void testConsoleAttributeNameString() throws Exception {
  69                 MRI descriptor = createDescriptor1();
  70                 MRI anotherDescriptor = null;
  71                 anotherDescriptor = MRI.createFromQualifiedName(descriptor.getQualifiedName());
  72                 assertTrue(descriptor.equals(anotherDescriptor));
  73         }
  74 
  75         /*
  76          * Class under test for void ConsoleAttributeName(String, String)
  77          */
  78         @Test
  79         public void testConsoleAttributeNameStringString() throws Exception {
  80                 MRI descriptor = createDescriptor1();
  81                 MRI anotherDescriptor = null;
  82                 anotherDescriptor = new MRI(Type.ATTRIBUTE, descriptor.getObjectName().getCanonicalName(),
  83                                 descriptor.getDataPath());
  84                 assertTrue(descriptor.equals(anotherDescriptor));
  85         }
  86 
  87         /*
  88          * Class under test for void ConsoleAttributeName(ObjectName, String)
  89          */
  90         @Test
  91         public void testConsoleAttributeNameObjectNameString() throws Exception {
  92                 MRI descriptor = createDescriptor1();
  93                 MRI anotherDescriptor = null;
  94                 anotherDescriptor = new MRI(Type.ATTRIBUTE, descriptor.getObjectName(), descriptor.getDataPath());
  95                 assertTrue(descriptor.equals(anotherDescriptor));
  96         }
  97 
  98         @Test
  99         public void testGetQualifiedName() throws Exception {
 100                 MRI descriptor = createDescriptor1();
 101                 assertEquals(descriptor.getQualifiedName().length(), (descriptor.getType().getTypeName().length() + 3
 102                                 + descriptor.getObjectName().getCanonicalName().length() + 1 + descriptor.getDataPath().length()));
 103                 // Interned.
 104                 assertTrue(descriptor.getQualifiedName() == createDescriptor1().getQualifiedName());
 105         }
 106 
 107         /*
 108          * Class under test for boolean equals(Object)
 109          */
 110         @Test
 111         public void testEqualsObject() throws Exception {
 112                 MRI descriptor = createDescriptor1();
 113                 MRI anotherDescriptor = createDescriptor1();
 114                 assertTrue(descriptor.equals(anotherDescriptor));
 115         }
 116 
 117         /*
 118          * Class under test for String toString()
 119          */
 120         @Test
 121         public void testToString() throws Exception {
 122                 // Should be interned.
 123                 assertTrue(createDescriptor1().toString() == createDescriptor1().toString());
 124         }
 125 
 126         @Test
 127         public void testCreateFromQualifiedName1() throws Exception {
 128                 MRI descriptor = MRI.createFromQualifiedName("attribute://java.lang:type=OperatingSystem/Arch"); //$NON-NLS-1$
 129                 assertEquals(createDescriptor1(), descriptor);
 130         }
 131 
 132         @Test
 133         public void testCreateFromQualifiedName2() throws Exception {
 134                 MRI descriptor = MRI.createFromQualifiedName("attribute://java.lang:type=OperatingSystem/Arch/Sub"); //$NON-NLS-1$
 135                 assertEquals(Type.ATTRIBUTE, descriptor.getType());
 136                 assertEquals("java.lang:type=OperatingSystem", descriptor.getObjectName().getCanonicalName()); //$NON-NLS-1$
 137                 assertEquals("Arch/Sub", descriptor.getDataPath()); //$NON-NLS-1$
 138         }
 139 
 140         @Test
 141         public void testMalformedQualifiedName1() throws Exception {
 142                 try {
 143                         MRI.createFromQualifiedName("smurf://java.lang:type=OperatingSystem/Arch"); //$NON-NLS-1$
 144                 } catch (IllegalArgumentException iae) {
 145                         return;
 146                 }
 147                 assertTrue("Should not be possible to create an attribute with the type smurf!", false); //$NON-NLS-1$
 148         }
 149 
 150         @Test
 151         public void testMalformedQualifiedName2() throws Exception {
 152                 try {
 153                         MRI.createFromQualifiedName("java.lang:type=OperatingSystem/Arch"); //$NON-NLS-1$
 154                 } catch (IllegalArgumentException iae) {
 155                         return;
 156                 }
 157                 assertTrue("Should not be possible to create an attribute name without specifying a type!", false); //$NON-NLS-1$
 158         }
 159 
 160         private MRI createDescriptor1() throws Exception {
 161                 return new MRI(Type.ATTRIBUTE, new ObjectName("java.lang:type=OperatingSystem"), "Arch"); //$NON-NLS-1$ //$NON-NLS-2$
 162         }
 163 
 164 }