forked from j62/ctbrec
1
0
Fork 0

Add more post-processing tests

This commit is contained in:
0xb00bface 2020-09-25 00:09:23 +02:00
parent 87d88b5bcb
commit b2b6a623ef
6 changed files with 167 additions and 2 deletions

View File

@ -77,7 +77,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -14,6 +14,7 @@ import java.time.Instant;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.mockito.MockedStatic;
import ctbrec.Config;
import ctbrec.Model;
@ -32,6 +33,8 @@ public abstract class AbstractPpTest {
Instant now = Instant.now();
RecordingManager recordingManager;
MockedStatic<Config> configStatic;
@Before
public void setup() throws IOException {
baseDir = Files.createTempDirectory("ctbrec_test_");
@ -50,12 +53,18 @@ public abstract class AbstractPpTest {
@After
public void teardown() throws IOException {
FileUtils.deleteDirectory(baseDir.toFile());
if (configStatic != null) {
configStatic.close();
configStatic = null;
}
}
Config mockConfig() {
Config config = mock(Config.class);
when(config.getSettings()).thenReturn(mockSettings());
when(config.getModelNotes(any())).thenReturn("tag, foo, bar");
configStatic = mockStatic(Config.class);
configStatic.when(Config::getInstance).thenReturn(config);
return config;
}

View File

@ -0,0 +1,25 @@
package ctbrec.recorder.postprocessing;
import java.io.IOException;
import org.junit.Test;
import ctbrec.Config;
import ctbrec.Recording;
public class DeleteOriginalTest extends AbstractPpTest {
@Test
public void testPostProcessWithSingleFile() throws IOException, InterruptedException {
Recording rec = new Recording();
rec.setModel(mockModel());
rec.setAbsoluteFile(original);
rec.setAbsoluteFile(postProcessed);
rec.setStartDate(now);
rec.setSingleFile(true);
Config config = mockConfig();
DeleteOriginal pp = new DeleteOriginal();
pp.postprocess(rec, null, config);
}
}

View File

@ -0,0 +1,55 @@
package ctbrec.recorder.postprocessing;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.Recording;
public class MoveDirectoryTest extends AbstractPpTest {
@Test
public void testOriginalFileReplacement() throws IOException {
Config config = mockConfig();
Recording rec = new Recording();
rec.setModel(mockModel());
rec.setAbsoluteFile(originalDir);
rec.setStartDate(now);
rec.setSingleFile(false);
Move pp = new Move();
pp.getConfig().put(Move.PATH_TEMPLATE, new File(baseDir.toFile(), Move.DEFAULT).getAbsolutePath());
pp.postprocess(rec, recordingManager, config);
Matcher m = Pattern.compile(baseDir.toString() + "/Mockita/\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}/original").matcher(rec.getAbsoluteFile().getCanonicalPath());
assertTrue(m.matches());
assertEquals(rec.getAbsoluteFile(), rec.getPostProcessedFile());
assertNotEquals(rec.getAbsoluteFile(), original);
}
@Test
public void absoluteFileShouldKeepBeingOriginalIfFilesDiffer() throws IOException {
Model model = mockModel();
Recording rec = mock(Recording.class);
when(rec.getModel()).thenReturn(model);
when(rec.getAbsoluteFile()).thenReturn(originalDir);
when(rec.getPostProcessedFile()).thenReturn(postProcessedDir);
when(rec.getStartDate()).thenReturn(now);
doThrow(new RuntimeException("Unexpected call of setAbsoluteFile")).when(rec).setAbsoluteFile(any());
Files.createDirectories(postProcessedDir.toPath());
Move pp = new Move();
Config config = mockConfig();
pp.getConfig().put(Move.PATH_TEMPLATE, new File(baseDir.toFile(), Move.DEFAULT).getAbsolutePath());
pp.postprocess(rec, recordingManager, config);
}
}

View File

@ -0,0 +1,76 @@
package ctbrec.recorder.postprocessing;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.Recording;
public class MoveSingleFileTest extends AbstractPpTest {
@Test
public void testOriginalFileReplacement() throws IOException {
Config config = mockConfig();
Recording rec = new Recording();
rec.setModel(mockModel());
rec.setAbsoluteFile(original);
rec.setStartDate(now);
rec.setSingleFile(true);
Move pp = new Move();
pp.getConfig().put(Move.PATH_TEMPLATE, new File(baseDir.toFile(), Move.DEFAULT).getAbsolutePath());
pp.postprocess(rec, recordingManager, config);
Matcher m = Pattern.compile(baseDir.toFile() + "/Mockita/\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}/original\\.ts").matcher(rec.getAbsoluteFile().toString());
assertTrue(m.matches());
assertEquals(rec.getAbsoluteFile(), rec.getPostProcessedFile());
assertNotEquals(rec.getAbsoluteFile(), original);
}
@Test
public void testEarlyExit() throws IOException {
Model model = mockModel();
Recording rec = mock(Recording.class);
when(rec.getModel()).thenReturn(model);
when(rec.getAbsoluteFile()).thenReturn(original);
when(rec.getPostProcessedFile()).thenReturn(original);
when(rec.getStartDate()).thenReturn(now);
doThrow(new RuntimeException("Unexpected call of setAbsoluteFile")).when(rec).setAbsoluteFile(any());
Move pp = new Move();
Config config = mockConfig();
pp.getConfig().put(Move.PATH_TEMPLATE, original.getParentFile().getCanonicalPath());
pp.postprocess(rec, recordingManager, config);
}
@Test
public void absoluteFileShouldKeepBeingOriginalIfFilesDiffer() throws IOException {
Model model = mockModel();
Recording rec = mock(Recording.class);
when(rec.getModel()).thenReturn(model);
when(rec.getAbsoluteFile()).thenReturn(original);
when(rec.getPostProcessedFile()).thenReturn(postProcessed);
when(rec.getStartDate()).thenReturn(now);
doThrow(new RuntimeException("Unexpected call of setAbsoluteFile")).when(rec).setAbsoluteFile(any());
Move pp = new Move();
Config config = mockConfig();
pp.getConfig().put(Move.PATH_TEMPLATE, new File(baseDir.toFile(), Move.DEFAULT).getAbsolutePath());
pp.postprocess(rec, recordingManager, config);
}
@Test
public void testToString() {
Move pp = new Move();
assertEquals("move", pp.toString());
pp.getConfig().put(Move.PATH_TEMPLATE, Move.DEFAULT);
assertEquals("move ["+Move.DEFAULT+"]", pp.toString());
}
}

View File

@ -124,7 +124,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<version>3.5.11</version>
<scope>test</scope>
</dependency>