-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cacheDurationMillis attribute to Flatten and Nested annotations
- Loading branch information
Showing
5 changed files
with
152 additions
and
24 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.weakref.jmx; | ||
|
||
import com.google.common.collect.Iterables; | ||
import org.testng.annotations.Test; | ||
|
||
import javax.management.Attribute; | ||
|
||
import java.util.Collection; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
public class TestExpiration | ||
{ | ||
private static final long CACHE_DURATION_MILLIS = 100; | ||
private static final String DEFAULT_VALUE = "DEFAULT_VALUE"; | ||
private static final String UPDATED_VALUE = "UPDATED_VALUE"; | ||
|
||
public static class SingleValueObject | ||
{ | ||
private String value = DEFAULT_VALUE; | ||
|
||
@Managed | ||
public String getValue() | ||
{ | ||
return value; | ||
} | ||
|
||
@Managed | ||
public void setValue(String value) | ||
{ | ||
this.value = value; | ||
} | ||
} | ||
|
||
public static class NestedWithExpiration | ||
{ | ||
@Nested(cacheDurationMillis = CACHE_DURATION_MILLIS) | ||
public SingleValueObject getSingleValueObject() | ||
{ | ||
return new SingleValueObject(); | ||
} | ||
} | ||
|
||
public static class FlattenWithExpiration | ||
{ | ||
@Flatten(cacheDurationMillis = CACHE_DURATION_MILLIS) | ||
public SingleValueObject getSingleValueObject() | ||
{ | ||
return new SingleValueObject(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testFlatterExpiration() | ||
throws Exception | ||
{ | ||
testExpiration(new FlattenWithExpiration()); | ||
} | ||
|
||
@Test | ||
public void testNestedExpiration() | ||
throws Exception | ||
{ | ||
testExpiration(new NestedWithExpiration()); | ||
} | ||
|
||
private void testExpiration(Object object) | ||
throws Exception | ||
{ | ||
|
||
MBean mBean = MBeanBuilder.from(object).build(); | ||
Collection<MBeanAttribute> attributes = mBean.getAttributes(); | ||
String attributeName = Iterables.getOnlyElement(attributes).getName(); | ||
|
||
assertEquals(mBean.getAttribute(attributeName), DEFAULT_VALUE); | ||
|
||
mBean.setAttribute(new Attribute(attributeName, UPDATED_VALUE)); | ||
assertEquals(mBean.getAttribute(attributeName), UPDATED_VALUE); | ||
|
||
Thread.sleep(CACHE_DURATION_MILLIS + 1); | ||
|
||
assertEquals(mBean.getAttribute(attributeName), DEFAULT_VALUE); | ||
} | ||
} |