You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import static org.junit.Assert.assertEquals;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.lang.reflect.InvocationTargetException;
  21. import java.net.URL;
  22. import java.net.URLClassLoader;
  23. import javax.validation.Validation;
  24. import org.apache.commons.io.IOUtils;
  25. import org.junit.Assert;
  26. import org.junit.Test;
  27. import com.vaadin.data.util.BeanUtil;
  28. import com.vaadin.tests.data.bean.BeanToValidate;
  29. import com.vaadin.ui.TextField;
  30. /**
  31. * @author Vaadin Ltd
  32. *
  33. */
  34. public class Jsr303Test {
  35. private static class TestClassLoader extends URLClassLoader {
  36. public TestClassLoader() {
  37. super(new URL[0], Thread.currentThread().getContextClassLoader());
  38. }
  39. @Override
  40. public Class<?> loadClass(String name) throws ClassNotFoundException {
  41. String vaadinPackagePrefix = getClass().getPackage().getName();
  42. vaadinPackagePrefix = vaadinPackagePrefix.substring(0,
  43. vaadinPackagePrefix.lastIndexOf('.'));
  44. if (name.equals(UnitTest.class.getName())) {
  45. super.loadClass(name);
  46. } else if (name
  47. .startsWith(Validation.class.getPackage().getName())) {
  48. throw new ClassNotFoundException();
  49. } else if (name.startsWith(vaadinPackagePrefix)) {
  50. String path = name.replace('.', '/').concat(".class");
  51. URL resource = Thread.currentThread().getContextClassLoader()
  52. .getResource(path);
  53. InputStream stream;
  54. try {
  55. stream = resource.openStream();
  56. byte[] bytes = IOUtils.toByteArray(stream);
  57. return defineClass(name, bytes, 0, bytes.length);
  58. } catch (IOException e) {
  59. throw new RuntimeException(e);
  60. }
  61. }
  62. return super.loadClass(name);
  63. }
  64. }
  65. public interface UnitTest {
  66. void execute();
  67. }
  68. public static class Jsr303UnitTest implements UnitTest {
  69. private final TextField nameField = new TextField();
  70. @Override
  71. public void execute() {
  72. Assert.assertFalse(BeanUtil.checkBeanValidationAvailable());
  73. Binder<BeanToValidate> binder = new Binder<>(BeanToValidate.class);
  74. BeanToValidate item = new BeanToValidate();
  75. String name = "Johannes";
  76. item.setFirstname(name);
  77. item.setAge(32);
  78. binder.bind(nameField, "firstname");
  79. binder.setBean(item);
  80. assertEquals(name, nameField.getValue());
  81. // BeanToValidate : @Size(min = 3, max = 16) for the firstName
  82. nameField.setValue("a");
  83. assertEquals(nameField.getValue(), item.getFirstname());
  84. }
  85. }
  86. @Test
  87. public void beanBinderWithoutJsr303() throws ClassNotFoundException,
  88. NoSuchMethodException, SecurityException, InstantiationException,
  89. IllegalAccessException, IllegalArgumentException,
  90. InvocationTargetException, IOException, InterruptedException {
  91. try (URLClassLoader loader = new TestClassLoader()) {
  92. Class<?> clazz = loader.loadClass(Jsr303UnitTest.class.getName());
  93. UnitTest test = (UnitTest) clazz.newInstance();
  94. test.execute();
  95. }
  96. }
  97. }