summaryrefslogtreecommitdiff
path: root/jps/jps-builders/testSrc/org/jetbrains/jps/builders/ModuleClasspathTest.kt
blob: 9d2960dc4d9ce2d505640353498d20e46fc296da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed 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.jetbrains.jps.builders

import com.intellij.openapi.application.ex.PathManagerEx
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.io.FileUtil.*
import org.jetbrains.jps.ModuleChunk
import org.jetbrains.jps.ProjectPaths
import org.jetbrains.jps.builders.java.JavaModuleBuildTargetType
import org.jetbrains.jps.incremental.ModuleBuildTarget
import java.io.File
import com.intellij.openapi.util.text.StringUtil
import org.junit.Assert
import java.util.Collections

/**
 * @author nik
 */
public class ModuleClasspathTest(): JpsBuildTestCase() {
    override fun setUp() {
        super.setUp()
        addJdk("1.6", "/jdk.jar")
        addJdk("1.5", "/jdk15.jar")
        loadProject("moduleClasspath/moduleClasspath.ipr")
    }

    private fun getProjectPath(): String {
        return FileUtil.toSystemIndependentName(getTestDataRootPath()) + "/moduleClasspath/moduleClasspath.ipr"
    }

    override fun getTestDataRootPath(): String {
        return FileUtil.toCanonicalPath(PathManagerEx.findFileUnderCommunityHome("jps/jps-builders/testData/output")!!.getAbsolutePath(), '/')!!
    }

    public fun testSimpleClasspath() {
        assertClasspath("util", false, listOf("util/lib/exported.jar", "/jdk15.jar"))
    }

    public fun testScopes() {
        assertClasspath("test-util", false, listOf("/jdk.jar", "test-util/lib/provided.jar"))
        assertClasspath("test-util", true, listOf("/jdk.jar", "test-util/lib/provided.jar", "test-util/lib/test.jar", "out/production/test-util"))
    }

    public fun testDepModules() {
        assertClasspath("main", false, listOf("util/lib/exported.jar", "out/production/util", "/jdk.jar", "main/lib/service.jar"))
        assertClasspath("main", true, listOf("out/production/main", "util/lib/exported.jar", "out/test/util", "out/production/util", "/jdk.jar", "out/test/test-util", "out/production/test-util", "main/lib/service.jar"))
    }

    public fun testCompilationClasspath() {
        val chunk = createChunk("main")
        assertClasspath(listOf("util/lib/exported.jar", "out/production/util", "/jdk.jar"), getPathsList(ProjectPaths.getPlatformCompilationClasspath(chunk, true)))
        assertClasspath(listOf("main/lib/service.jar"), getPathsList(ProjectPaths.getCompilationClasspath(chunk, true)))
    }

    private fun assertClasspath(moduleName: String, includeTests: Boolean, expected: List<String>) {
        val classpath = getPathsList(ProjectPaths.getCompilationClasspathFiles(createChunk(moduleName), includeTests, true, true))
        assertClasspath(expected, toSystemIndependentPaths(classpath))
    }

    private fun createChunk(moduleName: String): ModuleChunk {
        val module = myProject.getModules().firstOrNull { it.getName() == moduleName }
        return ModuleChunk(setOf(ModuleBuildTarget(module!!, JavaModuleBuildTargetType.PRODUCTION)))
    }

    private fun assertClasspath(expected: List<String>, classpath: List<String>) {
        val basePath = FileUtil.toSystemIndependentName(File(getProjectPath()).getParentFile()!!.getAbsolutePath()) + "/"
        val actual = toSystemIndependentPaths(classpath).map { StringUtil.trimStart(it, basePath) }
        Assert.assertEquals(expected.makeString("\n"), actual.makeString("\n"))
    }

    private fun toSystemIndependentPaths(classpath: List<String>): List<String> {
        return classpath.map(FileUtil::toSystemIndependentName)
    }

    public fun getPathsList(files: Collection<File>): List<String> {
        return files.map(::getCanonicalPath)
    }
}

private fun getCanonicalPath(file: File): String {
    val path = file.getPath()
    return if (path.contains(".")) FileUtil.toCanonicalPath(path)!! else FileUtil.toSystemIndependentName(path)
}