|
| 1 | +/* |
| 2 | + * Copyright 2000-2022 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.flow.component.menubar.tests; |
| 17 | + |
| 18 | +import com.vaadin.flow.component.UI; |
| 19 | +import com.vaadin.flow.component.html.Div; |
| 20 | +import com.vaadin.flow.component.html.NativeButton; |
| 21 | +import com.vaadin.flow.component.html.Span; |
| 22 | +import com.vaadin.flow.component.menubar.MenuBar; |
| 23 | +import com.vaadin.flow.router.Route; |
| 24 | + |
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.ObjectInputStream; |
| 29 | +import java.io.ObjectOutputStream; |
| 30 | + |
| 31 | +@Route("vaadin-menu-bar/serialization") |
| 32 | +public class MenuBarSerializationPage extends Div { |
| 33 | + |
| 34 | + private MenuBar menuBar; |
| 35 | + private NativeButton serializeAndDeserializeUiButton; |
| 36 | + private Span exceptionMessageSpan; |
| 37 | + |
| 38 | + public MenuBarSerializationPage() { |
| 39 | + menuBar = new MenuBar(); |
| 40 | + menuBar.addItem("Menu item"); |
| 41 | + |
| 42 | + serializeAndDeserializeUiButton = new NativeButton( |
| 43 | + "Serialize&Deserialize UI"); |
| 44 | + serializeAndDeserializeUiButton |
| 45 | + .addClickListener(event -> serializeAndDeserialize()); |
| 46 | + serializeAndDeserializeUiButton.setId("serialize-and-deserialize-ui"); |
| 47 | + |
| 48 | + exceptionMessageSpan = new Span(); |
| 49 | + exceptionMessageSpan.setId("exception-message-span"); |
| 50 | + |
| 51 | + add(serializeAndDeserializeUiButton, menuBar, exceptionMessageSpan); |
| 52 | + } |
| 53 | + |
| 54 | + private void serializeAndDeserialize() { |
| 55 | + ByteArrayOutputStream bs = new ByteArrayOutputStream(); |
| 56 | + try (ObjectOutputStream out = new ObjectOutputStream(bs)) { |
| 57 | + out.writeObject(UI.getCurrent()); |
| 58 | + } catch (IOException ex) { |
| 59 | + exceptionMessageSpan.setText(ex.getMessage()); |
| 60 | + } |
| 61 | + try (ObjectInputStream in = new ObjectInputStream( |
| 62 | + new ByteArrayInputStream(bs.toByteArray()))) { |
| 63 | + in.readObject(); |
| 64 | + } catch (IOException | ClassNotFoundException | ClassCastException ex) { |
| 65 | + exceptionMessageSpan.setText(ex.getMessage()); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments