|
| 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