| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2008, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 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 | * Test the generated Strong Types. |
|---|
| 46 | * @author Andreas Mandel |
|---|
| 47 | */ |
|---|
| 48 | 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 | final StringBuffer sb = new StringBuffer(); |
|---|
| 57 | for (int i = 0; i < LONG_STRING_LENGTH; i++) |
|---|
| 58 | { |
|---|
| 59 | sb.append(' '); |
|---|
| 60 | } |
|---|
| 61 | LONG_STRING = sb.toString(); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Testing the max length restriction in generated String types. |
|---|
| 66 | */ |
|---|
| 67 | public void testToLong () |
|---|
| 68 | { |
|---|
| 69 | try |
|---|
| 70 | { |
|---|
| 71 | final ConfigurationKey key |
|---|
| 72 | = ConfigurationKey.fromString(LONG_STRING.substring(0, |
|---|
| 73 | ConfigurationKey.MAX_LENGTH + 1)); |
|---|
| 74 | fail("Expected exception!"); |
|---|
| 75 | } |
|---|
| 76 | catch (ArgumentMaxLengthViolationException ex) |
|---|
| 77 | { |
|---|
| 78 | // expected; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Testing the min length restriction in generated String types. |
|---|
| 84 | */ |
|---|
| 85 | public void testToShort () |
|---|
| 86 | { |
|---|
| 87 | try |
|---|
| 88 | { |
|---|
| 89 | final ConfigurationKey key |
|---|
| 90 | = ConfigurationKey.fromString(""); |
|---|
| 91 | fail("Expected exception!"); |
|---|
| 92 | } |
|---|
| 93 | catch (ArgumentMinLengthViolationException ex) |
|---|
| 94 | { |
|---|
| 95 | // expected; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * Testing the max value restriction in generated Long types. |
|---|
| 101 | */ |
|---|
| 102 | public void testToHigh () |
|---|
| 103 | { |
|---|
| 104 | try |
|---|
| 105 | { |
|---|
| 106 | final RestrictedLong lg |
|---|
| 107 | = RestrictedLong.fromLong(RestrictedLong.MAX_VALUE + 1); |
|---|
| 108 | fail("Expected exception!"); |
|---|
| 109 | } |
|---|
| 110 | catch (ArgumentMaxValueViolationException ex) |
|---|
| 111 | { |
|---|
| 112 | // expected; |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * Testing the min value restriction in generated Long types. |
|---|
| 118 | */ |
|---|
| 119 | public void testToLow () |
|---|
| 120 | { |
|---|
| 121 | try |
|---|
| 122 | { |
|---|
| 123 | final RestrictedLong lg |
|---|
| 124 | = RestrictedLong.fromLong(RestrictedLong.MIN_VALUE - 1); |
|---|
| 125 | fail("Expected exception!"); |
|---|
| 126 | } |
|---|
| 127 | catch (ArgumentMinValueViolationException ex) |
|---|
| 128 | { |
|---|
| 129 | // expected; |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | * Test method for comparison. |
|---|
| 135 | */ |
|---|
| 136 | public void testComparison () |
|---|
| 137 | { |
|---|
| 138 | assertEquals("Comparing equal values", 0, |
|---|
| 139 | RestrictedLong.fromString("15") |
|---|
| 140 | .compareTo(RestrictedLong.fromString("15"))); |
|---|
| 141 | assertTrue("Comparing different values", |
|---|
| 142 | RestrictedLong.fromString("15") |
|---|
| 143 | .compareTo(RestrictedLong.fromString("21")) < 0); |
|---|
| 144 | assertTrue("Comparing different values", |
|---|
| 145 | RestrictedLong.fromString("21") |
|---|
| 146 | .compareTo(RestrictedLong.fromString("15")) > 0); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /** Test for enumeration implements tag. */ |
|---|
| 150 | public void testImplementsTaggedColor () |
|---|
| 151 | { |
|---|
| 152 | assertTrue("TaggedColor should implement Tagger interface", |
|---|
| 153 | TestTaggerInterface.class.isAssignableFrom(TaggedColor.class)); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | /** Test for restricted string implements tag. */ |
|---|
| 157 | public void testImplementsTaggedFooString () |
|---|
| 158 | { |
|---|
| 159 | assertTrue("TaggedFooString should implement Tagger interface", |
|---|
| 160 | TestTaggerInterface.class.isAssignableFrom(TaggedFooString.class)); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | /** Test for value objects implements tag. */ |
|---|
| 164 | public void testImplementsTaggedValueObject () |
|---|
| 165 | { |
|---|
| 166 | assertTrue( |
|---|
| 167 | "TaggedSampleValueObject should implement Tagger interface", |
|---|
| 168 | TestTaggerInterface.class.isAssignableFrom( |
|---|
| 169 | TaggedSampleValueObject.class)); |
|---|
| 170 | assertTrue( |
|---|
| 171 | "TaggedPlainSampleValueObject should implement Tagger interface", |
|---|
| 172 | TestTaggerInterface.class.isAssignableFrom( |
|---|
| 173 | TaggedPlainSampleValueObject.class)); |
|---|
| 174 | assertTrue( |
|---|
| 175 | "TaggedSerializableSampleValueObject " + |
|---|
| 176 | "should implement Tagger interface", |
|---|
| 177 | TestTaggerInterface.class.isAssignableFrom( |
|---|
| 178 | TaggedSerializableSampleValueObject.class)); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|