PageRenderTime 667ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/bval-parent-0.4/bval-guice/src/test/java/org/apache/bval/guice/GuiceAwareValidationTestCase.java

#
Java | 95 lines | 56 code | 17 blank | 22 comment | 0 complexity | 5b021c35fa4ab49e998691c1de65395e MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.bval.guice;
  18. import java.util.Set;
  19. import javax.inject.Inject;
  20. import javax.validation.ConstraintViolation;
  21. import javax.validation.ConstraintViolationException;
  22. import javax.validation.Validator;
  23. import junit.framework.TestCase;
  24. import com.google.inject.Guice;
  25. /**
  26. *
  27. *
  28. * @version $Id: GuiceAwareValidationTestCase.java 1074156 2011-02-24 14:11:34Z simonetripodi $
  29. */
  30. public final class GuiceAwareValidationTestCase extends TestCase {
  31. @Inject
  32. private Validator validator;
  33. @Inject
  34. private DummyCountryDao dummyCountryDao;
  35. public void setValidator(Validator validator) {
  36. this.validator = validator;
  37. }
  38. public void setDummyCountryDao(DummyCountryDao dummyCountryDao) {
  39. this.dummyCountryDao = dummyCountryDao;
  40. }
  41. @Override
  42. protected void setUp() throws Exception {
  43. Guice.createInjector(new ValidationModule()).injectMembers(this);
  44. }
  45. @Override
  46. protected void tearDown() throws Exception {
  47. this.validator = null;
  48. this.dummyCountryDao = null;
  49. }
  50. public void testInjectedValidation() {
  51. Country country = new Country();
  52. country.setName("Italy");
  53. country.setIso2Code("it");
  54. country.setIso3Code("ita");
  55. Set<ConstraintViolation<Country>> violations = this.validator.validate(country);
  56. assertTrue(violations.isEmpty());
  57. }
  58. public void testAOPInjectedValidation() {
  59. this.dummyCountryDao.insertCountry("Italy", "it", "ita");
  60. }
  61. public void testAOPInjectedFailedValidation() {
  62. try {
  63. this.dummyCountryDao.insertCountry("Italy", "ita", "ita");
  64. fail("javax.validation.ConstraintViolationException expected");
  65. } catch (ConstraintViolationException cve) {
  66. // do nothing
  67. }
  68. }
  69. public void testRethrowWrappedException() {
  70. try {
  71. this.dummyCountryDao.updateCountry(new Country());
  72. fail("org.apache.bval.guice.DummyException expected");
  73. } catch (Exception e) {
  74. assertEquals(DummyException.class, e.getClass());
  75. assertTrue(e.getMessage().startsWith("This is just a dummy message "));
  76. }
  77. }
  78. }