From dev-return-73395-archive-asf-public=cust-asf.ponee.io@zookeeper.apache.org Thu Sep 20 14:56:53 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id E1CF3180671 for ; Thu, 20 Sep 2018 14:56:52 +0200 (CEST) Received: (qmail 33886 invoked by uid 500); 20 Sep 2018 12:56:51 -0000 Mailing-List: contact dev-help@zookeeper.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zookeeper.apache.org Delivered-To: mailing list dev@zookeeper.apache.org Received: (qmail 33875 invoked by uid 99); 20 Sep 2018 12:56:51 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Sep 2018 12:56:51 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 1915AE0BEE; Thu, 20 Sep 2018 12:56:51 +0000 (UTC) From: eolivelli To: dev@zookeeper.apache.org Reply-To: dev@zookeeper.apache.org References: In-Reply-To: Subject: [GitHub] zookeeper pull request #630: ZOOKEEPER-2284:LogFormatter and SnapshotFormatt... Content-Type: text/plain Message-Id: <20180920125651.1915AE0BEE@git1-us-west.apache.org> Date: Thu, 20 Sep 2018 12:56:51 +0000 (UTC) Github user eolivelli commented on a diff in the pull request: https://github.com/apache/zookeeper/pull/630#discussion_r219151901 --- Diff: src/java/test/org/apache/zookeeper/ZKUtilTest.java --- @@ -0,0 +1,88 @@ +/** + * 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.zookeeper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assume.assumeTrue; + +import java.io.File; +import java.io.IOException; +import java.util.UUID; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class ZKUtilTest { + private static final File testData = new File(System.getProperty("test.data.dir", "build/test/data")); + + @BeforeClass + public static void init() { + testData.mkdirs(); + } + + @Test + public void testValidateFileInput() throws IOException { + File file = File.createTempFile("test", ".junit", testData); + file.deleteOnExit(); + String absolutePath = file.getAbsolutePath(); + String error = ZKUtil.validateFileInput(absolutePath); + assertNull(error); + } + + @Test + public void testValidateFileInputNotExist() { + String fileName = UUID.randomUUID().toString(); + File file = new File(testData, fileName); + String absolutePath = file.getAbsolutePath(); + String error = ZKUtil.validateFileInput(absolutePath); + assertNotNull(error); + String expectedMessage = "File '" + absolutePath + "' does not exist."; + assertEquals(expectedMessage, error); + } + + @Test + public void testValidateFileInputDirectory() throws Exception { + File file = File.createTempFile("test", ".junit", testData); + file.deleteOnExit(); + // delete file, as we need directory not file + file.delete(); + file.mkdir(); + String absolutePath = file.getAbsolutePath(); + String error = ZKUtil.validateFileInput(absolutePath); + assertNotNull(error); + String expectedMessage = "'" + absolutePath + "' is a direcory. it must be a file."; + assertEquals(expectedMessage, error); + } + + @Test + public void testUnreadableFileInput() throws Exception { + //skip this test on Windows, coverage on Linux + assumeTrue(!org.apache.zookeeper.Shell.WINDOWS); + File file = File.createTempFile("test", ".junit", testData); + file.setReadable(false, false); + file.deleteOnExit(); --- End diff -- Personally I don't like File#deleteOnExit because it is not clear when it will take effect, and it is clearer to have an explicit file.delete when the file is not needed anymore. In this case, for this simple test, I have no strong objection. The best solution would be to use JUnit TemporaryFolder Rule. ---