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