Skip to content
Permalink
Browse files

* fix issue JENKINS-8781

git-svn-id: https://svn.jenkins-ci.org/trunk/hudson/plugins/thinBackup@38689 71c3de6d-444a-0410-be80-ed276b4c234a
  • Loading branch information
tofuatjava committed Feb 13, 2011
1 parent 95ea764 commit f51b0952dab8a7fa30a912a0445844fb3b620185
@@ -84,4 +84,7 @@
<url>http://maven.hudson-labs.org:8081/content/repositories/releases/</url>
</repository>
</distributionManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2011 Matthias Steinkogler, Thomas Fürer
* Copyright (C) 2011 Matthias Steinkogler, Thomas F�rer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -19,6 +19,10 @@
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
@@ -29,6 +33,7 @@
import org.jvnet.hudson.plugins.thinbackup.utils.Utils;

public class HudsonRestore {
private static final Logger LOGGER = Logger.getLogger("hudson.plugins.thinbackup");
private final String backupPath;
private final String restoreBackupFrom;
private final File hudsonHome;
@@ -40,18 +45,24 @@ public HudsonRestore(final File hudsonConfigurationPath, final String backupPath
}

public void restore() throws IOException {
IOFileFilter suffixFilter = FileFilterUtils.suffixFileFilter(restoreBackupFrom);
suffixFilter = FileFilterUtils.andFileFilter(suffixFilter, DirectoryFileFilter.DIRECTORY);

if (!StringUtils.isEmpty(backupPath)) {
final File[] candidates = new File(backupPath).listFiles((FileFilter) suffixFilter);
if (candidates.length == 1) {
final File toRestore = candidates[0];
if (toRestore.getName().startsWith(BackupType.DIFF.toString())) {
restore(Utils.getReferencedFullBackup(toRestore));
try {
String directoryDateFormat = Utils.convertToDirectoryNameDateFormat(restoreBackupFrom);
IOFileFilter suffixFilter = FileFilterUtils.suffixFileFilter(directoryDateFormat);
suffixFilter = FileFilterUtils.andFileFilter(suffixFilter, DirectoryFileFilter.DIRECTORY);

if (!StringUtils.isEmpty(backupPath)) {
final File[] candidates = new File(backupPath).listFiles((FileFilter) suffixFilter);
if (candidates.length == 1) {
final File toRestore = candidates[0];
if (toRestore.getName().startsWith(BackupType.DIFF.toString())) {
restore(Utils.getReferencedFullBackup(toRestore));
}
restore(toRestore);
}
restore(toRestore);
}
} catch (ParseException e) {
LOGGER.log(Level.SEVERE,
MessageFormat.format("Cannot convert diplay date format ({0}) to directory date format.", restoreBackupFrom));
}
}

@@ -1,5 +1,5 @@
/**
* Copyright (C) 2011 Matthias Steinkogler, Thomas Fürer
* Copyright (C) 2011 Matthias Steinkogler, Thomas F�rer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -102,6 +102,11 @@ public static File getReferencedFullBackup(final File diffBackup) {
return referencedFullBackup;
}

public static String convertToDirectoryNameDateFormat(String displayFormatdate) throws ParseException {
Date displayDate = DISPLAY_DATE_FORMAT.parse(displayFormatdate);
return DIRECTORY_NAME_DATE_FORMAT.format(displayDate);
}

public static File getFormattedDirectory(final File directory, final BackupType backupType, final Date date) {
final File formattedDirectory = new File(directory, String.format("%s-%s", backupType,
DIRECTORY_NAME_DATE_FORMAT.format(date)));
@@ -0,0 +1,34 @@
package org.jvnet.hudson.plugins.thinbackup.utils;

import java.text.ParseException;

import junit.framework.Assert;

import org.junit.Test;

public class TestUtils {

@Test
public void testConvertToDirectoryNameDateFormat() throws ParseException {
String displayDate = "2011-02-13 10:48";
String fileDate = Utils.convertToDirectoryNameDateFormat(displayDate);
Assert.assertEquals("2011-02-13_10-48", fileDate);
}

@Test(expected = ParseException.class)
public void testBadFormatConvertToDirectoryNameDateFormat() throws ParseException {
String displayDate = "2011-02-13-10:48";
Utils.convertToDirectoryNameDateFormat(displayDate);
}

@Test(expected = ParseException.class)
public void testWrongFormatConvertToDirectoryNameDateFormat() throws ParseException {
String displayDate = "FULL-2011-02-13_10-48";
Utils.convertToDirectoryNameDateFormat(displayDate);
}

@Test(expected = ParseException.class)
public void testEmptyDateConvertToDirectoryNameDateFormat() throws ParseException {
Utils.convertToDirectoryNameDateFormat("");
}
}

0 comments on commit f51b095

Please sign in to comment.
You can’t perform that action at this time.