-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 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
41 changes: 41 additions & 0 deletions
41
src/main/java/io/github/mweirauch/metrics/jvm/extras/UptimeGauge.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,41 @@ | ||
/* | ||
* Copyright © 2016 Michael Weirauch ([email protected]) | ||
* | ||
* 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. | ||
*/ | ||
package io.github.mweirauch.metrics.jvm.extras; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.RuntimeMXBean; | ||
import java.util.Objects; | ||
|
||
import com.codahale.metrics.Gauge; | ||
|
||
public class UptimeGauge implements Gauge<Long> { | ||
|
||
private final RuntimeMXBean runtimeBean; | ||
|
||
public UptimeGauge() { | ||
this(ManagementFactory.getRuntimeMXBean()); | ||
} | ||
|
||
public UptimeGauge(RuntimeMXBean runtimeBean) { | ||
this.runtimeBean = Objects.requireNonNull(runtimeBean); | ||
} | ||
|
||
@Override | ||
public Long getValue() { | ||
return runtimeBean.getUptime(); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
src/test/java/io/github/mweirauch/metrics/jvm/extras/UptimeGaugeUnit0Test.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,74 @@ | ||
/* | ||
* Copyright © 2016 Michael Weirauch ([email protected]) | ||
* | ||
* 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. | ||
*/ | ||
package io.github.mweirauch.metrics.jvm.extras; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.RuntimeMXBean; | ||
|
||
import org.junit.Test; | ||
|
||
import com.google.common.testing.NullPointerTester; | ||
import com.google.common.testing.NullPointerTester.Visibility; | ||
|
||
public class UptimeGaugeUnit0Test { | ||
|
||
private final RuntimeMXBean runtimeBean = mock(RuntimeMXBean.class); | ||
|
||
@Test | ||
public void testNullContract() { | ||
final UptimeGauge uut = new UptimeGauge(runtimeBean); | ||
|
||
final NullPointerTester npt = new NullPointerTester(); | ||
|
||
npt.testConstructors(uut.getClass(), Visibility.PACKAGE); | ||
npt.testStaticMethods(uut.getClass(), Visibility.PACKAGE); | ||
npt.testInstanceMethods(uut, Visibility.PACKAGE); | ||
} | ||
|
||
@Test | ||
public void testInstantiation() { | ||
new UptimeGauge(); | ||
} | ||
|
||
@Test | ||
public void testGetValue() throws Exception { | ||
final UptimeGauge uut = new UptimeGauge(runtimeBean); | ||
when(runtimeBean.getUptime()).thenReturn(Long.valueOf(1337)); | ||
|
||
final Long value = uut.getValue(); | ||
|
||
assertNotNull(value); | ||
assertEquals(Long.valueOf(1337), value); | ||
} | ||
|
||
@Test | ||
public void testGetValueReal() throws Exception { | ||
final UptimeGauge uut = new UptimeGauge(ManagementFactory.getRuntimeMXBean()); | ||
when(runtimeBean.getUptime()).thenReturn(Long.valueOf(1337)); | ||
|
||
final Long value = uut.getValue(); | ||
|
||
assertNotNull(value); | ||
assertTrue(value > 0); | ||
} | ||
|
||
} |