]> source.dussan.org Git - vaadin-framework.git/commitdiff
Read input stream logic is corrected (#14533).
authorDenis Anisimov <denis@vaadin.com>
Fri, 26 Sep 2014 17:44:32 +0000 (20:44 +0300)
committerVaadin Code Review <review@vaadin.com>
Tue, 30 Sep 2014 07:26:55 +0000 (07:26 +0000)
Change-Id: I1a8a895d631889e04f7acbde29306e86da344a23

server/src/com/vaadin/ui/CustomLayout.java
server/tests/src/com/vaadin/tests/server/component/customlayout/CustomLayoutTest.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/combobox/ComboBoxInputPromptTest.java

index 7f1aa1ce4670c5f9806e7a9a2f0d350b9c0a0ed9..f4fe7fa66c9be739136f6926341d31ccc2a0fd60 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.vaadin.ui;
 
+import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -101,22 +102,20 @@ public class CustomLayout extends AbstractLayout implements LegacyComponent {
 
     protected void initTemplateContentsFromInputStream(
             InputStream templateStream) throws IOException {
-        InputStreamReader reader = new InputStreamReader(templateStream,
-                "UTF-8");
-        StringBuilder b = new StringBuilder(BUFFER_SIZE);
-
-        char[] cbuf = new char[BUFFER_SIZE];
-        int offset = 0;
-
-        while (true) {
-            int nrRead = reader.read(cbuf, offset, BUFFER_SIZE);
-            b.append(cbuf, 0, nrRead);
-            if (nrRead < BUFFER_SIZE) {
-                break;
+        BufferedReader reader = new BufferedReader(new InputStreamReader(
+                templateStream, "UTF-8"));
+        StringBuilder builder = new StringBuilder(BUFFER_SIZE);
+        try {
+            char[] cbuf = new char[BUFFER_SIZE];
+            int nRead;
+            while ((nRead = reader.read(cbuf, 0, BUFFER_SIZE)) > 0) {
+                builder.append(cbuf, 0, nRead);
             }
+        } finally {
+            reader.close();
         }
 
-        setTemplateContents(b.toString());
+        setTemplateContents(builder.toString());
     }
 
     @Override
diff --git a/server/tests/src/com/vaadin/tests/server/component/customlayout/CustomLayoutTest.java b/server/tests/src/com/vaadin/tests/server/component/customlayout/CustomLayoutTest.java
new file mode 100644 (file)
index 0000000..4d327e7
--- /dev/null
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * 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 com.vaadin.tests.server.component.customlayout;
+
+import java.io.ByteArrayInputStream;
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.ui.CustomLayout;
+
+/**
+ * 
+ * Tests for {@link CustomLayout}
+ * 
+ * @author Vaadin Ltd
+ */
+public class CustomLayoutTest {
+
+    @Test
+    public void ctor_inputStreamProvided_inputStreamIsRead()
+            throws IOException, IllegalArgumentException,
+            IllegalAccessException {
+        Integer buffer = getBufferSize();
+        StringBuilder builder = new StringBuilder();
+        for (int i = 0; i < buffer; i++) {
+            builder.append('a');
+        }
+        byte[] bytes = builder.toString().getBytes(Charset.forName("UTF-8"));
+        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
+        InputStreamImpl stream = new InputStreamImpl(inputStream, buffer / 2);
+        new CustomLayout(stream);
+
+        Assert.assertTrue("Stream is not closed in CustomLayout CTOR ",
+                stream.isClosed());
+        Assert.assertEquals("Number of read bytes is incorrect", bytes.length,
+                stream.getCount());
+    }
+
+    private Integer getBufferSize() throws IllegalAccessException {
+        Field[] fields = CustomLayout.class.getDeclaredFields();
+        List<Field> list = new ArrayList<Field>(fields.length);
+        for (Field field : fields) {
+            if ((field.getModifiers() & Modifier.STATIC) > 0) {
+                list.add(field);
+            }
+        }
+        Field field = null;
+        if (list.size() == 1) {
+            field = list.get(0);
+        } else {
+            for (Field fld : list) {
+                if (fld.getName().toLowerCase(Locale.ENGLISH)
+                        .startsWith("buffer")) {
+                    field = fld;
+                    break;
+                }
+            }
+        }
+        Assert.assertNotNull(
+                "Unable to find default buffer size in CustomLayout class",
+                field);
+        field.setAccessible(true);
+        Integer buffer = (Integer) field.get(null);
+        return buffer;
+    }
+
+    private static class InputStreamImpl extends FilterInputStream {
+
+        InputStreamImpl(InputStream inputStream, int maxArrayLength) {
+            super(inputStream);
+            this.maxArrayLength = maxArrayLength;
+        }
+
+        @Override
+        public int read() throws IOException {
+            int read = super.read();
+            if (read != -1) {
+                readCount++;
+            }
+            return read;
+        }
+
+        @Override
+        public int read(byte[] b) throws IOException {
+            if (b.length > maxArrayLength) {
+                return read(b, 0, maxArrayLength);
+            }
+            int count = super.read(b);
+            if (count != -1) {
+                readCount += count;
+            }
+            return count;
+        }
+
+        @Override
+        public int read(byte[] b, int off, int len) throws IOException {
+            if (len > maxArrayLength) {
+                return read(b, off, maxArrayLength);
+            }
+            int count = super.read(b, off, len);
+            if (count != -1) {
+                readCount += count;
+            }
+            return count;
+        }
+
+        @Override
+        public void close() throws IOException {
+            isClosed = true;
+            super.close();
+        }
+
+        int getCount() {
+            return readCount;
+        }
+
+        boolean isClosed() {
+            return isClosed;
+        }
+
+        private int readCount;
+        private boolean isClosed;
+        private int maxArrayLength;
+    }
+}
index cbd83c57344095f77c7daec7f719b94a6208de88..1e6f7e417056fd769d26223e8600b6669b28fe68 100644 (file)
@@ -18,16 +18,13 @@ package com.vaadin.tests.components.combobox;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.isEmptyString;
-import static org.junit.Assert.assertEquals;
 
-import com.vaadin.testbench.elements.ButtonElement;
-import com.vaadin.testbench.elements.ComboBoxElement;
-import com.vaadin.testbench.elements.TextFieldElement;
-import com.vaadin.tests.tb3.AbstractTB3Test;
 import org.junit.Test;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebElement;
 
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.testbench.elements.ComboBoxElement;
 import com.vaadin.tests.tb3.MultiBrowserTest;
 
 public class ComboBoxInputPromptTest extends MultiBrowserTest {