Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* 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 f51b095
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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));
}
}

Expand Down
@@ -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
Expand Down Expand Up @@ -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)));
Expand Down
@@ -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.