@Test public void testByteArrayObject() throws Exception { ModelNode description = createDescription(ModelType.OBJECT, ModelType.BYTES); TypeConverter converter = getConverter(description); assertMapType( assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, ArrayType.getPrimitiveArrayType(byte[].class)); ModelNode node = new ModelNode(); node.get("one").set(new byte[] {1, 2}); node.get("two").set(new byte[] {3, 4}); TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node)); Assert.assertEquals(2, tabularData.size()); Assert.assertTrue( Arrays.equals( new byte[] {1, 2}, (byte[]) tabularData.get(new Object[] {"one"}).get("value"))); Assert.assertTrue( Arrays.equals( new byte[] {3, 4}, (byte[]) tabularData.get(new Object[] {"two"}).get("value"))); // Allow plain map as well? Yeah why not! Map<String, byte[]> map = new HashMap<String, byte[]>(); map.put("one", new byte[] {1, 2}); map.put("two", new byte[] {3, 4}); Assert.assertEquals(node, converter.toModelNode(map)); }
@Test public void testSimpleTypeObject() throws Exception { ModelNode description = createDescription(ModelType.OBJECT, ModelType.LONG); TypeConverter converter = getConverter(description); assertMapType( assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, SimpleType.LONG); ModelNode node = new ModelNode(); node.get("one").set(1L); node.get("two").set(2L); TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node)); Assert.assertEquals(2, tabularData.size()); Assert.assertEquals(Long.valueOf(1), tabularData.get(new Object[] {"one"}).get("value")); Assert.assertEquals(Long.valueOf(2), tabularData.get(new Object[] {"two"}).get("value")); Assert.assertEquals(node, converter.toModelNode(tabularData)); // Allow plain map as well? Yeah why not! Map<String, Long> map = new HashMap<String, Long>(); map.put("one", 1L); map.put("two", 2L); Assert.assertEquals(node, converter.toModelNode(map)); }
/** * Adds the attribute to the given list. * * @param mBeanAttributes The attributes * @param root The root attribute * @param value The value */ private void addAttributeRoots( List<AttributeNode> mBeanAttributes, AttributeNode root, Object value) { if (value instanceof CompositeData) { mBeanAttributes.add(root); CompositeData compositeData = (CompositeData) value; CompositeType type = compositeData.getCompositeType(); for (String key : type.keySet()) { AttributeNode attribute = new AttributeNode(key, root); root.addChild(attribute); addAttributeItems(attribute, compositeData.get(key)); } } else if (value instanceof TabularData) { mBeanAttributes.add(root); TabularData tabularData = (TabularData) value; for (Object keyList : tabularData.keySet()) { @SuppressWarnings("unchecked") Object[] keys = ((List<Object>) keyList).toArray(new Object[0]); AttributeNode attribute = new AttributeNode(String.valueOf(keys[0]), root); root.addChild(attribute); addAttributeItems(attribute, tabularData.get(keys)); } } else if (value instanceof Long || value instanceof Integer || value instanceof Double) { root.setValidLeaf(true); root.setRgb(getRGB(root.getQualifiedName())); mBeanAttributes.add(root); } }
private static String getProperty(TabularData td, String propName) { CompositeData cd = td.get(new Object[] {propName}); if (cd != null) { String key = (String) cd.get("key"); if (!propName.equals(key)) { throw new RuntimeException( "TEST FAILED: " + key + " property found" + " but expected to be " + propName); } return (String) cd.get("value"); } return null; }
/** * Reads in the numeric value of mapped to the tabularDataKey value specified when this object was * created * * @param jmxConnection * @param f * @param beanName * @throws Exception */ protected void readValueFromBean( MBeanServerConnection jmxConnection, AggregateFunction f, ObjectName beanName) throws Exception { checkBeanExists(jmxConnection, beanName); Object attribute = safelyGetAttribute(jmxConnection, beanName); if (!(attribute instanceof TabularData)) { raiseValueException("The attribute was not of type TabularData"); } TabularData tabularData = (TabularData) attribute; Object[] key = {tabularDataKey}; if (!tabularData.containsKey(key)) { raiseValueException("No value is available for the key Object[] {" + tabularDataKey + "}"); } CompositeData compositeData = null; try { compositeData = tabularData.get(key); } catch (InvalidKeyException k) { raiseValueException("The TabularDataKey " + tabularDataKey + " was invalid", k); } if (compositeData == null) { raiseValueException( "Even after checking the TabularDataKey " + tabularDataKey + " existed, we were still return a null value"); // key asynchronously removed on remote server?, probably possible, but never seen it } Object value = getValueFromCompositeData(compositeData, "value"); if (value == null) { raiseValueException( "The value attribute of the CompositeDataItem with tabular key " + tabularDataKey + " was null"); } Double v = convertToDouble(value); // yippee! after all this checking we actually have a value! f.addValue(v); }
@Test public void testSimpleTypeObjectExpressions() throws Exception { ModelNode description = createDescription(ModelType.OBJECT, ModelType.LONG); description.get(EXPRESSIONS_ALLOWED).set(true); TypeConverter converter = getConverter(description); assertMapType( assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, SimpleType.LONG); ModelNode node = new ModelNode(); node.get("one").setExpression("${this.should.not.exist.!!!!!:1}"); node.get("two").setExpression("${this.should.not.exist.!!!!!:2}"); TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node)); Assert.assertEquals(2, tabularData.size()); Assert.assertEquals(Long.valueOf(1), tabularData.get(new Object[] {"one"}).get("value")); Assert.assertEquals(Long.valueOf(2), tabularData.get(new Object[] {"two"}).get("value")); }
@Test public void testSimpleTypeObjectEmpty() throws Exception { ModelNode description = createDescription(ModelType.OBJECT, ModelType.LONG); TypeConverter converter = getConverter(description); assertMapType( assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, SimpleType.LONG); ModelNode node = new ModelNode(); node.get("one").set(""); node.get("two").set(""); TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node)); Assert.assertEquals(2, tabularData.size()); Assert.assertNull(tabularData.get(new Object[] {"one"}).get("value")); Assert.assertNull(tabularData.get(new Object[] {"two"}).get("value")); ModelNode expected = new ModelNode(); expected.get("one"); expected.get("two"); Assert.assertEquals(expected, converter.toModelNode(tabularData)); }
/** * Gets the initial file name. * * @param jvm The active JVM * @return The file name, or <tt>null</tt> if file name is specified * @throws JvmCoreException */ String getInitialFileName(IActiveJvm jvm) throws JvmCoreException { ObjectName objectName; try { objectName = new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME); } catch (MalformedObjectNameException e) { throw new JvmCoreException(IStatus.ERROR, e.getMessage(), e); } catch (NullPointerException e) { throw new JvmCoreException(IStatus.ERROR, e.getMessage(), e); } TabularData initialName = (TabularData) jvm.getMBeanServer().getAttribute(objectName, "SystemProperties"); // $NON-NLS-1$ CompositeData compisiteData = initialName.get(new Object[] {"user.home"}); // $NON-NLS-1$ String home = compisiteData.values().toArray(new String[0])[1]; StringBuffer initialFileName = new StringBuffer(home); initialFileName .append(File.separator) .append(new Date().getTime()) .append('.') .append(SnapshotType.Hprof.getExtension()); return initialFileName.toString(); }