| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.types; |
| 34 | | | |
| 35 | | | import junit.framework.TestCase; |
| 36 | | | |
| 37 | | | import org.jcoderz.commons.ArgumentMaxLengthViolationException; |
| 38 | | | import org.jcoderz.commons.ArgumentMaxValueViolationException; |
| 39 | | | import org.jcoderz.commons.ArgumentMinLengthViolationException; |
| 40 | | | import org.jcoderz.commons.ArgumentMinValueViolationException; |
| 41 | | | import org.jcoderz.commons.config.ConfigurationKey; |
| 42 | | | import org.jcoderz.commons.test.RestrictedLong; |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | @author |
| 47 | | | |
| 48 | 100 | | public class StrongTypesTest |
| 49 | | | extends TestCase |
| 50 | | | { |
| 51 | | | private static final int LONG_STRING_LENGTH = 2048; |
| 52 | | | private static final String LONG_STRING; |
| 53 | | | |
| 54 | | | static |
| 55 | | | { |
| 56 | 100 | | final StringBuffer sb = new StringBuffer(); |
| 57 | 100 | | for (int i = 0; i < LONG_STRING_LENGTH; i++) |
| 58 | | | { |
| 59 | 100 | | sb.append(' '); |
| 60 | | | } |
| 61 | 100 | | LONG_STRING = sb.toString(); |
| 62 | 100 | | } |
| 63 | | | |
| 64 | | | |
| 65 | | | |
| 66 | | | |
| 67 | | | public void testToLong () |
| 68 | | | { |
| 69 | | | try |
| 70 | | | { |
| 71 | 0 | (1) | final ConfigurationKey key |
| 72 | | | = ConfigurationKey.fromString(LONG_STRING.substring(0, |
| 73 | | | ConfigurationKey.MAX_LENGTH + 1)); |
| 74 | 0 | (2) | fail("Expected exception!"); |
| 75 | | | } |
| 76 | 100 | | catch (ArgumentMaxLengthViolationException ex) |
| 77 | | | { |
| 78 | | | |
| 79 | 0 | | } |
| 80 | 100 | | } |
| 81 | | | |
| 82 | | | |
| 83 | | | |
| 84 | | | |
| 85 | | | public void testToShort () |
| 86 | | | { |
| 87 | | | try |
| 88 | | | { |
| 89 | 0 | (3) | final ConfigurationKey key |
| 90 | | | = ConfigurationKey.fromString(""); |
| 91 | 0 | | fail("Expected exception!"); |
| 92 | | | } |
| 93 | 100 | | catch (ArgumentMinLengthViolationException ex) |
| 94 | | | { |
| 95 | | | |
| 96 | 0 | | } |
| 97 | 100 | | } |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | | | |
| 102 | | | public void testToHigh () |
| 103 | | | { |
| 104 | | | try |
| 105 | | | { |
| 106 | 0 | (4) | final RestrictedLong lg |
| 107 | | | = RestrictedLong.fromLong(RestrictedLong.MAX_VALUE + 1); |
| 108 | 0 | | fail("Expected exception!"); |
| 109 | | | } |
| 110 | 100 | | catch (ArgumentMaxValueViolationException ex) |
| 111 | | | { |
| 112 | | | |
| 113 | 0 | | } |
| 114 | 100 | | } |
| 115 | | | |
| 116 | | | |
| 117 | | | |
| 118 | | | |
| 119 | | | public void testToLow () |
| 120 | | | { |
| 121 | | | try |
| 122 | | | { |
| 123 | 0 | (5) | final RestrictedLong lg |
| 124 | | | = RestrictedLong.fromLong(RestrictedLong.MIN_VALUE - 1); |
| 125 | 0 | | fail("Expected exception!"); |
| 126 | | | } |
| 127 | 100 | | catch (ArgumentMinValueViolationException ex) |
| 128 | | | { |
| 129 | | | |
| 130 | 0 | | } |
| 131 | 100 | | } |
| 132 | | | |
| 133 | | | |
| 134 | | | |
| 135 | | | |
| 136 | | | public void testComparison () |
| 137 | | | { |
| 138 | 100 | | assertEquals("Comparing equal values", 0, |
| 139 | | | RestrictedLong.fromString("15") |
| 140 | | | .compareTo(RestrictedLong.fromString("15"))); |
| 141 | 75 | | assertTrue("Comparing different values", |
| 142 | | | RestrictedLong.fromString("15") |
| 143 | | | .compareTo(RestrictedLong.fromString("21")) < 0); |
| 144 | 75 | | assertTrue("Comparing different values", |
| 145 | | | RestrictedLong.fromString("21") |
| 146 | | | .compareTo(RestrictedLong.fromString("15")) > 0); |
| 147 | 100 | | } |
| 148 | | | |
| 149 | | | |
| 150 | | | public void testImplementsTaggedColor () |
| 151 | | | { |
| 152 | 71 | | assertTrue("TaggedColor should implement Tagger interface", |
| 153 | | | TestTaggerInterface.class.isAssignableFrom(TaggedColor.class)); |
| 154 | 100 | | } |
| 155 | | | |
| 156 | | | |
| 157 | | | public void testImplementsTaggedFooString () |
| 158 | | | { |
| 159 | 71 | | assertTrue("TaggedFooString should implement Tagger interface", |
| 160 | | | TestTaggerInterface.class.isAssignableFrom(TaggedFooString.class)); |
| 161 | 100 | | } |
| 162 | | | |
| 163 | | | |
| 164 | | | public void testImplementsTaggedValueObject () |
| 165 | | | { |
| 166 | 71 | | assertTrue( |
| 167 | | | "TaggedSampleValueObject should implement Tagger interface", |
| 168 | | | TestTaggerInterface.class.isAssignableFrom( |
| 169 | | | TaggedSampleValueObject.class)); |
| 170 | 71 | | assertTrue( |
| 171 | | | "TaggedPlainSampleValueObject should implement Tagger interface", |
| 172 | | | TestTaggerInterface.class.isAssignableFrom( |
| 173 | | | TaggedPlainSampleValueObject.class)); |
| 174 | 71 | | assertTrue( |
| 175 | | (6) | "TaggedSerializableSampleValueObject " + |
| 176 | | | "should implement Tagger interface", |
| 177 | | | TestTaggerInterface.class.isAssignableFrom( |
| 178 | | | TaggedSerializableSampleValueObject.class)); |
| 179 | 100 | | } |
| 180 | | | } |