Skip to content

Commit

Permalink
Fixes #358 - Add test for LogRotateCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem committed Sep 23, 2024
1 parent 15a04da commit 5e414a6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions cli/src/main/java/com/manorrock/sphynx/cli/LogCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
subcommands = {
LogDeleteCommand.class,
LogListCommand.class,
LogRotateCommand.class,
LogViewCommand.class
})
public class LogCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Integer call() throws Exception {

File[] logFiles = logsDirectory.listFiles();
Arrays.sort(logFiles, Comparator.comparingLong(File::lastModified).reversed());
for(int i=10; i<logFiles.length; i++) {
for(int i=0; i<10; i++) {
System.out.println("Deleting " + logFiles[i].getAbsolutePath());
logFiles[i].delete();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2002-2024 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.manorrock.sphynx.cli;

import java.io.File;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

/**
* A test validating the LogRotateCommand works properly.
*
* @author Manfred Riem ([email protected])
*/
public class LogRotateCommandTest {

/**
* Test to validate DeleteCommand class.
*/
@Test
public void testLogDeleteCommand() throws Exception {
Cli cli = new Cli();
cli.setArguments(new String[]{"create", "--name", "log-rotate-command-test"});
cli.run();
for (int i = 0; i < 15; i++) {
cli.setArguments(new String[]{"execute", "--name", "log-rotate-command-test"});
cli.run();
}
File logDirectory = new File(System.getProperty("user.home"),
".manorrock/sphynx/jobs/log-rotate-command-test/logs");
assertTrue(logDirectory.listFiles().length > 10);
cli.setArguments(new String[]{"log", "rotate", "--name", "log-rotate-command-test"});
cli.run();
assertTrue(logDirectory.listFiles().length < 10);
assertEquals(0, cli.getExitCode());
cli.setArguments(new String[]{"delete", "--name", "log-rotate-command-test"});
cli.run();
}
}

0 comments on commit 5e414a6

Please sign in to comment.