"""Offline Eclipse Run Configuration (.launch XML) & argument assembly verifier for Ch15 (run-configuration)."""
import json
import shlex
import tempfile
import xml.etree.ElementTree as ET
from pathlib import Path
VERSION = "CPython 3.12.14 offline Eclipse .launch-configuration verifier (desktop Eclipse SWT GUI absent)"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def parse_launch_xml(xml_text):
root = ET.fromstring(xml_text)
data = {"type": root.attrib["type"]}
for elem in root:
key = elem.attrib["key"]
val = elem.attrib["value"]
if elem.tag == "booleanAttribute":
data[key] = val.lower() == "true"
else:
data[key] = val
return data
def build_java_command(cfg):
cmd = ["java"]
vm_args = cfg.get("org.eclipse.jdt.launching.VM_ARGUMENTS", "").strip()
if vm_args:
cmd.extend(shlex.split(vm_args))
cmd.append(cfg["org.eclipse.jdt.launching.MAIN_TYPE"])
prog_args = cfg.get("org.eclipse.jdt.launching.PROGRAM_ARGUMENTS", "").strip()
if prog_args:
cmd.extend(shlex.split(prog_args))
return cmd
def main():
cases = []
# Case 1: Serialize & parse Eclipse Java Application .launch XML
launch_xml = """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="OrderDemo"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.mbsky.demo.OrderMain"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="--mode batch --limit 50"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Xms128m -Xmx512m -Denv=staging"/>
<booleanAttribute key="org.eclipse.debug.ui.ATTR_CAPTURE_IN_CONSOLE" value="true"/>
<stringAttribute key="org.eclipse.debug.ui.ATTR_CAPTURE_IN_FILE" value="logs/run-output.log"/>
</launchConfiguration>
"""
cfg = parse_launch_xml(launch_xml)
cases.append(
check(
"launch_xml_serialization_and_parse",
"Parse Eclipse Java Application .launch XML verifying project, main class, arguments, and Common I/O",
{
"type": "org.eclipse.jdt.launching.localJavaApplication",
"project": "OrderDemo",
"main_type": "com.mbsky.demo.OrderMain",
"capture_console": True,
"capture_file": "logs/run-output.log",
},
{
"type": cfg["type"],
"project": cfg["org.eclipse.jdt.launching.PROJECT_ATTR"],
"main_type": cfg["org.eclipse.jdt.launching.MAIN_TYPE"],
"capture_console": cfg["org.eclipse.debug.ui.ATTR_CAPTURE_IN_CONSOLE"],
"capture_file": cfg["org.eclipse.debug.ui.ATTR_CAPTURE_IN_FILE"],
},
)
)
# Case 2: Command assembly places VM arguments before MainClass and Program arguments after MainClass
cmd = build_java_command(cfg)
main_idx = cmd.index("com.mbsky.demo.OrderMain")
cases.append(
check(
"program_vs_vm_arguments_command_order",
"Verify VM arguments precede main class while Program arguments follow main class",
{
"vm_slice": ["-Xms128m", "-Xmx512m", "-Denv=staging"],
"prog_slice": ["--mode", "batch", "--limit", "50"],
},
{
"vm_slice": cmd[1:main_idx],
"prog_slice": cmd[main_idx + 1:],
},
)
)
# Case 3: Common tab standard output routing to console and/or output file
with tempfile.TemporaryDirectory() as tmp:
out_path = Path(tmp) / "run-output.log"
console_buf = []
payload = "OrderBatch completed: 50 items\n"
if cfg["org.eclipse.debug.ui.ATTR_CAPTURE_IN_CONSOLE"]:
console_buf.append(payload)
if cfg.get("org.eclipse.debug.ui.ATTR_CAPTURE_IN_FILE"):
out_path.write_text(payload, encoding="utf-8")
file_content = out_path.read_text(encoding="utf-8")
cases.append(
check(
"common_tab_stdio_redirection",
"Verify Common tab console capture and file redirection both receive program output",
{"console_lines": 1, "file_matches_payload": True},
{"console_lines": len(console_buf), "file_matches_payload": file_content == payload},
)
)
# Case 4: Multiple Run Configurations coexist for the same Java project
profiles = {
"OrderMain-Dev": {
"org.eclipse.jdt.launching.MAIN_TYPE": "com.mbsky.demo.OrderMain",
"org.eclipse.jdt.launching.VM_ARGUMENTS": "-Denv=dev",
"org.eclipse.jdt.launching.PROGRAM_ARGUMENTS": "--dry-run",
},
"OrderMain-ProdCheck": {
"org.eclipse.jdt.launching.MAIN_TYPE": "com.mbsky.demo.OrderMain",
"org.eclipse.jdt.launching.VM_ARGUMENTS": "-Denv=prod",
"org.eclipse.jdt.launching.PROGRAM_ARGUMENTS": "--verify-only",
},
}
cases.append(
check(
"multi_launch_profile_isolation",
"Verify multiple named Run Configurations under one project produce isolated command lines",
{
"profile_count": 2,
"dev_cmd": ["java", "-Denv=dev", "com.mbsky.demo.OrderMain", "--dry-run"],
"prod_cmd": ["java", "-Denv=prod", "com.mbsky.demo.OrderMain", "--verify-only"],
},
{
"profile_count": len(profiles),
"dev_cmd": build_java_command(profiles["OrderMain-Dev"]),
"prod_cmd": build_java_command(profiles["OrderMain-ProdCheck"]),
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/eclipse-run-configuration-lab/explore-run-configuration.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-run-configuration.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联35章) · 含当前章节或实例 · mbskycom202612a2259.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2286.zip