DATACOUCH-71 - Add support for JSR 303 validation.

This changeset adds validation support for entities, and since this depends
on events, the event functionality has been added as well. Now also generic
event listeners can be attached.
This commit is contained in:
Michael Nitschinger
2014-03-10 13:07:05 +01:00
parent 5a176a5b68
commit bef4d52ace
17 changed files with 724 additions and 7 deletions

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.core.mapping.event;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
/**
* @author Michael Nitschinger
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = EventContextConfiguration.class)
public class AbstractCouchbaseEventListenerTests {
@Autowired
private CouchbaseTemplate couchbaseTemplate;
@Autowired
private SimpleMappingEventListener eventListener;
@Test
public void shouldEmitEvents() {
assertEquals(0, eventListener.onBeforeSaveEvents.size());
assertEquals(0, eventListener.onAfterSaveEvents.size());
assertEquals(0, eventListener.onBeforeConvertEvents.size());
couchbaseTemplate.save(new User("john smith", 18));
assertEquals(1, eventListener.onBeforeSaveEvents.size());
assertEquals(1, eventListener.onAfterSaveEvents.size());
assertEquals(1, eventListener.onBeforeConvertEvents.size());
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.core.mapping.event;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.TestApplicationConfig;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
/**
* @author Michael Nitschinger
*/
@Configuration
public class EventContextConfiguration extends TestApplicationConfig {
@Bean
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
}
@Bean
public ValidatingCouchbaseEventListener validatingCouchbaseEventListener() {
return new ValidatingCouchbaseEventListener(validator());
}
@Bean
public SimpleMappingEventListener simpleMappingEventListener() {
return new SimpleMappingEventListener();
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.core.mapping.event;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import java.util.ArrayList;
/**
* @author Michael Nitschinger
*/
public class SimpleMappingEventListener extends AbstractCouchbaseEventListener<Object> {
public final ArrayList<BeforeConvertEvent<Object>> onBeforeConvertEvents = new ArrayList<BeforeConvertEvent<Object>>();
public final ArrayList<BeforeSaveEvent<Object>> onBeforeSaveEvents = new ArrayList<BeforeSaveEvent<Object>>();
public final ArrayList<AfterSaveEvent<Object>> onAfterSaveEvents = new ArrayList<AfterSaveEvent<Object>>();
@Override
public void onBeforeConvert(Object source) {
onBeforeConvertEvents.add(new BeforeConvertEvent<Object>(source));
}
@Override
public void onBeforeSave(Object source, CouchbaseDocument doc) {
onBeforeSaveEvents.add(new BeforeSaveEvent<Object>(source, doc));
}
@Override
public void onAfterSave(Object source, CouchbaseDocument doc) {
onAfterSaveEvents.add(new AfterSaveEvent<Object>(source, doc));
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.core.mapping.event;
import org.springframework.data.annotation.Id;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
/**
* @author Michael Nitschinger
*/
public class User {
@Id
private String id;
@Size(min = 10)
private String name;
@Min(18)
private Integer age;
public User(String name, Integer age) {
id = "id";
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.couchbase.core.mapping.event;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.validation.ConstraintViolationException;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
/**
* @author Michael Nitschinger
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = EventContextConfiguration.class)
public class ValidatingCouchbaseEventListenerTests {
@Autowired
CouchbaseTemplate template;
@Test
public void shouldThrowConstraintViolationException() {
User user = new User("john", 17);
try {
template.save(user);
fail();
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations().size(), equalTo(2));
}
}
@Test
public void shouldNotThrowAnyExceptions() {
template.save(new User("john smith", 18));
}
}