| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
package org.aeonbits.owner.converters; |
| 10 |
|
| 11 |
import org.aeonbits.owner.Config; |
| 12 |
import org.aeonbits.owner.ConfigFactory; |
| 13 |
import org.aeonbits.owner.util.bytesize.ByteSize; |
| 14 |
import org.aeonbits.owner.util.bytesize.ByteSizeUnit; |
| 15 |
import org.junit.Test; |
| 16 |
|
| 17 |
import java.lang.reflect.InvocationTargetException; |
| 18 |
import java.lang.reflect.Method; |
| 19 |
|
| 20 |
import static org.junit.Assert.*; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class ByteSizeConverterTest { |
| 26 |
public interface ByteSizeConfig extends Config { |
| 27 |
@ConverterClass(ByteSizeConverter.class) |
| 28 |
@DefaultValue("10 byte") |
| 29 |
ByteSize singular10byteWithSpace(); |
| 30 |
|
| 31 |
@ConverterClass(ByteSizeConverter.class) |
| 32 |
@DefaultValue("10byte") |
| 33 |
ByteSize singular10byteWithoutSpace(); |
| 34 |
|
| 35 |
@ConverterClass(ByteSizeConverter.class) |
| 36 |
@DefaultValue("10 bytes") |
| 37 |
ByteSize plural10byte(); |
| 38 |
|
| 39 |
@ConverterClass(ByteSizeConverter.class) |
| 40 |
@DefaultValue("10m") |
| 41 |
ByteSize short10mebibytes(); |
| 42 |
|
| 43 |
@ConverterClass(ByteSizeConverter.class) |
| 44 |
@DefaultValue("10mi") |
| 45 |
ByteSize medium10mebibytes(); |
| 46 |
|
| 47 |
@ConverterClass(ByteSizeConverter.class) |
| 48 |
@DefaultValue("10mib") |
| 49 |
ByteSize long10mebibytes(); |
| 50 |
|
| 51 |
@ConverterClass(ByteSizeConverter.class) |
| 52 |
@DefaultValue("10 megabytes") |
| 53 |
ByteSize full10megabytes(); |
| 54 |
|
| 55 |
@ConverterClass(ByteSizeConverter.class) |
| 56 |
@DefaultValue("10 sillybyte") |
| 57 |
ByteSize invalidSillybyte(); |
| 58 |
|
| 59 |
@ConverterClass(ByteSizeConverter.class) |
| 60 |
@DefaultValue("megabyte") |
| 61 |
ByteSize invalidNoNumber(); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void testValidByteSizeConverter(){ |
| 66 |
ByteSizeConfig cfg = ConfigFactory.create(ByteSizeConfig.class); |
| 67 |
ByteSize bs; |
| 68 |
|
| 69 |
bs = cfg.plural10byte(); |
| 70 |
assertEquals(bs, new ByteSize(10, ByteSizeUnit.BYTES)); |
| 71 |
|
| 72 |
bs = cfg.singular10byteWithoutSpace(); |
| 73 |
assertEquals(bs, new ByteSize(10, ByteSizeUnit.BYTES)); |
| 74 |
|
| 75 |
bs = cfg.singular10byteWithSpace(); |
| 76 |
assertEquals(bs, new ByteSize(10, ByteSizeUnit.BYTES)); |
| 77 |
|
| 78 |
ByteSize compare = new ByteSize(10, ByteSizeUnit.MEBIBYTES); |
| 79 |
assertEquals(compare, cfg.short10mebibytes()); |
| 80 |
assertEquals(compare, cfg.medium10mebibytes()); |
| 81 |
assertEquals(compare, cfg.long10mebibytes()); |
| 82 |
assertNotEquals(compare, cfg.full10megabytes()); |
| 83 |
assertEquals(new ByteSize(10, ByteSizeUnit.MEGABYTES), cfg.full10megabytes()); |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void testInvalid() throws NoSuchMethodException, IllegalAccessException { |
| 88 |
ByteSizeConfig cfg = ConfigFactory.create(ByteSizeConfig.class); |
| 89 |
ByteSize bs; |
| 90 |
for (String method : new String[]{"invalidSillybyte", "invalidNoNumber"}) { |
| 91 |
Method m = ByteSizeConfig.class.getDeclaredMethod(method); |
| 92 |
try { |
| 93 |
bs = (ByteSize) m.invoke(cfg); |
| 94 |
fail(String.format("Invalid byte size [%s] should have thrown an exception. Instead we parsed: %s", method, bs)); |
| 95 |
} catch (InvocationTargetException e) { |
| 96 |
if (!(e.getCause() instanceof IllegalArgumentException)){ |
| 97 |
fail("Got an unexpected exception type when calling method: " + method); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
} |