Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/java/org/weakref/jmx/testing/TestingMBeanServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.lang.String.format;
import static java.util.stream.Collectors.toMap;

public class TestingMBeanServer
implements MBeanServer
Expand Down Expand Up @@ -72,7 +76,14 @@ public ObjectInstance getObjectInstance(ObjectName name)
@Override
public Set<ObjectName> queryNames(ObjectName name, QueryExp query)
{
throw new UnsupportedOperationException();
if (query != null) {
throw new UnsupportedOperationException("QueryExp is not supported");
}
return mbeans.entrySet()
.stream()
.filter(entry -> name.apply(entry.getKey()))
.map(Map.Entry::getKey)
.collect(toImmutableSet());
}

@Override
Expand Down
153 changes: 153 additions & 0 deletions src/test/java/org/weakref/jmx/TestTestingMBeanServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package org.weakref.jmx;

import org.testng.annotations.Test;
import org.weakref.jmx.testing.TestingMBeanServer;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.Query;
import java.util.Map;
import java.util.Set;

import static java.util.Collections.emptySet;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class TestTestingMBeanServer
{
@Test
public void testUnsupportedQueryExp()
{
TestingMBeanServer server = new TestingMBeanServer();

try {
server.queryNames(makeObjectName("test", Map.of("type", "Test")), Query.eq(Query.attr("a"), Query.value("a")));
}
catch (Exception e) {
assertTrue(e instanceof UnsupportedOperationException);
}
}

@Test
public void testNonMatchingQuery()
throws InstanceAlreadyExistsException
{
TestingMBeanServer server = new TestingMBeanServer();
server.registerMBean(BEAN, makeObjectName("test", Map.of("type", "Other")));

assertEquals(server.queryNames(makeObjectName("test", Map.of("type", "Test")), null), Set.of());
}

@Test
public void testMatchingWildcard()
throws InstanceAlreadyExistsException
{
TestingMBeanServer server = new TestingMBeanServer();
server.registerMBean(BEAN, makeObjectName("test1", Map.of("type", "Other")));
server.registerMBean(BEAN, makeObjectName("test2", Map.of("type", "Other")));
server.registerMBean(BEAN, makeObjectName("test3", Map.of("type", "Other")));
server.registerMBean(BEAN, makeObjectName("test4", Map.of("type", "Other")));

assertEquals(server.queryNames(ObjectName.WILDCARD, null).size(), 4);
assertEquals(server.queryNames(ObjectName.WILDCARD, null), Set.of(
makeObjectName("test1", Map.of("type", "Other")),
makeObjectName("test2", Map.of("type", "Other")),
makeObjectName("test3", Map.of("type", "Other")),
makeObjectName("test4", Map.of("type", "Other"))));
}

@Test
public void testMatchingSpecificDomain()
throws InstanceAlreadyExistsException, MalformedObjectNameException
{
TestingMBeanServer server = new TestingMBeanServer();
server.registerMBean(BEAN, makeObjectName("test1", Map.of("type", "Other")));
server.registerMBean(BEAN, makeObjectName("test1", Map.of("type", "Other2")));
server.registerMBean(BEAN, makeObjectName("test2", Map.of("type", "Other")));
server.registerMBean(BEAN, makeObjectName("test3", Map.of("type", "Other")));
server.registerMBean(BEAN, makeObjectName("test4", Map.of("type", "Other")));
server.registerMBean(BEAN, makeObjectName("test4", Map.of("type", "Other2")));

assertEquals(server.queryNames(new ObjectName("test1:*"), null), Set.of(
makeObjectName("test1", Map.of("type", "Other")),
makeObjectName("test1", Map.of("type", "Other2"))));
}

@Test
public void testMatchingSpecificDomainAndProperty()
throws InstanceAlreadyExistsException, MalformedObjectNameException
{
TestingMBeanServer server = new TestingMBeanServer();
server.registerMBean(BEAN, makeObjectName("test1", Map.of("type", "Other")));
server.registerMBean(BEAN, makeObjectName("test1", Map.of("type", "Other2")));
server.registerMBean(BEAN, makeObjectName("test2", Map.of("type", "Other")));
server.registerMBean(BEAN, makeObjectName("test3", Map.of("type", "Other")));
server.registerMBean(BEAN, makeObjectName("test4", Map.of("type", "Other")));
server.registerMBean(BEAN, makeObjectName("test4", Map.of("type", "Other2")));

assertEquals(server.queryNames(new ObjectName("test1:type=*"), null), Set.of(
makeObjectName("test1", Map.of("type", "Other")),
makeObjectName("test1", Map.of("type", "Other2"))));

assertEquals(server.queryNames(
new ObjectName("test1:type=Other2"), null),
Set.of(makeObjectName("test1", Map.of("type", "Other2"))));
}

@Test
public void testMatchingByProperty()
throws InstanceAlreadyExistsException, MalformedObjectNameException
{
TestingMBeanServer server = new TestingMBeanServer();
server.registerMBean(BEAN, makeObjectName("test1", Map.of("type", "Other")));
server.registerMBean(BEAN, makeObjectName("test1", Map.of("type", "Other2")));
server.registerMBean(BEAN, makeObjectName("test1", Map.of("type", "Another")));
server.registerMBean(BEAN, makeObjectName("test1", Map.of("type", "Another2")));

assertEquals(server.queryNames(new ObjectName("test1:type=*"), null), Set.of(
makeObjectName("test1", Map.of("type", "Other")),
makeObjectName("test1", Map.of("type", "Other2")),
makeObjectName("test1", Map.of("type", "Another")),
makeObjectName("test1", Map.of("type", "Another2"))));

assertEquals(server.queryNames(new ObjectName("test1:type=Other"), null), Set.of(
makeObjectName("test1", Map.of("type", "Other"))));

assertEquals(server.queryNames(new ObjectName("test1:type=Other*"), null), Set.of(
makeObjectName("test1", Map.of("type", "Other")),
makeObjectName("test1", Map.of("type", "Other2"))));

assertEquals(server.queryNames(new ObjectName("test1:type=Another*"), null), Set.of(
makeObjectName("test1", Map.of("type", "Another")),
makeObjectName("test1", Map.of("type", "Another2"))));

assertEquals(server.queryNames(new ObjectName("test1:type=*ther*"), null), Set.of(
makeObjectName("test1", Map.of("type", "Other")),
makeObjectName("test1", Map.of("type", "Other2")),
makeObjectName("test1", Map.of("type", "Another")),
makeObjectName("test1", Map.of("type", "Another2"))));

assertEquals(server.queryNames(new ObjectName("test1:type=*ther"), null), Set.of(
makeObjectName("test1", Map.of("type", "Other")),
makeObjectName("test1", Map.of("type", "Another"))));

assertEquals(server.queryNames(new ObjectName("test1:type=*ther?"), null), Set.of(
makeObjectName("test1", Map.of("type", "Other2")),
makeObjectName("test1", Map.of("type", "Another2"))));
}

private static ObjectName makeObjectName(String domain, Map<String, String> properties)
{
try {
return ObjectName.getInstance(new ObjectNameBuilder(domain)
.withProperties(properties)
.build());
}
catch (MalformedObjectNameException e) {
throw new RuntimeException(e);
}
}

private static MBean BEAN = new MBean("TestBean", "TestDescription", emptySet(), emptySet());
}