forked from j62/ctbrec
Add test for Rename pp for directories
This commit is contained in:
parent
4167b222ba
commit
d23f3fea04
|
@ -78,8 +78,12 @@ public abstract class AbstractPlaceholderAwarePostProcessor extends AbstractPost
|
|||
}
|
||||
|
||||
private CharSequence getFileSuffix(Recording rec) {
|
||||
String filename = rec.getPostProcessedFile().getName();
|
||||
return filename.substring(filename.lastIndexOf('.') + 1);
|
||||
if(rec.isSingleFile()) {
|
||||
String filename = rec.getPostProcessedFile().getName();
|
||||
return filename.substring(filename.lastIndexOf('.') + 1);
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private CharSequence getSanitizedSiteName(Recording rec) {
|
||||
|
|
|
@ -15,6 +15,7 @@ public class Rename extends AbstractPlaceholderAwarePostProcessor {
|
|||
private static final Logger LOG = LoggerFactory.getLogger(Rename.class);
|
||||
public static final String FILE_NAME_TEMPLATE = "filename.template";
|
||||
public static final String DEFAULT = "${modelSanitizedName}_${localDateTime}.${fileSuffix}";
|
||||
public static final String DEFAULT_DIR = "${modelSanitizedName}_${localDateTime}";
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
|
@ -23,7 +24,8 @@ public class Rename extends AbstractPlaceholderAwarePostProcessor {
|
|||
|
||||
@Override
|
||||
public void postprocess(Recording rec, Config config) throws IOException {
|
||||
String filenameTemplate = getConfig().getOrDefault(FILE_NAME_TEMPLATE, DEFAULT);
|
||||
String defaultTemplate = rec.isSingleFile() ? DEFAULT : DEFAULT_DIR;
|
||||
String filenameTemplate = getConfig().getOrDefault(FILE_NAME_TEMPLATE, defaultTemplate);
|
||||
String filename = fillInPlaceHolders(filenameTemplate, rec, config);
|
||||
File src = rec.getPostProcessedFile();
|
||||
File target = new File(src.getParentFile(), filename);
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
package ctbrec.recorder.postprocessing;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.*;
|
||||
import static java.nio.file.StandardOpenOption.*;
|
||||
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 org.apache.commons.io.FileUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
import ctbrec.Config;
|
||||
import ctbrec.Model;
|
||||
import ctbrec.Settings;
|
||||
import ctbrec.sites.Site;
|
||||
import ctbrec.sites.chaturbate.Chaturbate;
|
||||
|
||||
public abstract class AbstractPpTest {
|
||||
Path baseDir;
|
||||
Path recDir;
|
||||
File original;
|
||||
File postProcessed;
|
||||
File originalDir;
|
||||
File postProcessedDir;
|
||||
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");
|
||||
originalDir = new File(recDir.toFile(), "original");
|
||||
postProcessedDir = new File(recDir.toFile(), "postProcessed");
|
||||
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);
|
||||
Files.createDirectories(originalDir.toPath());
|
||||
FileUtils.touch(new File(originalDir, "playlist.m3u8"));
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() throws IOException {
|
||||
FileUtils.deleteDirectory(baseDir.toFile());
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package ctbrec.recorder.postprocessing;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
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 RenameDirectoryTest 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);
|
||||
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}").matcher(rec.getAbsoluteFile().getName());
|
||||
assertTrue(m.matches());
|
||||
assertEquals(rec.getAbsoluteFile(), rec.getPostProcessedFile());
|
||||
assertNotEquals(rec.getAbsoluteFile(), original);
|
||||
}
|
||||
|
||||
@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(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());
|
||||
Rename pp = new Rename();
|
||||
pp.postprocess(rec, config);
|
||||
}
|
||||
}
|
|
@ -1,63 +1,29 @@
|
|||
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());
|
||||
}
|
||||
public class RenameSingleFileTest 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);
|
||||
Rename pp = new Rename();
|
||||
pp.postprocess(rec, config);
|
||||
|
||||
|
@ -78,7 +44,7 @@ public class RenameSingleFileTest {
|
|||
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.getConfig().put(Rename.FILE_NAME_TEMPLATE, original.getName());
|
||||
pp.postprocess(rec, config);
|
||||
}
|
||||
|
||||
|
@ -104,24 +70,4 @@ public class RenameSingleFileTest {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue