Browse Source

REPORT-15314 103模板信息收集=>另存为增加字段

research/10.0
plough 6 years ago
parent
commit
7fdb747e80
  1. 15
      designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java
  2. 50
      designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java
  3. 24
      designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java
  4. 119
      designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java
  5. 125
      designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoTest.java
  6. 49
      designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoTestHelper.java

15
designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java

@ -145,13 +145,17 @@ public abstract class JTemplate<T extends BaseBook, U extends BaseUndoState<?>>
} }
private void collectInfo() { // 执行收集操作 private void collectInfo() { // 执行收集操作
collectInfo(StringUtils.EMPTY);
}
private void collectInfo(String originID) { // 执行收集操作
if (openTime == 0) { // 旧模板,不收集数据 if (openTime == 0) { // 旧模板,不收集数据
return; return;
} }
long saveTime = System.currentTimeMillis(); // 保存模板的时间点 long saveTime = System.currentTimeMillis(); // 保存模板的时间点
try { try {
long timeConsume = ((saveTime - openTime) / ONE_THOUSAND); // 制作模板耗时(单位:s) 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) { // 不管收集过程中出现任何异常,都不应该影响模版保存 } catch (Throwable th) { // 不管收集过程中出现任何异常,都不应该影响模版保存
} }
openTime = saveTime; // 更新 openTime,准备下一次计算 openTime = saveTime; // 更新 openTime,准备下一次计算
@ -605,15 +609,20 @@ public abstract class JTemplate<T extends BaseBook, U extends BaseUndoState<?>>
} }
} }
// 保存新模板时会进入此方法(新建模板直接保存,或者另存为)
protected boolean saveNewFile(FILE editingFILE, String oldName) { protected boolean saveNewFile(FILE editingFILE, String oldName) {
String originID = StringUtils.EMPTY;
if (StringUtils.isNotEmpty(this.template.getTemplateID())) {
originID = this.template.getTemplateID();
}
// 在保存之前,初始化 templateID // 在保存之前,初始化 templateID
initForCollect(); // 如果保存新模板(新建模板直接保存,或者另存为),则添加 templateID initForCollect();
this.editingFILE = editingFILE; this.editingFILE = editingFILE;
boolean result = this.saveFile(); boolean result = this.saveFile();
if (result) { if (result) {
DesignerFrameFileDealerPane.getInstance().refresh(); DesignerFrameFileDealerPane.getInstance().refresh();
collectInfo(); collectInfo(originID);
} }
//更换最近打开 //更换最近打开
DesignerEnvManager.getEnvManager().replaceRecentOpenedFilePath(oldName, this.getPath()); DesignerEnvManager.getEnvManager().replaceRecentOpenedFilePath(oldName, this.getPath());

50
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.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap; 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 XML_CONSUMING_MAP = "consumingMap";
private static final String ATTR_DAY_COUNT = "day_count"; private static final String ATTR_DAY_COUNT = "day_count";
private static final String ATTR_TEMPLATE_ID = "templateID"; 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_PROCESS = "process";
private static final String ATTR_FLOAT_COUNT = "float_count"; private static final String ATTR_FLOAT_COUNT = "float_count";
private static final String ATTR_WIDGET_COUNT = "widget_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_CREATE_TIME = "create_time";
private static final String ATTR_UUID = "uuid"; private static final String ATTR_UUID = "uuid";
private static final String ATTR_TIME_CONSUME = "time_consume"; 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_VERSION = "version";
private static final String ATTR_USERNAME = "username"; 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 static final int COMPLETE_DAY_COUNT = 15; // 判断模板是否完成的天数
private int idleDayCount; // 到现在为止,模版闲置(上次保存后没有再编辑过)的天数 private int idleDayCount; // 到现在为止,模版闲置(上次保存后没有再编辑过)的天数
private String templateID; private String templateID = StringUtils.EMPTY;
private String originID = StringUtils.EMPTY;
// todo: processMap 和 consumingMap 还可以再拆解为小类,以后继续重构 // todo: processMap 和 consumingMap 还可以再拆解为小类,以后继续重构
private HashMap<String, Object> processMap = new HashMap<>(); private Map<String, Object> processMap = new HashMap<>();
private HashMap<String, Object> consumingMap = new HashMap<>(); private Map<String, Object> consumingMap = new HashMap<>();
private TemplateInfo() { private TemplateInfo() {
} }
private TemplateInfo(String templateID, String originID) {
this.templateID = templateID;
this.originID = originID;
}
static TemplateInfo newInstanceByRead(XMLableReader reader) { static TemplateInfo newInstanceByRead(XMLableReader reader) {
TemplateInfo templateInfo = new TemplateInfo(); TemplateInfo templateInfo = new TemplateInfo();
reader.readXMLObject(templateInfo); reader.readXMLObject(templateInfo);
@ -60,6 +69,10 @@ class TemplateInfo implements XMLReadable, XMLWriter {
} }
static TemplateInfo newInstance(String templateID) { static TemplateInfo newInstance(String templateID) {
return newInstance(templateID, StringUtils.EMPTY, 0);
}
static TemplateInfo newInstance(String templateID, String originID, int originTime) {
HashMap<String, Object> consumingMap = new HashMap<>(); HashMap<String, Object> consumingMap = new HashMap<>();
String username = MarketConfig.getInstance().getBbsUsername(); String username = MarketConfig.getInstance().getBbsUsername();
@ -72,12 +85,14 @@ class TemplateInfo implements XMLReadable, XMLWriter {
consumingMap.put(ATTR_UUID, uuid); consumingMap.put(ATTR_UUID, uuid);
consumingMap.put(ATTR_ACTIVITYKEY, activitykey); consumingMap.put(ATTR_ACTIVITYKEY, activitykey);
consumingMap.put(ATTR_TEMPLATE_ID, templateID); consumingMap.put(ATTR_TEMPLATE_ID, templateID);
consumingMap.put(ATTR_ORIGIN_ID, originID);
consumingMap.put(ATTR_CREATE_TIME, createTime); 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_JAR_TIME, jarTime);
consumingMap.put(ATTR_VERSION, version); consumingMap.put(ATTR_VERSION, version);
TemplateInfo templateInfo = new TemplateInfo(); TemplateInfo templateInfo = new TemplateInfo(templateID, originID);
templateInfo.consumingMap = consumingMap; templateInfo.consumingMap = consumingMap;
return templateInfo; return templateInfo;
@ -87,12 +102,8 @@ class TemplateInfo implements XMLReadable, XMLWriter {
return templateID; return templateID;
} }
HashMap<String, Object> getTemplateInfo() { int getTimeConsume() {
HashMap<String, Object> templateInfo = new HashMap<>(); return (int)consumingMap.get(ATTR_TIME_CONSUME);
templateInfo.put(XML_PROCESS_MAP, processMap);
templateInfo.put(XML_CONSUMING_MAP, consumingMap);
templateInfo.put(ATTR_DAY_COUNT, idleDayCount);
return templateInfo;
} }
public void writeXML(XMLPrintWriter writer) { public void writeXML(XMLPrintWriter writer) {
@ -100,6 +111,9 @@ class TemplateInfo implements XMLReadable, XMLWriter {
if (StringUtils.isNotEmpty(templateID)) { if (StringUtils.isNotEmpty(templateID)) {
writer.attr(ATTR_TEMPLATE_ID, this.templateID); writer.attr(ATTR_TEMPLATE_ID, this.templateID);
} }
if (StringUtils.isNotEmpty(originID)) {
writer.attr(ATTR_ORIGIN_ID, this.originID);
}
if (idleDayCount >= 0) { if (idleDayCount >= 0) {
writer.attr(ATTR_DAY_COUNT, this.idleDayCount); 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_JAR_TIME, (String) consumingMap.get(ATTR_JAR_TIME));
writer.attr(ATTR_CREATE_TIME, (String) consumingMap.get(ATTR_CREATE_TIME)); writer.attr(ATTR_CREATE_TIME, (String) consumingMap.get(ATTR_CREATE_TIME));
writer.attr(ATTR_UUID, (String) consumingMap.get(ATTR_UUID)); 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_VERSION, (String) consumingMap.get(ATTR_VERSION));
writer.attr(ATTR_USERNAME, (String) consumingMap.get(ATTR_USERNAME)); writer.attr(ATTR_USERNAME, (String) consumingMap.get(ATTR_USERNAME));
writer.end(); writer.end();
@ -136,6 +151,7 @@ class TemplateInfo implements XMLReadable, XMLWriter {
if (!reader.isChildNode()) { if (!reader.isChildNode()) {
idleDayCount = reader.getAttrAsInt(ATTR_DAY_COUNT, 0); idleDayCount = reader.getAttrAsInt(ATTR_DAY_COUNT, 0);
templateID = reader.getAttrAsString(ATTR_TEMPLATE_ID, StringUtils.EMPTY); templateID = reader.getAttrAsString(ATTR_TEMPLATE_ID, StringUtils.EMPTY);
originID = reader.getAttrAsString(ATTR_ORIGIN_ID, StringUtils.EMPTY);
} else { } else {
try { try {
String name = reader.getTagName(); 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_JAR_TIME, reader.getAttrAsString(ATTR_JAR_TIME, StringUtils.EMPTY));
consumingMap.put(ATTR_CREATE_TIME, reader.getAttrAsString(ATTR_CREATE_TIME, StringUtils.EMPTY)); consumingMap.put(ATTR_CREATE_TIME, reader.getAttrAsString(ATTR_CREATE_TIME, StringUtils.EMPTY));
consumingMap.put(ATTR_TEMPLATE_ID, templateID); 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_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_VERSION, reader.getAttrAsString(ATTR_VERSION, "8.0"));
consumingMap.put(ATTR_USERNAME, reader.getAttrAsString(ATTR_USERNAME, StringUtils.EMPTY)); 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 floatCount = (int) processMap.get(ATTR_FLOAT_COUNT);
int blockCount = (int) processMap.get(ATTR_BLOCK_COUNT); int blockCount = (int) processMap.get(ATTR_BLOCK_COUNT);
int widgetCount = (int) processMap.get(ATTR_WIDGET_COUNT); int widgetCount = (int) processMap.get(ATTR_WIDGET_COUNT);
boolean isTestTemplate = false; boolean isTestTemplate;
if (reportType == 0) { // 普通报表 if (reportType == 0) { // 普通报表
isTestTemplate = cellCount <= VALID_CELL_COUNT && floatCount <= 1 && widgetCount <= VALID_WIDGET_COUNT; isTestTemplate = cellCount <= VALID_CELL_COUNT && floatCount <= 1 && widgetCount <= VALID_WIDGET_COUNT;
} else if (reportType == 1) { // 聚合报表 } else if (reportType == 1) { // 聚合报表
@ -200,8 +218,8 @@ class TemplateInfo implements XMLReadable, XMLWriter {
return isComplete() && !isTestTemplate(); return isComplete() && !isTestTemplate();
} }
void addTimeConsume(long timeConsume) { void addTimeConsume(int timeConsume) {
timeConsume += (long) consumingMap.get(ATTR_TIME_CONSUME); // 加上之前的累计编辑时间 timeConsume += (int)consumingMap.get(ATTR_TIME_CONSUME); // 加上之前的累计编辑时间
consumingMap.put(ATTR_TIME_CONSUME, timeConsume); consumingMap.put(ATTR_TIME_CONSUME, timeConsume);
} }

24
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 * @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()) { if (!shouldCollectInfo()) {
return; 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); templateInfo.addTimeConsume(timeConsume);
// 收集模版基本信息 // 收集模版基本信息
@ -110,6 +124,10 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter {
templateInfoMap = new HashMap<>(); templateInfoMap = new HashMap<>();
setDesignerOpenDate(); setDesignerOpenDate();
loadFromFile();
}
void loadFromFile() {
if (!getInfoFile().exists()) { if (!getInfoFile().exists()) {
return; return;
} }
@ -123,7 +141,7 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter {
} }
} }
private TemplateInfo getOrCreateTemplateInfoByID(String templateID) { TemplateInfo getOrCreateTemplateInfoByID(String templateID) {
if (templateInfoMap.containsKey(templateID)) { if (templateInfoMap.containsKey(templateID)) {
return templateInfoMap.get(templateID); return templateInfoMap.get(templateID);
} }

119
designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java

@ -1,8 +1,13 @@
package com.fr.design.mainframe.template.info; 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.invoke.Reflect;
import com.fr.stable.ProductConstants; 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.easymock.EasyMock;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; 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.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import java.io.File;
import java.io.IOException;
import java.util.Map; 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.assertEquals;
import static org.junit.Assert.assertTrue;
/** /**
* Created by plough on 2019/4/18. * Created by plough on 2019/4/18.
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest(ProductConstants.class) @PrepareForTest({ProductConstants.class, MarketConfig.class, ProductConstants.class, GeneralUtils.class})
public class TemplateInfoCollectorTest { public class TemplateInfoCollectorTest {
private String filePath;
private String initialFileContent;
private TemplateProcessInfo mockProcessInfo;
@Before @Before
public void setUp() { public void setUp() throws IOException {
PowerMock.mockStatic(ProductConstants.class); 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")); String dirPath = filePath.substring(0, filePath.indexOf("tpl.info"));
EasyMock.expect(ProductConstants.getEnvHome()).andReturn(dirPath).anyTimes(); EasyMock.expect(ProductConstants.getEnvHome()).andReturn(dirPath).anyTimes();
EasyMock.replay(); EasyMock.replay();
PowerMock.replayAll(); 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 @Test
@ -39,4 +70,84 @@ public class TemplateInfoCollectorTest {
assertEquals("2019-04-18", Reflect.on(collector).field("designerOpenDate").get()); assertEquals("2019-04-18", Reflect.on(collector).field("designerOpenDate").get());
assertEquals(7, ((Map) Reflect.on(collector).field("templateInfoMap").get()).size()); 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<String, Object> 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<String, Object> 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"));
}
} }

125
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 = "<TemplateInfo templateID=\"16a988ce-8529-42f5-b17c-2ee849355071\" day_count=\"9\">\n" +
"<processMap process=\"\" float_count=\"0\" widget_count=\"0\" cell_count=\"1\" block_count=\"0\" report_type=\"0\"/>\n" +
"<consumingMap activitykey=\"2e0ea413-fa9c241e0-9723-4354fce51e81\" jar_time=\"不是安装版本\" create_time=\"2019-03-26 16:13\" uuid=\"476ca2cc-f789-4c5d-8e89-ef146580775c\" time_consume=\"129\" version=\"10.0\" username=\"plough\"/>\n" +
"</TemplateInfo>";
private static final String SAVE_AS_INFO = "<TemplateInfo templateID=\"49avd2c4-1104-92j2-wx24-3dd0k2136080\" originID=\"16a988ce-8529-42f5-b17c-2ee849355071\" day_count=\"9\">\n" +
"<processMap process=\"\" float_count=\"0\" widget_count=\"0\" cell_count=\"1\" block_count=\"0\" report_type=\"0\"/>\n" +
"<consumingMap activitykey=\"2e0ea413-fa9c241e0-9723-4354fce51e81\" jar_time=\"不是安装版本\" create_time=\"2019-03-26 16:13\" uuid=\"476ca2cc-f789-4c5d-8e89-ef146580775c\" time_consume=\"429\" originTime=\"129\" version=\"10.0\" username=\"plough\"/>\n" +
"</TemplateInfo>";
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<String, Object> 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<String, Object> 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);
}
}

49
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();
}
}
Loading…
Cancel
Save