T
- 子类public abstract class WriteTempletFile<T extends WriteTempletFile<T>> extends Object implements AddPlaceholder
文件名:WriteTempletFile.java
用途: 快速写入模板型的文件进行的工具。可通过类中提供的词语替换和默认内容的方法,对一类模板型的文件内容(如,测试用例)
进行快速写作,提高工作效率。并且,若存在内容相同,但模板不同的文件,还可通过调用toWriteFileJson()
方法
以完成各个子类之间的消息互通,达到快速生成多种同一内容,不同存放文件的目的。
在注释中,存在以下名词需要解释:
编码时间:2021年5月15日上午11:13:26
修改时间:2023年5月22日 下午5:22:17
Modifier and Type | Field and Description |
---|---|
protected int |
caseIndex
指向当前需要编写的用例下标
|
protected WriteFileData |
data
存储需要写入到文件中的数据
|
protected String |
endField
存储判断是否自动换行的字段
|
static String |
KEY_CASE |
static String |
KEY_CONTENT |
static String |
KEY_DATA |
static String |
KEY_DEFAULT |
static String |
KEY_TEMPLET |
static String |
KEY_TEXT |
protected Placeholder |
placeholder
占位符类对象
|
static String |
WORD_SIGN
待替换词语的标记
|
protected int |
writeRowNum
存储需要分页的行数
|
Modifier | Constructor and Description |
---|---|
protected |
WriteTempletFile()
无参构造,方便子类进行特殊的构造方法
|
protected |
WriteTempletFile(FileTemplet templet)
构造对象,初始化创建文件的模板
|
Modifier and Type | Method and Description |
---|---|
T |
addContent(String field,
int index,
Placeholder placeholder,
String... contents)
根据传入的字段信息,将指定的内容插入到用例相应字段的指定下标下,并且可传入临时的占位符类对象,对文本中的占位符进行替换,且不影响已添加的替换词语
|
T |
addContent(String field,
int index,
String... contents)
根据传入的字段信息,将指定的内容插入到用例相应字段的指定下标下
|
T |
addContent(String field,
Placeholder placeholder,
String... contents)
根据传入的字段信息,将指定的内容插入到用例相应字段的末尾,并且可传入临时的占位符类对象,对文本中的占位符进行替换,且不影响已添加的替换词语
|
T |
addContent(String field,
String... contents)
根据传入的字段信息,将指定的内容插入到用例相应字段的最后一行
|
void |
addReplaceFunction(String regex,
DataFunction function)
用于添加待替换的词语及相应的替换方法
|
void |
addReplaceWord(String word,
String replaceWord)
该方法用于添加被替换的词语以及替换的内容
|
protected int |
analysisIndex(int maxNum,
int index,
boolean isReciprocal)
用于解析真实下标内容
|
void |
clearCase(int index)
用于移除指定下标的一条用例
|
T |
clearContent(String field,
int index)
用于清除用例字段下指定下标段落的内容
|
void |
clearFieldValue(String field)
用于移除字段的默认值
|
protected abstract void |
contentWriteTemplet(FileTemplet templet,
int caseStartIndex,
int caseEndIndex)
用于将缓存的数据内容,写入到模板中
|
protected abstract void |
createTempletFile(FileTemplet templet)
用于创建模板文件
|
protected T |
disposeWriteFieldsContent(Collection<String> fieldList,
VoidSupplier sup)
该方法用于处理在一次性插入多个字段内容时,进行的自动换行处理。
|
T |
end()
标记完成一条用例数据的编写,并将内容插入到最后一行内容下
|
T |
end(int contentIndex)
标记完成一条用例数据的编写,并将内容插入到指定行内容下
|
protected abstract List<String> |
getAllTempletJson()
将类中存储的模板类转换为模板json串,通过List集合收集后,进行返回
|
T |
getCase(int index)
用于获取指定下标的用例内容,并对内容进行重写
|
Placeholder |
getPlaceholder()
该方法用于返回当前存储的占位符类对象,若修改了占位符类对象的内容,
需要通过
setPlaceholder(Placeholder) 方法对重新生成的占位符类对象进行整体设置 |
protected com.alibaba.fastjson.JSONObject |
getTextJson(String field,
int textIndex)
用于返回指定下标的文本json
|
WriteFileData |
getWriteData()
用于返回当前待写入模板的信息类对象
|
protected void |
replenishDefaultContent()
用于在用例中补充默认的字段内容,若用例存在内容,则不添加默认值
|
void |
setContentJson(String writeJsonText)
用于接收一个内容json串来替换当前写入的内容
|
void |
setEndField(String endField)
该方法用于设置自动换行的字段,当重新插入该字段后,则自动切换到下一行
|
void |
setFieldValue(String field,
String... contents)
用于向字段中添加默认内容
|
void |
setFileTemplet(FileTemplet templet)
用于重置当前的写入模板
|
void |
setPlaceholder(Placeholder placeholder)
该方法用于设置自定义的占位符类对象
|
void |
setWriteData(WriteFileData data)
用于将其他写入类对象的待写入信息设置到当前类对象中
|
void |
setWriteRowNum(int writeRowNum)
用于设置自动写入文件的极限用例数量
|
String |
toTempletJson()
用于根据当前实际的模板数量,返回拼接后的模板json内容
|
String |
toWriteFileJson()
用于返回主体json串
|
abstract void |
write()
用于将编写的所有内容写入到文件中
|
protected abstract void |
write(FileTemplet templet,
int caseStartIndex,
int caseEndIndex)
用于将编写的部分内容写入到文件中
|
public static final String KEY_CONTENT
public static final String KEY_TEXT
public static final String KEY_DEFAULT
public static final String KEY_CASE
public static final String KEY_TEMPLET
public static final String KEY_DATA
public static final String WORD_SIGN
protected WriteFileData data
protected int caseIndex
protected int writeRowNum
protected String endField
protected Placeholder placeholder
protected WriteTempletFile(FileTemplet templet)
templet
- 模板类对象protected WriteTempletFile()
public Placeholder getPlaceholder()
setPlaceholder(Placeholder)
方法对重新生成的占位符类对象进行整体设置public void setPlaceholder(Placeholder placeholder)
注意:该方法是对整个占位符类对象进行整体设置,需要保证添加的占位符的前后标志与设置的占位符标志保持一致
placeholder
- 占位符类对象public void addReplaceWord(String word, String replaceWord)
AddPlaceholder
addReplaceWord
in interface AddPlaceholder
word
- 需要替换的内容replaceWord
- 替换后的内容public void addReplaceFunction(String regex, DataFunction function)
AddPlaceholder
该方法允许添加待替换词语的处理方式,在写入用例时,若指定的待替换内容符合此方法指定的正则时,则会使用存储的替换方式,
对词语进行替换。例如,占位符前后标志均为“#”,则:
@Test
public void addReplaceWordTest_DataDriverFunction() {
// 定义词语匹配规则和处理方式,当匹配到正则后,将获取“随机:”后的字母
// 若字母为“N”,则随机生成两位数字字符串
// 若字母为“Y”,则随机生成两位中文字符串
test.addReplaceWord(new DataDriverFunction("随机:[NC]", text -> {
return "N".equals(text.split(":")[1]) ? RandomString.randomString(2, 2, StringMode.NUM)
: RandomString.randomString(2, 2, StringMode.CH);
}));
}
部分定义方法可调用工具类Functions
类获取,以其中一个方法为例,其传参方法为如下:
DataDriverFunction
driverFunction = Functions.randomCarId()
;
addReplaceFunction(DataDriverFunction.getRegex()
, DataDriverFunction.getFunction()
);
addReplaceFunction
in interface AddPlaceholder
regex
- 需要替换的内容正则表达式function
- 替换词语使用的函数public void setFieldValue(String field, String... contents)
当完成一条用例编写后,若未写入存在默认内容的字段时,则在完成用例后自动将默认内容写入字段中。 若字段存在内容,则不会写入默认值
注意:若模板中不存在指定的字段,则不写入该默认值;每次重新编写字段默认值时,会覆盖上次编写的内容
field
- 字段contents
- 默认内容public void setWriteRowNum(int writeRowNum)
设置该值后,则开启自动写入文件功能,当用例数达到指定数量时,则会自动调用写入文件的方法,将缓存的内容写入到文件中
注意:自动写入文件并非会将所有的内容写入到文件中,例如,设置值为5,用例存在11条,则前10条用例会被自动写入到文件,而最后一条数据,
由于未达到设定值,则不会写入到文件中,需要手动调用write()
方法
writeRowNum
- 自动写入到文件的极限用例数量public void clearFieldValue(String field)
field
- 字段public void setFileTemplet(FileTemplet templet)
templet
- 模板类对象public void setContentJson(String writeJsonText)
该方法可用于在更换内容但不替换当前模板时使用。可通过其他写入类的toWriteFileJson()
方法,获取到内容json串后,来替换当前类对象的内容json串
注意:设置内容json串后,当前写入的内容将被覆盖
writeJsonText
- 指定的内容json串public void setWriteData(WriteFileData data)
调用该方法后,会将传入的WriteFileData
类对象中,除模板外的信息覆盖到当前的模板类中,等同于进行数据的复制
data
- 待写入信息类对象,可通过getWriteData()
方法获取public void setEndField(String endField)
注意:若不启动该功能,则将自动换行字段设置为空或不存在的字段即可
endField
- 自动换行字段public WriteFileData getWriteData()
public T getCase(int index)
调用该方法后,当前编写的用例将指向为指定下标的用例。若当前正在编写用例,调用该方法后,将覆盖当前编写的用例。
注意:若下标为负数或大于当前内容最大个数时,则不获取指定内容。下标从0开始计算,及0表示第一条用例
index
- 用例下标public T addContent(String field, String... contents)
方法允许传入多条内容,每条内容在写入到文件时,均以换行符隔开。
注意:当设置了自动换行字段时,则在插入该字段内容后(内容非空时),则会自动进行换行,可参考setEndField(String)
方法
field
- 字段idcontents
- 相应字段的内容public T addContent(String field, int index, String... contents)
方法允许传入多条内容,每条内容在写入到文件时,均以换行符隔开。若指定的下标小于0或大于当前内容的最大个数时,则将内容写入到集合最后
注意:当设置了自动换行字段时,则在插入该字段内容后(内容非空时),则会在插入当前内容前自动进行换行,再插入该内容,可参考setEndField(String)
的设置方法
field
- 字段idindex
- 指定插入的位置contents
- 相应字段的内容public T addContent(String field, int index, Placeholder placeholder, String... contents)
方法允许传入多条内容,每条内容在写入到文件时,均以换行符隔开。若指定的下标小于0或大于当前内容的最大个数时,则将内容写入到集合最后
注意:
setEndField(String)
的设置方法field
- 字段idindex
- 指定插入的位置placeholder
- 占位符类contents
- 相应字段的内容public T addContent(String field, Placeholder placeholder, String... contents)
方法允许传入多条内容,每条内容在写入到文件时,均以换行符隔开。若指定的下标小于0或大于当前内容的最大个数时,则将内容写入到集合最后
注意:
setEndField(String)
的设置方法field
- 字段idplaceholder
- 占位符类对象contents
- 相应字段的内容public T clearContent(String field, int index)
field
- 字段index
- 段落下标public void clearCase(int index)
index
- 用例下标public T end()
调用该方法后,则将数据的缓存写入到文本的缓存内容中,以表示当前段落数据编写完成, 之后再次调用添加数据方法时,则写入一段新的内容。
public T end(int contentIndex)
调用该方法后,则将数据的缓存写入到文本的缓存内容中,以表示当前段落数据编写完成, 之后再次调用添加数据方法时,则写入一段新的内容。
注意:若调用了getCase(int)
方法覆盖指定行内容的用例时,则传入的下标无效
contentIndex
- 内容下标public String toWriteFileJson()
在主体json串中,包括文件模板内容、默认值内容以及需要写入到文件中的内容,可通过该json串对文件写入类进行构造
public abstract void write()
protected abstract void write(FileTemplet templet, int caseStartIndex, int caseEndIndex)
方法支持反序遍历,即指定的下标为负数时,则表示反序遍历用例集,至指定位置的用例。若两个下标一致,则不处理数据
templet
- 需要写入的模板类对象caseStartIndex
- 写入文件开始下标caseEndIndex
- 写入文件结束下标protected abstract void contentWriteTemplet(FileTemplet templet, int caseStartIndex, int caseEndIndex)
templet
- 模板类caseStartIndex
- 用例起始行caseEndIndex
- 用例结束行public String toTempletJson()
由于部分写入类可传入多个模板,为方便构造,则需要对所有模板的json进行返回
protected abstract List<String> getAllTempletJson()
protected void replenishDefaultContent()
protected com.alibaba.fastjson.JSONObject getTextJson(String field, int textIndex)
获取文本json支持反序遍历,当传入的内容为负数时,则表示反序遍历集合,找到指定下标的内容
field
- 字段textIndex
- 文本下标protected int analysisIndex(int maxNum, int index, boolean isReciprocal)
数字最大为下标允许的最大值,是可以达到的,在解析集合下标时,其最大限制一定为“集合长度 - 1”
maxNum
- 数字最大限制index
- 下标isReciprocal
- 是否允许反向遍历protected abstract void createTempletFile(FileTemplet templet)
templet
- 模板文件对象protected T disposeWriteFieldsContent(Collection<String> fieldList, VoidSupplier sup)
当需要一次性对多个字段进行写入时(例如,插入一条测试用例),则需要将所有的字段看成是一个整体,若需要插入的字段包含自动换行的字段时,则先进行换行操作,再插入整体内容
fieldList
- 需要换行的字段集合sup
- 需要执行的操作,为函数式接口VoidSupplier
Copyright © 2024. All rights reserved.