Browse Source

REPORT-14865 更新日志推送=>实现弹窗逻辑的核心类

research/10.0
plough 6 years ago
parent
commit
41c3d413de
  1. 47
      designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManager.java
  2. 84
      designer-base/src/main/java/com/fr/design/onlineupdate/push/DesignerUpdateInfo.java
  3. 32
      designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerPushUpdateManagerTest.java
  4. 95
      designer-base/src/test/java/com/fr/design/onlineupdate/push/DesignerUpdateInfoTest.java

47
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());
}
}

84
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;
}
}

32
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());
}
}

95
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
}
}
}
Loading…
Cancel
Save