This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 7.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit bf074ae239b15594ace6671668c9d797536cffe8 Author: govi20 <govindasakhar...@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 9263bdf..87760c2 100644 --- a/java/org/apache/catalina/session/FileStore.java +++ b/java/org/apache/catalina/session/FileStore.java @@ -138,17 +138,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++; } } @@ -183,12 +183,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)) { @@ -198,9 +198,9 @@ public final class FileStore extends StoreBase { // Build and return the list of session identifiers List<String> list = new ArrayList<String>(); 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()]); @@ -221,11 +221,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-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org