Browse Source

REPORT-15314 103模板信息收集=>重构=>单元测试

research/10.0
plough 6 years ago
parent
commit
5e5815d88b
  1. 4
      designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java
  2. 6
      designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java
  3. 117
      designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java
  4. 16
      designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java
  5. 35
      designer-base/src/test/resources/com/fr/design/mainframe/template/info/tpl.info

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

@ -78,6 +78,7 @@ import java.util.regex.Pattern;
public abstract class JTemplate<T extends BaseBook, U extends BaseUndoState<?>> extends TargetComponent<T> implements ToolBarMenuDockPlus, DesignerProxy {
// TODO ALEX_SEP editingFILE这个属性一定要吗?如果非要不可,有没有可能保证不为null
private static final int PREFIX_NUM = 3000;
private static final int ONE_THOUSAND = 1000;
private FILE editingFILE = null;
// alex:初始状态为saved,这样不管是新建模板,还是打开模板,如果未做任何操作直接关闭,不提示保存
private boolean saved = true;
@ -149,7 +150,8 @@ public abstract class JTemplate<T extends BaseBook, U extends BaseUndoState<?>>
}
long saveTime = System.currentTimeMillis(); // 保存模板的时间点
try {
TemplateInfoCollector.getInstance().collectInfo(template.getTemplateID(), getProcessInfo(), openTime, saveTime);
long timeConsume = ((saveTime - openTime) / ONE_THOUSAND); // 制作模板耗时(单位:s)
TemplateInfoCollector.getInstance().collectInfo(template.getTemplateID(), getProcessInfo(), timeConsume);
} catch (Throwable th) { // 不管收集过程中出现任何异常,都不应该影响模版保存
}
openTime = saveTime; // 更新 openTime,准备下一次计算

6
designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java

@ -53,13 +53,13 @@ class TemplateInfo implements XMLReadable, XMLWriter {
private TemplateInfo() {
}
static TemplateInfo newInstance(XMLableReader reader) {
static TemplateInfo newInstanceByRead(XMLableReader reader) {
TemplateInfo templateInfo = new TemplateInfo();
reader.readXMLObject(templateInfo);
return templateInfo;
}
static TemplateInfo newInstance(String templateID, long timeConsume) {
static TemplateInfo newInstance(String templateID) {
HashMap<String, Object> consumingMap = new HashMap<>();
String username = MarketConfig.getInstance().getBbsUsername();
@ -73,7 +73,7 @@ class TemplateInfo implements XMLReadable, XMLWriter {
consumingMap.put(ATTR_ACTIVITYKEY, activitykey);
consumingMap.put(ATTR_TEMPLATE_ID, templateID);
consumingMap.put(ATTR_CREATE_TIME, createTime);
consumingMap.put(ATTR_TIME_CONSUME, timeConsume);
consumingMap.put(ATTR_TIME_CONSUME, 0);
consumingMap.put(ATTR_JAR_TIME, jarTime);
consumingMap.put(ATTR_VERSION, version);

117
designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java

@ -3,9 +3,7 @@ package com.fr.design.mainframe.template.info;
import com.fr.base.FRContext;
import com.fr.design.DesignerEnvManager;
import com.fr.general.ComparatorUtils;
import com.fr.general.IOUtils;
import com.fr.log.FineLoggerFactory;
import com.fr.stable.EncodeConstants;
import com.fr.stable.ProductConstants;
import com.fr.stable.StableUtils;
import com.fr.stable.StringUtils;
@ -17,16 +15,10 @@ import com.fr.stable.xml.XMLableReader;
import com.fr.third.javax.xml.stream.XMLStreamException;
import com.fr.workspace.WorkContext;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
@ -41,40 +33,13 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter {
private static final String XML_TAG = "TplInfo";
private static final String XML_DESIGNER_OPEN_DATE = "DesignerOpenDate";
private static final String XML_TEMPLATE_INFO_LIST = "TemplateInfoList";
private static final String XML_FILE_NAME = "tpl.info";
private static final int ONE_THOUSAND = 1000;
private static TemplateInfoCollector instance;
private Map<String, TemplateInfo> templateInfoMap;
private String designerOpenDate; //设计器最近一次打开日期
@SuppressWarnings("unchecked")
private TemplateInfoCollector() {
setDesignerOpenDate();
loadTemplateInfoMap();
}
private void loadTemplateInfoMap() {
templateInfoMap = new HashMap<>();
try {
XMLableReader xmlReader = XMLableReader.createXMLableReader(new FileReader(getInfoFile()));
xmlReader.readXMLObject(this);
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取缓存文件存放路径
*/
private static File getInfoFile() {
return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), XML_FILE_NAME));
init();
}
public static TemplateInfoCollector getInstance() {
@ -94,32 +59,23 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter {
/**
* 收集模板信息如果之前没有记录则新增如果已有记录则更新
* 同时将最新数据保存到文件中
* @param timeConsume 本次制作耗时单位为 s
*/
public void collectInfo(String templateID, TemplateProcessInfo processInfo, long openTime, long saveTime) {
public void collectInfo(String templateID, TemplateProcessInfo processInfo, long timeConsume) {
if (!shouldCollectInfo()) {
return;
}
long timeConsume = ((saveTime - openTime) / ONE_THOUSAND); // 制作模板耗时(单位:s)
TemplateInfo templateInfo;
if (templateInfoMap.containsKey(templateID)) { // 已有记录
templateInfo = templateInfoMap.get(templateID);
templateInfo.addTimeConsume(timeConsume);
} else { // 新增
templateInfo = TemplateInfo.newInstance(templateID, timeConsume);
}
TemplateInfo templateInfo = getOrCreateTemplateInfoByID(templateID);
// 收集制作耗时
templateInfo.addTimeConsume(timeConsume);
// 收集模版基本信息
templateInfo.updateProcessMap(processInfo);
// 保存模板时,让 day_count 归零
// 刷新闲置日计数器
templateInfo.setIdleDayCount(0);
templateInfoMap.put(templateID, templateInfo);
saveInfo(); // 每次更新之后,都同步到暂存文件中
// 每次更新之后,都同步到暂存文件中
saveInfo();
}
/**
@ -143,9 +99,37 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter {
saveInfo();
}
private static String getFileContent(File xmlFile) throws FileNotFoundException, UnsupportedEncodingException {
InputStream is = new FileInputStream(xmlFile);
return IOUtils.inputStream2String(is);
/**
* 获取缓存文件存放路径
*/
private static File getInfoFile() {
return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), XML_FILE_NAME));
}
private void init() {
templateInfoMap = new HashMap<>();
setDesignerOpenDate();
if (!getInfoFile().exists()) {
return;
}
try {
XMLableReader xmlReader = XMLableReader.createXMLableReader(new FileReader(getInfoFile()));
xmlReader.readXMLObject(this);
} catch (XMLStreamException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
} catch (FileNotFoundException e) {
// do nothing
}
}
private TemplateInfo getOrCreateTemplateInfoByID(String templateID) {
if (templateInfoMap.containsKey(templateID)) {
return templateInfoMap.get(templateID);
}
TemplateInfo templateInfo = TemplateInfo.newInstance(templateID);
templateInfoMap.put(templateID, templateInfo);
return templateInfo;
}
/**
@ -156,7 +140,7 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter {
}
/**
* 判断今天是否第一次打开设计器
* 判断今天是否第一次打开设计器为了防止同一天内多次 addDayCount
*/
private boolean designerOpenFirstTime() {
String today = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
@ -195,7 +179,6 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter {
}
}
private void removeTestTemplates() {
// 删除所有已完成的测试模版
ArrayList<String> testTemplateKeys = new ArrayList<>(); // 保存测试模板的key
@ -221,8 +204,9 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter {
String name = reader.getTagName();
if (XML_DESIGNER_OPEN_DATE.equals(name)) {
this.designerOpenDate = reader.getElementValue();
} else if (XML_TEMPLATE_INFO_LIST.equals(name)) {
readTemplateInfoList(reader);
} else if (TemplateInfo.XML_TAG.equals(name)) {
TemplateInfo templateInfo = TemplateInfo.newInstanceByRead(reader);
templateInfoMap.put(templateInfo.getTemplateID(), templateInfo);
}
} catch (Exception ex) {
// 什么也不做,使用默认值
@ -230,17 +214,6 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter {
}
}
private void readTemplateInfoList(XMLableReader reader) {
reader.readXMLObject(new XMLReadable() {
public void readXML(XMLableReader reader) {
if (TemplateInfo.XML_TAG.equals(reader.getTagName())) {
TemplateInfo templateInfo = TemplateInfo.newInstance(reader);
templateInfoMap.put(templateInfo.getTemplateID(), templateInfo);
}
}
});
}
@Override
public void writeXML(XMLPrintWriter writer) {
writer.startTAG(XML_TAG);

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

@ -13,7 +13,6 @@ import org.powermock.modules.junit4.PowerMockRunner;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Created by plough on 2019/4/18.
@ -25,22 +24,19 @@ public class TemplateInfoCollectorTest {
@Before
public void setUp() {
PowerMock.mockStatic(ProductConstants.class);
EasyMock.expect(ProductConstants.getEnvHome()).andReturn("/Users/plough/.FineReport100").anyTimes();
String 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();
}
// todo: 单元测试需要一般化,不能依赖特定电脑
@Test
public void testReadXML() {
TemplateInfoCollector collector = TemplateInfoCollector.getInstance();
assertEquals("2019-04-18", Reflect.on(collector).field("designerOpenDate").get());
assertTrue(((Map)Reflect.on(collector).field("templateInfoMap").get()).size() > 0);
}
@Test
public void testSend() {
TemplateInfoCollector tic = TemplateInfoCollector.getInstance();
// tic.sendTemplateInfo();
assertEquals(7, ((Map) Reflect.on(collector).field("templateInfoMap").get()).size());
}
}

35
designer-base/src/test/resources/com/fr/design/mainframe/template/info/tpl.info

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<TplInfo xmlVersion="20170720" releaseVersion="10.0.0">
<DesignerOpenDate>
<![CDATA[2019-04-18]]></DesignerOpenDate>
<TemplateInfoList>
<TemplateInfo templateID="16a988ce-8529-42f5-b17c-2ee849355071" day_count="9">
<processMap process="" float_count="0" widget_count="0" cell_count="1" block_count="0" report_type="0"/>
<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"/>
</TemplateInfo>
<TemplateInfo templateID="23817e4f-64b6-438e-b23b-91c1a4d0b254" day_count="9">
<processMap process="" float_count="0" widget_count="0" cell_count="231" block_count="0" report_type="0"/>
<consumingMap activitykey="2e0ea413-fa9c241e0-9723-4354fce51e81" jar_time="不是安装版本" create_time="2019-03-26 16:52" uuid="476ca2cc-f789-4c5d-8e89-ef146580775c" time_consume="91" version="10.0" username="plough"/>
</TemplateInfo>
<TemplateInfo templateID="31319947-5ce7-4d82-97e4-3cfea825df9c" day_count="9">
<processMap process="" float_count="0" widget_count="0" cell_count="3" block_count="0" report_type="0"/>
<consumingMap activitykey="2e0ea413-fa9c241e0-9723-4354fce51e81" jar_time="不是安装版本" create_time="2019-03-26 16:49" uuid="476ca2cc-f789-4c5d-8e89-ef146580775c" time_consume="160" version="10.0" username="plough"/>
</TemplateInfo>
<TemplateInfo templateID="43bdd914-a3f7-405c-ad96-2feaf28bed74" day_count="8">
<processMap process="" float_count="0" widget_count="0" cell_count="1" block_count="0" report_type="0"/>
<consumingMap activitykey="2e0ea413-fa9c241e0-9723-4354fce51e81" jar_time="2019.01.04.18.06.01.38" create_time="2019-03-27 16:17" uuid="476ca2cc-f789-4c5d-8e89-ef146580775c" time_consume="1426" version="10.0" username="plough"/>
</TemplateInfo>
<TemplateInfo templateID="cd527ae5-06e4-48fb-a73e-e9a06c9e7c58" day_count="12">
<processMap process="" float_count="0" widget_count="0" cell_count="3" block_count="0" report_type="0"/>
<consumingMap activitykey="2e0ea413-fa9c241e0-9723-4354fce51e81" jar_time="2019.01.04.18.06.01.38" create_time="2019-03-15 10:02" uuid="476ca2cc-f789-4c5d-8e89-ef146580775c" time_consume="430" version="10.0" username="plough"/>
</TemplateInfo>
<TemplateInfo templateID="08f9c404-8124-4615-a34c-735dcabee137" day_count="9">
<processMap process="" float_count="0" widget_count="0" cell_count="2" block_count="0" report_type="0"/>
<consumingMap activitykey="2e0ea413-fa9c241e0-9723-4354fce51e81" jar_time="不是安装版本" create_time="2019-03-25 17:22" uuid="476ca2cc-f789-4c5d-8e89-ef146580775c" time_consume="1497" version="10.0" username="plough"/>
</TemplateInfo>
<TemplateInfo templateID="981314a5-9c5b-4831-97d8-15e76735f9e2" day_count="1">
<processMap process="" float_count="0" widget_count="0" cell_count="3" block_count="0" report_type="0"/>
<consumingMap activitykey="2e0ea413-fa9c241e0-9723-4354fce51e81" jar_time="不是安装版本" create_time="2019-04-17 16:24" uuid="476ca2cc-f789-4c5d-8e89-ef146580775c" time_consume="155" version="10.0" username="plough"/>
</TemplateInfo>
</TemplateInfoList>
</TplInfo>
Loading…
Cancel
Save