Skip to content

Commit 230bc8f

Browse files
committed
Add unit tests for UsageService behavior
1 parent 6600f9f commit 230bc8f

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.usage;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertNotNull;
36+
37+
import org.junit.After;
38+
import org.junit.Before;
39+
import org.junit.Test;
40+
import org.scijava.Context;
41+
import org.scijava.command.Command;
42+
import org.scijava.command.CommandService;
43+
import org.scijava.event.EventService;
44+
import org.scijava.module.Module;
45+
46+
/**
47+
* Tests accumulation of {@link Module} executions.
48+
*
49+
* @author Curtis Rueden
50+
*/
51+
public class ModuleUsageTest {
52+
53+
private UsageService usageService;
54+
private CommandService commandService;
55+
56+
@Before
57+
public void setUp() {
58+
final Context context =
59+
new Context(EventService.class, CommandService.class, UsageService.class);
60+
usageService = context.getService(UsageService.class);
61+
commandService = context.getService(CommandService.class);
62+
}
63+
64+
@After
65+
public void tearDown() {
66+
usageService.getContext().dispose();
67+
}
68+
69+
/** Tests accumulation of {@link Module} executions. */
70+
@Test
71+
public void testModuleStats() throws Exception {
72+
commandService.run(MyCommand.class, false).get();
73+
final String id = "command:" + MyCommand.class.getName();
74+
final UsageStats usageStats = usageService.getStats().get(id);
75+
assertNotNull(usageStats);
76+
assertEquals(id, usageStats.getIdentifier());
77+
}
78+
79+
public static class MyCommand implements Command {
80+
@Override
81+
public void run() {
82+
// NB: No action needed.
83+
}
84+
}
85+
86+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.usage;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertFalse;
36+
import static org.junit.Assert.assertNotNull;
37+
import static org.junit.Assert.assertNotSame;
38+
import static org.junit.Assert.assertNull;
39+
import static org.junit.Assert.assertSame;
40+
import static org.junit.Assert.assertTrue;
41+
42+
import java.util.Map;
43+
44+
import org.junit.After;
45+
import org.junit.Before;
46+
import org.junit.Test;
47+
import org.scijava.Context;
48+
import org.scijava.Identifiable;
49+
import org.scijava.Locatable;
50+
51+
/**
52+
* Tests {@link UsageService}.
53+
*
54+
* @author Curtis Rueden
55+
*/
56+
public class UsageServiceTest {
57+
58+
private UsageService usageService;
59+
60+
@Before
61+
public void setUp() {
62+
final Context context = new Context(UsageService.class);
63+
usageService = context.getService(UsageService.class);
64+
}
65+
66+
@After
67+
public void tearDown() {
68+
usageService.getContext().dispose();
69+
}
70+
71+
/** Tests {@link UsageService#getStats()}. */
72+
@Test
73+
public void testUsage() {
74+
final Thing foo = new Thing("foo", "file:/foo");
75+
final Thing bar = new Thing("bar", "file:/bar");
76+
77+
final Map<String, UsageStats> stats = usageService.getStats();
78+
// no stats at first
79+
assertTrue(stats.isEmpty());
80+
assertNull(stats.get(foo));
81+
82+
// increment foo
83+
usageService.increment(foo);
84+
assertEquals(1, stats.size());
85+
final UsageStats fooStats = stats.get(foo.getIdentifier());
86+
assertNotNull(fooStats);
87+
assertSame(fooStats, usageService.getUsage(foo));
88+
assertEquals(foo.getIdentifier(), fooStats.getIdentifier());
89+
assertEquals("file:/foo", fooStats.getLocation());
90+
assertEquals(1, fooStats.getCount());
91+
92+
// increment bar
93+
assertFalse(stats.containsKey(bar));
94+
usageService.increment(bar);
95+
assertEquals(2, stats.size());
96+
final UsageStats barStats = stats.get(bar.getIdentifier());
97+
assertNotNull(barStats);
98+
assertSame(barStats, usageService.getUsage(bar));
99+
assertEquals(bar.getIdentifier(), barStats.getIdentifier());
100+
assertEquals("file:/bar", barStats.getLocation());
101+
assertEquals(1, barStats.getCount());
102+
103+
// increment foo again
104+
usageService.increment(foo);
105+
assertEquals(2, stats.size());
106+
assertEquals(2, fooStats.getCount());
107+
assertEquals(1, barStats.getCount());
108+
109+
// clear stats, and ensure it doesn't nuke existing references
110+
usageService.clearStats();
111+
assertEquals(2, stats.size());
112+
assertEquals(2, fooStats.getCount());
113+
assertEquals(1, barStats.getCount());
114+
final Map<String, UsageStats> newStats = usageService.getStats();
115+
assertNotSame(stats, newStats);
116+
assertTrue(newStats.isEmpty());
117+
assertNotSame(fooStats, usageService.getUsage(foo));
118+
assertNotSame(barStats, usageService.getUsage(bar));
119+
120+
// verify that increments affect new stats now, not old ones
121+
usageService.increment(foo);
122+
assertEquals(2, fooStats.getCount());
123+
assertEquals(1, barStats.getCount());
124+
assertEquals(1, newStats.get(foo.getIdentifier()).getCount());
125+
}
126+
127+
// -- Helper classes --
128+
129+
private static class Thing implements Identifiable, Locatable {
130+
131+
private String id;
132+
private String location;
133+
134+
private Thing(final String id, final String location) {
135+
this.id = id;
136+
this.location = location;
137+
}
138+
139+
@Override
140+
public String getLocation() {
141+
return location;
142+
}
143+
144+
@Override
145+
public String getIdentifier() {
146+
return id;
147+
}
148+
149+
}
150+
151+
}

0 commit comments

Comments
 (0)