1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * The contents of this file are subject to the terms of either the Universal Permissive License 7 * v 1.0 as shown at http://oss.oracle.com/licenses/upl 8 * 9 * or the following license: 10 * 11 * Redistribution and use in source and binary forms, with or without modification, are permitted 12 * provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions 15 * and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 18 * conditions and the following disclaimer in the documentation and/or other materials provided with 19 * the distribution. 20 * 21 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to 22 * endorse or promote products derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 26 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 31 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 package org.openjdk.jmc.common.test.unit; 34 35 import static org.junit.Assert.assertEquals; 36 import static org.junit.Assert.assertNotNull; 37 import static org.junit.Assert.assertTrue; 38 import static org.junit.Assert.fail; 39 import static org.openjdk.jmc.common.IDisplayable.AUTO; 40 import static org.openjdk.jmc.common.IDisplayable.EXACT; 41 import static org.openjdk.jmc.common.IDisplayable.VERBOSE; 42 import static org.openjdk.jmc.common.unit.DecimalPrefix.MICRO; 43 import static org.openjdk.jmc.common.unit.DecimalPrefix.NONE; 44 import static org.openjdk.jmc.common.unit.UnitLookup.EPOCH_MS; 45 import static org.openjdk.jmc.common.unit.UnitLookup.EPOCH_NS; 46 import static org.openjdk.jmc.common.unit.UnitLookup.MEMORY; 47 import static org.openjdk.jmc.common.unit.UnitLookup.TIMESPAN; 48 import static org.openjdk.jmc.common.unit.UnitLookup.TIMESTAMP; 49 50 import org.junit.Test; 51 import org.openjdk.jmc.common.test.MCTestCase; 52 import org.openjdk.jmc.common.unit.IQuantity; 53 import org.openjdk.jmc.common.unit.IRange; 54 import org.openjdk.jmc.common.unit.ITypedQuantity; 55 import org.openjdk.jmc.common.unit.IUnit; 56 import org.openjdk.jmc.common.unit.LinearKindOfQuantity; 57 import org.openjdk.jmc.common.unit.LinearUnit; 58 import org.openjdk.jmc.common.unit.QuantityRange; 59 import org.openjdk.jmc.common.unit.UnitLookup; 60 61 @SuppressWarnings("nls") 62 public class ContentTypeTest extends MCTestCase { 63 64 static public void assertContains(String expectedSubStr, String actual) { 65 if (!actual.contains(expectedSubStr)) { 66 fail("expected to contain:<" + expectedSubStr + "> did not:<" + actual + ">"); 67 } 68 } 69 70 static public void assertNotContain(String unexpectedSubStr, String actual) { 71 if (actual.contains(unexpectedSubStr)) { 72 fail("didn't expect to contain:<" + unexpectedSubStr + "> did:<" + actual + ">"); 73 } 74 } 75 76 @Test 77 public void testTimestampInteractive() throws Exception { 78 for (IUnit unit : TIMESTAMP.getAllUnits()) { 79 // Doesn't work with ms, as the era designator (B.C.E) typically isn't in the used pattern. 80 // IQuantity quantity = unit.quantity(-4937981208836185383L); 81 IQuantity quantity = unit.quantity(-49379812088L); 82 AdHocQuantityTest.assertInteractiveRoundtrip(quantity); 83 quantity = unit.quantity(4937981208836185L); 84 AdHocQuantityTest.assertInteractiveRoundtrip(quantity); 85 } 86 } 87 88 @Test 89 public void testTimestampMilliRangeFormat() throws Exception { 90 IRange<IQuantity> range = QuantityRange.createWithEnd(EPOCH_MS.quantity(4200000000123L), 91 EPOCH_MS.quantity(4200000000999L)); 92 assertContains("999", range.displayUsing(AUTO)); 93 assertContains("999", range.displayUsing(EXACT)); 94 assertContains("999", range.displayUsing(VERBOSE)); 95 } 96 97 @Test 98 public void testTimestampNanoRangeFormat() throws Exception { 99 IRange<IQuantity> range = QuantityRange.createWithEnd(EPOCH_NS.quantity(4200000000123000000L), 100 EPOCH_NS.quantity(4200000000999999999L)); 101 assertContains("999", range.displayUsing(AUTO)); 102 assertContains("999", range.displayUsing(EXACT)); 103 assertContains("999", range.displayUsing(VERBOSE)); 104 } 105 106 @Test 107 public void testTimestampPersisted() throws Exception { 108 for (IUnit unit : TIMESTAMP.getAllUnits()) { 109 IQuantity quantity = unit.quantity(-4937981208836185383L); 110 AdHocQuantityTest.assertPersistedRoundtrip(quantity); 111 quantity = unit.quantity(4937981208836185383L); 112 AdHocQuantityTest.assertPersistedRoundtrip(quantity); 113 } 114 } 115 116 @Test 117 public void testTimeSpanUnits() { 118 for (IUnit unit : UnitLookup.TIMESPAN.getAllUnits()) { 119 IQuantity quantity = unit.quantity(17); 120 assertTrue(quantity.toString().contains("17")); 121 } 122 } 123 124 @Test 125 public void testTimeSpanFormatAlmostWeeks() { 126 LinearUnit days = TIMESPAN.getUnit("d"); 127 IQuantity almost2wk = days.quantity(13.5); 128 String dispStr = almost2wk.displayUsing(AUTO); 129 assertContains("2", dispStr); 130 assertNotContain("14", dispStr); 131 assertNotContain("13", dispStr); 132 assertNotContain("12", dispStr); 133 } 134 135 @Test 136 public void testTimeSpanFormatAlmostWeeksNeg() { 137 LinearUnit days = TIMESPAN.getUnit("d"); 138 IQuantity almost2wk = days.quantity(-13.5); 139 String dispStr = almost2wk.displayUsing(AUTO); 140 assertContains("2", dispStr); 141 assertNotContain("14", dispStr); 142 assertNotContain("13", dispStr); 143 assertNotContain("12", dispStr); 144 } 145 146 @Test 147 public void testTimeSpanFormatFewMinutes() { 148 LinearUnit seconds = TIMESPAN.getUnit(NONE); 149 IQuantity fewMinutes = seconds.quantity(100); 150 String dispStr = fewMinutes.displayUsing(AUTO); 151 assertContains("1", dispStr); 152 assertContains("40", dispStr); 153 assertNotContain("20", dispStr); 154 } 155 156 @Test 157 public void testTimeSpanFormatFewMinutesNeg() { 158 LinearUnit seconds = TIMESPAN.getUnit(NONE); 159 IQuantity fewMinutes = seconds.quantity(-100); 160 String dispStr = fewMinutes.displayUsing(AUTO); 161 assertContains("1", dispStr); 162 assertContains("40", dispStr); 163 assertNotContain("20", dispStr); 164 } 165 166 @Test 167 public void testTimeSpanZeroDays() { 168 LinearUnit days = TIMESPAN.getUnit("d"); 169 IQuantity noTime = days.quantity(0); 170 String dispStr = noTime.displayUsing(AUTO); 171 assertContains("0", dispStr); 172 } 173 174 @Test 175 public void testTimeSpanZeroDaysNeg() { 176 LinearUnit days = TIMESPAN.getUnit("d"); 177 IQuantity noTime = days.quantity(-0); 178 String dispStr = noTime.displayUsing(AUTO); 179 assertContains("0", dispStr); 180 } 181 182 @Test 183 public void testMicrosecondMu() { 184 // FIXME: This should probably return the real microsecond unit instead of null. 185 assertNull(UnitLookup.getUnitOrNull("timespan:\u03bcs")); 186 } 187 188 @Test 189 public void testMicrosecondU() { 190 IUnit us = TIMESPAN.getUnit(MICRO); 191 assertEquals(us, UnitLookup.getUnitOrNull("timespan:us")); 192 } 193 194 @Test 195 public void testPercentagePercent() { 196 IUnit percent = UnitLookup.getUnitOrNull("percentage:%"); 197 assertNotNull(percent); 198 } 199 200 @Test 201 public void testPercentageUnity() { 202 IUnit unity = UnitLookup.getUnitOrNull("percentage:"); 203 assertNotNull(unity); 204 } 205 206 @Test 207 public void testMemoryUnits() throws Exception { 208 LinearUnit MiB = MEMORY.getUnit("MiB"); 209 assertNotNull(MiB); 210 LinearUnit GiB = MEMORY.getUnit("GiB"); 211 assertNotNull(GiB); 212 IQuantity limit32bit = GiB.quantity(4); 213 assertEquals(4096, limit32bit.longValueIn(MiB)); 214 } 215 216 @Test 217 public void testMemoryUnitsGenerally() throws Exception { 218 LinearUnit MiB = (LinearUnit) UnitLookup.getUnitOrNull("memory:MiB"); 219 assertNotNull(MiB); 220 LinearUnit GiB = (LinearUnit) UnitLookup.getUnitOrNull("memory:GiB"); 221 assertNotNull(GiB); 222 IQuantity limit32bit = GiB.quantity(4); 223 assertEquals(4096, limit32bit.longValueIn(MiB)); 224 } 225 226 @Test 227 public void testBadMemoryUnits() throws Exception { 228 assertNull(MEMORY.getUnit("MB")); 229 assertNull(MEMORY.getUnit("GB")); 230 assertNull(MEMORY.getUnit("KB")); 231 assertNull(MEMORY.getUnit("kB")); 232 } 233 234 @Test 235 public void testBadMemoryUnitsGenerally() throws Exception { 236 assertNull(UnitLookup.getUnitOrNull("memory:MB")); 237 assertNull(UnitLookup.getUnitOrNull("memory:GB")); 238 assertNull(UnitLookup.getUnitOrNull("memory:KB")); 239 assertNull(UnitLookup.getUnitOrNull("memory:kB")); 240 } 241 242 @Test 243 public void testCustomUnitConversion() throws Exception { 244 LinearKindOfQuantity memory = MEMORY; 245 ITypedQuantity<LinearUnit> unitBase = memory.parsePersisted("14075211111991929 TiB"); 246 LinearUnit customUnit = memory.makeCustomUnit(unitBase); 247 IQuantity quantity = customUnit.quantity(-690087618782632812L); 248 IQuantity wellKnown = memory.parsePersisted(quantity.persistableString()); 249 AdHocQuantityTest.assertNearlySame(wellKnown, quantity); 250 AdHocQuantityTest.assertNearlySame(quantity, wellKnown); 251 } 252 253 }