MaxLengthTextFieldTest.java| Commit | Message |
|---|---|
9ebb88522 | 2016-07-18 Initial commit |
PropertyModel<String> model = new PropertyModel<>(new PFUserDO(), "username");
assertInteger(255, MaxLengthTextField.getMaxLength(model)); // From @Column(length=255)
assertInteger(255, MaxLengthTextField.getMaxLength(model, 300)); // Override ignored (DB wins)
assertInteger(100, MaxLengthTextField.getMaxLength(model, 100)); // Manual override wins (100 < 255)
model = new PropertyModel<>(new BaseSearchFilter(), "searchString");
assertNull(MaxLengthTextField.getMaxLength(model)); // No @Column = no maxlength
assertInteger(100, MaxLengthTextField.getMaxLength(model, 100)); // Manual override
assertNull(MaxLengthTextField.getMaxLength(Model.of("test"))); // Plain model = no maxlength| # | Model | Manual arg | Expected | Why |
|---|---|---|---|---|
| 1 | PFUserDO.username | none | 255 | @Column(length=255) on the entity |
| 2 | PFUserDO.username | 300 | 255 | 300 > 255 — DB constraint wins, manual override ignored |
| 3 | PFUserDO.username | 100 | 100 | 100 < 255 — stricter manual limit wins |
| 4 | BaseSearchFilter.searchString | none | null | No @Column annotation — no limit |
| 5 | BaseSearchFilter.searchString | 100 | 100 | Manual limit on non-annotated field |
| 6 | Plain Model.of("test") | none | null | No entity backing — can't read annotations |
| 7 | Plain Model.of("test") | 100 | 100 | Manual limit on plain model |
assertInteger helper: Custom assertion that checks both non-null AND equals. Avoids the common pitfall of assertEquals(255, value) passing when value is null (autounboxing would throw NPE).
The algorithm under test:
getMaxLength(propertyModel, manualMaxLength)follows this logic:PropertyModel(e.g.,"username")@Column(length = N)annotation@Columnannotation exists, return null (no maxlength attribute on the HTML input)This prevents a common bug: users type 300 characters into a form field, submit, and the database truncates to 255. With HTML
maxlengthset from the JPA annotation, the browser prevents over-long input before submission.