Browse Source

http API以及单元测试补充

pull/1/head
richie 5 years ago
parent
commit
9b7524ca26
  1. 8
      readme.md
  2. 37
      src/main/java/com/fanruan/api/cal/ContainerKit.java
  3. 15
      src/main/java/com/fanruan/api/cal/ParameterKit.java
  4. 14
      src/main/java/com/fanruan/api/conf/BaseConfiguration.java
  5. 22
      src/main/java/com/fanruan/api/consts/EncodeConstantsKit.java
  6. 48
      src/main/java/com/fanruan/api/macro/EncodeConstants.java
  7. 4
      src/main/java/com/fanruan/api/net/http/HttpKit.java
  8. 4
      src/main/java/com/fanruan/api/net/http/rs/HttpRequest.java
  9. 39
      src/main/java/com/fanruan/api/xml/XmlKit.java
  10. 24
      src/test/java/com/fanruan/api/cal/ContainerKitTest.java
  11. 46
      src/test/java/com/fanruan/api/cal/ParameterKitTest.java

8
readme.md

@ -24,7 +24,7 @@ SessionKit#getSession(@NotNull String sessionID);
## 提交新API要求
* 所有的API方法的返回值和参数,仅允许使用基本类型、接口类型或者使用了@Open标记的对象类型。
* 所有的API方法的返回值和参数,仅允许使用JDK自带的类、接口类型、插件抽象类、注解或者使用了@Open标记的对象类型。
* 所有的API方法均需要有单元测试覆盖。
@ -54,10 +54,10 @@ SessionKit#getSession(@NotNull String sessionID);
## 如何提交新的API
1、访问[https://git.fanruan.com](https://git.fanruan.com)并注册一个自己的账号;
1、访问[https://git.fanruan.com](https://git.fanruan.com)并注册一个自己的账号;
2、将[https://git.fanruan.com/fanruan/finekit](https://git.fanruan.com/fanruan/finekit) fork到自己账户下;
2、然后将[https://git.fanruan.com/fanruan/finekit](https://git.fanruan.com/fanruan/finekit) fork到自己账户下;
3、新增相关的API代码,并提交到自己的仓库;
3、克隆FineKit代码到本地,新增相关的API代码,并提交到自己的仓库;
4、提交Pull Request到FineKit主仓库,等待审核通过被合并即可。

37
src/main/java/com/fanruan/api/cal/ContainerKit.java

@ -0,0 +1,37 @@
package com.fanruan.api.cal;
import com.fr.general.FArray;
import com.fr.stable.ArrayProvider;
import java.util.Collection;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-08-29
* 创建容器的工具类
*/
public class ContainerKit {
/**
* 创建一个插件可识别的数组对象
*
* @param array 原始数组
* @param <T> 数组元素类型
* @return 插件可识别的数组对象
*/
public static <T> ArrayProvider<T> newArray(T[] array) {
return new FArray<>(array);
}
/**
* 创建一个插件可识别的数组对象
*
* @param collection 原始集合
* @param <T> 数组元素类型
* @return 插件可识别的数组对象
*/
public static <T> ArrayProvider<T> newArray(Collection<T> collection) {
return new FArray<>(collection);
}
}

15
src/main/java/com/fanruan/api/cal/ParameterKit.java

@ -37,6 +37,17 @@ public class ParameterKit {
return new Parameter(name, value);
}
/**
* 从字符串中分析中有哪些需要的参数
*
* @param text 字符串
* @param hasColumnRow 是否需要分析格子类型的参数
* @return 字符串中包含的参数集合
*/
public static Parameter[] analyze4Parameters(String text, boolean hasColumnRow) {
return ParameterHelper.analyze4Parameters(text, hasColumnRow);
}
/**
* 分析公式中所携带的参数
*
@ -71,11 +82,11 @@ public class ParameterKit {
/**
* 创建一个用于计算的参数对名字空间
*
* @param ps ParameterProvider的数组
* @param ps 参数数组数组
* @return 名字空间用于传递给算子做计算
*/
public static @NotNull ParameterMapNameSpace createParameterProvider(ParameterProvider[] ps) {
public static @NotNull ParameterMapNameSpace createParameterMapNameSpace(ParameterProvider[] ps) {
return ParameterMapNameSpace.create(ps);
}
}

14
src/main/java/com/fanruan/api/conf/BaseConfiguration.java

@ -7,6 +7,20 @@ import com.fr.config.DefaultConfiguration;
* @author richie
* @version 10.0
* Created by richie on 2019-08-16
* 插件要实现配置类的时候直接继承该抽象类即可
* <p>
* private static class MyConf extends BaseConfiguration {
*
* private static volatile MyConf instance = null;
*
* public static MyConf getInstance() {
* if (instance == null) {
* instance = getConfigInstance(MyConf.class);
* }
* return instance;
* }
* }
* <p/>
*/
public abstract class BaseConfiguration extends DefaultConfiguration {

22
src/main/java/com/fanruan/api/consts/EncodeConstantsKit.java

@ -1,22 +0,0 @@
package com.fanruan.api.consts;
import com.fr.stable.EncodeConstants;
public class EncodeConstantsKit {
public static final String ENCODING_UTF_8 = EncodeConstants.ENCODING_UTF_8;
public static final String ENCODING_GBK = EncodeConstants.ENCODING_GBK;
public static final String ENCODING_BIG5 = EncodeConstants.ENCODING_BIG5;
public static final String ENCODING_ISO_8859_1 = EncodeConstants.ENCODING_ISO_8859_1;
public static final String ENCODING_UTF_16 = EncodeConstants.ENCODING_UTF_16;
public static final String ENCODING_EUC_JP = EncodeConstants.ENCODING_EUC_JP;
public static final String ENCODING_EUC_KR = EncodeConstants.ENCODING_EUC_KR;
public static final String ENCODING_CP850 = EncodeConstants.ENCODING_CP850;
}

48
src/main/java/com/fanruan/api/macro/EncodeConstants.java

@ -0,0 +1,48 @@
package com.fanruan.api.macro;
/**
* 编码常量
*/
public interface EncodeConstants {
/**
* utf-8编码
*/
String ENCODING_UTF_8 = com.fr.stable.EncodeConstants.ENCODING_UTF_8;
/**
* gbk编码
*/
String ENCODING_GBK = com.fr.stable.EncodeConstants.ENCODING_GBK;
/**
* big5编码一般用于繁体中文
*/
String ENCODING_BIG5 = com.fr.stable.EncodeConstants.ENCODING_BIG5;
/**
* ios-8859-1编码
*/
String ENCODING_ISO_8859_1 = com.fr.stable.EncodeConstants.ENCODING_ISO_8859_1;
/**
* utf16编码
*/
String ENCODING_UTF_16 = com.fr.stable.EncodeConstants.ENCODING_UTF_16;
/**
* euc-jp编码
*/
String ENCODING_EUC_JP = com.fr.stable.EncodeConstants.ENCODING_EUC_JP;
/**
* euc-kr编码
*/
String ENCODING_EUC_KR = com.fr.stable.EncodeConstants.ENCODING_EUC_KR;
/**
* cp850编码
*/
String ENCODING_CP850 = com.fr.stable.EncodeConstants.ENCODING_CP850;
}

4
src/main/java/com/fanruan/api/net/http/HttpKit.java

@ -1,6 +1,6 @@
package com.fanruan.api.net.http;
import com.fanruan.api.consts.EncodeConstantsKit;
import com.fanruan.api.macro.EncodeConstants;
import com.fanruan.api.log.LogKit;
import com.fanruan.api.net.http.rs.BaseHttpResponseHandle;
import com.fanruan.api.net.http.rs.HttpRequest;
@ -641,7 +641,7 @@ public class HttpKit {
*/
public static String buildUrl(String url, Map<String, String> params) {
try {
return buildUrl(url, params, EncodeConstantsKit.ENCODING_UTF_8);
return buildUrl(url, params, EncodeConstants.ENCODING_UTF_8);
} catch (UnsupportedEncodingException ignore) {
}
return url;

4
src/main/java/com/fanruan/api/net/http/rs/HttpRequest.java

@ -1,6 +1,6 @@
package com.fanruan.api.net.http.rs;
import com.fanruan.api.consts.EncodeConstantsKit;
import com.fanruan.api.macro.EncodeConstants;
import com.fr.third.org.apache.http.HttpEntity;
import com.fr.third.org.apache.http.client.config.RequestConfig;
import org.jetbrains.annotations.NotNull;
@ -114,7 +114,7 @@ public class HttpRequest {
private RequestConfig config = DEFAULT;
@Nullable
private HttpEntity httpEntity;
private String encoding = EncodeConstantsKit.ENCODING_UTF_8;
private String encoding = EncodeConstants.ENCODING_UTF_8;
private HttpRequestType method = HttpRequestType.GET;
private Builder() {

39
src/main/java/com/fanruan/api/xml/XmlKit.java

@ -1,20 +1,49 @@
package com.fanruan.api.xml;
import com.fr.general.xml.GeneralXMLTools;
import com.fr.stable.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLReadable;
import com.fr.stable.xml.XMLTools;
import com.fr.stable.xml.XMLable;
import com.fr.stable.xml.XMLableReader;
import com.fr.stable.xml.XMLPrintWriter;
import java.io.InputStream;
/**
* xml读写相关类
*/
public class XmlKit {
/**
* 从输入流中读取对象
* @param xmlReadable xml读取对象
* @param inputStream xml输入流
*
* @param read xml读取对象
* @param in xml输入流
*/
public static void readInputStreamXML(XMLReadable read, InputStream in) throws Exception {
XMLTools.readInputStreamXML(read, in);
}
/**
* 将xml读取为具体的java对象
*
* @param reader xml读取器
* @return java对象
*/
public static XMLable readXMLable(XMLableReader reader) {
return GeneralXMLTools.readXMLable(reader);
}
/**
* 将java对象写为xml文件
*
* @param writer xml写入器
* @param xml 实际java对象
* @param tagName xml标签名
*/
public static void readInputStreamXML(XMLReadable xmlReadable, InputStream inputStream) throws Exception {
XMLTools.readInputStreamXML(xmlReadable, inputStream);
public static void writeXMLable(XMLPrintWriter writer, XMLable xml, String tagName) {
GeneralXMLTools.writeXMLable(writer, xml, tagName);
}
}

24
src/test/java/com/fanruan/api/cal/ContainerKitTest.java

@ -0,0 +1,24 @@
package com.fanruan.api.cal;
import com.fr.stable.ArrayProvider;
import com.fr.third.guava.collect.Lists;
import org.junit.Assert;
import org.junit.Test;
/**
* @author richie
* @version 10.0
* Created by richie on 2019-08-29
*/
public class ContainerKitTest {
@Test
public void newArray() {
ArrayProvider<String> array = ContainerKit.newArray(new String[]{"abc", "xyz"});
Assert.assertEquals("xyz", array.elementAt(1));
ArrayProvider<String> array2 = ContainerKit.newArray(Lists.newArrayList("abc", "xyz"));
Assert.assertEquals("abc", array2.elementAt(0));
}
}

46
src/test/java/com/fanruan/api/cal/ParameterKitTest.java

@ -31,6 +31,33 @@ public class ParameterKitTest extends Prepare {
Assert.assertArrayEquals(new String[]{"p1", "p2", "p4", "p5"}, names);
}
@Test
public void analyze4Parameters() {
String sql = "SELECT * FROM Equipment where 'Tel' = ${if(true, 1, 2)}";
ParameterProvider[] parameters = ParameterKit.analyze4Parameters(sql, false);
Assert.assertEquals(0, parameters.length);
parameters = ParameterKit.analyze4Parameters(sql, true);
Assert.assertEquals(0, parameters.length);
sql = "SELECT * FROM s订单 where 1=1 ${if(len(aa)==0,\"\",\" and 货主城市 in (\"+\"'\"+treelayer(aa,true,\"\\',\\'\")+\"'\"+\")\")}";
parameters = ParameterKit.analyze4Parameters(sql, false);
Assert.assertEquals(1, parameters.length);
Assert.assertEquals(parameters[0].getName(), "aa");
sql = "SELECT * FROM Equipment where 'Tel' = ${if(false, 1, 2)}";
parameters = ParameterKit.analyze4Parameters(sql, false);
Assert.assertEquals(0, parameters.length);
parameters = ParameterKit.analyze4Parameters(sql, true);
Assert.assertEquals(0, parameters.length);
sql = "SELECT * FROM s订单 where 1=1 ${if(len(aa)==0,\"\",\" and 货主城市 in (\"+\"'\"+treelayer(aa,false,\"\\',\\'\")+\"'\"+\")\")}";
parameters = ParameterKit.analyze4Parameters(sql, false);
Assert.assertEquals(1, parameters.length);
Assert.assertEquals(parameters[0].getName(), "aa");
sql = "SELECT * FROM s订单 where 1=1 ${if(len(aa)==0,\"\",\" and 货主城市 in (\"+\"'\"+treelayer(aa,\"true\",\"\\',\\'\")+\"'\"+\")\")}";
parameters = ParameterKit.analyze4Parameters(sql, false);
Assert.assertEquals(1, parameters.length);
Assert.assertEquals(parameters[0].getName(), "aa");
}
@Test
public void createParameterMapNameSpace() {
Map<String, Object> map = new HashMap<>();
@ -49,6 +76,25 @@ public class ParameterKitTest extends Prepare {
}
}
@Test
public void createParameterMapNameSpace2() {
ParameterProvider[] parameters = new ParameterProvider[]{
ParameterKit.newParameter("p1", 100),
ParameterKit.newParameter("p2", 200)
};
NameSpace ns = ParameterKit.createParameterMapNameSpace(parameters);
Calculator calculator = Calculator.createCalculator();
calculator.pushNameSpace(ns);
try {
Object r = calculator.eval("=$p1 + $p2");
Assert.assertEquals(300, r);
} catch (UtilEvalError utilEvalError) {
Assert.fail();
} finally {
calculator.removeNameSpace(ns);
}
}
@Test
public void newParameter() {
ParameterProvider provider = ParameterKit.newParameter();

Loading…
Cancel
Save