Jak wymusić walidację Constraintów?

0

Próbuję przetestować czy działa walidacja argumentów konstruktora w Encji.
Zrobiłem chyba wszystko jak napisane w poradniku hibernate: //http://hibernate.org/validator/documentation/getting-started/

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.junit.Before;
import org.junit.Test;

public class AddressTest {

	private static Validator validator;

	@Before
	public void setUp() {
		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		validator = factory.getValidator();
	}

	@Test
	public void testAddressStringStringStringString() {
		// when
		Address address = new Address("Wielka WARSZAAAAAAWA", "Karabeli", "1/1", "000-000");
		Set<ConstraintViolation<Address>> violations = validator.validate(address);

		// then
		assertTrue(violations.isEmpty());
		assertEquals("000-000", address.getPostalCode());
	}
//http://hibernate.org/validator/documentation/getting-started/
}

.

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.Pattern;

import org.springframework.validation.annotation.Validated;

@Validated
@Entity
public class Address {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long id;
	
	private String city;
	private String street;
	private String houseNumberEtc;

	@Pattern(regexp = "\\d+{2}-\\d+{3}")
	private String postalCode;

	public Address() {

	}

	public Address(String city, String street, String houseNumberEtc, @Pattern(regexp = "\\d+{2}-\\d+{3}") String postalCode) {
		this.city = city;
		this.street = street;
		this.houseNumberEtc = houseNumberEtc;
		this.postalCode = postalCode;
	}
	
	public Long getId() {
		return id;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getHouseNumberEtc() {
		return houseNumberEtc;
	}

	public void setHouseNumberEtc(String houseNumberEtc) {
		this.houseNumberEtc = houseNumberEtc;
	}

	public String getPostalCode() {
		return postalCode;
	}

	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}

	@Override
	public String toString() {
		return "Address [" + city + street + houseNumberEtc + postalCode + "]";
	}
}

Test jednostkowy przechodzi a nie powinien, bo kod pocztowy jest zły.
Wywala się tylko jak próbuję zapisać adres z błędnym kodem do bazy.

Co robię źle?

1

nie znam tego paskudnego frameworka z adnotacjami ale na oko regexy sa zle, powinno raczej byc \\d{2}-\\d{3} albo jeszcze lepiej ^\\d{2}-\\d{3}$ zamiast tego co ty masz, albo rob {n} albo + a nie na raz bo to chyba nie ma sensu :)

0

dzięki teraz, działa...
to teraz mi powiedzcie czemu nie działa to:

@Validated
@Entity
public class Vehicle {

	@Id
	@Column(length = 17, unique = true, updatable = false)
	public final String vin; // Vehicle Identification Number

	@DecimalMin(value = "0.0")
	private int mileageKm;

	@DecimalMin(value = "0.0")
	private double capacityKg;

	private LocalDate purchaseDate;

	@OneToOne
	private Driver driver;

	protected Vehicle() {
		this.vin = null;
	}

	public Vehicle(@Length(min=17, max=17) String vin) {
		this.vin = vin;
	}

	// getteroza i setteroza
}

.

public class VehicleTest {
	
	private static Validator validator;

	@Before
	public void setUp() {
		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		validator = factory.getValidator();
	}
	
	
	@Test
	public void testVehicleInvalidVin() { 
		// given
		String invalidVin = "1";
		
		// when
		Vehicle vehicle = new Vehicle(invalidVin);
		Set<ConstraintViolation<Vehicle>> violations = validator.validate(vehicle);
		
		// then
		System.out.println(violations.isEmpty());
		assertTrue(violations.isEmpty());
		assertEquals("1", new Vehicle(invalidVin).getVin());
	}
}

test się powinien wywalić, bo zmienna ma długość == 1, a ma mieć 17...

0

ok ,zrobiłem tak:

@Validated
@Entity
public class Vehicle {

	@Id
	@Length(min=17, max=17) 
	@Column(length = 17, unique = true, updatable = false)
	public final String vin; // Vehicle Identification Number

i teraz działa. Nie czaję czemu wywołuje się tylko constrain ten z deklaracji zmiennej a ten z konstruktora nie, no ale na razie mniejsza o to, dzięki

0

Atrybut length reprezentuje długośc kolumny, więc jest maxem - podobnie jak ustawienie długości kolumny po stronie bazy danych.

    @Column(length = 17, unique = true, updatable = false)

1 użytkowników online, w tym zalogowanych: 0, gości: 1