| 1 |
package ezvcard.property; |
| 2 |
|
| 3 |
import static ezvcard.property.PropertySensei.assertCopy; |
| 4 |
import static ezvcard.property.PropertySensei.assertEqualsMethod; |
| 5 |
import static ezvcard.property.PropertySensei.assertNothingIsEqual; |
| 6 |
import static ezvcard.property.PropertySensei.assertValidate; |
| 7 |
import static org.junit.Assert.assertEquals; |
| 8 |
import static org.junit.Assert.assertFalse; |
| 9 |
import static org.junit.Assert.assertNull; |
| 10 |
|
| 11 |
import java.time.Instant; |
| 12 |
import java.time.ZoneId; |
| 13 |
import java.time.ZoneOffset; |
| 14 |
|
| 15 |
import org.junit.Test; |
| 16 |
|
| 17 |
import ezvcard.VCardVersion; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class TimezoneTest { |
| 52 |
@Test |
| 53 |
public void constructors() throws Exception { |
| 54 |
Timezone property = new Timezone((String) null); |
| 55 |
assertNull(property.getOffset()); |
| 56 |
assertNull(property.getText()); |
| 57 |
|
| 58 |
property = new Timezone(ZoneOffset.ofHours(1)); |
| 59 |
assertEquals(ZoneOffset.ofHours(1), property.getOffset()); |
| 60 |
assertNull(property.getText()); |
| 61 |
|
| 62 |
property = new Timezone(ZoneId.ofOffset("GMT", ZoneOffset.ofHours(1))); |
| 63 |
assertEquals(ZoneOffset.ofHours(1), property.getOffset()); |
| 64 |
assertEquals("GMT+01:00", property.getText()); |
| 65 |
|
| 66 |
property = new Timezone("text"); |
| 67 |
assertNull(property.getOffset()); |
| 68 |
assertEquals("text", property.getText()); |
| 69 |
|
| 70 |
property = new Timezone(ZoneOffset.ofHours(1), "text"); |
| 71 |
assertEquals(ZoneOffset.ofHours(1), property.getOffset()); |
| 72 |
assertEquals("text", property.getText()); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void set_value() { |
| 77 |
Timezone property = new Timezone((String) null); |
| 78 |
|
| 79 |
property.setOffset(ZoneOffset.ofHours(1)); |
| 80 |
assertEquals(ZoneOffset.ofHours(1), property.getOffset()); |
| 81 |
assertNull(property.getText()); |
| 82 |
|
| 83 |
property.setText("text"); |
| 84 |
assertEquals(ZoneOffset.ofHours(1), property.getOffset()); |
| 85 |
assertEquals("text", property.getText()); |
| 86 |
} |
| 87 |
|
| 88 |
@Test |
| 89 |
public void validate() { |
| 90 |
Timezone empty = new Timezone((ZoneOffset) null); |
| 91 |
assertValidate(empty).versions(VCardVersion.V2_1).run(8, 20); |
| 92 |
assertValidate(empty).versions(VCardVersion.V3_0).run(8); |
| 93 |
assertValidate(empty).versions(VCardVersion.V4_0).run(8); |
| 94 |
|
| 95 |
Timezone withOffset = new Timezone(ZoneOffset.ofHoursMinutes(-5, -30)); |
| 96 |
assertValidate(withOffset).run(); |
| 97 |
|
| 98 |
Timezone withText = new Timezone("America/New_York"); |
| 99 |
assertValidate(withText).versions(VCardVersion.V2_1).run(20); |
| 100 |
assertValidate(withText).versions(VCardVersion.V3_0).run(); |
| 101 |
assertValidate(withText).versions(VCardVersion.V4_0).run(); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void toTimeZone_offset() { |
| 106 |
Timezone t = new Timezone(ZoneOffset.ofHoursMinutes(-5, -30)); |
| 107 |
ZoneId actual = t.toTimeZone(); |
| 108 |
assertEquals(ZoneOffset.ofHoursMinutes(-5, -30), actual); |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void toTimeZone_timezone_id() { |
| 113 |
Timezone t = new Timezone("America/New_York"); |
| 114 |
ZoneId actual = t.toTimeZone(); |
| 115 |
assertEquals(ZoneId.of("America/New_York"), actual); |
| 116 |
} |
| 117 |
|
| 118 |
@Test |
| 119 |
public void toTimeZone_text() { |
| 120 |
Timezone t = new Timezone("text"); |
| 121 |
ZoneId actual = t.toTimeZone(); |
| 122 |
assertNull(actual); |
| 123 |
} |
| 124 |
|
| 125 |
@Test |
| 126 |
public void toTimeZone_text_and_offset() { |
| 127 |
Timezone t = new Timezone(ZoneOffset.ofHoursMinutes(-5, -30), "text"); |
| 128 |
ZoneId actual = t.toTimeZone(); |
| 129 |
|
| 130 |
assertEquals(ZoneOffset.ofHoursMinutes(-5, -30), actual.getRules().getOffset(Instant.now())); |
| 131 |
assertEquals("-05:30", actual.getId()); |
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void toStringValues() { |
| 136 |
Timezone property = new Timezone("text"); |
| 137 |
assertFalse(property.toStringValues().isEmpty()); |
| 138 |
} |
| 139 |
|
| 140 |
@Test |
| 141 |
public void copy() { |
| 142 |
Timezone original = new Timezone((String) null); |
| 143 |
assertCopy(original); |
| 144 |
|
| 145 |
original = new Timezone("text"); |
| 146 |
assertCopy(original); |
| 147 |
|
| 148 |
original = new Timezone(ZoneOffset.ofHours(1)); |
| 149 |
assertCopy(original); |
| 150 |
} |
| 151 |
|
| 152 |
@Test |
| 153 |
public void equals() { |
| 154 |
|
| 155 |
assertNothingIsEqual( |
| 156 |
new Timezone((String) null), |
| 157 |
new Timezone("text"), |
| 158 |
new Timezone("text2"), |
| 159 |
new Timezone(ZoneOffset.ofHours(1)), |
| 160 |
new Timezone(ZoneOffset.ofHours(2)), |
| 161 |
new Timezone(ZoneOffset.ofHours(1), "text") |
| 162 |
); |
| 163 |
|
| 164 |
assertEqualsMethod(Timezone.class, "text") |
| 165 |
.constructor(new Class<?>[]{String.class}, (String)null).test() |
| 166 |
.constructor("text").test() |
| 167 |
.constructor(ZoneOffset.ofHours(1)).test() |
| 168 |
.constructor(ZoneOffset.ofHours(1), "text").test(); |
| 169 |
|
| 170 |
} |
| 171 |
} |