Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
1f0773ea
提交
1f0773ea
authored
3月 06, 2019
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve TestKeywords
上级
12627199
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
89 行增加
和
27 行删除
+89
-27
TestKeywords.java
h2/src/test/org/h2/test/unit/TestKeywords.java
+89
-27
没有找到文件。
h2/src/test/org/h2/test/unit/TestKeywords.java
浏览文件 @
1f0773ea
...
...
@@ -9,7 +9,8 @@ import java.sql.Connection;
import
java.sql.DriverManager
;
import
java.sql.ResultSet
;
import
java.sql.Statement
;
import
java.util.HashSet
;
import
java.util.HashMap
;
import
java.util.Map.Entry
;
import
org.h2.command.Parser
;
import
org.h2.test.TestBase
;
...
...
@@ -25,6 +26,14 @@ import org.objectweb.asm.Opcodes;
*/
public
class
TestKeywords
extends
TestBase
{
private
enum
TokenType
{
IDENTIFIER
,
KEYWORD
,
CONTEXT_SENSITIVE_KEYWORD
;
}
/**
* Run just this test.
*
...
...
@@ -37,13 +46,13 @@ public class TestKeywords extends TestBase {
@Override
public
void
test
()
throws
Exception
{
final
Hash
Set
<
String
>
set
=
new
HashSet
<>();
final
Hash
Map
<
String
,
TokenType
>
tokens
=
new
HashMap
<>();
ClassReader
r
=
new
ClassReader
(
Parser
.
class
.
getResourceAsStream
(
"Parser.class"
));
r
.
accept
(
new
ClassVisitor
(
Opcodes
.
ASM7
)
{
@Override
public
FieldVisitor
visitField
(
int
access
,
String
name
,
String
descriptor
,
String
signature
,
Object
value
)
{
add
(
set
,
value
);
add
(
value
);
return
null
;
}
...
...
@@ -53,18 +62,18 @@ public class TestKeywords extends TestBase {
return
new
MethodVisitor
(
Opcodes
.
ASM7
)
{
@Override
public
void
visitLdcInsn
(
Object
value
)
{
add
(
set
,
value
);
add
(
value
);
}
};
}
void
add
(
HashSet
<
String
>
set
,
Object
value
)
{
void
add
(
Object
value
)
{
if
(!(
value
instanceof
String
))
{
return
;
}
String
s
=
(
String
)
value
;
int
l
=
s
.
length
();
if
(
l
==
0
||
ParserUtil
.
getSaveTokenType
(
s
,
false
,
0
,
l
,
true
)
!=
ParserUtil
.
IDENTIFIER
)
{
if
(
l
==
0
)
{
return
;
}
for
(
int
i
=
0
;
i
<
l
;
i
++)
{
...
...
@@ -73,15 +82,34 @@ public class TestKeywords extends TestBase {
return
;
}
}
set
.
add
(
s
);
final
TokenType
type
;
switch
(
ParserUtil
.
getSaveTokenType
(
s
,
false
,
0
,
l
,
true
))
{
case
ParserUtil
.
IDENTIFIER
:
type
=
TokenType
.
IDENTIFIER
;
break
;
case
ParserUtil
.
KEYWORD
:
type
=
TokenType
.
CONTEXT_SENSITIVE_KEYWORD
;
break
;
default
:
type
=
TokenType
.
KEYWORD
;
}
tokens
.
put
(
s
,
type
);
}
},
ClassReader
.
SKIP_DEBUG
|
ClassReader
.
SKIP_FRAMES
);
try
(
Connection
conn
=
DriverManager
.
getConnection
(
"jdbc:h2:mem:keywords"
))
{
Statement
stat
=
conn
.
createStatement
();
for
(
String
s
:
set
)
{
for
(
Entry
<
String
,
TokenType
>
entry
:
tokens
.
entrySet
())
{
String
s
=
entry
.
getKey
();
TokenType
type
=
entry
.
getValue
();
Throwable
exception1
=
null
,
exception2
=
null
;
try
{
stat
.
execute
(
"CREATE TABLE "
+
s
+
'('
+
s
+
" INT)"
);
stat
.
execute
(
"INSERT INTO "
+
s
+
'('
+
s
+
") VALUES (10)"
);
}
catch
(
Throwable
t
)
{
exception1
=
t
;
}
if
(
exception1
==
null
)
{
try
{
try
(
ResultSet
rs
=
stat
.
executeQuery
(
"SELECT "
+
s
+
" FROM "
+
s
))
{
assertTrue
(
rs
.
next
());
assertEquals
(
10
,
rs
.
getInt
(
1
));
...
...
@@ -95,16 +123,50 @@ public class TestKeywords extends TestBase {
}
stat
.
execute
(
"DROP TABLE "
+
s
);
stat
.
execute
(
"CREATE TABLE TEST("
+
s
+
" VARCHAR) AS VALUES '-'"
);
String
str
;
try
(
ResultSet
rs
=
stat
.
executeQuery
(
"SELECT TRIM("
+
s
+
" FROM '--a--') FROM TEST"
))
{
assertTrue
(
rs
.
next
());
assertEquals
(
"a"
,
rs
.
getString
(
1
)
);
str
=
rs
.
getString
(
1
);
}
stat
.
execute
(
"DROP TABLE TEST"
);
try
(
ResultSet
rs
=
stat
.
executeQuery
(
"SELECT ROW_NUMBER() OVER("
+
s
+
") WINDOW "
+
s
+
" AS ()"
))
{
}
if
(!
"a"
.
equals
(
str
))
{
exception2
=
new
AssertionError
();
}
}
catch
(
Throwable
t
)
{
throw
new
AssertionError
(
s
+
" cannot be used as identifier."
,
t
);
exception2
=
t
;
stat
.
execute
(
"DROP TABLE IF EXISTS TEST"
);
}
}
switch
(
type
)
{
case
IDENTIFIER:
if
(
exception1
!=
null
)
{
throw
new
AssertionError
(
s
+
" must be a keyword."
,
exception1
);
}
if
(
exception2
!=
null
)
{
throw
new
AssertionError
(
s
+
" must be a context-sensitive keyword."
,
exception2
);
}
break
;
case
KEYWORD:
if
(
exception1
==
null
&&
exception2
==
null
)
{
throw
new
AssertionError
(
s
+
" may be removed from a list of keywords."
);
}
if
(
exception1
==
null
)
{
throw
new
AssertionError
(
s
+
" may be a context-sensitive keyword."
);
}
break
;
case
CONTEXT_SENSITIVE_KEYWORD:
if
(
exception1
!=
null
)
{
throw
new
AssertionError
(
s
+
" must be a keyword."
,
exception1
);
}
if
(
exception2
==
null
)
{
throw
new
AssertionError
(
s
+
" may be removed from a list of context-sensitive keywords."
);
}
break
;
default
:
fail
();
}
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论