Skip to content

Commit

Permalink
move java test class to java dir
Browse files Browse the repository at this point in the history
  • Loading branch information
peacewong committed Nov 28, 2023
1 parent 79cad71 commit e95b085
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.linkis.ecm.restful;

import org.apache.linkis.common.conf.Configuration;
import org.apache.linkis.ecm.server.util.ECMUtils;
import org.apache.linkis.server.Message;
import org.apache.linkis.server.utils.ModuleUserUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.linkis.entrance.interceptor.impl;

import static org.junit.jupiter.api.Assertions.*;
package org.apache.linkis.entrance.interceptor.impl;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class TemplateConfUtilsTest {

@Test
void getCustomTemplateConfName() {
String sqlCode = ""
@Test
void getCustomTemplateConfName() {
String sqlCode =
""
+ "--注解\n"
+ "select * from table;\n"
+ " --注解 \n"
Expand All @@ -33,10 +34,11 @@ void getCustomTemplateConfName() {
+ " --@set yy=123\n"
+ " --注解";

String res = TemplateConfUtils.getCustomTemplateConfName(sqlCode, "sql");
assertEquals(res, "");
String res = TemplateConfUtils.getCustomTemplateConfName(sqlCode, "sql");
Assertions.assertEquals(res, "");

String sqlCode2 = ""
String sqlCode2 =
""
+ "---@set 123=注解\n"
+ "select * from table;\n"
+ " --注解 \n"
Expand All @@ -45,10 +47,11 @@ void getCustomTemplateConfName() {
+ " --@set yy=123\n"
+ " --注解";

res = TemplateConfUtils.getCustomTemplateConfName(sqlCode2, "sql");
assertEquals(res, "");
res = TemplateConfUtils.getCustomTemplateConfName(sqlCode2, "sql");
Assertions.assertEquals(res, "");

String sqlCode3 = ""
String sqlCode3 =
""
+ "---@set ec.resource.name=345\n"
+ "select * from table;\n"
+ " --注解 \n"
Expand All @@ -58,19 +61,21 @@ void getCustomTemplateConfName() {
+ " --@set yy=123\n"
+ " --注解";

res = TemplateConfUtils.getCustomTemplateConfName(sqlCode3, "sql");
assertEquals(res, "345");
res = TemplateConfUtils.getCustomTemplateConfName(sqlCode3, "sql");
Assertions.assertEquals(res, "345");

String sqlCode4 = ""
String sqlCode4 =
""
+ "---@set ec.resource.name= name1 \n"
+ " select \"--注解\" as test\n"
+ " --@set yy=123\n"
+ " --注解";

res = TemplateConfUtils.getCustomTemplateConfName(sqlCode4, "sql");
assertEquals(res, "name1");
res = TemplateConfUtils.getCustomTemplateConfName(sqlCode4, "sql");
Assertions.assertEquals(res, "name1");

String sqlCode5 = ""
String sqlCode5 =
""
+ "##@set ec.resource.name=pyname1\n"
+ "select * from table;\n"
+ " --注解 \n"
Expand All @@ -80,44 +85,44 @@ void getCustomTemplateConfName() {
+ "#@set yy=123\n"
+ " #注解";

res = TemplateConfUtils.getCustomTemplateConfName(sqlCode5, "python");
assertEquals(res, "pyname1");
res = TemplateConfUtils.getCustomTemplateConfName(sqlCode5, "python");
Assertions.assertEquals(res, "pyname1");

String sqlCode6 = ""
String sqlCode6 =
""
+ "///@set ec.resource.name= scalaname1 \n"
+ " select \"//注解\" as test\n"
+ "//@set yy=123\n"
+ " #注解";

res = TemplateConfUtils.getCustomTemplateConfName(sqlCode6, "scala");
assertEquals(res, "scalaname1");
res = TemplateConfUtils.getCustomTemplateConfName(sqlCode6, "scala");
Assertions.assertEquals(res, "scalaname1");

String sqlCode7 = ""
String sqlCode7 =
""
+ "---@set ec.resource.name= hqlname1 \n"
+ " select \"--注解\" as test\n"
+ " --@set yy=123\n"
+ " --注解";

res = TemplateConfUtils.getCustomTemplateConfName(sqlCode7, "hql");
assertEquals(res, "hqlname1");
res = TemplateConfUtils.getCustomTemplateConfName(sqlCode7, "hql");
Assertions.assertEquals(res, "hqlname1");

String sqlCode8 = "---@set ec.resource.name=linkis_test2;\n"
String sqlCode8 =
"---@set ec.resource.name=linkis_test2;\n"
+ " ---@set ec.resource.name=scriptis_test hive;\n"
+ " select * from dss autotest.demo data limit 100;";
res=TemplateConfUtils.getCustomTemplateConfName(sqlCode8,"hql");
assertEquals(res,"linkis_test2");


}
@Test
void getCustomTemplateConfName2() {


String sqlCode9= "---@set ec.resource.name=linkis_test2;\r\n---@set ec.resource.name=scriptis_test_hive;\r\n--@set limitn=100\r\nselect * from dss_autotest.demo_data limit ${limitn};\r\n";
res = TemplateConfUtils.getCustomTemplateConfName(sqlCode8, "hql");
Assertions.assertEquals(res, "linkis_test2");
}

@Test
void getCustomTemplateConfName2() {

String sqlCode9 =
"---@set ec.resource.name=linkis_test2;\r\n---@set ec.resource.name=scriptis_test_hive;\r\n--@set limitn=100\r\nselect * from dss_autotest.demo_data limit ${limitn};\r\n";

String res=TemplateConfUtils.getCustomTemplateConfName(sqlCode9,"hql");
assertEquals(res,"linkis_test2");
}
}
String res = TemplateConfUtils.getCustomTemplateConfName(sqlCode9, "hql");
Assertions.assertEquals(res, "linkis_test2");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,49 @@

package org.apache.linkis.entrance.interceptor.impl;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import org.apache.linkis.common.utils.CodeAndRunTypeUtils;

import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;
import scala.reflect.internal.TypeDebugging;

public class TestCommentHelper {
String sqlCode=""
+ "--注解\n"
+ "select * from table;\n"
+ " --注解 \n"
+ "--注解\n"
+ " select \"--注解\" as test\n"
+ " --@set yy=123\n"
+ " --注解";
String sqlCode =
""
+ "--注解\n"
+ "select * from table;\n"
+ " --注解 \n"
+ "--注解\n"
+ " select \"--注解\" as test\n"
+ " --@set yy=123\n"
+ " --注解";

String scalaCode=""
+ "// 注解\n"
+ "print(1+1)\n"
+ "//@set yy=123\n"
+ " print(2)\n"
+ " // 注解 \n"
+ "// test\n"
+ "print(\"//注解测试\")";
String scalaCode =
""
+ "// 注解\n"
+ "print(1+1)\n"
+ "//@set yy=123\n"
+ " print(2)\n"
+ " // 注解 \n"
+ "// test\n"
+ "print(\"//注解测试\")";

String scalaCodeRes="print(1+1)\n"
+ "print(2)\n"
+ "print(\"//注解测试\")";
String scalaCodeRes = "print(1+1)\n" + "print(2)\n" + "print(\"//注解测试\")";

@Test
void sqlDealCommentTest() {
String code=SQLCommentHelper.dealComment(sqlCode);
//System.out.println(code);
}
@Test
void sqlDealCommentTest() {
String code = SQLCommentHelper.dealComment(sqlCode);
// System.out.println(code);
}

@Test
void scalaDealCommentTest() {
String code=ScalaCommentHelper.dealComment(scalaCode);
String[] lines = Arrays.stream(code.split("\n"))
@Test
void scalaDealCommentTest() {
String code = ScalaCommentHelper.dealComment(scalaCode);
String[] lines =
Arrays.stream(code.split("\n"))
.map(String::trim)
.filter(x -> StringUtils.isNotBlank(x))
.toArray(String[]::new);
String result=String.join("\n",lines);
// assertEquals(result,scalaCodeRes);
}


}
String result = String.join("\n", lines);
// assertEquals(result,scalaCodeRes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.linkis.entrance.interceptor.impl;

import org.apache.linkis.entrance.log.Cache;
import org.apache.linkis.entrance.log.HDFSCacheLogWriter;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import oshi.util.FileUtil;

class TestHDFSCacheLogWriter {

@Test
void write() throws IOException {

Cache cache = new Cache(5);
String fileName = UUID.randomUUID().toString().replace("-", "") + "-test.log";
String logPath = System.getProperty("java.io.tmpdir") + File.separator + fileName;
System.out.println(logPath);
String chartSet = "utf-8";
String username = System.getProperty("user.name");

File file = new File(logPath);
file.createNewFile();

HDFSCacheLogWriter logWriter =
new HDFSCacheLogWriter(
// "D:\\DataSphere\\linkis\\docs\\test.log",
logPath, chartSet, cache, username);

String[] msgArr =
new String[] {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",
"17", "18"
};

List<String> msgList = new ArrayList<String>(Arrays.asList(msgArr));
String msg = String.join("\n", msgList);

logWriter.write(msg);
logWriter.flush();

List<String> list = FileUtil.readFile(logPath);
String res = String.join("\n", list);

res = res.replace("\n\n", "\n");
res = StringUtils.strip(res, " \n");
Assertions.assertEquals(res, msg);
}
}
Loading

0 comments on commit e95b085

Please sign in to comment.