Skip to content

记录

Kotlin 测试生成进展记录

Java to Kotlin Converter

进展:

  1. 可以使用 Idea 插件的形式调用 ConvertJavaToKotlin Action,已经完成了 demo(IdeaProjects/j2k-plugin);
  2. 参考插件项目:Repos/multi-file-kotlin-converter-plugin, Repos/vcs-kotlin-converter, workspace/evosuite;
  3. 参考 Soot 项目:Repos/ERCatcher

TODO LIST:

  • 写一个插件项目 demo,实现 Java to Kotlin
  • 学习 idea 插件的开发
  • 搭建整体架构

架构:

graph LR
A(Android APK) --Soot--> B(Jimple)
B --测试生成--> C(Bytecode测试用例)
C --Fernflower--> D(Java测试用例)
D --J2K--> E(Kotlin测试用例)

整体架构由两部分构成:

  • 核心算法部分:
    • 使用 Soot 分析 Android APK 得到 Jimple
    • 在 Jimple 上生成 Bytecode 形式的测试用例
    • 使用 Fernflower 将 Bytecode 反编译为 Java
  • idea 插件部分:
    • 获取待分析的 Android APK,启动核心算法
    • 将生成的 Java 测试用例转化为 Kotlin

Android Studio Version

Android Studio Chipmunk | 2021.2.1 Patch 1
Build #AI-212.5712.43.2112.8609683, built on May 19, 2022
Runtime version: 11.0.12+7-b1504.28-7817840 amd64
VM: OpenJDK 64-Bit Server VM by Oracle Corporation
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 8
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: CheckStyle-IDEA (5.67.4), org.jetbrains.kotlin (212-1.7.0-release-281-AS5457.46)

img-20220725155055

对应 IntelliJ IDEA Version:2021.2.4

IntelliJ Platform Plugin SDK

PART I - Plugins

Plugin Structure

  • Manage State: Magage some state or logic that is only needed when the user performs a specific operation -> Service
  • Persisting State: Store the state of your plugin at the application or project level -> Service (PersistentStateComponent interface, Persisting State of Components)
  • SubScribing to Events: Subcribe to events -> listener | create an extension for a dedicated extension point
  • Application Startup -> listener (subsribing to the AppLiftcycleListener topic)

img-20220727192911

Figure 1: Getting Service Flow

Other

  1. Android Studio Plugin Development
  2. Tools - Gradle IntelliJ Plugin
  3. 将运行插件后启动的 Android Studio 示例运行在 JBR 上
  4. Gradle IntelliJ Plugin - Usage Examples
  5. IntelliJ Platform Explorer: A search tool for browsing Extension Points, Services and Listeners
  6. Explore the IntelliJ PlatformAPI
  7. Gerrit Code Review: 一个 IntelliJ Plugin 例子
  8. EvoSuite 插件修改版

Part II - Base Platform

架构设计

graph LR
A(Idea 插件) --"选择参数、命令行调用"--> B(jar 包)
graph LR
A(程序分析) --"选择参数、命令行调用"--> B(测试生成)
  1. Idea 插件
    1. Java -> Kotlin
    2. 界面化(运行、参数设置、运行输出)
    3. 自动设置 project 和 module 路径
  2. jar 包
    1. 代码逻辑
    2. 生成算法

UI 设计

  1. 添加右键 popmenu -> Run KotSuite
  2. 添加底部 toolWindow -> 显示 KotSuite 运行日志
  3. 添加 Dialog -> 显示运行选项

img-20220813172956

img-20220814012122

img-20220814012156

img-20220814012216

未来工作

  1. 学习 IntelliJ Plugin 开发,阅读文档;
  2. 看 EvoSuite 源码,学习插件 + jar 包的项目架构;
  3. 看 Gerrit 插件源码;

Reference Projects

  1. IdeaProjects/evosuite -> 插件和 jar 包的组合方式(主要参考项目)
  2. IdeaProjects/gerrit-intellij-plugin -> 插件和 jar 包的组合方式
  3. Repos/intellij-community -> intellij platform 开源平台
  4. IdeaProjects/kotsuite -> 框架项目
  5. IdeaProjects/intellij-sdk-code-samples -> 插件官方样例
  6. https://github.com/GZoltar/gzoltar -> Java 项目架构

数据集

AndroidTest

Categories of Android tests

instrumentation test

Developing Android unit and instrumentation tests - Tutorial

  • app/src/main/kotlin - for your source code of your main application build
  • app/src/test/kotlin - for any unit test which can run on the JVM
  • app/src/androidTest/kotlin - for any test which should run on an Android device

Soot

Soot技术博客