plough
6 years ago
4 changed files with 252 additions and 6 deletions
@ -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; |
||||
} |
||||
|
||||
} |
@ -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()); |
||||
} |
||||
|
||||
} |
@ -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…
Reference in new issue