use jbuild::runner::{build_classpath, detect_main_class, extract_main_class_from_config};
use jbuild::build::BuildSystem;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_build_classpath_maven() {
let temp_dir = TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src/main/java")).unwrap();
fs::create_dir_all(temp_dir.path().join("target/classes")).unwrap();
let pom_content = r#"<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test-project</artifactId>
<version>1.0.0</version>
</project>"#;
fs::write(temp_dir.path().join("pom.xml"), pom_content).unwrap();
let classpath = build_classpath(temp_dir.path(), &BuildSystem::Maven).unwrap();
assert!(classpath.contains("target/classes"));
}
#[test]
fn test_build_classpath_gradle() {
let temp_dir = TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src/main/java")).unwrap();
fs::create_dir_all(temp_dir.path().join("build/classes/java/main")).unwrap();
let gradle_content = r#"
plugins {
id 'java'
}
"#;
fs::write(temp_dir.path().join("build.gradle"), gradle_content).unwrap();
let classpath = build_classpath(temp_dir.path(), &BuildSystem::Gradle).unwrap();
assert!(classpath.contains("build/classes/java/main"));
}
#[test]
fn test_detect_main_class_from_sources() {
let temp_dir = TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src/main/java/com/example")).unwrap();
let main_class_content = r#"
package com.example;
public class MainApp {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
"#;
fs::write(
temp_dir.path().join("src/main/java/com/example/MainApp.java"),
main_class_content,
).unwrap();
let main_class = detect_main_class(temp_dir.path()).unwrap();
assert!(main_class.is_some());
}
#[test]
fn test_detect_main_class_multiple_candidates() {
let temp_dir = TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src/main/java/com/example")).unwrap();
let main1_content = r#"
package com.example;
public class App1 {
public static void main(String[] args) {
System.out.println("App1");
}
}
"#;
fs::write(
temp_dir.path().join("src/main/java/com/example/App1.java"),
main1_content,
).unwrap();
let main2_content = r#"
package com.example;
public class App2 {
public static void main(String[] args) {
System.out.println("App2");
}
}
"#;
fs::write(
temp_dir.path().join("src/main/java/com/example/App2.java"),
main2_content,
).unwrap();
let main_class = detect_main_class(temp_dir.path()).unwrap();
assert!(main_class.is_some());
}
#[test]
fn test_detect_main_class_no_main_method() {
let temp_dir = TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src/main/java/com/example")).unwrap();
let class_content = r#"
package com.example;
public class Library {
public void doSomething() {
// Library method
}
}
"#;
fs::write(
temp_dir.path().join("src/main/java/com/example/Library.java"),
class_content,
).unwrap();
let main_class = detect_main_class(temp_dir.path()).unwrap();
assert_eq!(main_class, None);
}
#[test]
fn test_extract_main_class_from_maven_config() {
let temp_dir = TempDir::new().unwrap();
let pom_content = r#"<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test-project</artifactId>
<version>1.0.0</version>
<properties>
<exec.mainClass>com.example.MyMainClass</exec.mainClass>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>${exec.mainClass}</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>"#;
fs::write(temp_dir.path().join("pom.xml"), pom_content).unwrap();
let main_class = extract_main_class_from_config(temp_dir.path()).unwrap();
assert!(main_class.is_none() || main_class == Some("${exec.mainClass}".to_string()));
}
#[test]
fn test_extract_main_class_from_gradle_config() {
let temp_dir = TempDir::new().unwrap();
let gradle_content = r#"
plugins {
id 'application'
}
application {
mainClass = 'com.example.GradleMain'
}
"#;
fs::write(temp_dir.path().join("build.gradle"), gradle_content).unwrap();
let main_class = extract_main_class_from_config(temp_dir.path()).unwrap();
assert_eq!(main_class, Some("com.example.GradleMain".to_string()));
}
#[test]
fn test_build_classpath_non_existent_directories() {
let temp_dir = TempDir::new().unwrap();
let pom_content = r#"<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test-project</artifactId>
<version>1.0.0</version>
</project>"#;
fs::write(temp_dir.path().join("pom.xml"), pom_content).unwrap();
let classpath = build_classpath(temp_dir.path(), &BuildSystem::Maven).unwrap();
assert!(classpath.is_empty() || classpath == ".");
}
#[test]
fn test_detect_main_class_nested_packages() {
let temp_dir = TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src/main/java/com/example/deep/nested")).unwrap();
let main_class_content = r#"
package com.example.deep.nested;
public class DeepMain {
public static void main(String[] args) {
System.out.println("Deep main class");
}
}
"#;
fs::write(
temp_dir.path().join("src/main/java/com/example/deep/nested/DeepMain.java"),
main_class_content,
).unwrap();
let main_class = detect_main_class(temp_dir.path()).unwrap();
assert!(main_class.is_some());
}
#[test]
fn test_detect_main_class_ignore_kotlin() {
let temp_dir = TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src/main/java/com/example")).unwrap();
let kotlin_content = r#"
package com.example
object KotlinMain {
@JvmStatic
fun main(args: Array<String>) {
println("Kotlin main")
}
}
"#;
fs::write(
temp_dir.path().join("src/main/java/com/example/KotlinMain.kt"),
kotlin_content,
).unwrap();
let java_content = r#"
package com.example;
public class JavaMain {
public static void main(String[] args) {
System.out.println("Java main");
}
}
"#;
fs::write(
temp_dir.path().join("src/main/java/com/example/JavaMain.java"),
java_content,
).unwrap();
let main_class = detect_main_class(temp_dir.path()).unwrap();
assert!(main_class.is_some());
}
#[test]
fn test_extract_main_class_malformed_config() {
let temp_dir = TempDir::new().unwrap();
let malformed_pom = r#"<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test-project</artifactId>
<version>1.0.0</version>
<!-- Malformed XML - missing closing tags -->
"#;
fs::write(temp_dir.path().join("pom.xml"), malformed_pom).unwrap();
let main_class = extract_main_class_from_config(temp_dir.path()).unwrap();
assert_eq!(main_class, None);
}
#[test]
fn test_build_classpath_special_characters() {
let temp_dir = TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src/main/java")).unwrap();
let pom_content = r#"<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test-project</artifactId>
<version>1.0.0</version>
</project>"#;
fs::write(temp_dir.path().join("pom.xml"), pom_content).unwrap();
let classpath = build_classpath(temp_dir.path(), &BuildSystem::Maven).unwrap();
assert!(!classpath.contains('\n')); assert!(!classpath.contains('\r')); }
#[test]
fn test_detect_main_class_ignore_inner_classes() {
let temp_dir = TempDir::new().unwrap();
fs::create_dir_all(temp_dir.path().join("src/main/java/com/example")).unwrap();
let class_content = r#"
package com.example;
public class OuterClass {
public static void main(String[] args) {
System.out.println("Outer main");
}
public static class InnerClass {
public static void main(String[] args) {
System.out.println("Inner main - should not be detected");
}
}
}
"#;
fs::write(
temp_dir.path().join("src/main/java/com/example/OuterClass.java"),
class_content,
).unwrap();
let main_class = detect_main_class(temp_dir.path()).unwrap();
assert!(main_class.is_some());
}