ConnectorParserTest.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package org.dbsyncer.parser;
  2. import org.apache.commons.io.FileUtils;
  3. import org.dbsyncer.common.util.JsonUtil;
  4. import org.dbsyncer.connector.config.ConnectorConfig;
  5. import org.dbsyncer.connector.enums.ConnectorEnum;
  6. import org.dbsyncer.parser.model.Connector;
  7. import org.dbsyncer.parser.model.Mapping;
  8. import org.dbsyncer.parser.model.TableGroup;
  9. import org.json.JSONException;
  10. import org.json.JSONObject;
  11. import org.junit.Test;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.net.URL;
  15. /**
  16. * @author AE86
  17. * @version 1.0.0
  18. * @date 2019/10/9 23:46
  19. */
  20. public class ConnectorParserTest {
  21. @Test
  22. public void testConnector() throws IOException, JSONException {
  23. String json = readJson("Connector.json");
  24. System.out.println(json);
  25. // 解析基本信息
  26. JSONObject conn = new JSONObject(json);
  27. JSONObject config = (JSONObject) conn.remove("config");
  28. Connector connector = JsonUtil.jsonToObj(conn.toString(), Connector.class);
  29. // 解析配置
  30. String connectorType = config.getString("connectorType");
  31. Class<?> configClass = ConnectorEnum.getConfigClass(connectorType);
  32. Object obj = JsonUtil.jsonToObj(config.toString(), configClass);
  33. connector.setConfig((ConnectorConfig) obj);
  34. System.out.println(connector);
  35. }
  36. @Test
  37. public void testMapping() throws IOException, JSONException {
  38. String json = readJson("Mapping.json");
  39. System.out.println(json);
  40. // 解析基本信息
  41. JSONObject map = new JSONObject(json);
  42. Mapping mapping = JsonUtil.jsonToObj(map.toString(), Mapping.class);
  43. System.out.println(mapping);
  44. }
  45. @Test
  46. public void testTableGroup() throws IOException, JSONException {
  47. String json = readJson("TableGroup.json");
  48. System.out.println(json);
  49. // 解析基本信息
  50. JSONObject group = new JSONObject(json);
  51. TableGroup tableGroup = JsonUtil.jsonToObj(group.toString(), TableGroup.class);
  52. System.out.println(tableGroup);
  53. }
  54. /**
  55. * 读取JSON文件
  56. *
  57. * @param fileName
  58. * @return
  59. * @throws IOException
  60. */
  61. private String readJson(String fileName) throws IOException {
  62. ClassLoader loader = this.getClass().getClassLoader();
  63. URL fileURL = loader.getResource(fileName);
  64. if (null != fileURL) {
  65. String filePath = fileURL.getFile();
  66. File file = new File(filePath);
  67. return FileUtils.readFileToString(file, "UTF-8");
  68. }
  69. return "";
  70. }
  71. }