Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Support BigInteger as return type for method Invocation #334
- Loading branch information
Showing
6 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
agent/core/src/main/java/org/jolokia/converter/json/simplifier/BigIntegerSimplifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.jolokia.converter.json.simplifier; | ||
|
||
import java.math.BigInteger; | ||
|
||
/* | ||
* Copyright 2009-2018 Roland Huss | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
/** | ||
* Simplifier for BigIntegers which result in a map with a single key <code>bigint</code> | ||
* | ||
* @author Neven Radovanović | ||
* @since June 27, 2018 | ||
*/ | ||
public class BigIntegerSimplifier extends SimplifierExtractor<BigInteger> { | ||
|
||
/** | ||
* No arg constructor as required for simplifiers | ||
*/ | ||
public BigIntegerSimplifier() { | ||
super(BigInteger.class); | ||
addExtractor("bigint", new BigIntegerAttributeExtractor()); | ||
} | ||
|
||
private static class BigIntegerAttributeExtractor implements AttributeExtractor<BigInteger> { | ||
/** {@inheritDoc} */ | ||
public Object extract(BigInteger pBigInt) { return pBigInt.toString(); } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
agent/core/src/test/java/org/jolokia/converter/json/SimplifiersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.jolokia.converter.json; | ||
|
||
import mockit.Mock; | ||
import mockit.MockUp; | ||
import org.jolokia.converter.json.simplifier.BigIntegerSimplifier; | ||
import org.jolokia.converter.json.simplifier.UrlSimplifier; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import javax.management.AttributeNotFoundException; | ||
import java.math.BigInteger; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Stack; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
/** | ||
* @author Neven Radovanović | ||
* @since June 27, 2018 | ||
*/ | ||
@Test() | ||
public class SimplifiersTest { | ||
|
||
private BigIntegerSimplifier bigIntegerSimplifier; | ||
private UrlSimplifier urlSimplifier; | ||
private ObjectToJsonConverter converter; | ||
|
||
@BeforeMethod | ||
public void setup() { | ||
bigIntegerSimplifier = new BigIntegerSimplifier(); | ||
urlSimplifier = new UrlSimplifier(); | ||
|
||
new MockUp<ObjectToJsonConverter>() { | ||
@Mock | ||
public ValueFaultHandler getValueFaultHandler() { | ||
return new PathAttributeFilterValueFaultHandler(ValueFaultHandler.THROWING_VALUE_FAULT_HANDLER); | ||
} | ||
}; | ||
// Needed for subclassing final object | ||
converter = new ObjectToJsonConverter(null, null); | ||
converter.setupContext(); | ||
} | ||
|
||
@Test | ||
public void bigIntegerSimplifier() throws AttributeNotFoundException { | ||
BigInteger bigInt = new BigInteger("12345678901234567890"); | ||
Object result = bigIntegerSimplifier.extractObject(converter, bigInt, new Stack<String>(), false); | ||
assertEquals(result, bigInt); | ||
} | ||
|
||
@Test | ||
public void bigIntegerSimplifierJson() throws AttributeNotFoundException { | ||
BigInteger bigInt = new BigInteger("12345678901234567890"); | ||
Object result = bigIntegerSimplifier.extractObject(converter, bigInt, new Stack<String>(), true); | ||
assertEquals(result.toString(), "{\"bigint\":\"12345678901234567890\"}"); | ||
} | ||
|
||
@Test | ||
public void urlSimplifier() throws AttributeNotFoundException, MalformedURLException { | ||
URL url = new URL("https://www.jolokia.org"); | ||
Object result = urlSimplifier.extractObject(converter, url, new Stack<String>(), false); | ||
assertEquals(result, url); | ||
} | ||
|
||
@Test | ||
public void urlSimplifierJson() throws AttributeNotFoundException, MalformedURLException { | ||
URL url = new URL("https://www.jolokia.org"); | ||
Object result = urlSimplifier.extractObject(converter, url, new Stack<String>(), true); | ||
assertEquals(result.toString(), "{\"url\":\"https:\\/\\/www.jolokia.org\"}"); | ||
} | ||
|
||
|
||
private class PathAttributeFilterValueFaultHandler implements ValueFaultHandler { | ||
private final ValueFaultHandler origHandler; | ||
|
||
PathAttributeFilterValueFaultHandler(ValueFaultHandler pOrigHandler) { | ||
origHandler = pOrigHandler; | ||
} | ||
|
||
public <T extends Throwable> Object handleException(T exception) throws T { | ||
if (exception instanceof AttributeNotFoundException) { | ||
throw new AttributeFilteredException(exception.getMessage()); | ||
} else { | ||
return origHandler.handleException(exception); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters