You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2019/12/05 12:56:36 UTC

[tomcat] 01/02: PR #227 Unit test FileStore utility methods

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit d2c22d7d1c49928a97e2caec695fb9357726d391
Author: govi20 <go...@gmail.com>
AuthorDate: Sun Dec 1 17:45:24 2019 +0530

    PR #227 Unit test FileStore utility methods
---
 java/org/apache/catalina/session/FileStore.java    | 28 +++----
 .../org/apache/catalina/session/FileStoreTest.java | 95 ++++++++++++++++++++++
 2 files changed, 107 insertions(+), 16 deletions(-)

diff --git a/java/org/apache/catalina/session/FileStore.java b/java/org/apache/catalina/session/FileStore.java
index a1916dc..48d464e 100644
--- a/java/org/apache/catalina/session/FileStore.java
+++ b/java/org/apache/catalina/session/FileStore.java
@@ -127,17 +127,17 @@ public final class FileStore extends StoreBase {
     @Override
     public int getSize() throws IOException {
         // Acquire the list of files in our storage directory
-        File file = directory();
-        if (file == null) {
+        File dir = directory();
+        if (dir == null) {
             return 0;
         }
-        String files[] = file.list();
+        String files[] = dir.list();
 
         // Figure out which files are sessions
         int keycount = 0;
         if (files != null) {
-            for (int i = 0; i < files.length; i++) {
-                if (files[i].endsWith(FILE_EXT)) {
+            for (String file : files) {
+                if (file.endsWith(FILE_EXT)) {
                     keycount++;
                 }
             }
@@ -172,12 +172,12 @@ public final class FileStore extends StoreBase {
     @Override
     public String[] keys() throws IOException {
         // Acquire the list of files in our storage directory
-        File file = directory();
-        if (file == null) {
+        File dir = directory();
+        if (dir == null) {
             return new String[0];
         }
 
-        String files[] = file.list();
+        String files[] = dir.list();
 
         // Bugzilla 32130
         if((files == null) || (files.length < 1)) {
@@ -187,9 +187,9 @@ public final class FileStore extends StoreBase {
         // Build and return the list of session identifiers
         List<String> list = new ArrayList<>();
         int n = FILE_EXT.length();
-        for (int i = 0; i < files.length; i++) {
-            if (files[i].endsWith(FILE_EXT)) {
-                list.add(files[i].substring(0, files[i].length() - n));
+        for (String file : files) {
+            if (file.endsWith(FILE_EXT)) {
+                list.add (file.substring(0, file.length() - n));
             }
         }
         return list.toArray(new String[list.size()]);
@@ -210,11 +210,7 @@ public final class FileStore extends StoreBase {
     public Session load(String id) throws ClassNotFoundException, IOException {
         // Open an input stream to the specified pathname, if any
         File file = file(id);
-        if (file == null) {
-            return null;
-        }
-
-        if (!file.exists()) {
+        if (file == null || !file.exists()) {
             return null;
         }
 
diff --git a/test/org/apache/catalina/session/FileStoreTest.java b/test/org/apache/catalina/session/FileStoreTest.java
new file mode 100644
index 0000000..330c4a0
--- /dev/null
+++ b/test/org/apache/catalina/session/FileStoreTest.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.catalina.session;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.catalina.Manager;
+import org.apache.tomcat.unittest.TesterContext;
+import org.apache.tomcat.unittest.TesterServletContext;
+import org.apache.tomcat.util.http.fileupload.FileUtils;
+import org.junit.*;
+
+/**
+ * Test utility methods of FileStore class
+ *
+ * @author Govinda Sakhare
+ */
+public class FileStoreTest {
+
+    private static final String SESS_TEMPPATH = "SESS_TEMP";
+    private static final File dir = new File(SESS_TEMPPATH);
+    private static FileStore fileStore;
+    private static File file1 = new File(SESS_TEMPPATH + "/tmp1.session");
+    private static File file2 = new File(SESS_TEMPPATH + "/tmp2.session");
+    private static Manager manager = new StandardManager();
+
+
+    @BeforeClass
+    public static void setup() throws IOException {
+        TesterContext testerContext = new TesterContext();
+        testerContext.setServletContext(new TesterServletContext());
+        manager.setContext(testerContext);
+        fileStore = new FileStore();
+        fileStore.setManager(manager);
+    }
+
+
+    @AfterClass
+    public static void cleanup() throws IOException {
+        FileUtils.cleanDirectory(dir);
+        FileUtils.deleteDirectory(dir);
+    }
+
+
+    @Before
+    public void beforeEachTest() throws IOException {
+        fileStore.setDirectory(SESS_TEMPPATH);
+        dir.mkdir();
+        file1.createNewFile();
+        file2.createNewFile();
+    }
+
+
+    @Test
+    public void getSize() throws Exception {
+        Assert.assertEquals(2, fileStore.getSize());
+    }
+
+
+    @Test
+    public void clear() throws Exception {
+        fileStore.clear();
+        Assert.assertEquals(0, fileStore.getSize());
+    }
+
+
+    @Test
+    public void keys() throws Exception {
+        Assert.assertArrayEquals(new String[]{"tmp1", "tmp2"}, fileStore.keys());
+        fileStore.clear();
+        Assert.assertArrayEquals(new String[]{}, fileStore.keys());
+    }
+
+
+    @Test
+    public void removeTest() throws Exception {
+        fileStore.remove("tmp1");
+        Assert.assertEquals(1, fileStore.getSize());
+    }
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org