Example usage for org.apache.commons.io FileUtils touch

List of usage examples for org.apache.commons.io FileUtils touch

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils touch.

Prototype

public static void touch(File file) throws IOException 

Source Link

Document

Implements the same behaviour as the "touch" utility on Unix.

Usage

From source file:org.silverpeas.core.contribution.content.wysiwyg.service.process.MailContentProcessTest.java

@SuppressWarnings("unchecked")
@BeforeEach/*  ww  w. j a  v  a  2s .  c  o  m*/
public void setup() throws Exception {
    originalOdt = new File(getClass().getResource("/" + ODT_NAME).getPath());
    assertThat(originalOdt.exists(), is(true));
    originalImage = new File(getClass().getResource("/" + IMAGE_NAME).getPath());
    assertThat(originalImage.exists(), is(true));
    originalImageWithResize100x = new File(originalImage.getParentFile(), "100x/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x);
    assertThat(originalImageWithResize100x.exists(), is(true));
    originalImageWithResize100x100 = new File(originalImage.getParentFile(), "100x100/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x100);
    assertThat(originalImageWithResize100x100.exists(), is(true));

    // SilverpeasFile
    List<SilverpeasFileProcessor> processors = (List<SilverpeasFileProcessor>) FieldUtils
            .readDeclaredStaticField(SilverpeasFileProvider.class, "processors", true);
    processors.clear();
    SilverpeasFileProvider.addProcessor(new AttachmentUrlLinkProcessor());

    // The mock instance
    AttachmentService mockAttachmentService = mock(AttachmentService.class);
    when(TestBeanContainer.getMockedBeanContainer().getBeanByType(AttachmentService.class))
            .thenReturn(mockAttachmentService);

    /*
    Mocking methods of attachment service instance
     */

    // searchDocumentById returns always a simple document which the PK is the one specified from
    // method parameters.
    when(mockAttachmentService.searchDocumentById(any(SimpleDocumentPK.class), anyString()))
            .then(invocation -> {
                SimpleDocumentPK pk = (SimpleDocumentPK) invocation.getArguments()[0];
                SimpleDocument simpleDocument = mock(SimpleDocument.class);
                when(simpleDocument.getPk()).thenReturn(pk);
                if (pk.getId().contains(ODT_ATTACHMENT_ID)) {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalOdt.getPath());
                } else {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalImage.getPath());
                }
                return simpleDocument;
            });

    /*
    Setting the server start URL
     */
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    when(mockHttpServletRequest.getScheme()).thenReturn("http");
    when(mockHttpServletRequest.getServerName()).thenReturn("www.unit-test-silverpeas.org");
    when(mockHttpServletRequest.getServerPort()).thenReturn(80);
    URLUtil.setCurrentServerUrl(mockHttpServletRequest);
}

From source file:org.silverpeas.core.contribution.content.wysiwyg.service.process.TestMailContentProcess.java

@SuppressWarnings("unchecked")
@Before//from   ww  w  .jav  a 2 s  . c o m
public void setup() throws Exception {
    originalOdt = new File(getClass().getResource("/" + ODT_NAME).getPath());
    assertThat(originalOdt.exists(), is(true));
    originalImage = new File(getClass().getResource("/" + IMAGE_NAME).getPath());
    assertThat(originalImage.exists(), is(true));
    originalImageWithResize100x = new File(originalImage.getParentFile(), "100x/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x);
    assertThat(originalImageWithResize100x.exists(), is(true));
    originalImageWithResize100x100 = new File(originalImage.getParentFile(), "100x100/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x100);
    assertThat(originalImageWithResize100x100.exists(), is(true));

    // SilverpeasFile
    List<SilverpeasFileProcessor> processors = (List<SilverpeasFileProcessor>) FieldUtils
            .readDeclaredStaticField(SilverpeasFileProvider.class, "processors", true);
    processors.clear();
    SilverpeasFileProvider.addProcessor(new AttachmentUrlLinkProcessor());

    // The mock instance
    AttachmentService mockAttachmentService = mock(AttachmentService.class);
    when(TestBeanContainer.getMockedBeanContainer().getBeanByType(AttachmentService.class))
            .thenReturn(mockAttachmentService);

    /*
    Mocking methods of attachment service instance
     */

    // searchDocumentById returns always a simple document which the PK is the one specified from
    // method parameters.
    when(mockAttachmentService.searchDocumentById(any(SimpleDocumentPK.class), anyString()))
            .then(invocation -> {
                SimpleDocumentPK pk = (SimpleDocumentPK) invocation.getArguments()[0];
                SimpleDocument simpleDocument = mock(SimpleDocument.class);
                when(simpleDocument.getPk()).thenReturn(pk);
                if (pk.getId().contains(ODT_ATTACHMENT_ID)) {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalOdt.getPath());
                } else {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalImage.getPath());
                }
                return simpleDocument;
            });

    /*
    Setting the server start URL
     */
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    when(mockHttpServletRequest.getScheme()).thenReturn("http");
    when(mockHttpServletRequest.getServerName()).thenReturn("www.unit-test-silverpeas.org");
    when(mockHttpServletRequest.getServerPort()).thenReturn(80);
    URLUtil.setCurrentServerUrl(mockHttpServletRequest);
}

From source file:org.silverpeas.core.contribution.content.wysiwyg.service.TestWysiwygContentTransformer.java

@SuppressWarnings("unchecked")
@Before//from   ww  w .  j  a  v a  2s. c om
public void setup() throws Exception {
    originalOdt = new File(getClass().getResource("/" + ODT_NAME).getPath());
    assertThat(originalOdt.exists(), is(true));
    originalImage = new File(getClass().getResource("/" + IMAGE_NAME).getPath());
    assertThat(originalImage.exists(), is(true));
    originalImageWithResize100x = new File(originalImage.getParentFile(), "100x/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x);
    assertThat(originalImageWithResize100x.exists(), is(true));
    originalImageWithResize100x100 = new File(originalImage.getParentFile(), "100x100/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x100);
    assertThat(originalImageWithResize100x100.exists(), is(true));

    // SilverpeasFile
    List<SilverpeasFileProcessor> processors = (List<SilverpeasFileProcessor>) FieldUtils
            .readDeclaredStaticField(SilverpeasFileProvider.class, "processors", true);
    processors.clear();
    SilverpeasFileProvider.addProcessor(new AttachmentUrlLinkProcessor());

    // The mock instance
    AttachmentService mockAttachmentService = mock(AttachmentService.class);
    when(TestBeanContainer.getMockedBeanContainer().getBeanByType(AttachmentService.class))
            .thenReturn(mockAttachmentService);

    /*
    Mocking methods of attachment service instance
     */

    // searchDocumentById returns always a simple document which the PK is the one specified
    // from method parameters.
    when(mockAttachmentService.searchDocumentById(any(SimpleDocumentPK.class), anyString()))
            .then(invocation -> {
                SimpleDocumentPK pk = (SimpleDocumentPK) invocation.getArguments()[0];
                SimpleDocument simpleDocument = mock(SimpleDocument.class);
                when(simpleDocument.getPk()).thenReturn(pk);
                if (pk.getId().contains(ODT_ATTACHMENT_ID)) {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalOdt.getPath());
                } else {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalImage.getPath());
                }
                return simpleDocument;
            });

    /*
    Setting the server start URL
     */
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    when(mockHttpServletRequest.getScheme()).thenReturn("http");
    when(mockHttpServletRequest.getServerName()).thenReturn("www.unit-test-silverpeas.org");
    when(mockHttpServletRequest.getServerPort()).thenReturn(80);
    URLUtil.setCurrentServerUrl(mockHttpServletRequest);
}

From source file:org.silverpeas.core.contribution.content.wysiwyg.service.WysiwygContentTransformerTest.java

@SuppressWarnings("unchecked")
@BeforeEach/*from  w  w w .j  a v  a2  s . c o  m*/
public void setup() throws Exception {
    originalOdt = new File(getClass().getResource("/" + ODT_NAME).getPath());
    assertThat(originalOdt.exists(), is(true));
    originalImage = new File(getClass().getResource("/" + IMAGE_NAME).getPath());
    assertThat(originalImage.exists(), is(true));
    originalImageWithResize100x = new File(originalImage.getParentFile(), "100x/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x);
    assertThat(originalImageWithResize100x.exists(), is(true));
    originalImageWithResize100x100 = new File(originalImage.getParentFile(), "100x100/" + IMAGE_NAME);
    FileUtils.touch(originalImageWithResize100x100);
    assertThat(originalImageWithResize100x100.exists(), is(true));

    // SilverpeasFile
    List<SilverpeasFileProcessor> processors = (List<SilverpeasFileProcessor>) FieldUtils
            .readDeclaredStaticField(SilverpeasFileProvider.class, "processors", true);
    processors.clear();
    SilverpeasFileProvider.addProcessor(new AttachmentUrlLinkProcessor());

    // The mock instance
    AttachmentService mockAttachmentService = mock(AttachmentService.class);
    when(TestBeanContainer.getMockedBeanContainer().getBeanByType(AttachmentService.class))
            .thenReturn(mockAttachmentService);

    /*
    Mocking methods of attachment service instance
     */

    // searchDocumentById returns always a simple document which the PK is the one specified
    // from method parameters.
    when(mockAttachmentService.searchDocumentById(any(SimpleDocumentPK.class), anyString()))
            .then(invocation -> {
                SimpleDocumentPK pk = (SimpleDocumentPK) invocation.getArguments()[0];
                SimpleDocument simpleDocument = mock(SimpleDocument.class);
                when(simpleDocument.getPk()).thenReturn(pk);
                if (pk.getId().contains(ODT_ATTACHMENT_ID)) {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalOdt.getPath());
                } else {
                    when(simpleDocument.getAttachmentPath()).thenReturn(originalImage.getPath());
                }
                return simpleDocument;
            });

    /*
    Setting the server start URL
     */
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    when(mockHttpServletRequest.getScheme()).thenReturn("http");
    when(mockHttpServletRequest.getServerName()).thenReturn("www.unit-test-silverpeas.org");
    when(mockHttpServletRequest.getServerPort()).thenReturn(80);
    URLUtil.setCurrentServerUrl(mockHttpServletRequest);
}

From source file:org.silverpeas.core.io.media.image.thumbnail.control.ComponentInstanceThumbnailDeletionIT.java

@Before
public void setup() throws Exception {
    thumbnailController = ServiceProvider.getService(ThumbnailController.class);

    for (String componentInstanceId : new String[] { "kmelia144", "kmelia160" }) {
        File componentInstanceRepo = new File(ThumbnailController.getImageDirectory(componentInstanceId));
        componentInstanceRepos.put(componentInstanceId, componentInstanceRepo);

        for (int i = 0; i < COMPONENT_INSTANCE_NB_THUMBNAILS; i++) {
            FileUtils.touch(new File(componentInstanceRepo, String.format("File_%1$s.jpg", (i + 1))));
        }/*from   ww  w.j  a va2  s .  c om*/

        SilverLogger.getLogger(this).info("{0}ImagePath={1} with files {2}", componentInstanceId,
                componentInstanceRepo, Arrays.asList(componentInstanceRepo.listFiles()).stream()
                        .map(file -> file.getName()).collect(Collectors.joining(", ")));
    }
}

From source file:org.silverpeas.core.io.temp.TestLastModifiedDateFileTask.java

@SuppressWarnings("ConstantConditions")
@Test// w ww  .j  av a 2 s  .  co m
public void verifyLastModifiedDate() throws Exception {
    List<File> files = new ArrayList<File>();
    File fileTest = new File(tempPath, "file.txt");
    FileUtils.touch(fileTest);
    files.add(fileTest);

    fileTest = new File(tempPath, "folder");
    fileTest.mkdirs();
    files.add(fileTest);

    fileTest = new File(fileTest, "file1.txt");
    FileUtils.touch(fileTest);
    files.add(fileTest);

    fileTest = new File(fileTest.getParentFile(), "file2.txt");
    FileUtils.touch(fileTest);
    files.add(fileTest);

    fileTest = new File(fileTest.getParentFile(), "otherFolder");
    fileTest.mkdirs();
    files.add(fileTest);

    fileTest = new File(fileTest, "otherFile1.txt");
    FileUtils.touch(fileTest);
    files.add(fileTest);

    fileTest = new File(fileTest.getParentFile(), "otherFile2.txt");
    FileUtils.touch(fileTest);
    files.add(fileTest);

    Thread.sleep(200);
    long oneSecondAfterFileCreation = System.currentTimeMillis();

    List<Pair<File, Long>> fileLastModifiedDate = new ArrayList<Pair<File, Long>>();
    for (File file : files) {
        fileLastModifiedDate.add(Pair.of(file, file.lastModified()));
    }

    for (Pair<File, Long> fileOrFolder : fileLastModifiedDate) {
        assertThat(fileOrFolder.getKey().getName(), fileOrFolder.getKey().lastModified(),
                is(fileOrFolder.getValue()));
        assertThat(fileOrFolder.getKey().getName(), fileOrFolder.getKey().lastModified(),
                lessThan(oneSecondAfterFileCreation));
    }

    Thread.sleep(1001);
    File[] tempRootFiles = tempPath.listFiles();
    assertThat(tempRootFiles, arrayWithSize(2));
    for (File tempRootFile : tempRootFiles) {
        LastModifiedDateFileTask.addFile(tempRootFile);
    }

    long l = 0;
    while (LastModifiedDateFileTask.isRunning()) {
        l++;
    }
    assertThat("This assertion shows that the thread stops after all the files are performed", l,
            greaterThan(0l));

    Logger.getAnonymousLogger().info(MessageFormat
            .format("Calling LastModifiedDateFileThread.isRunning() {0} times", String.valueOf(l)));

    for (Pair<File, Long> fileOrFolder : fileLastModifiedDate) {
        assertThat(fileOrFolder.getKey().getName(), fileOrFolder.getKey().lastModified(),
                greaterThan(fileOrFolder.getValue()));
        assertThat(fileOrFolder.getKey().getName(), fileOrFolder.getKey().lastModified(),
                greaterThan(oneSecondAfterFileCreation));
    }
}

From source file:org.silverpeas.core.io.upload.TestUploadSession.java

@Test
public void remove() throws Exception {
    UploadSessionFile uploadSessionFile = initializeUploadSessionAndRegisterFile();
    assertThat(uploadSessionFile.getServerFile().exists(), is(false));
    boolean removed = uploadSessionFile.getUploadSession().remove("path/test/name");
    assertThat(removed, is(false));//  w w w.  j a v  a2  s .c  o  m
    assertThat(uploadSessionFile.getServerFile().exists(), is(false));
    FileUtils.touch(uploadSessionFile.getServerFile());
    assertThat(uploadSessionFile.getServerFile().exists(), is(true));
    assertThat(uploadSessionFile.getServerFile().getPath(), is(
            new File(getUploadSessionFile(uploadSessionFile.getUploadSession()), "path/test/name").getPath()));

    // This next section, as it exists a current file writing, it is not possible to remove the
    // server file.
    uploadSessionFile.getUploadSession().markFileWritingInProgress(uploadSessionFile);
    removed = uploadSessionFile.getUploadSession().remove("path/test/name");
    assertThat(removed, is(false));
    assertThat(uploadSessionFile.getServerFile().exists(), is(true));

    // This next section, it is possible
    uploadSessionFile.getUploadSession().markFileWritingDone(uploadSessionFile);
    removed = uploadSessionFile.getUploadSession().remove("path/test/name");
    assertThat(removed, is(true));
    assertThat(uploadSessionFile.getServerFile().exists(), is(false));
}

From source file:org.silverpeas.core.process.io.file.FileHandler.java

/**
 * @see FileUtils/*  w w w  .  jav a  2 s.  c  o  m*/
 */
protected void touch(final FileBasePath basePath, final File file, final String... names) throws Exception {
    verify(basePath, file);
    FileUtils.touch(getFileForWriting(basePath, FileUtils.getFile(file, names), true));
}

From source file:org.silverpeas.core.util.FileUtilTest.java

@BeforeEach
public void setUp() throws IOException {
    rootFolder = File.createTempFile("root", "Folder");
    if (rootFolder.exists()) {
        FileUtils.deleteQuietly(rootFolder);
        rootFolder = new File(FileUtils.getTempDirectory(), "rootFolder_" + UUID.randomUUID().toString());
    }/*from   w  ww  .  ja  va2 s.  co m*/
    rootFolder.mkdirs();
    FileUtils.touch(new File(rootFolder, "atRoot.txt"));
    File subFolderA = new File(rootFolder, "SubFolderA");
    subFolderA.mkdir();
    FileUtils.touch(new File(subFolderA, "atSubFolderA_1.txt"));
    FileUtils.touch(new File(subFolderA, "atSubFolderA_2.txt"));
    File subFolderB = new File(rootFolder, "SubFolderB");
    subFolderB.mkdir();
    FileUtils.touch(new File(subFolderB, "atSubFolderB_1.txt"));
    FileUtils.touch(new File(subFolderB, "sameName.txt"));
    File subFolderBSubFolderA = new File(subFolderB, "SubFolderBSubFolderA");
    subFolderBSubFolderA.mkdir();
    FileUtils.touch(new File(subFolderBSubFolderA, "atSubFolderBSubFolderA_1.txt"));
    FileUtils.touch(new File(subFolderBSubFolderA, "atSubFolderBSubFolderA_2.txt"));
    FileUtils.touch(new File(subFolderBSubFolderA, "sameName.txt"));

    String[] expectedFiles = new String[] { "/atRoot.txt", "/SubFolderA/atSubFolderA_1.txt",
            "/SubFolderA/atSubFolderA_2.txt", "/SubFolderB/atSubFolderB_1.txt", "/SubFolderB/sameName.txt",
            "/SubFolderB/SubFolderBSubFolderA/atSubFolderBSubFolderA_1.txt",
            "/SubFolderB/SubFolderBSubFolderA/atSubFolderBSubFolderA_2.txt",
            "/SubFolderB/SubFolderBSubFolderA/sameName.txt" };

    List<String> actualFiles = new ArrayList<String>();
    int substringIndex = rootFolder.getPath().length();
    for (File file : FileUtils.listFiles(rootFolder, FileFilterUtils.trueFileFilter(),
            FileFilterUtils.trueFileFilter())) {
        actualFiles.add(FilenameUtils.separatorsToUnix(file.getPath().substring(substringIndex)));
    }
    assertThat(actualFiles, containsInAnyOrder(expectedFiles));
}

From source file:org.sipfoundry.sipxconfig.snmp.SnmpConfig.java

@Override
public void replicate(ConfigManager manager, ConfigRequest request) throws IOException {
    if (!request.applies(SnmpManager.FEATURE, LocationsManager.FEATURE, Alarms.FEATURE)) {
        return;/*  w  w w . j  a v  a 2s .  com*/
    }
    Set<Location> locations = request.locations(manager);
    boolean enabled = manager.getFeatureManager().isFeatureEnabled(SnmpManager.FEATURE);
    boolean alarmsEnabled = manager.getFeatureManager().isFeatureEnabled(Alarms.FEATURE);
    File gdir = manager.getGlobalDataDirectory();
    SnmpSettings settings = m_snmp.getSettings();
    List<Alarm> alarms = null;
    if (alarmsEnabled) {
        alarms = m_alarmServerManager.getAlarms();
    }

    Writer cfdat = new FileWriter(new File(gdir, "snmpd.cfdat"));
    try {
        CfengineModuleConfiguration config = new CfengineModuleConfiguration(cfdat);
        config.writeClass(SnmpManager.FEATURE.getId(), enabled);
        config.writeSettings("snmp_", settings.getSettings().getSetting("cfdat"));
    } finally {
        IOUtils.closeQuietly(cfdat);
    }

    File fixDeadProcs = new File(gdir, "snmp_fix_dead_processes");
    if (enabled && settings.isFixDeadProcesses()) {
        FileUtils.touch(fixDeadProcs);
    } else if (fixDeadProcs.exists()) {
        fixDeadProcs.delete();
    }

    if (enabled) {
        for (Location location : locations) {
            File dir = manager.getLocationDataDirectory(location);
            List<ProcessDefinition> defs = m_snmp.getProcessDefinitions(location);
            Writer wtr = new FileWriter(new File(dir, "snmpd.conf.part"));
            try {
                if (alarmsEnabled) {
                    writeActiveAlarms(wtr, alarms, location);
                }
                writeProcesses(wtr, defs);
            } finally {
                IOUtils.closeQuietly(wtr);
            }
        }
    }
}