From 7fdb747e80fec43b3b75105f25dae18cb22cbd50 Mon Sep 17 00:00:00 2001 From: plough Date: Fri, 19 Apr 2019 15:21:23 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-15314=20103=E6=A8=A1=E6=9D=BF=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=94=B6=E9=9B=86=3D>=E5=8F=A6=E5=AD=98=E4=B8=BA?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/design/mainframe/JTemplate.java | 15 ++- .../mainframe/template/info/TemplateInfo.java | 50 ++++--- .../template/info/TemplateInfoCollector.java | 24 +++- .../info/TemplateInfoCollectorTest.java | 119 ++++++++++++++++- .../template/info/TemplateInfoTest.java | 125 ++++++++++++++++++ .../template/info/TemplateInfoTestHelper.java | 49 +++++++ 6 files changed, 356 insertions(+), 26 deletions(-) create mode 100644 designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoTest.java create mode 100644 designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoTestHelper.java diff --git a/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java b/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java index f4aa5bc9ba..9708dcb65a 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java @@ -145,13 +145,17 @@ public abstract class JTemplate> } private void collectInfo() { // 执行收集操作 + collectInfo(StringUtils.EMPTY); + } + + private void collectInfo(String originID) { // 执行收集操作 if (openTime == 0) { // 旧模板,不收集数据 return; } long saveTime = System.currentTimeMillis(); // 保存模板的时间点 try { long timeConsume = ((saveTime - openTime) / ONE_THOUSAND); // 制作模板耗时(单位:s) - TemplateInfoCollector.getInstance().collectInfo(template.getTemplateID(), getProcessInfo(), timeConsume); + TemplateInfoCollector.getInstance().collectInfo(template.getTemplateID(), originID, getProcessInfo(), (int)timeConsume); } catch (Throwable th) { // 不管收集过程中出现任何异常,都不应该影响模版保存 } openTime = saveTime; // 更新 openTime,准备下一次计算 @@ -605,15 +609,20 @@ public abstract class JTemplate> } } + // 保存新模板时会进入此方法(新建模板直接保存,或者另存为) protected boolean saveNewFile(FILE editingFILE, String oldName) { + String originID = StringUtils.EMPTY; + if (StringUtils.isNotEmpty(this.template.getTemplateID())) { + originID = this.template.getTemplateID(); + } // 在保存之前,初始化 templateID - initForCollect(); // 如果保存新模板(新建模板直接保存,或者另存为),则添加 templateID + initForCollect(); this.editingFILE = editingFILE; boolean result = this.saveFile(); if (result) { DesignerFrameFileDealerPane.getInstance().refresh(); - collectInfo(); + collectInfo(originID); } //更换最近打开 DesignerEnvManager.getEnvManager().replaceRecentOpenedFilePath(oldName, this.getPath()); diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java index 7f2e1e7661..af644a1e81 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java @@ -14,6 +14,7 @@ import com.fr.stable.xml.XMLableReader; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashMap; +import java.util.Map; /** * 对应一张模版的记录 @@ -26,6 +27,7 @@ class TemplateInfo implements XMLReadable, XMLWriter { private static final String XML_CONSUMING_MAP = "consumingMap"; private static final String ATTR_DAY_COUNT = "day_count"; private static final String ATTR_TEMPLATE_ID = "templateID"; + private static final String ATTR_ORIGIN_ID = "originID"; private static final String ATTR_PROCESS = "process"; private static final String ATTR_FLOAT_COUNT = "float_count"; private static final String ATTR_WIDGET_COUNT = "widget_count"; @@ -37,6 +39,7 @@ class TemplateInfo implements XMLReadable, XMLWriter { private static final String ATTR_CREATE_TIME = "create_time"; private static final String ATTR_UUID = "uuid"; private static final String ATTR_TIME_CONSUME = "time_consume"; + private static final String ATTR_ORIGIN_TIME = "originTime"; private static final String ATTR_VERSION = "version"; private static final String ATTR_USERNAME = "username"; @@ -45,14 +48,20 @@ class TemplateInfo implements XMLReadable, XMLWriter { private static final int COMPLETE_DAY_COUNT = 15; // 判断模板是否完成的天数 private int idleDayCount; // 到现在为止,模版闲置(上次保存后没有再编辑过)的天数 - private String templateID; + private String templateID = StringUtils.EMPTY; + private String originID = StringUtils.EMPTY; // todo: processMap 和 consumingMap 还可以再拆解为小类,以后继续重构 - private HashMap processMap = new HashMap<>(); - private HashMap consumingMap = new HashMap<>(); + private Map processMap = new HashMap<>(); + private Map consumingMap = new HashMap<>(); private TemplateInfo() { } + private TemplateInfo(String templateID, String originID) { + this.templateID = templateID; + this.originID = originID; + } + static TemplateInfo newInstanceByRead(XMLableReader reader) { TemplateInfo templateInfo = new TemplateInfo(); reader.readXMLObject(templateInfo); @@ -60,6 +69,10 @@ class TemplateInfo implements XMLReadable, XMLWriter { } static TemplateInfo newInstance(String templateID) { + return newInstance(templateID, StringUtils.EMPTY, 0); + } + + static TemplateInfo newInstance(String templateID, String originID, int originTime) { HashMap consumingMap = new HashMap<>(); String username = MarketConfig.getInstance().getBbsUsername(); @@ -72,12 +85,14 @@ class TemplateInfo implements XMLReadable, XMLWriter { consumingMap.put(ATTR_UUID, uuid); consumingMap.put(ATTR_ACTIVITYKEY, activitykey); consumingMap.put(ATTR_TEMPLATE_ID, templateID); + consumingMap.put(ATTR_ORIGIN_ID, originID); consumingMap.put(ATTR_CREATE_TIME, createTime); - consumingMap.put(ATTR_TIME_CONSUME, 0); + consumingMap.put(ATTR_TIME_CONSUME, originTime); // timeConsume 在原来模版的基础上累加 + consumingMap.put(ATTR_ORIGIN_TIME, originTime); consumingMap.put(ATTR_JAR_TIME, jarTime); consumingMap.put(ATTR_VERSION, version); - TemplateInfo templateInfo = new TemplateInfo(); + TemplateInfo templateInfo = new TemplateInfo(templateID, originID); templateInfo.consumingMap = consumingMap; return templateInfo; @@ -87,12 +102,8 @@ class TemplateInfo implements XMLReadable, XMLWriter { return templateID; } - HashMap getTemplateInfo() { - HashMap templateInfo = new HashMap<>(); - templateInfo.put(XML_PROCESS_MAP, processMap); - templateInfo.put(XML_CONSUMING_MAP, consumingMap); - templateInfo.put(ATTR_DAY_COUNT, idleDayCount); - return templateInfo; + int getTimeConsume() { + return (int)consumingMap.get(ATTR_TIME_CONSUME); } public void writeXML(XMLPrintWriter writer) { @@ -100,6 +111,9 @@ class TemplateInfo implements XMLReadable, XMLWriter { if (StringUtils.isNotEmpty(templateID)) { writer.attr(ATTR_TEMPLATE_ID, this.templateID); } + if (StringUtils.isNotEmpty(originID)) { + writer.attr(ATTR_ORIGIN_ID, this.originID); + } if (idleDayCount >= 0) { writer.attr(ATTR_DAY_COUNT, this.idleDayCount); } @@ -126,7 +140,8 @@ class TemplateInfo implements XMLReadable, XMLWriter { writer.attr(ATTR_JAR_TIME, (String) consumingMap.get(ATTR_JAR_TIME)); writer.attr(ATTR_CREATE_TIME, (String) consumingMap.get(ATTR_CREATE_TIME)); writer.attr(ATTR_UUID, (String) consumingMap.get(ATTR_UUID)); - writer.attr(ATTR_TIME_CONSUME, (long) consumingMap.get(ATTR_TIME_CONSUME)); + writer.attr(ATTR_TIME_CONSUME, (int)consumingMap.get(ATTR_TIME_CONSUME)); + writer.attr(ATTR_ORIGIN_TIME, (int)consumingMap.get(ATTR_ORIGIN_TIME)); writer.attr(ATTR_VERSION, (String) consumingMap.get(ATTR_VERSION)); writer.attr(ATTR_USERNAME, (String) consumingMap.get(ATTR_USERNAME)); writer.end(); @@ -136,6 +151,7 @@ class TemplateInfo implements XMLReadable, XMLWriter { if (!reader.isChildNode()) { idleDayCount = reader.getAttrAsInt(ATTR_DAY_COUNT, 0); templateID = reader.getAttrAsString(ATTR_TEMPLATE_ID, StringUtils.EMPTY); + originID = reader.getAttrAsString(ATTR_ORIGIN_ID, StringUtils.EMPTY); } else { try { String name = reader.getTagName(); @@ -152,8 +168,10 @@ class TemplateInfo implements XMLReadable, XMLWriter { consumingMap.put(ATTR_JAR_TIME, reader.getAttrAsString(ATTR_JAR_TIME, StringUtils.EMPTY)); consumingMap.put(ATTR_CREATE_TIME, reader.getAttrAsString(ATTR_CREATE_TIME, StringUtils.EMPTY)); consumingMap.put(ATTR_TEMPLATE_ID, templateID); + consumingMap.put(ATTR_ORIGIN_ID, originID); consumingMap.put(ATTR_UUID, reader.getAttrAsString(ATTR_UUID, StringUtils.EMPTY)); - consumingMap.put(ATTR_TIME_CONSUME, reader.getAttrAsLong(ATTR_TIME_CONSUME, 0)); + consumingMap.put(ATTR_TIME_CONSUME, reader.getAttrAsInt(ATTR_TIME_CONSUME, 0)); + consumingMap.put(ATTR_ORIGIN_TIME, reader.getAttrAsInt(ATTR_ORIGIN_TIME, 0)); consumingMap.put(ATTR_VERSION, reader.getAttrAsString(ATTR_VERSION, "8.0")); consumingMap.put(ATTR_USERNAME, reader.getAttrAsString(ATTR_USERNAME, StringUtils.EMPTY)); } @@ -173,7 +191,7 @@ class TemplateInfo implements XMLReadable, XMLWriter { int floatCount = (int) processMap.get(ATTR_FLOAT_COUNT); int blockCount = (int) processMap.get(ATTR_BLOCK_COUNT); int widgetCount = (int) processMap.get(ATTR_WIDGET_COUNT); - boolean isTestTemplate = false; + boolean isTestTemplate; if (reportType == 0) { // 普通报表 isTestTemplate = cellCount <= VALID_CELL_COUNT && floatCount <= 1 && widgetCount <= VALID_WIDGET_COUNT; } else if (reportType == 1) { // 聚合报表 @@ -200,8 +218,8 @@ class TemplateInfo implements XMLReadable, XMLWriter { return isComplete() && !isTestTemplate(); } - void addTimeConsume(long timeConsume) { - timeConsume += (long) consumingMap.get(ATTR_TIME_CONSUME); // 加上之前的累计编辑时间 + void addTimeConsume(int timeConsume) { + timeConsume += (int)consumingMap.get(ATTR_TIME_CONSUME); // 加上之前的累计编辑时间 consumingMap.put(ATTR_TIME_CONSUME, timeConsume); } diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java index 103047be75..5d5e5dd5bc 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java @@ -59,14 +59,28 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { /** * 收集模板信息。如果之前没有记录,则新增;如果已有记录,则更新。 * 同时将最新数据保存到文件中。 + * @param templateID 模版id + * @param originID 模版的原始id,仅对另存为的模版有效,对于非另存为的模版,值总是为空 + * @param processInfo 包含模版的一些基本信息(如模版类型、包含控件数量等) * @param timeConsume 本次制作耗时,单位为 s */ - public void collectInfo(String templateID, TemplateProcessInfo processInfo, long timeConsume) { + public void collectInfo(String templateID, String originID, TemplateProcessInfo processInfo, int timeConsume) { if (!shouldCollectInfo()) { return; } - TemplateInfo templateInfo = getOrCreateTemplateInfoByID(templateID); + TemplateInfo templateInfo; + if (this.contains(templateID)) { + templateInfo = templateInfoMap.get(templateID); + } else if (!this.contains(originID)) { + templateInfo = TemplateInfo.newInstance(templateID); + templateInfoMap.put(templateID, templateInfo); + } else { + int originTime = templateInfoMap.get(originID).getTimeConsume(); + templateInfo = TemplateInfo.newInstance(templateID, originID, originTime); + templateInfoMap.put(templateID, templateInfo); + } + // 收集制作耗时 templateInfo.addTimeConsume(timeConsume); // 收集模版基本信息 @@ -110,6 +124,10 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { templateInfoMap = new HashMap<>(); setDesignerOpenDate(); + loadFromFile(); + } + + void loadFromFile() { if (!getInfoFile().exists()) { return; } @@ -123,7 +141,7 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { } } - private TemplateInfo getOrCreateTemplateInfoByID(String templateID) { + TemplateInfo getOrCreateTemplateInfoByID(String templateID) { if (templateInfoMap.containsKey(templateID)) { return templateInfoMap.get(templateID); } diff --git a/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java b/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java index 1c38235017..eb9aa23468 100644 --- a/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java +++ b/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java @@ -1,8 +1,13 @@ package com.fr.design.mainframe.template.info; +import com.fr.config.MarketConfig; +import com.fr.general.GeneralUtils; import com.fr.invoke.Reflect; import com.fr.stable.ProductConstants; +import com.fr.stable.StringUtils; +import com.fr.third.org.apache.commons.io.FileUtils; import org.easymock.EasyMock; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -10,27 +15,53 @@ import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import java.io.File; +import java.io.IOException; import java.util.Map; +import static com.fr.design.mainframe.template.info.TemplateInfoTestHelper.assertJsonStringEquals; +import static com.fr.design.mainframe.template.info.TemplateInfoTestHelper.setUpMockForNewInstance; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; /** * Created by plough on 2019/4/18. */ @RunWith(PowerMockRunner.class) -@PrepareForTest(ProductConstants.class) +@PrepareForTest({ProductConstants.class, MarketConfig.class, ProductConstants.class, GeneralUtils.class}) public class TemplateInfoCollectorTest { + private String filePath; + private String initialFileContent; + private TemplateProcessInfo mockProcessInfo; @Before - public void setUp() { + public void setUp() throws IOException { PowerMock.mockStatic(ProductConstants.class); - String filePath = getClass().getResource("tpl.info").getPath(); + filePath = getClass().getResource("tpl.info").getPath(); String dirPath = filePath.substring(0, filePath.indexOf("tpl.info")); EasyMock.expect(ProductConstants.getEnvHome()).andReturn(dirPath).anyTimes(); - EasyMock.replay(); PowerMock.replayAll(); + + mockProcessInfo = EasyMock.mock(TemplateProcessInfo.class); + EasyMock.expect(mockProcessInfo.getBlockCount()).andReturn(3).anyTimes(); + EasyMock.expect(mockProcessInfo.getCellCount()).andReturn(13).anyTimes(); + EasyMock.expect(mockProcessInfo.getFloatCount()).andReturn(1).anyTimes(); + EasyMock.expect(mockProcessInfo.getReportType()).andReturn(0).anyTimes(); + EasyMock.expect(mockProcessInfo.getWidgetCount()).andReturn(0).anyTimes(); + EasyMock.replay(mockProcessInfo); + + // 缓存 tpl.info + initialFileContent = FileUtils.readFileToString(new File(filePath), "utf-8"); + + Reflect.on(TemplateInfoCollector.class).set("instance", null); + } + + @After + public void tearDown() throws IOException { + // 恢复 tpl.info + FileUtils.writeStringToFile(new File(filePath), initialFileContent, "utf-8"); } @Test @@ -39,4 +70,84 @@ public class TemplateInfoCollectorTest { assertEquals("2019-04-18", Reflect.on(collector).field("designerOpenDate").get()); assertEquals(7, ((Map) Reflect.on(collector).field("templateInfoMap").get()).size()); } + + @Test + public void testCollectInfo() { + TemplateInfoCollector collector = TemplateInfoCollector.getInstance(); + + String templateID = "16a988ce-8529-42f5-b17c-2ee849355071"; + int timeConsume = 200; + + collector.collectInfo(templateID, StringUtils.EMPTY, mockProcessInfo, timeConsume); + + // 检查是否写入成功 + collector.loadFromFile(); + TemplateInfo templateInfo = collector.getOrCreateTemplateInfoByID(templateID); + + assertJsonStringEquals("{\"process\":\"\",\"float_count\":1,\"widget_count\":0," + + "\"cell_count\":13,\"block_count\":3,\"report_type\":0," + + "\"templateID\":\"16a988ce-8529-42f5-b17c-2ee849355071\"}",templateInfo.getProcessMapJsonString()); + + assertJsonStringEquals("{\"activitykey\":\"2e0ea413-fa9c241e0-9723-4354fce51e81\"," + + "\"jar_time\":\"不是安装版本\",\"create_time\":\"2019-03-26 16:13\"," + + "\"templateID\":\"16a988ce-8529-42f5-b17c-2ee849355071\",\"originID\":\"\"," + + "\"uuid\":\"476ca2cc-f789-4c5d-8e89-ef146580775c\",\"time_consume\":329,\"originTime\":0," + + "\"version\":\"10.0\",\"username\":\"plough\"}", templateInfo.getConsumingMapJsonString()); + } + + @Test + public void testCollectInfoForNewTemplate() throws Exception { + setUpMockForNewInstance(); + + TemplateInfoCollector collector = TemplateInfoCollector.getInstance(); + + String templateID = "73a97777-8jnk-47cd-b57c-2ee89991279796"; + int timeConsume = 200; + + collector.collectInfo(templateID, StringUtils.EMPTY, mockProcessInfo, timeConsume); + + // 检查是否写入成功 + collector.loadFromFile(); + assertTrue(collector.contains(templateID)); + + TemplateInfo templateInfo = collector.getOrCreateTemplateInfoByID(templateID); + assertEquals(templateID, templateInfo.getTemplateID()); + + assertJsonStringEquals("{\"process\":\"\",\"float_count\":1,\"widget_count\":0," + + "\"cell_count\":13,\"block_count\":3,\"report_type\":0," + + "\"templateID\":\"73a97777-8jnk-47cd-b57c-2ee89991279796\"}",templateInfo.getProcessMapJsonString()); + + Map consumingMap = Reflect.on(templateInfo).field("consumingMap").get(); + assertEquals(templateID, consumingMap.get("templateID")); + assertEquals(StringUtils.EMPTY, consumingMap.get("originID")); + assertEquals(200, consumingMap.get("time_consume")); + assertEquals(0, consumingMap.get("originTime")); + } + + @Test + public void testCollectInfoWhenSaveAs() throws Exception { + setUpMockForNewInstance(); + + TemplateInfoCollector collector = TemplateInfoCollector.getInstance(); + + String templateID = "423238d4-5223-22vj-vlsj-42jc49245iw3"; + String originID = "16a988ce-8529-42f5-b17c-2ee849355071"; + int timeConsume = 200; + + collector.collectInfo(templateID, originID, mockProcessInfo, timeConsume); + + // 检查是否写入成功 + collector.loadFromFile(); + TemplateInfo templateInfo = collector.getOrCreateTemplateInfoByID(templateID); + + assertJsonStringEquals("{\"process\":\"\",\"float_count\":1,\"widget_count\":0," + + "\"cell_count\":13,\"block_count\":3,\"report_type\":0," + + "\"templateID\":\"423238d4-5223-22vj-vlsj-42jc49245iw3\"}", templateInfo.getProcessMapJsonString()); + + Map consumingMap = Reflect.on(templateInfo).field("consumingMap").get(); + assertEquals(templateID, consumingMap.get("templateID")); + assertEquals(originID, consumingMap.get("originID")); + assertEquals(329, consumingMap.get("time_consume")); + assertEquals(129, consumingMap.get("originTime")); + } } diff --git a/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoTest.java b/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoTest.java new file mode 100644 index 0000000000..19f6066ff8 --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoTest.java @@ -0,0 +1,125 @@ +package com.fr.design.mainframe.template.info; + +import com.fr.config.MarketConfig; +import com.fr.general.GeneralUtils; +import com.fr.invoke.Reflect; +import com.fr.stable.ProductConstants; +import com.fr.stable.StringUtils; +import com.fr.stable.xml.XMLableReader; +import com.fr.third.javax.xml.stream.XMLStreamException; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.io.StringReader; +import java.util.Map; + +import static com.fr.design.mainframe.template.info.TemplateInfoTestHelper.assertJsonStringEquals; +import static com.fr.design.mainframe.template.info.TemplateInfoTestHelper.setUpMockForNewInstance; +import static org.junit.Assert.assertEquals; + +/** + * Created by plough on 2019/4/19. + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({MarketConfig.class, ProductConstants.class, GeneralUtils.class}) +public class TemplateInfoTest { + + private static final String NORMAL_INFO = "\n" + + "\n" + + "\n" + + ""; + + private static final String SAVE_AS_INFO = "\n" + + "\n" + + "\n" + + ""; + + private TemplateInfo templateInfo; + private TemplateInfo templateInfoSaveAs; // 另存为的模版记录 + + @Before + public void setUp() throws XMLStreamException { + templateInfo = createTemplateInfo(NORMAL_INFO); + templateInfoSaveAs = createTemplateInfo(SAVE_AS_INFO); + } + + @Test + public void testNewInstance() throws Exception { + setUpMockForNewInstance(); + + String templateID = "24avc8n2-1iq8-iuj2-wx24-8yy0i8132302"; + TemplateInfo templateInfo = TemplateInfo.newInstance(templateID); + assertEquals(templateID, templateInfo.getTemplateID()); + assertEquals(StringUtils.EMPTY, Reflect.on(templateInfo).field("originID").get()); + assertEquals(0, Reflect.on(templateInfo).field("idleDayCount").get()); + assertEquals("{}", templateInfo.getProcessMapJsonString()); + + Map consumingMap = Reflect.on(templateInfo).field("consumingMap").get(); + assertEquals(templateID, consumingMap.get("templateID")); + assertEquals(0, consumingMap.get("originTime")); + assertEquals(StringUtils.EMPTY, consumingMap.get("originID")); + assertEquals(0, consumingMap.get("time_consume")); + assertEquals("不是安装版本", consumingMap.get("jar_time")); + assertEquals("plough", consumingMap.get("username")); + assertEquals("10.0", consumingMap.get("version")); + } + + @Test + public void testNewInstanceWithMoreArgs() throws Exception { + setUpMockForNewInstance(); + + String templateID = "24121212-u2c8-ncd2-82nx-8ud0i8138888"; + String originID = "24avc8n2-1iq8-iuj2-wx24-8yy0i8132302"; + int originTime = 100; + TemplateInfo templateInfo = TemplateInfo.newInstance(templateID, originID, originTime); + assertEquals(templateID, templateInfo.getTemplateID()); + assertEquals(originID, Reflect.on(templateInfo).field("originID").get()); + assertEquals(0, Reflect.on(templateInfo).field("idleDayCount").get()); + assertEquals("{}", templateInfo.getProcessMapJsonString()); + + Map consumingMap = Reflect.on(templateInfo).field("consumingMap").get(); + assertEquals(templateID, consumingMap.get("templateID")); + assertEquals(originTime, consumingMap.get("originTime")); + assertEquals(originID, consumingMap.get("originID")); + assertEquals(originTime, consumingMap.get("time_consume")); + assertEquals("不是安装版本", consumingMap.get("jar_time")); + assertEquals("plough", consumingMap.get("username")); + assertEquals("10.0", consumingMap.get("version")); + } + + @Test + public void testGetTemplateID() { + assertEquals("16a988ce-8529-42f5-b17c-2ee849355071", templateInfo.getTemplateID()); + assertEquals("49avd2c4-1104-92j2-wx24-3dd0k2136080", templateInfoSaveAs.getTemplateID()); + } + + @Test + public void testGetConsumingMapJsonString() { + assertJsonStringEquals("{\"activitykey\":\"2e0ea413-fa9c241e0-9723-4354fce51e81\",\"jar_time\":\"不是安装版本\"," + + "\"create_time\":\"2019-03-26 16:13\",\"templateID\":\"16a988ce-8529-42f5-b17c-2ee849355071\",\"originID\":\"\"," + + "\"uuid\":\"476ca2cc-f789-4c5d-8e89-ef146580775c\",\"time_consume\":129,\"originTime\":0,\"version\":\"10.0\"," + + "\"username\":\"plough\"}", templateInfo.getConsumingMapJsonString()); + + assertJsonStringEquals("{\"activitykey\":\"2e0ea413-fa9c241e0-9723-4354fce51e81\",\"jar_time\":\"不是安装版本\"," + + "\"create_time\":\"2019-03-26 16:13\",\"templateID\":\"49avd2c4-1104-92j2-wx24-3dd0k2136080\",\"originID\":\"16a988ce-8529-42f5-b17c-2ee849355071\"," + + "\"uuid\":\"476ca2cc-f789-4c5d-8e89-ef146580775c\",\"time_consume\":429,\"originTime\":129,\"version\":\"10.0\"," + + "\"username\":\"plough\"}", templateInfoSaveAs.getConsumingMapJsonString()); + } + + @Test + public void testGetProcessMapJsonString() { + assertJsonStringEquals("{\"process\":\"\",\"float_count\":0,\"widget_count\":0,\"cell_count\":1," + + "\"block_count\":0,\"report_type\":0,\"templateID\":\"16a988ce-8529-42f5-b17c-2ee849355071\"}", templateInfo.getProcessMapJsonString()); + assertJsonStringEquals("{\"process\":\"\",\"float_count\":0,\"widget_count\":0,\"cell_count\":1," + + "\"block_count\":0,\"report_type\":0,\"templateID\":\"49avd2c4-1104-92j2-wx24-3dd0k2136080\"}", templateInfoSaveAs.getProcessMapJsonString()); + } + + private TemplateInfo createTemplateInfo(String xmlContent) throws XMLStreamException { + StringReader sr = new StringReader(xmlContent); + XMLableReader xmlReader = XMLableReader.createXMLableReader(sr); + return TemplateInfo.newInstanceByRead(xmlReader); + } +} diff --git a/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoTestHelper.java b/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoTestHelper.java new file mode 100644 index 0000000000..be2b589892 --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoTestHelper.java @@ -0,0 +1,49 @@ +package com.fr.design.mainframe.template.info; + +import com.fr.config.MarketConfig; +import com.fr.general.ComparatorUtils; +import com.fr.general.GeneralUtils; +import com.fr.json.JSONObject; +import com.fr.stable.ProductConstants; +import org.easymock.EasyMock; +import org.powermock.api.easymock.PowerMock; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +import static org.junit.Assert.assertTrue; + +/** + * Created by plough on 2019/4/19. + */ +class TemplateInfoTestHelper { + static void assertJsonStringEquals(String jo1, String jo2) { + // HashMap 是无序的,所以不能直接比较它生成的 json 字符串 + assertTrue(ComparatorUtils.equals(new JSONObject(jo1), new JSONObject(jo2))); + } + + private static void setFinalStatic(Field field, Object newValue) throws Exception { + field.setAccessible(true); + // remove final modifier from field + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(null, newValue); + } + + static void setUpMockForNewInstance() throws Exception { + MarketConfig mockMarketConfig = EasyMock.mock(MarketConfig.class); + EasyMock.expect(mockMarketConfig.getBbsUsername()).andReturn("plough").anyTimes(); + + PowerMock.mockStatic(MarketConfig.class); + EasyMock.expect(MarketConfig.getInstance()).andReturn(mockMarketConfig).anyTimes(); + + PowerMock.mockStatic(GeneralUtils.class); + EasyMock.expect(GeneralUtils.readBuildNO()).andReturn("不是安装版本").anyTimes(); + + setFinalStatic(ProductConstants.class.getDeclaredField("VERSION"), "10.0"); + + EasyMock.replay(mockMarketConfig); + PowerMock.replayAll(); + } +}