From 9e8009e3193b3763eb2a33576fef2825bb72da82 Mon Sep 17 00:00:00 2001 From: bryant Date: Tue, 29 Sep 2020 17:03:19 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-40472=20=E8=AE=BE=E8=AE=A1=E5=99=A8?= =?UTF-8?q?=E5=86=85=E8=BF=98=E5=8E=9F=E5=88=B0=E5=8E=86=E5=8F=B2=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=90=8E=E5=A4=87=E4=BB=BD=E7=9A=84=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9=E6=B2=A1=E6=9C=89=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../update/factory/UpdateFileFactory.java | 50 +++++++++++++++++++ .../update/ui/dialog/RestoreDialog.java | 2 +- .../update/ui/dialog/UpdateMainDialog.java | 2 +- .../design/update/utils/UpdateFileUtils.java | 33 +++--------- .../update/factory/UpdateFileFactoryTest.java | 28 +++++++++++ .../update/utils/UpdateFileUtilsTest.java | 10 ++-- 6 files changed, 93 insertions(+), 32 deletions(-) create mode 100644 designer-base/src/main/java/com/fr/design/update/factory/UpdateFileFactory.java create mode 100644 designer-base/src/test/java/com/fr/design/update/factory/UpdateFileFactoryTest.java diff --git a/designer-base/src/main/java/com/fr/design/update/factory/UpdateFileFactory.java b/designer-base/src/main/java/com/fr/design/update/factory/UpdateFileFactory.java new file mode 100644 index 0000000000..cf992fe08b --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/update/factory/UpdateFileFactory.java @@ -0,0 +1,50 @@ +package com.fr.design.update.factory; + +import com.fr.decision.update.data.UpdateConstants; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.StableUtils; + +import java.io.File;; + +/** + * @author Bryant + * @version 10.0 + * Created by Bryant on 2020-09-29 + */ +public class UpdateFileFactory { + + private UpdateFileFactory() { + } + + public static File[] getBackupVersions() { + File[] versions = null; + try { + File backupDir = new File(StableUtils.pathJoin(StableUtils.getInstallHome(), UpdateConstants.DESIGNER_BACKUP_DIR)); + StableUtils.mkdirs(backupDir); + versions = backupDir.listFiles(); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + return versions; + } + + public static boolean isBackupVersionsValid(String version) { + boolean designerValid = false; + boolean envValid = false; + try { + File designerLib = new File(StableUtils.pathJoin(version, UpdateConstants.DESIGNERBACKUPPATH)); + File[] jars = designerLib.listFiles(); + if (jars != null && jars.length > 0) { + designerValid = true; + } + File envLib = new File(StableUtils.pathJoin(version, UpdateConstants.BACKUPPATH)); + jars = envLib.listFiles(); + if (jars != null && jars.length > 0) { + envValid = true; + } + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + return designerValid && envValid; + } +} diff --git a/designer-base/src/main/java/com/fr/design/update/ui/dialog/RestoreDialog.java b/designer-base/src/main/java/com/fr/design/update/ui/dialog/RestoreDialog.java index f54b811852..69f3f61604 100644 --- a/designer-base/src/main/java/com/fr/design/update/ui/dialog/RestoreDialog.java +++ b/designer-base/src/main/java/com/fr/design/update/ui/dialog/RestoreDialog.java @@ -86,7 +86,7 @@ public class RestoreDialog extends JDialog { JPanel jarListPane = new JPanel(); jarListPane.setLayout(new BoxLayout(jarListPane, BoxLayout.Y_AXIS)); - String[] jarBackupFiles = UpdateFileUtils.listFilteredFiles(StableUtils.getInstallHome(), UpdateConstants.DESIGNER_BACKUP_DIR); + String[] jarBackupFiles = UpdateFileUtils.listBackupVersions(); ArrayUtils.reverse(jarBackupFiles); String[] jarFilesList = ((jarBackupFiles.length < NUMOFCELL_LEAST) ? Arrays.copyOf(jarBackupFiles, NUMOFCELL_LEAST) : jarBackupFiles); diff --git a/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java b/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java index 6163d44b55..3c91ebf145 100644 --- a/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java +++ b/designer-base/src/main/java/com/fr/design/update/ui/dialog/UpdateMainDialog.java @@ -320,7 +320,7 @@ public class UpdateMainDialog extends UIDialog { } }); //choose RestoreLabel to show - boolean isNeedRestore = ArrayUtils.isNotEmpty(UpdateFileUtils.listFilteredFiles(StableUtils.getInstallHome(), getBackupDirectory())); + boolean isNeedRestore = ArrayUtils.isNotEmpty(UpdateFileUtils.listBackupVersions()); jarRestoreLabel = isNeedRestore ? jarRestorePreviousRevision : noJarPreviousRevision; } diff --git a/designer-base/src/main/java/com/fr/design/update/utils/UpdateFileUtils.java b/designer-base/src/main/java/com/fr/design/update/utils/UpdateFileUtils.java index 9caaf2d863..7a3c0a2a8a 100644 --- a/designer-base/src/main/java/com/fr/design/update/utils/UpdateFileUtils.java +++ b/designer-base/src/main/java/com/fr/design/update/utils/UpdateFileUtils.java @@ -1,5 +1,6 @@ package com.fr.design.update.utils; +import com.fr.design.update.factory.UpdateFileFactory; import com.fr.stable.StableUtils; import java.io.File; @@ -12,41 +13,19 @@ import java.util.List; * Created by Bryant on 2020-09-25 */ public class UpdateFileUtils { - /** * 列出过滤后的文件 * - * @param installHome 安装目录 - * @param backupdir 备份目录 * @return String数组 */ - public static String[] listFilteredFiles(String installHome, String backupdir) { - File backupDir = new File(StableUtils.pathJoin(installHome, backupdir)); - StableUtils.mkdirs(backupDir); - File[] versionBackup = backupDir.listFiles(); + public static String[] listBackupVersions() { + File[] versionBackup = UpdateFileFactory.getBackupVersions(); List versions = new ArrayList<>(); if (versionBackup != null) { for (File file : versionBackup) { - boolean canBackup = true; - if (file.isDirectory()) { - File[] libs = file.listFiles(); - if (libs == null || libs.length == 0) { - canBackup = false; - } else { - for (File lib : libs) { - if (lib.isDirectory()) { - File[] jars = lib.listFiles(); - if (jars == null || jars.length == 0) { - canBackup = false; - } - } - } - } - if (canBackup) { - versions.add(file.getName()); - } - } - if (!canBackup) { + if (UpdateFileFactory.isBackupVersionsValid(file.getAbsolutePath())) { + versions.add(file.getName()); + } else { StableUtils.deleteFile(file); } } diff --git a/designer-base/src/test/java/com/fr/design/update/factory/UpdateFileFactoryTest.java b/designer-base/src/test/java/com/fr/design/update/factory/UpdateFileFactoryTest.java new file mode 100644 index 0000000000..6c2211e76c --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/update/factory/UpdateFileFactoryTest.java @@ -0,0 +1,28 @@ +package com.fr.design.update.factory; + +import com.fr.decision.update.data.UpdateConstants; +import com.fr.stable.StableUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; + +public class UpdateFileFactoryTest { + + @Test + public void testGetBackupVersions() { + Assert.assertEquals(0, UpdateFileFactory.getBackupVersions().length); + File backupDir = new File(StableUtils.pathJoin(StableUtils.getInstallHome(), UpdateConstants.DESIGNER_BACKUP_DIR)); + StableUtils.deleteFile(backupDir); + } + + @Test + public void testIsBackupVersionsValid() { + File des = new File(StableUtils.pathJoin(StableUtils.getInstallHome(), UpdateConstants.DESIGNER_BACKUP_DIR, "test", UpdateConstants.BACKUPPATH, "test")); + File env = new File(StableUtils.pathJoin(StableUtils.getInstallHome(), UpdateConstants.DESIGNER_BACKUP_DIR, "test", UpdateConstants.DESIGNERBACKUPPATH, "test")); + StableUtils.mkdirs(des); + StableUtils.mkdirs(env); + Assert.assertTrue(UpdateFileFactory.isBackupVersionsValid(StableUtils.pathJoin(StableUtils.getInstallHome(), UpdateConstants.DESIGNER_BACKUP_DIR, "test"))); + StableUtils.deleteFile(new File(StableUtils.pathJoin(StableUtils.getInstallHome(), UpdateConstants.DESIGNER_BACKUP_DIR))); + } +} diff --git a/designer-base/src/test/java/com/fr/design/update/utils/UpdateFileUtilsTest.java b/designer-base/src/test/java/com/fr/design/update/utils/UpdateFileUtilsTest.java index a188125ed2..8788675352 100644 --- a/designer-base/src/test/java/com/fr/design/update/utils/UpdateFileUtilsTest.java +++ b/designer-base/src/test/java/com/fr/design/update/utils/UpdateFileUtilsTest.java @@ -1,5 +1,6 @@ package com.fr.design.update.utils; +import com.fr.decision.update.data.UpdateConstants; import com.fr.stable.StableUtils; import org.junit.Assert; import org.junit.Test; @@ -15,9 +16,12 @@ public class UpdateFileUtilsTest { @Test public void testListFilteredFiles() { - StableUtils.mkdirs(new File("home/backup/version/lib/jars")); - String[] result = UpdateFileUtils.listFilteredFiles("home", "backup"); + File des = new File(StableUtils.pathJoin(StableUtils.getInstallHome(), UpdateConstants.DESIGNER_BACKUP_DIR, "test", UpdateConstants.BACKUPPATH, "test")); + File env = new File(StableUtils.pathJoin(StableUtils.getInstallHome(), UpdateConstants.DESIGNER_BACKUP_DIR, "test", UpdateConstants.DESIGNERBACKUPPATH, "test")); + StableUtils.mkdirs(des); + StableUtils.mkdirs(env); + String[] result = UpdateFileUtils.listBackupVersions(); Assert.assertEquals(1, result.length); - StableUtils.deleteFile(new File("home")); + StableUtils.deleteFile(new File(StableUtils.pathJoin(StableUtils.getInstallHome(), UpdateConstants.DESIGNER_BACKUP_DIR))); } }