From 41c3d413deb7e7a278dc546b4ac39d08e013e4db Mon Sep 17 00:00:00 2001 From: plough Date: Wed, 10 Apr 2019 13:48:54 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-14865=20=E6=9B=B4=E6=96=B0=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=8E=A8=E9=80=81=3D>=E5=AE=9E=E7=8E=B0=E5=BC=B9?= =?UTF-8?q?=E7=AA=97=E9=80=BB=E8=BE=91=E7=9A=84=E6=A0=B8=E5=BF=83=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../push/DesignerPushUpdateManager.java | 47 +++++++-- .../onlineupdate/push/DesignerUpdateInfo.java | 84 ++++++++++++++++ .../push/DesignerPushUpdateManagerTest.java | 32 +++++++ .../push/DesignerUpdateInfoTest.java | 95 +++++++++++++++++++ 4 files changed, 252 insertions(+), 6 deletions(-) create mode 100644 designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerUpdateInfo.java create mode 100644 designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManagerTest.java create mode 100644 designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerUpdateInfoTest.java diff --git a/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManager.java b/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManager.java index 469c0196e..3e9abd982 100644 --- a/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManager.java +++ b/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManager.java @@ -1,6 +1,10 @@ package com.fr.design.onlineupdate.push; +import com.fr.general.CloudCenter; import com.fr.general.GeneralContext; +import com.fr.general.GeneralUtils; +import com.fr.general.http.HttpClient; +import com.fr.json.JSONObject; import com.fr.workspace.WorkContext; /** @@ -8,10 +12,11 @@ import com.fr.workspace.WorkContext; */ public class DesignerPushUpdateManager { private static DesignerPushUpdateManager singleton; -// private DesignerUpdateInfo updateInfo; + private DesignerUpdateInfo updateInfo; + private DesignerPushUpdateConfigManager config; private DesignerPushUpdateManager() { - + config = DesignerPushUpdateConfigManager.getInstance(); } public static DesignerPushUpdateManager getInstance() { @@ -21,19 +26,49 @@ public class DesignerPushUpdateManager { return singleton; } + private void initUpdateInfo() { + String currentVersion = GeneralUtils.readFullBuildNO(); + + // todo:耗时请求,可能需要优化 + HttpClient hc = new HttpClient(CloudCenter.getInstance().acquireUrlByKind("jar10.update")); + String latestVersion = new JSONObject(hc.getResponseText()).optString("versionNO"); + + String updatePushInfo = CloudCenter.getInstance().acquireUrlByKind("update.push"); + JSONObject pushData = new JSONObject(updatePushInfo); + + updateInfo = new DesignerUpdateInfo(currentVersion, latestVersion, pushData); + } + /** * "自动更新推送"选项是否生效 */ public boolean isAutoPushUpdateSupported() { + boolean isLocalEnv = WorkContext.getCurrent().isLocal(); + boolean isChineseEnv = GeneralContext.isChineseEnv(); + + return isAutoPushUpdateSupported(isLocalEnv, isChineseEnv); + } + + private boolean isAutoPushUpdateSupported(boolean isLocalEnv, boolean isChineseEnv) { // 远程设计和非中文环境,都不生效 - return WorkContext.getCurrent().isLocal() && GeneralContext.isChineseEnv(); + return isLocalEnv && isChineseEnv; } + /** * 检查更新,如果有合适的更新版本,则弹窗 */ -// public void checkAndPop() { -// updateInfo. -// } + public void popUpDialog() { + if (!shouldPopUp()) { + return; + } + // todo: do pop + } + private boolean shouldPopUp() { + if (updateInfo == null) { + initUpdateInfo(); + } + return isAutoPushUpdateSupported() && updateInfo.hasNewPushVersion(config.getLastIgnoredVersion()); + } } diff --git a/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerUpdateInfo.java b/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerUpdateInfo.java new file mode 100644 index 000000000..0c3ecff4c --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerUpdateInfo.java @@ -0,0 +1,84 @@ +package com.fr.design.onlineupdate.push; + +import com.fr.general.ComparatorUtils; +import com.fr.json.JSONObject; +import com.fr.stable.StringUtils; + +import java.security.InvalidParameterException; + +/** + * Created by plough on 2019/4/8. + */ +class DesignerUpdateInfo { + private static final String KEY_VERSION = "version"; + private static final String KEY_CONTENT = "content"; + private static final String KEY_BACKGROUND_URL = "background"; + private static final String KEY_MORE_INFO_URL = "more"; + + private final String currentVersion; // 当前版本 + private final String latestVersion; // 最新版本 + + private final String pushVersion; // 推送版本 + private final String pushContent; // 推送更新内容 + private final String backgroundUrl; // 推送背景图片 url + private final String moreInfoUrl; // 更多新特性 + + DesignerUpdateInfo(String currentVersion, String latestVersion, JSONObject pushData) { + this.currentVersion = currentVersion; + this.latestVersion = latestVersion; + + this.pushVersion = pushData.optString(KEY_VERSION); + this.pushContent = pushData.optString(KEY_CONTENT); + this.backgroundUrl = pushData.optString(KEY_BACKGROUND_URL); + this.moreInfoUrl = pushData.optString(KEY_MORE_INFO_URL); + + // 简单做下参数校验 + if (hasEmptyField()) { + throw new InvalidParameterException(); + } + } + + private boolean hasEmptyField() { + return StringUtils.isEmpty(currentVersion) + || StringUtils.isEmpty(latestVersion) + || StringUtils.isEmpty(pushVersion) + || StringUtils.isEmpty(pushContent) + || StringUtils.isEmpty(backgroundUrl) + || StringUtils.isEmpty(moreInfoUrl); + } + + String getCurrentVersion() { + return currentVersion; + } + + String getPushVersion() { + return pushVersion; + } + + String getLatestVersion() { + return latestVersion; + } + + String getPushContent() { + return pushContent; + } + + String getBackgroundUrl() { + return backgroundUrl; + } + + String getMoreInfoUrl() { + return moreInfoUrl; + } + + boolean hasNewPushVersion(String lastIgnoredVersion) { + boolean result = ComparatorUtils.compare(pushVersion, currentVersion) > 0 + && ComparatorUtils.compare(pushVersion, latestVersion) <= 0; + if (StringUtils.isNotEmpty(lastIgnoredVersion)) { + result = result && ComparatorUtils.compare(pushVersion, lastIgnoredVersion) > 0; + } + + return result; + } + +} diff --git a/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManagerTest.java b/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManagerTest.java new file mode 100644 index 000000000..4e3f823e9 --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManagerTest.java @@ -0,0 +1,32 @@ +package com.fr.design.onlineupdate.push; + +import com.fr.invoke.Reflect; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + +/** + * Created by plough on 2019/4/8. + */ +public class DesignerPushUpdateManagerTest { + @Test + public void testSingleton() { + DesignerPushUpdateManager m1 = DesignerPushUpdateManager.getInstance(); + DesignerPushUpdateManager m2 = DesignerPushUpdateManager.getInstance(); + assertSame(m1, m2); + } + + @Test + public void testIsAutoPushUpdateSupported() { + // 中文环境 + 本地设计 -> true + DesignerPushUpdateManager pushUpdateManager = DesignerPushUpdateManager.getInstance(); + assertEquals(true, Reflect.on(pushUpdateManager).call("isAutoPushUpdateSupported", true, true).get()); + + // 非中文环境 || 远程设计 -> false + assertEquals(false, Reflect.on(pushUpdateManager).call("isAutoPushUpdateSupported", false, true).get()); + assertEquals(false, Reflect.on(pushUpdateManager).call("isAutoPushUpdateSupported", true, false).get()); + assertEquals(false, Reflect.on(pushUpdateManager).call("isAutoPushUpdateSupported", false, false).get()); + } + +} diff --git a/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerUpdateInfoTest.java b/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerUpdateInfoTest.java new file mode 100644 index 000000000..b346d9b4a --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerUpdateInfoTest.java @@ -0,0 +1,95 @@ +package com.fr.design.onlineupdate.push; + +import com.fr.json.JSONObject; +import com.fr.stable.StringUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.security.InvalidParameterException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Created by plough on 2019/4/9. + */ +public class DesignerUpdateInfoTest { + private static final String CURRENT_VERSION = "2018.09.03.xx"; + private static final String LATEST_VERSION = "2019.04.03.yy"; + private static final String PUSH_VERSION = "2019.01.03.21.11"; + private static final String PUSH_CONTENT = "the update desc content"; + private static final String PUSH_BACKGROUND = "http://image.fr.com/123.jpg"; + private static final String PUSH_MORE = "http://help.finereport.com/xxx"; + private DesignerUpdateInfo updateInfo; + + @Before + public void setUp() { + JSONObject pushData = JSONObject.create(); + + pushData.put("version", PUSH_VERSION); + pushData.put("content", PUSH_CONTENT); + pushData.put("background", PUSH_BACKGROUND); + pushData.put("more", PUSH_MORE); + + updateInfo = new DesignerUpdateInfo(CURRENT_VERSION, LATEST_VERSION, pushData); + } + + @Test + public void testGetters() { + assertEquals(CURRENT_VERSION, updateInfo.getCurrentVersion()); + assertEquals(LATEST_VERSION, updateInfo.getLatestVersion()); + assertEquals(PUSH_VERSION, updateInfo.getPushVersion()); + assertEquals(PUSH_CONTENT, updateInfo.getPushContent()); + assertEquals(PUSH_BACKGROUND, updateInfo.getBackgroundUrl()); + assertEquals(PUSH_MORE, updateInfo.getMoreInfoUrl()); + } + + @Test + public void testHasNewPushVersion() { + // (1)最近被跳过的维护版本号 X0; + // (2)本地版本号 Y; + // (3)最新的推送版本号 X; + // (4)最新的版本号 Z + // 必须满足:Y < X <= Z && X > X0,才返回 true + + // 1 true + assertTrue(hasNewVersion("2019.01.03.xx", "2018.05.03.xx", "2019.04.03.yy", "2018.05.03.xx")); + assertTrue(hasNewVersion("2019.01.03.xx", "2018.05.03.xx", "2019.04.03.yy", null)); + assertTrue(hasNewVersion("2019.01.03.xx", "2018.05.03.xx", "2019.04.03.yy", StringUtils.EMPTY)); + + // 2 false + // 2.1 X <= Y && X > X0 + assertFalse(hasNewVersion("2019.01.03.xx", "2019.03.03.xx", "2019.04.03.yy", "2018.05.03.xx")); + assertFalse(hasNewVersion("2019.03.03.xx", "2019.03.03.xx", "2019.04.03.yy", "2018.05.03.xx")); + + // 2.2 X > Z && X > X0 + assertFalse(hasNewVersion("2020.01.03.xx", "2019.03.03.xx", "2019.04.03.yy", "2018.05.03.xx")); + + // 2.3 Y < X <= Z && X <= X0 + assertFalse(hasNewVersion("2019.01.03.xx", "2018.05.03.xx", "2019.04.03.yy", "2019.02.03.xx")); + assertFalse(hasNewVersion("2019.01.03.xx", "2018.05.03.xx", "2019.04.03.yy", "2019.01.03.xx")); + } + + + private boolean hasNewVersion(String X, String Y, String Z, String X0) { + JSONObject pushData = JSONObject.create(); + pushData.put("version", X); + pushData.put("content", PUSH_CONTENT); + pushData.put("background", PUSH_BACKGROUND); + pushData.put("more", PUSH_MORE); + DesignerUpdateInfo updateInfo = new DesignerUpdateInfo(Y, Z, pushData); + return updateInfo.hasNewPushVersion(X0); + } + + @Test + public void testParameterValidation() { + try { + DesignerUpdateInfo updateInfo = new DesignerUpdateInfo(null, null, new JSONObject()); + Assert.fail("should not reach here!"); + } catch (InvalidParameterException e) { + // do nothing + } + } +}