Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

UIAccessExceptionHandling.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.vaadin.tests.components.ui;
  2. import java.util.Map;
  3. import java.util.concurrent.ExecutionException;
  4. import java.util.concurrent.Future;
  5. import com.vaadin.server.DefaultErrorHandler;
  6. import com.vaadin.server.ErrorHandler;
  7. import com.vaadin.server.ErrorHandlingRunnable;
  8. import com.vaadin.server.VaadinRequest;
  9. import com.vaadin.server.VaadinService;
  10. import com.vaadin.tests.components.AbstractTestUIWithLog;
  11. import com.vaadin.ui.Button;
  12. import com.vaadin.ui.UI;
  13. import com.vaadin.util.CurrentInstance;
  14. public class UIAccessExceptionHandling extends AbstractTestUIWithLog
  15. implements ErrorHandler {
  16. private Future<Void> future;
  17. @Override
  18. protected void setup(VaadinRequest request) {
  19. getSession().setErrorHandler(this);
  20. addComponent(
  21. new Button("Throw RuntimeException on UI.access", event -> {
  22. log.clear();
  23. // Ensure beforeClientResponse is invoked
  24. markAsDirty();
  25. future = access(() -> {
  26. throw new RuntimeException();
  27. });
  28. }));
  29. addComponent(new Button("Throw RuntimeException on Session.access",
  30. event -> {
  31. log.clear();
  32. // Ensure beforeClientResponse is invoked
  33. markAsDirty();
  34. VaadinService service = VaadinService.getCurrent();
  35. future = service.accessSession(getSession(), () -> {
  36. throw new RuntimeException();
  37. });
  38. }));
  39. addComponent(new Button(
  40. "Throw RuntimeException after removing instances", event -> {
  41. log.clear();
  42. // Ensure beforeClientResponse is invoked
  43. markAsDirty();
  44. assert UI.getCurrent() == UIAccessExceptionHandling.this;
  45. Map<Class<?>, CurrentInstance> instances = CurrentInstance
  46. .getInstances();
  47. CurrentInstance.clearAll();
  48. assert UI.getCurrent() == null;
  49. future = access(() -> {
  50. throw new RuntimeException();
  51. });
  52. CurrentInstance.restoreInstances(instances);
  53. }));
  54. addComponent(
  55. new Button("Throw through ErrorHandlingRunnable", event -> {
  56. access(new ErrorHandlingRunnable() {
  57. @Override
  58. public void run() {
  59. log.clear();
  60. throw new NullPointerException();
  61. }
  62. @Override
  63. public void handleError(Exception exception) {
  64. // "Handle" other exceptions, but leave NPE for
  65. // default handler
  66. if (exception instanceof NullPointerException) {
  67. NullPointerException npe = (NullPointerException) exception;
  68. throw npe;
  69. }
  70. }
  71. });
  72. }));
  73. addComponent(new Button("Clear", event -> log.clear()));
  74. }
  75. @Override
  76. public void beforeClientResponse(boolean initial) {
  77. if (future != null) {
  78. try {
  79. future.get();
  80. } catch (InterruptedException e) {
  81. e.printStackTrace();
  82. } catch (ExecutionException e) {
  83. log("Exception caught on get: " + e.getClass().getName());
  84. } finally {
  85. future = null;
  86. }
  87. }
  88. }
  89. @Override
  90. public void error(com.vaadin.server.ErrorEvent event) {
  91. log("Exception caught on execution with "
  92. + event.getClass().getSimpleName() + " : "
  93. + event.getThrowable().getClass().getName());
  94. DefaultErrorHandler.doDefault(event);
  95. }
  96. @Override
  97. protected String getTestDescription() {
  98. return "Test for handling exceptions in UI.access and Session.access";
  99. }
  100. @Override
  101. protected Integer getTicketNumber() {
  102. return Integer.valueOf(12703);
  103. }
  104. }