Add test for Rename post-processor

This commit is contained in:
0xb00bface 2020-09-22 14:58:20 +02:00
parent e1bce0acf5
commit 4167b222ba
4 changed files with 152 additions and 3 deletions

View File

@ -75,6 +75,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>

View File

@ -14,8 +14,7 @@ import ctbrec.Recording;
public abstract class AbstractPlaceholderAwarePostProcessor extends AbstractPostProcessor {
@SuppressWarnings("unused")
private String[] placeHolders = {
public static final String[] PLACE_HOLDERS = {
"${modelName}",
"${modelDisplayName}",
"${modelSanitizedName}",
@ -59,7 +58,7 @@ public abstract class AbstractPlaceholderAwarePostProcessor extends AbstractPost
}
private String replaceDateTime(Recording rec, String filename, String placeHolder, ZoneId zone) {
String pattern = "yyyy-mm-dd_HH-mm-ss";
String pattern = "yyyy-MM-dd_HH-mm-ss";
Matcher m = Pattern.compile("\\$\\{" + placeHolder + "(?:\\((.*?)\\))?\\}").matcher(filename);
if (m.find()) {
String p = m.group(1);

View File

@ -0,0 +1,127 @@
package ctbrec.recorder.postprocessing;
import static java.nio.charset.StandardCharsets.*;
import static java.nio.file.StandardOpenOption.*;
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.nio.file.Path;
import java.time.Instant;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import ctbrec.Config;
import ctbrec.Model;
import ctbrec.Recording;
import ctbrec.Settings;
import ctbrec.sites.Site;
import ctbrec.sites.chaturbate.Chaturbate;
public class RenameSingleFileTest {
private Path baseDir;
private Path recDir;
private File original;
private File postProcessed;
private Instant now = Instant.now();
@Before
public void setup() throws IOException {
baseDir = Files.createTempDirectory("ctbrec_test_");
recDir = baseDir.resolve("recordings");
original = new File(recDir.toFile(), "original.ts");
postProcessed = new File(recDir.toFile(), "postProcessed.ts");
Files.createDirectories(original.getParentFile().toPath());
Files.write(original.toPath(), "foobar".getBytes(UTF_8), CREATE_NEW, WRITE, TRUNCATE_EXISTING);
Files.write(postProcessed.toPath(), "foobar".getBytes(UTF_8), CREATE_NEW, WRITE, TRUNCATE_EXISTING);
}
@After
public void teardown() throws IOException {
FileUtils.deleteDirectory(baseDir.toFile());
}
@Test
public void testOriginalFileReplacement() throws IOException {
Config config = mockConfig();
Recording rec = new Recording();
rec.setModel(mockModel());
rec.setAbsoluteFile(original);
rec.setStartDate(now);
Rename pp = new Rename();
pp.postprocess(rec, config);
Matcher m = Pattern.compile("Mockita_\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}\\.ts").matcher(rec.getAbsoluteFile().getName());
assertTrue(m.matches());
assertEquals(rec.getAbsoluteFile(), rec.getPostProcessedFile());
assertNotEquals(rec.getAbsoluteFile(), original);
}
@Test
public void testEarlyExit() throws IOException {
Config config = mockConfig();
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());
Rename pp = new Rename();
pp.getConfig().put(Rename.FILE_NAME_TEMPLATE, "original.ts");
pp.postprocess(rec, config);
}
@Test
public void absoluteFileShouldKeepBeingOriginalIfFilesDiffer() throws IOException {
Config config = mockConfig();
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());
Rename pp = new Rename();
pp.postprocess(rec, config);
}
@Test
public void testToString() {
Rename pp = new Rename();
assertEquals("rename", pp.toString());
pp.getConfig().put(Rename.FILE_NAME_TEMPLATE, Rename.DEFAULT);
assertEquals("rename [${modelSanitizedName}_${localDateTime}.${fileSuffix}]", pp.toString());
}
Config mockConfig() {
Config config = mock(Config.class);
when(config.getSettings()).thenReturn(mockSettings());
when(config.getModelNotes(any())).thenReturn("tag, foo, bar");
return config;
}
Model mockModel() {
Site site = new Chaturbate();
Model model = site.createModel("Mockita");
model.setDisplayName("Mockita Boobilicious");
return model;
}
Settings mockSettings() {
Settings settings = new Settings();
settings.recordingsDir = recDir.toString();
return settings;
}
}

View File

@ -26,6 +26,14 @@
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
@ -114,6 +122,12 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.5.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
@ -126,4 +140,8 @@
</dependency>
</dependencies>
</dependencyManagement>
</project>