You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kh...@apache.org on 2017/03/22 23:14:13 UTC
geode git commit: Split tests into two classes, one with a real cache,
one without.
Repository: geode
Updated Branches:
refs/heads/feature/GEODE-2420 fb3526ae5 -> 88eaa5b09
Split tests into two classes, one with a real cache, one without.
Project: http://git-wip-us.apache.org/repos/asf/geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/88eaa5b0
Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/88eaa5b0
Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/88eaa5b0
Branch: refs/heads/feature/GEODE-2420
Commit: 88eaa5b09ff0fbfc5957afc39980a49a296caa76
Parents: fb3526a
Author: Ken Howe <kh...@pivotal.io>
Authored: Wed Mar 22 16:13:13 2017 -0700
Committer: Ken Howe <kh...@pivotal.io>
Committed: Wed Mar 22 16:13:13 2017 -0700
----------------------------------------------------------------------
.../cli/functions/SizeExportLogsFunction.java | 89 +-------
.../SizeExportLogsFunctionCacheTest.java | 168 ++++++++++++++
.../SizeExportLogsFunctionFileTest.java | 176 +++++++++++++++
.../SizeExportLogsFunctionIntegrationTest.java | 218 -------------------
4 files changed, 353 insertions(+), 298 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/geode/blob/88eaa5b0/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunction.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunction.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunction.java
index dfd8f3c..3a09219 100644
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunction.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunction.java
@@ -15,6 +15,8 @@
package org.apache.geode.management.internal.cli.functions;
import org.apache.commons.lang.StringUtils;
+import org.apache.geode.cache.Cache;
+import org.apache.geode.cache.CacheFactory;
import org.apache.geode.cache.execute.Function;
import org.apache.geode.cache.execute.FunctionContext;
import org.apache.geode.distributed.DistributedMember;
@@ -35,6 +37,10 @@ public class SizeExportLogsFunction extends ExportLogsFunction implements Functi
private static final Logger LOGGER = LogService.getLogger();
private static final long serialVersionUID = 1L;
+ protected Cache getCache() {
+ return CacheFactory.getAnyInstance();
+ }
+
@Override
public void execute(final FunctionContext context) {
try {
@@ -44,22 +50,17 @@ public class SizeExportLogsFunction extends ExportLogsFunction implements Functi
long estimatedSize = estimateLogFileSize(cache.getMyId(), config.getLogFile(), config.getStatisticArchiveFile(), args);
- if (estimatedSize == -1) {
- context.getResultSender().lastResult(null);
- } else {
context.getResultSender().lastResult(Arrays.asList(new long[]{estimatedSize}));
- }
} catch (Exception e) {
- LOGGER.error(e);
+ e.printStackTrace();
+ LOGGER.error(e.getMessage());
context.getResultSender().sendException(e);
}
}
long estimateLogFileSize(final DistributedMember member, final File logFile, final File statArchive, final Args args)
throws ParseException, IOException {
- LOGGER.info("ExportLogsFunction started for member {}", member);
-
File baseLogFile = null;
File baseStatsFile = null;
@@ -75,80 +76,8 @@ public class SizeExportLogsFunction extends ExportLogsFunction implements Functi
long estimatedSize = new LogSizer(logFilter, baseLogFile, baseStatsFile).getFilteredSize();
- LOGGER.info("Estimated log file size: " + estimatedSize);
+ LOGGER.info("Estimated exported log file size: " + estimatedSize);
return estimatedSize;
}
-
-// @Override
-// public boolean isHA() {
-// return false;
-// }
-//
-// public static class Args implements Serializable {
-// private LocalDateTime startTime;
-// private LocalDateTime endTime;
-// private Level logLevel;
-// private boolean thisLogLevelOnly;
-// private boolean includeLogs;
-// private boolean includeStats;
-//
-// public Args(String startTime, String endTime, String logLevel, boolean logLevelOnly,
-// boolean logsOnly, boolean statsOnly) {
-// this.startTime = parseTime(startTime);
-// this.endTime = parseTime(endTime);
-//
-// if (StringUtils.isBlank(logLevel)) {
-// this.logLevel = Level.INFO;
-// } else {
-// this.logLevel = Level.getLevel(logLevel.toUpperCase());
-// }
-// this.thisLogLevelOnly = logLevelOnly;
-//
-// this.includeLogs = !statsOnly;
-// this.includeStats = !logsOnly;
-// }
-//
-// public LocalDateTime getStartTime() {
-// return startTime;
-// }
-//
-// public LocalDateTime getEndTime() {
-// return endTime;
-// }
-//
-// public Level getLogLevel() {
-// return logLevel;
-// }
-//
-// public boolean isThisLogLevelOnly() {
-// return thisLogLevelOnly;
-// }
-//
-// public boolean isIncludeLogs() {
-// return includeLogs;
-// }
-//
-// public boolean isIncludeStats() {
-// return includeStats;
-// }
-// }
-//
-// public static LocalDateTime parseTime(String dateString) {
-// if (dateString == null) {
-// return null;
-// }
-//
-// try {
-// SimpleDateFormat df = new SimpleDateFormat(ExportLogsCommand.FORMAT);
-// return df.parse(dateString).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
-// } catch (ParseException e) {
-// try {
-// SimpleDateFormat df = new SimpleDateFormat(ExportLogsCommand.ONLY_DATE_FORMAT);
-// return df.parse(dateString).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
-// } catch (ParseException e1) {
-// return null;
-// }
-// }
-// }
}
http://git-wip-us.apache.org/repos/asf/geode/blob/88eaa5b0/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunctionCacheTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunctionCacheTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunctionCacheTest.java
new file mode 100644
index 0000000..bcd6da7
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunctionCacheTest.java
@@ -0,0 +1,168 @@
+/*
+ * 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.geode.management.internal.cli.functions;
+
+import static org.assertj.core.api.Assertions.*;
+import static org.apache.geode.distributed.ConfigurationProperties.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.geode.cache.Cache;
+import org.apache.geode.cache.CacheFactory;
+import org.apache.geode.cache.execute.FunctionContext;
+import org.apache.geode.cache.execute.ResultSender;
+import org.apache.geode.distributed.DistributedMember;
+import org.apache.geode.internal.cache.execute.FunctionContextImpl;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.rules.TestName;
+
+import java.io.File;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Properties;
+
+public class SizeExportLogsFunctionCacheTest {
+
+ private Cache cache;
+
+ private SizeExportLogsFunction.Args nonFilteringArgs;
+ private TestResultSender resultSender;
+ private FunctionContext functionContext;
+ private File dir;
+ private DistributedMember member;
+ File logFile;
+ File statFile;
+ String name;
+
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+ @Rule
+ public TestName testName = new TestName();
+
+ @Before
+ public void before() throws Throwable {
+ name = testName.getMethodName();
+
+ this.dir = this.temporaryFolder.getRoot();
+ logFile = new File(dir, name + ".log");
+ statFile = new File(dir, name + ".gfs");
+
+ this.nonFilteringArgs = new ExportLogsFunction.Args(null, null, null, false,false, false);
+ functionContext = new FunctionContextImpl("functionId", nonFilteringArgs, resultSender);
+
+ }
+
+ @After
+ public void after() throws Exception {
+ if (this.cache != null) {
+ this.cache.close();
+ }
+ FileUtils.deleteDirectory(dir);
+ }
+
+ @Test
+ public void withFiles_returnsCombinedSizeResult() throws Throwable {
+ Properties config = new Properties();
+ config.setProperty(NAME, name);
+ config.setProperty(LOG_FILE, logFile.getAbsolutePath());
+ config.setProperty(STATISTIC_ARCHIVE_FILE, statFile.getAbsolutePath());
+
+ this.cache = new CacheFactory(config).create();
+ TestResultSender resultSender = new TestResultSender();
+ FunctionContext context = new FunctionContextImpl("functionId", nonFilteringArgs, resultSender);
+
+ long expectedSize = FileUtils.sizeOf(logFile) + FileUtils.sizeOf(statFile);
+ new SizeExportLogsFunction().execute(context);
+ getAndVerifySizeEstimate(resultSender, expectedSize);
+ }
+
+ @Test
+ public void noFiles_returnsZeroResult() throws Throwable {
+ Properties config = new Properties();
+ config.setProperty(NAME, name);
+
+ this.cache = new CacheFactory(config).create();
+
+ TestResultSender resultSender = new TestResultSender();
+ FunctionContext context = new FunctionContextImpl("functionId", nonFilteringArgs, resultSender);
+
+ new SizeExportLogsFunction().execute(context);
+ getAndVerifySizeEstimate(resultSender, 0L);
+ }
+
+ private void getAndVerifySizeEstimate(TestResultSender resultSender, long expectedSize)
+ throws Throwable {
+ List<?> results = resultSender.getResults();
+
+ assertThat(results).isNotNull();
+ assertThat(results.size()).isEqualTo(1);
+ List<?> result = (List<?>)results.get(0);
+ assertThat(result).isNotNull();
+ assertThat(((long[])result.get(0))[0]).isEqualTo(expectedSize);
+
+ }
+
+ @Test
+ public void withFunctionError_shouldThrow() throws Throwable {
+ this.cache = new CacheFactory().create();
+
+ TestResultSender resultSender = new TestResultSender();
+ FunctionContext context = new FunctionContextImpl("functionId", null, resultSender);
+
+ new SizeExportLogsFunction().execute(context);
+ assertThatThrownBy(() -> resultSender.getResults()).isInstanceOf(NullPointerException.class);
+
+// List<?> results = resultSender.getResults();
+// List<?> result = (List<?>)results.get(0);
+// assertThat(result).isInstanceOf(Exception.class);
+
+ }
+
+ private static class TestResultSender implements ResultSender {
+
+ private final List<Object> results = new LinkedList<Object>();
+
+ private Throwable t;
+
+ protected List<Object> getResults() throws Throwable {
+ if (t != null) {
+ throw t;
+ }
+ return Collections.unmodifiableList(results);
+ }
+
+ @Override
+ public void lastResult(final Object lastResult) {
+ results.add(lastResult);
+ }
+
+ @Override
+ public void sendResult(final Object oneResult) {
+ results.add(oneResult);
+ }
+
+ @Override
+ public void sendException(final Throwable t) {
+ this.t = t;
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/geode/blob/88eaa5b0/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunctionFileTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunctionFileTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunctionFileTest.java
new file mode 100644
index 0000000..72ba7f8
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunctionFileTest.java
@@ -0,0 +1,176 @@
+/*
+ * 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.geode.management.internal.cli.functions;
+
+import static org.assertj.core.api.Assertions.*;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import org.apache.geode.cache.execute.FunctionContext;
+import org.apache.geode.cache.execute.ResultSender;
+import org.apache.geode.internal.cache.execute.FunctionContextImpl;
+import org.apache.geode.management.internal.cli.functions.ExportLogsFunction.Args;
+import org.apache.commons.io.FileUtils;
+import org.apache.geode.distributed.DistributedMember;
+import org.apache.geode.test.junit.categories.IntegrationTest;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.TemporaryFolder;
+import org.junit.rules.TestName;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+@Category(IntegrationTest.class)
+public class SizeExportLogsFunctionFileTest {
+
+ private File dir;
+ private DistributedMember member;
+ private SizeExportLogsFunction.Args nonFilteringArgs;
+ private FunctionContext functionContext;
+
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+ @Rule
+ public TestName testName = new TestName();
+
+ @Before
+ public void before() throws Exception {
+ this.dir = this.temporaryFolder.getRoot();
+ this.member = mock(DistributedMember.class);
+ this.nonFilteringArgs = new Args(null, null, null, false,false, false);
+ }
+
+ @Test
+ public void bothFiles_returnsCombinedSize() throws Exception {
+ List<File> logFiles = createLogFiles(new File(dir.getName(), testName.getMethodName()), 1, 1,
+ FileUtils.ONE_KB);
+ File logFile = logFiles.get(0);
+ long logFileSize = FileUtils.sizeOf(logFiles.get(0));
+
+ List<File> statFiles = createStatFiles(new File(dir.getName(), testName.getMethodName()), 1, 1, FileUtils.ONE_KB);
+ File statArchive = statFiles.get(0);
+ long statFileSize = FileUtils.sizeOf(statArchive);
+
+ SizeExportLogsFunction function = new SizeExportLogsFunction();
+ assertThat(function.estimateLogFileSize(this.member, logFile, statArchive, nonFilteringArgs)).isEqualTo(logFileSize + statFileSize);
+ }
+
+ private long expectedSize;
+ @Test
+ public void manyFiles_returnsCombinedSize() throws Exception {
+ expectedSize = 0;
+ List<File> logFiles = createLogFiles(new File(dir.getName(), testName.getMethodName()), 1, 3, FileUtils.ONE_KB);
+ logFiles.forEach((file) -> {expectedSize += FileUtils.sizeOf(file);});
+// long logFileSize = FileUtils.sizeOf(logFile);
+
+ List<File> statFiles = createStatFiles(new File(dir.getName(), testName.getMethodName()), 1, 2, FileUtils.ONE_KB * 2);
+ statFiles.forEach((file) -> {expectedSize += FileUtils.sizeOf(file);});
+// long statFileSize = FileUtils.sizeOf(statArchive);
+
+ SizeExportLogsFunction function = new SizeExportLogsFunction();
+ assertThat(function.estimateLogFileSize(this.member, logFiles.get(0), statFiles.get(0), nonFilteringArgs)).isEqualTo(expectedSize);
+ }
+
+ @Test
+ public void emptyFiles_returnsZeroSize() throws Exception {
+ List<File> logFiles = createLogFiles(new File(dir.getName(), testName.getMethodName()), 1, 3, 0);
+// logFiles.forEach((file) -> {expectedSize += FileUtils.sizeOf(file);});
+// long logFileSize = FileUtils.sizeOf(logFile);
+
+ List<File> statFiles = createStatFiles(new File(dir.getName(), testName.getMethodName()), 1, 2, 0);
+// statFiles.forEach((file) -> {expectedSize += FileUtils.sizeOf(file);});
+// long statFileSize = FileUtils.sizeOf(statArchive);
+ SizeExportLogsFunction function = new SizeExportLogsFunction();
+ assertThat(function.estimateLogFileSize(this.member, logFiles.get(0), statFiles.get(0), nonFilteringArgs)).isEqualTo(0);
+ }
+
+ @Test
+ public void nullFiles_returnsZeroSize() throws Exception {
+ File nullLogFile = new File(dir.getPath(), "nullLogFile");
+ File nullStatFile = new File(dir.getPath(), "nullStatFile");
+ SizeExportLogsFunction function = new SizeExportLogsFunction();
+ assertThat(function.estimateLogFileSize(this.member, nullLogFile, nullStatFile, nonFilteringArgs)).isEqualTo(0);
+ }
+
+ private List<File> createLogFiles(File logFile, int mainId, int numberOfFiles, long sizeOfFile)
+ throws IOException {
+ List<File> files = new ArrayList<>();
+ for (int i = 0; i < numberOfFiles; i++) {
+ String name = baseName(logFile.getName()) + "-" + formatId(mainId) + "-" + formatId(i+1) + ".log";
+ File file = createFile(name, sizeOfFile, true);
+ files.add(file);
+ }
+ return files;
+ }
+
+ private List<File> createStatFiles(File logFile, int mainId, int numberOfFiles, long sizeOfFile)
+ throws IOException {
+ List<File> files = new ArrayList<>();
+ for (int i = 0; i < numberOfFiles; i++) {
+ String name = baseName(logFile.getName()) + "-" + formatId(mainId) + "-" + formatId(i+1) + ".gfs";
+ File file = createFile(name, sizeOfFile, false);
+ files.add(file);
+ }
+ return files;
+ }
+
+ @Test
+ public void negativeEstimatedSize() throws Exception {
+
+ }
+
+ private String baseName(String logFileName) {
+ // base log file: myfile.log
+ // mainId childId for rolling
+ // myfile-01-01.log
+ // myfile-01-02.log
+ // pass in myfile.log
+ // return myfile
+ return null;
+ }
+
+ private String formatId(final int id) {
+ return String.format("%02d", id);
+ }
+
+ private File createFile(String name, long sizeInBytes, boolean lineFeed) throws IOException {
+ File file = new File(this.dir, name);
+ fillUpFile(file, sizeInBytes, lineFeed);
+ return file;
+ }
+
+ private void fillUpFile(File file, long sizeInBytes, boolean lineFeed) throws IOException {
+ PrintWriter writer = new PrintWriter(file, "UTF-8");
+ while (FileUtils.sizeOf(file) < sizeInBytes) {
+ writer.print("this is a line of data in the file");
+ if (lineFeed) {
+ writer.println();
+ }
+ }
+ writer.close();
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/geode/blob/88eaa5b0/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunctionIntegrationTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunctionIntegrationTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunctionIntegrationTest.java
deleted file mode 100644
index 4bba6f2..0000000
--- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/SizeExportLogsFunctionIntegrationTest.java
+++ /dev/null
@@ -1,218 +0,0 @@
-/*
- * 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.geode.management.internal.cli.functions;
-
-import static org.assertj.core.api.Assertions.*;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.when;
-
-import org.apache.geode.cache.execute.FunctionContext;
-import org.apache.geode.cache.execute.ResultSender;
-import org.apache.geode.internal.cache.execute.FunctionContextImpl;
-import org.apache.geode.management.internal.cli.functions.ExportLogsFunction.Args;
-import org.apache.commons.io.FileUtils;
-import org.apache.geode.distributed.DistributedMember;
-import org.apache.geode.test.junit.categories.IntegrationTest;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-import org.junit.rules.TemporaryFolder;
-import org.junit.rules.TestName;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-
-@Category(IntegrationTest.class)
-public class SizeExportLogsFunctionIntegrationTest {
-
- private File dir;
- private DistributedMember member;
- private SizeExportLogsFunction.Args nonFilteringArgs;
- private TestResultSender resultSender;
- private FunctionContext functionContext;
-
- @Rule
- public TemporaryFolder temporaryFolder = new TemporaryFolder();
-
- @Rule
- public TestName testName = new TestName();
-
- @Before
- public void before() throws Exception {
- this.dir = this.temporaryFolder.getRoot();
- this.member = mock(DistributedMember.class);
-
- this.nonFilteringArgs = new Args(null, null, null, false,false, false);
- resultSender = new TestResultSender();
- functionContext = new FunctionContextImpl("functionId", nonFilteringArgs, resultSender);
- }
-
- @Test
- public void bothFiles_returnsCombinedSize() throws Exception {
- List<File> logFiles = createLogFiles(new File(dir.getName(), testName.getMethodName()), 1, 1,
- FileUtils.ONE_KB);
- File logFile = logFiles.get(0);
- long logFileSize = FileUtils.sizeOf(logFiles.get(0));
-
- List<File> statFiles = createStatFiles(new File(dir.getName(), testName.getMethodName()), 1, 1, FileUtils.ONE_KB);
- File statArchive = statFiles.get(0);
- long statFileSize = FileUtils.sizeOf(statArchive);
-
- SizeExportLogsFunction function = new SizeExportLogsFunction();
- assertThat(function.estimateLogFileSize(this.member, logFile, statArchive, nonFilteringArgs)).isEqualTo(logFileSize + statFileSize);
- }
-
- private long expectedSize;
- @Test
- public void manyFiles_returnsCombinedSize() throws Exception {
- expectedSize = 0;
- List<File> logFiles = createLogFiles(new File(dir.getName(), testName.getMethodName()), 1, 3, FileUtils.ONE_KB);
- logFiles.forEach((file) -> {expectedSize += FileUtils.sizeOf(file);});
-// long logFileSize = FileUtils.sizeOf(logFile);
-
- List<File> statFiles = createStatFiles(new File(dir.getName(), testName.getMethodName()), 1, 2, FileUtils.ONE_KB * 2);
- statFiles.forEach((file) -> {expectedSize += FileUtils.sizeOf(file);});
-// long statFileSize = FileUtils.sizeOf(statArchive);
-
- SizeExportLogsFunction function = new SizeExportLogsFunction();
- assertThat(function.estimateLogFileSize(this.member, logFiles.get(0), statFiles.get(0), nonFilteringArgs)).isEqualTo(expectedSize);
- }
-
- @Test
- public void emptyFiles_returnsZeroSize() throws Exception {
- List<File> logFiles = createLogFiles(new File(dir.getName(), testName.getMethodName()), 1, 3, 0);
-// logFiles.forEach((file) -> {expectedSize += FileUtils.sizeOf(file);});
-// long logFileSize = FileUtils.sizeOf(logFile);
-
- List<File> statFiles = createStatFiles(new File(dir.getName(), testName.getMethodName()), 1, 2, 0);
-// statFiles.forEach((file) -> {expectedSize += FileUtils.sizeOf(file);});
-// long statFileSize = FileUtils.sizeOf(statArchive);
- SizeExportLogsFunction function = new SizeExportLogsFunction();
- assertThat(function.estimateLogFileSize(this.member, logFiles.get(0), statFiles.get(0), nonFilteringArgs)).isEqualTo(0);
- }
-
- @Test
- public void nullFiles_returnsZeroSize() throws Exception {
- File nullLogFile = new File(dir.getPath(), "nullLogFile");
- File nullStatFile = new File(dir.getPath(), "nullStatFile");
- SizeExportLogsFunction function = new SizeExportLogsFunction();
- assertThat(function.estimateLogFileSize(this.member, nullLogFile, nullStatFile, nonFilteringArgs)).isEqualTo(0);
- }
-
- private List<File> createLogFiles(File logFile, int mainId, int numberOfFiles, long sizeOfFile)
- throws IOException {
- List<File> files = new ArrayList<>();
- for (int i = 0; i < numberOfFiles; i++) {
- String name = baseName(logFile.getName()) + "-" + formatId(mainId) + "-" + formatId(i+1) + ".log";
- File file = createFile(name, sizeOfFile, true);
- files.add(file);
- }
- return files;
- }
-
- private List<File> createStatFiles(File logFile, int mainId, int numberOfFiles, long sizeOfFile)
- throws IOException {
- List<File> files = new ArrayList<>();
- for (int i = 0; i < numberOfFiles; i++) {
- String name = baseName(logFile.getName()) + "-" + formatId(mainId) + "-" + formatId(i+1) + ".gfs";
- File file = createFile(name, sizeOfFile, false);
- files.add(file);
- }
- return files;
- }
-
- @Test
- public void negativeEstimatedSize() throws Exception {
- File nullLogFile = new File(dir.getPath(), "nullLogFile");
- File nullStatFile = new File(dir.getPath(), "nullStatFile");
- SizeExportLogsFunction function = new SizeExportLogsFunction();
- SizeExportLogsFunction spyFunction = spy(function);
-
- doReturn(-1L).when(spyFunction).estimateLogFileSize(this.member, nullLogFile, nullStatFile, nonFilteringArgs);
-// when(spyFunction.estimateLogFileSize(this.member, nullLogFile, nullStatFile, nonFilteringArgs)).thenReturn(-1L);
-
- spyFunction.execute(functionContext);
-
-
- }
-
- private String baseName(String logFileName) {
- // base log file: myfile.log
- // mainId childId for rolling
- // myfile-01-01.log
- // myfile-01-02.log
- // pass in myfile.log
- // return myfile
- return null;
- }
-
- private String formatId(final int id) {
- return String.format("%02d", id);
- }
-
- private File createFile(String name, long sizeInBytes, boolean lineFeed) throws IOException {
- File file = new File(this.dir, name);
- fillUpFile(file, sizeInBytes, lineFeed);
- return file;
- }
-
- private void fillUpFile(File file, long sizeInBytes, boolean lineFeed) throws IOException {
- PrintWriter writer = new PrintWriter(file, "UTF-8");
- while (FileUtils.sizeOf(file) < sizeInBytes) {
- writer.print("this is a line of data in the file");
- if (lineFeed) {
- writer.println();
- }
- }
- writer.close();
- }
-
- private static class TestResultSender implements ResultSender {
-
- private final List<Object> results = new LinkedList<Object>();
-
- private Throwable t;
-
- protected List<Object> getResults() throws Throwable {
- if (t != null) {
- throw t;
- }
- return Collections.unmodifiableList(results);
- }
-
- @Override
- public void lastResult(final Object lastResult) {
- results.add(lastResult);
- }
-
- @Override
- public void sendResult(final Object oneResult) {
- results.add(oneResult);
- }
-
- @Override
- public void sendException(final Throwable t) {
- this.t = t;
- }
- }
-}
\ No newline at end of file