|
| 1 | +<%@ page import="org.labkey.api.data.JdbcType" %> |
| 2 | +<%@ page import="org.junit.Test" %> |
| 3 | +<%@ page import="static org.junit.Assert.*" %> |
| 4 | +<%@ page import="org.labkey.api.data.BaseColumnInfo" %> |
| 5 | +<%@ page import="org.jetbrains.annotations.NotNull" %> |
| 6 | +<%@ page import="java.math.BigDecimal" %> |
| 7 | +<%@ page import="org.labkey.api.exp.PropertyType" %> |
| 8 | +<%@ page import="org.labkey.api.ontology.Quantity" %> |
| 9 | +<%@ page import="org.labkey.api.ontology.Unit" %> |
| 10 | +<%@ page import="org.labkey.api.ontology.KindOfQuantity" %> |
| 11 | +<%@ page import="org.labkey.api.data.MutableColumnInfo" %> |
| 12 | +<%@ page import="org.labkey.api.data.WrappedColumnInfo" %> |
| 13 | +<%@ page import="org.apache.commons.beanutils.ConversionException" %> |
| 14 | +<%@ page import="org.labkey.api.data.ColumnInfo" %> |
| 15 | +<%@ page import="org.labkey.api.data.dialect.SqlDialect" %> |
| 16 | +<%@ page import="org.labkey.api.data.CoreSchema" %> |
| 17 | +<%@ page import="java.nio.ByteBuffer" %> |
| 18 | +<%@ page extends="org.labkey.api.jsp.JspTest.BVT" %> |
| 19 | +<%-- |
| 20 | +This tests uses MockRequest to test some expected Headers and Meta tags for various types of requests. |
| 21 | +--%> |
| 22 | +<%! |
| 23 | + void testConvert(ColumnInfo col, Object expected, Object val) |
| 24 | + { |
| 25 | + var result = col.convert(val); |
| 26 | + assertNotNull(result); |
| 27 | + //assertEquals(col.getJdbcType().getJavaClass(), result.getClass()); |
| 28 | + assertEquals(expected, result); |
| 29 | + } |
| 30 | +
|
| 31 | + void testConvertsToNull(ColumnInfo col, Object val) |
| 32 | + { |
| 33 | + var result = col.convert(val); |
| 34 | + assertNull(result); |
| 35 | + } |
| 36 | +
|
| 37 | + void testConversionException(ColumnInfo col, Object val) |
| 38 | + { |
| 39 | + try |
| 40 | + { |
| 41 | + col.convert(val); |
| 42 | + fail(); |
| 43 | + } |
| 44 | + catch (ConversionException x) |
| 45 | + { |
| 46 | + return; |
| 47 | + } |
| 48 | + } |
| 49 | +
|
| 50 | + void testConvert(JdbcType type, Object expected, Object val) |
| 51 | + { |
| 52 | + var col = new BaseColumnInfo("~", null, type); |
| 53 | + testConvert(col.lock(), expected, val); |
| 54 | + } |
| 55 | +
|
| 56 | + void testConvertsToNull(JdbcType type, Object val) |
| 57 | + { |
| 58 | + var col = new BaseColumnInfo("~", null, type); |
| 59 | + testConvertsToNull(col.lock(), val); |
| 60 | + } |
| 61 | +
|
| 62 | + void testConversionException(JdbcType type, Object val) |
| 63 | + { |
| 64 | + var col = new BaseColumnInfo("~", null, type); |
| 65 | + testConversionException(col.lock(), val); |
| 66 | + } |
| 67 | +
|
| 68 | + void testConvert(PropertyType pt, Object expected, @NotNull Object val) |
| 69 | + { |
| 70 | + var col = new BaseColumnInfo("~", null, pt.getJdbcType()); |
| 71 | + col.setPropertyType(pt); |
| 72 | + testConvert(col.lock(), expected, val); |
| 73 | + } |
| 74 | +
|
| 75 | + void testConvertsToNull(PropertyType pt, Object val) |
| 76 | + { |
| 77 | + var col = new BaseColumnInfo("~", null, pt.getJdbcType()); |
| 78 | + col.setPropertyType(pt); |
| 79 | + testConvertsToNull(col.lock(), val); |
| 80 | + } |
| 81 | +
|
| 82 | + void testConversionException(PropertyType pt, Object val) |
| 83 | + { |
| 84 | + var col = new BaseColumnInfo("~", null, pt.getJdbcType()); |
| 85 | + col.setPropertyType(pt); |
| 86 | + testConversionException(col.lock(), val); |
| 87 | + } |
| 88 | +
|
| 89 | + void testQuantity(Unit displayUnit, Quantity expected, Object value) |
| 90 | + { |
| 91 | + // UNDONE: setDisplayUnit is NYI??? |
| 92 | + var col = new BaseColumnInfo("~", null, JdbcType.DOUBLE) |
| 93 | + { |
| 94 | + @Override |
| 95 | + public Unit getDisplayUnit() |
| 96 | + { |
| 97 | + return displayUnit; |
| 98 | + } |
| 99 | +
|
| 100 | + @Override |
| 101 | + public KindOfQuantity getKindOfQuantity() |
| 102 | + { |
| 103 | + return null==displayUnit ? null : displayUnit.getKindOfQuantity(); |
| 104 | + } |
| 105 | + }; |
| 106 | + testConvert(col.lock(), expected, value); |
| 107 | + } |
| 108 | +
|
| 109 | +
|
| 110 | + /** This test is for the integrated ColumnInfo.convert() logic. |
| 111 | + * <p></p> |
| 112 | + * A lot of this testing is redundant with lower-level unit testing, |
| 113 | + * however, this still serves as a basic conversion smoke test. |
| 114 | + * <p></p> |
| 115 | + * In particular, the PropertyType conversions are pretty |
| 116 | + * redundant with ConvertHelper.convert() and JdbcType.convert() |
| 117 | + * (PropertyType predates JdbcType), but there are some differences |
| 118 | + * in implementation. We should try to reconcile these differences. |
| 119 | + */ |
| 120 | + @Test |
| 121 | + public void testColumnConvert() throws Exception |
| 122 | + { |
| 123 | + // see also ConvertHelper.testEmpty() |
| 124 | +
|
| 125 | + // w/o propertyType |
| 126 | + for (JdbcType type : JdbcType.values()) |
| 127 | + { |
| 128 | + switch (type) |
| 129 | + { |
| 130 | + case BIGINT -> |
| 131 | + { |
| 132 | + testConvert(type, Long.valueOf(5), Integer.valueOf(5)); |
| 133 | + testConvert(type, Long.valueOf(5), "5"); |
| 134 | + testConvert(type, Long.valueOf(5), Double.valueOf(5.00000)); |
| 135 | + testConversionException(type, Double.valueOf(5.00001)); |
| 136 | + testConvert(type, Long.valueOf(5), new BigDecimal("5.000")); |
| 137 | + testConversionException(type, new BigDecimal("5.001")); |
| 138 | + testConversionException(type, "5g"); |
| 139 | + testConvertsToNull(type, ""); |
| 140 | + testConvertsToNull(type, null); |
| 141 | + } |
| 142 | + case BINARY, LONGVARBINARY, VARBINARY -> |
| 143 | + { |
| 144 | + } |
| 145 | + case BOOLEAN -> {} |
| 146 | + case CHAR,LONGVARCHAR,VARCHAR -> |
| 147 | + { |
| 148 | + testConvertsToNull(type, null); |
| 149 | + // NOTE StandardDataIterator optionally trims, but convert() does not. |
| 150 | + // see SimpleTranslator.createConvertColumn() |
| 151 | + testConvert(type, " no trim ", " no trim "); |
| 152 | + // JdbcType does not convert empty string to null, ColumnInfo.convert() and PropertyType.conver() do |
| 153 | + assertNull("", type.convert("")); |
| 154 | + testConvertsToNull(type, ""); |
| 155 | + testConvertsToNull(type, null); |
| 156 | + } |
| 157 | + case DECIMAL -> {} |
| 158 | + case DOUBLE -> {} |
| 159 | + case INTEGER -> {} |
| 160 | + case REAL -> {} |
| 161 | + case SMALLINT, TINYINT -> {} |
| 162 | + case DATE -> {} |
| 163 | + case TIME -> {} |
| 164 | + case TIMESTAMP -> {} |
| 165 | + case GUID -> {} |
| 166 | + case ARRAY, NULL, OTHER -> { /* ignore */ } |
| 167 | + default -> fail("We missed a JdbcType: " + type.name()); |
| 168 | + } |
| 169 | + } |
| 170 | +
|
| 171 | + // testArray() |
| 172 | +
|
| 173 | + // w/ propertyType |
| 174 | + for (var type : PropertyType.values()) |
| 175 | + { |
| 176 | + switch (type) |
| 177 | + { |
| 178 | + case BOOLEAN -> {} |
| 179 | + case STRING -> |
| 180 | + { |
| 181 | + testConvertsToNull(type, null); |
| 182 | + testConvertsToNull(type, ""); |
| 183 | + testConvert(type, " no trim ", " no trim "); |
| 184 | + } |
| 185 | + case MULTI_LINE -> |
| 186 | + { |
| 187 | + testConvertsToNull(type, null); |
| 188 | + testConvertsToNull(type, ""); |
| 189 | + testConvert(type, " no trim ", " no trim "); |
| 190 | + } |
| 191 | + case MULTI_CHOICE -> {} |
| 192 | + case RESOURCE -> {} |
| 193 | + case INTEGER -> {} |
| 194 | + case BIGINT -> |
| 195 | + { |
| 196 | + testConvert(type, Long.valueOf(5), Integer.valueOf(5)); |
| 197 | + testConvert(type, Long.valueOf(5), "5"); |
| 198 | + testConversionException(type, new BigDecimal("5.001")); |
| 199 | + testConvertsToNull(type, null); |
| 200 | + testConvertsToNull(type, ""); |
| 201 | + testConversionException(type, "5g"); |
| 202 | + } |
| 203 | + case BINARY -> {} |
| 204 | + case FILE_LINK -> {} |
| 205 | + case ATTACHMENT -> {} |
| 206 | + case DATE_TIME -> {} |
| 207 | + case DATE -> {} |
| 208 | + case TIME -> {} |
| 209 | + case DOUBLE -> {} |
| 210 | + case FLOAT -> {} |
| 211 | + case DECIMAL -> {} |
| 212 | + case XML_TEXT -> {} |
| 213 | + default -> fail("We missed a PropertyType: " + type.name()); |
| 214 | + } |
| 215 | + } |
| 216 | +
|
| 217 | + // Quantity |
| 218 | + Unit unit = Unit.kg; |
| 219 | + testQuantity(unit, Quantity.of(5000,unit.getBase()), "5"); |
| 220 | + } |
| 221 | +
|
| 222 | +
|
| 223 | + public void testLocked(MutableColumnInfo col) |
| 224 | + { |
| 225 | + col.setAlias("!"); |
| 226 | + col.lock(); |
| 227 | + try |
| 228 | + { |
| 229 | + col.setAlias("!"); |
| 230 | + fail("not locked?"); |
| 231 | + } |
| 232 | + catch (IllegalStateException x) |
| 233 | + { |
| 234 | + // success |
| 235 | + } |
| 236 | + } |
| 237 | +
|
| 238 | + static class _ColumnInfo extends BaseColumnInfo |
| 239 | + { |
| 240 | + _ColumnInfo() |
| 241 | + { |
| 242 | + super("~", JdbcType.INTEGER); |
| 243 | + } |
| 244 | +
|
| 245 | + @Override |
| 246 | + public SqlDialect getSqlDialect() |
| 247 | + { |
| 248 | + return CoreSchema.getInstance().getSqlDialect(); |
| 249 | + } |
| 250 | + } |
| 251 | +
|
| 252 | + @Test |
| 253 | + public void testLocked() |
| 254 | + { |
| 255 | + testLocked(new _ColumnInfo()); |
| 256 | + testLocked(WrappedColumnInfo.wrap(new _ColumnInfo())); |
| 257 | + } |
| 258 | +%> |
0 commit comments