Diff junit-4.13.2 with a junit-5.9.2

/usr/portage/dev-java/junit/junit-5.9.2.ebuild 2023-10-09 14:52:29.432368349 +0300
1 1
# Copyright 1999-2023 Gentoo Authors
2 2
# Distributed under the terms of the GNU General Public License v2
3 3

  
4
# Skeleton command:
5
# java-ebuilder --generate-ebuild --workdir . --pom junit4-r4.13.2/pom.xml --download-uri https://github.com/junit-team/junit4/archive/refs/tags/r4.13.2.tar.gz --slot 4 --keywords "~amd64 ~arm64 ~ppc64 ~x86" --ebuild junit-4.13.2.ebuild
4
EAPI=8
6 5

  
7
EAPI=7
8

  
9
JAVA_PKG_IUSE="doc source test"
10
MAVEN_ID="junit:junit:4.13.2"
6
# Declare the 'doc' USE flag in IUSE -- not JAVA_PKG_IUSE -- to
7
# prevent java-pkg-simple.eclass from handling Javadoc; instead,
8
# let this ebuild handle Javadoc generation and installation itself.
9
# This ebuild invokes java-pkg-simple.eclass's phase functions
10
# multiple times to build multiple modules, but the eclass always
11
# installs each module's Javadoc to the same directory, which would
12
# trigger an error when the second module's Javadoc is installed.
13
JAVA_PKG_IUSE="source"
14
IUSE="doc migration-support suite vintage"
11 15

  
12 16
inherit java-pkg-2 java-pkg-simple
13 17

  
14 18
DESCRIPTION="Simple framework to write repeatable tests"
15
HOMEPAGE="https://junit.org/junit4/"
16
SRC_URI="https://github.com/${PN}-team/${PN}4/archive/refs/tags/r${PV}.tar.gz -> ${P}.tar.gz"
17

  
18
LICENSE="EPL-1.0"
19
SLOT="4"
20
KEYWORDS="amd64 ~arm arm64 ppc64 x86 ~amd64-linux ~x86-linux ~ppc-macos ~x64-macos ~x64-solaris"
21

  
22
# Common dependencies
23
# POM: ${PN}4-r${PV}/pom.xml
24
# org.hamcrest:hamcrest-core:1.3 -> >=dev-java/hamcrest-core-1.3:1.3
25

  
26
CDEPEND="
27
	dev-java/hamcrest-core:1.3
19
HOMEPAGE="https://junit.org/junit5/"
20
SRC_URI="https://github.com/junit-team/junit5/archive/r${PV}.tar.gz -> ${P}.tar.gz"
21
S="${WORKDIR}/junit5-r${PV}"
22

  
23
LICENSE="EPL-2.0"
24
SLOT="5"
25
KEYWORDS="amd64 ~arm arm64 ppc64 x86"
26

  
27
CP_DEPEND="
28
	dev-java/apiguardian-api:0
29
	dev-java/opentest4j:0
30
	dev-java/open-test-reporting-events:0
31
	dev-java/picocli:0
32
	dev-java/univocity-parsers:0
28 33
"
29 34

  
30
# Compile dependencies
31
# POM: ${PN}4-r${PV}/pom.xml
32
# test? org.hamcrest:hamcrest-library:1.3 -> >=dev-java/hamcrest-library-1.3:1.3
35
# java-utils-2.eclass does not support
36
# USE-conditional dependencies in CP_DEPEND
37
COND_DEPEND="
38
	migration-support? ( dev-java/junit:4 )
39
	vintage? ( dev-java/junit:4 )
40
"
33 41

  
42
# Limiting JDK version to >=11 for module-info.java in this package
43
# https://bugs.gentoo.org/796875#c3
34 44
DEPEND="
35
	>=virtual/jdk-1.8:*
36
	test? (
37
		dev-java/hamcrest-library:1.3
38
	)
39
	${CDEPEND}"
45
	>=virtual/jdk-11:*
46
	${CP_DEPEND}
47
	${COND_DEPEND}
48
"
49

  
40 50
RDEPEND="
41 51
	>=virtual/jre-1.8:*
42
	${CDEPEND}"
43
BDEPEND="app-arch/unzip"
44
S="${WORKDIR}"
45

  
46
JAVA_ENCODING="ISO-8859-1"
47

  
48
JAVA_GENTOO_CLASSPATH="hamcrest-core-1.3"
49
JAVA_SRC_DIR="${PN}4-r${PV}/src/main/java"
50
JAVA_RESOURCE_DIRS="${PN}4-r${PV}/src/main/resources"
51

  
52
JAVA_TEST_GENTOO_CLASSPATH="hamcrest-core-1.3,hamcrest-library-1.3"
53
JAVA_TEST_SRC_DIR="${PN}4-r${PV}/src/test/java"
54
JAVA_TEST_RESOURCE_DIRS="${PN}4-r${PV}/src/test/resources"
55

  
56
src_prepare() {
57
	default
58
	java-pkg_clean
52
	${CP_DEPEND}
53
	${COND_DEPEND}
54
"
55

  
56
src_configure() {
57
	# Please make sure to declare a module's dependencies before the module itself.
58
	# Useful upstream documentation regarding modules and dependencies:
59
	# https://junit.org/junit5/docs/current/user-guide/index.html#dependency-metadata
60
	# https://junit.org/junit5/docs/current/user-guide/index.html#dependency-diagram
61
	JUNIT5_MODULES=(
62
		junit-platform-commons
63
		junit-platform-engine
64
		junit-platform-launcher
65
		junit-platform-reporting
66
		junit-platform-console # For launching tests from CLI;
67
		# an eclass would need it to support running tests using JUnit 5
68

  
69
		$(use suite && echo \
70
			junit-platform-suite-api \
71
			junit-platform-suite-commons \
72
			junit-platform-suite-engine \
73
		)
74

  
75
		junit-jupiter-api
76
		junit-jupiter-engine # For JUnit Jupiter tests -- the so-called
77
		# "JUnit 5 tests", which cannot run on earlier JUnit versions
78
		junit-jupiter-params # For parameterized tests; the junit-jupiter
79
		# aggregator module includes it, so building it unconditionally
80
		$(usev migration-support junit-jupiter-migrationsupport)
81

  
82
		$(usev vintage junit-vintage-engine)
83

  
84
		# Modules not included:
85
		# - junit-bom: Has no sources; solely for helping Maven and Gradle
86
		#   projects that use JUnit 5 manage dependencies easier
87
		# - junit-jupiter: Aggregator; does not have 'src/main/java'
88
		# - junit-platform-console-standalone: Has no sources; solely used
89
		#   by the upstream to build a fat JAR that bundles everything, so
90
		#   users can use just this single JAR to run JUnit 5
91
		# - junit-platform-jfr: For an experimental feature
92
		# - junit-platform-runner: Deprecated
93
		# - junit-platform-suite: Aggregator; does not have 'src/main/java'
94
		# - junit-platform-testkit: Requires >=dev-java/assertj-core-3.14.0
95
	)
96
	local cp_packages=()
97
	(use migration-support || use vintage) && cp_packages+=( junit-4 )
98
	local save_IFS="${IFS}"
99
	IFS=',' JAVA_GENTOO_CLASSPATH="${cp_packages[*]}"
100
	IFS="${save_IFS}"
101

  
102
	JUNIT5_VM_VERSION="$(java-config --get-env PROVIDES_VERSION)"
103
}
104

  
105
junit5_foreach_module() {
106
	local module
107
	for module in "${JUNIT5_MODULES[@]}"; do
108
		junit5_module_do "${module}" "${@}"
109
	done
110
}
111

  
112
junit5_module_do() {
113
	local module="${1}"
114
	# Invocation of the passed function will not be guarded by '|| die'.
115
	# Like the case for multibuild_foreach_variant(), it is recommended
116
	# that the passed function itself calls 'die'.
117
	local func=( "${@:2}" )
118

  
119
	einfo "Running '${func[@]}' for ${module} ..."
120
	pushd "${module}" > /dev/null || die "Failed to enter directory '${module}'"
121

  
122
	# Set up Java eclass variables that are
123
	# supposed to be set in the ebuild global scope
124

  
125
	local JAVA_JAR_FILENAME="${module}.jar"
126

  
127
	local JAVA_SRC_DIR=(
128
		src/main/java
129
		src/module
130
	)
131

  
132
	local JAVA_RESOURCE_DIRS=()
133
	local default_resource_dir="src/main/resources"
134
	[[ -d "${default_resource_dir}" ]] &&
135
		JAVA_RESOURCE_DIRS+=( "${default_resource_dir}" )
136

  
137
	if [[ "${module}" == junit-platform-console ]]; then
138
		local JAVA_MAIN_CLASS="org.junit.platform.console.ConsoleLauncher"
139
		local JAVA_LAUNCHER_FILENAME="${module}"
140
	fi
141

  
142
	# Invoke the passed function
143
	"${func[@]}"
144
	local ret="${?}"
145

  
146
	popd > /dev/null || die "Failed to leave directory '${module}'"
147
	return "${ret}"
148
}
149

  
150
junit5_gen_cp() {
151
	echo "$(java-pkg_getjars --build-only --with-dependencies \
152
		"${JAVA_GENTOO_CLASSPATH}"):${JAVA_GENTOO_CLASSPATH_EXTRA}"
153
}
154

  
155
junit5_module_compile() {
156
	if [[ "${module}" == junit-platform-console ]]; then
157
		# Unlike other modules that have a src/main/java9 directory, for this
158
		# module, the upstream puts the class files built from src/main/java9
159
		# in their JAR's top-level directory instead of META-INF/versions/9
160
		cp -rv src/main/java9/* src/main/java/ ||
161
			die "Failed to merge ${module}'s sources for Java 9+"
162
		# Remove for the [[ -d src/main/java9 ]] test
163
		# during versioned directory handling
164
		rm -rv src/main/java9 ||
165
			die "Failed to remove ${module}'s Java 9+ source directory"
166
	fi
167

  
168
	java-pkg-simple_src_compile
169
	local sources="sources.lst"
170
	local classes="target/classes"
171

  
172
	# Collect a list of all compiler input files for building Javadoc
173
	local source
174
	while read source; do
175
		echo "${module}/${source}" >> "${all_sources}"
176
	done < "${sources}" ||
177
		die "Failed to add ${module}'s sources to Javadoc input list"
178

  
179
	# Handle classes that will go into versioned directories.  This will be
180
	# no longer needed after https://bugs.gentoo.org/900433 is implemented.
181
	local vm_ver
182
	for vm_ver in 9 17; do
183
		local versioned_src="src/main/java${vm_ver}"
184
		if [[ -d "${versioned_src}" ]]; then
185
			if ver_test "${JUNIT5_VM_VERSION}" -ge "${vm_ver}"; then
186
				local versioned_classes="target/${vm_ver}/classes"
187
				mkdir -p "${versioned_classes}" ||
188
					die "Failed to create directory for ${module}'s Java ${vm_ver}+ classes"
189
				ejavac -d "${versioned_classes}" -encoding "${JAVA_ENCODING}" \
190
					-classpath "${classes}:$(junit5_gen_cp)" ${JAVAC_ARGS} \
191
					$(find "${versioned_src}" -type f -name '*.java')
192
				"$(java-config --jar)" -uvf "${JAVA_JAR_FILENAME}" \
193
					--release "${vm_ver}" -C "${versioned_classes}" . ||
194
					die "Failed to add ${module}'s Java ${vm_ver}+ classes to JAR"
195
			else
196
				# Modules that may hit this branch as of 5.9.2:
197
				# - junit-platform-console:
198
				#   src/main/java17/.../ConsoleUtils.java tries to use
199
				#   java.io.Console.charset() (available since Java 17) to get
200
				#   the default output charset.  It is fine to not use this
201
				#   file, even if the built artifacts will be used on JRE 17+,
202
				#   as src/main/java/.../ConsoleUtils.java still gets the
203
				#   default from java.nio.charset.Charset.defaultCharset().
204
				elog "JDK ${JUNIT5_VM_VERSION} used; skipping Java ${vm_ver}-dependent parts in ${module}"
205
			fi
206
		fi
207
	done
208

  
209
	# Add the current module's JAR to classpath
210
	# for the module's reverse dependencies in this package
211
	JAVA_GENTOO_CLASSPATH_EXTRA+=":${S}/${module}/${JAVA_JAR_FILENAME}"
212
}
213

  
214
src_compile() {
215
	local all_sources="${S}/all-sources.lst"
216
	junit5_foreach_module junit5_module_compile
217

  
218
	if use doc; then
219
		einfo "Generating Javadoc for all modules ..."
220
		local apidoc="target/api"
221
		mkdir -p "${apidoc}" || die "Failed to create Javadoc directory"
222
		ejavadoc -d "${apidoc}" \
223
			-encoding "${JAVA_ENCODING}" -docencoding UTF-8 -charset UTF-8 \
224
			-classpath "$(junit5_gen_cp)" ${JAVADOC_ARGS:- -quiet} \
225
			-windowtitle "JUnit ${PV} API" \
226
			"@${all_sources}"
227
	fi
59 228
}
60 229

  
61 230
src_test() {
62
	cd "${JAVA_TEST_SRC_DIR}" || die
231
	# Running the JUnit 5 modules' tests (located in each module's
232
	# 'src/test/java') has a few obstacles:
233
	# - Some test sources use text blocks -- a feature introduced in Java 15.
234
	#   A JDK at a lower version, e.g. 11, cannot compile them.
235
	# - Some test classes depend on JUnit 5 modules that this ebuild does not
236
	#   include, like junit-platform-runner and junit-platform-testkit.
237
	#
238
	# Therefore, this ebuild uses a simpler approach to test the artifacts just
239
	# built: it uses the artifacts to run tests in examples under the
240
	# 'documentation/src' directory.  The test coverage will not be impressive,
241
	# but at least this approach verifies that the copy of JUnit 5 just built
242
	# is capable of running some simple tests launched from CLI.
243

  
244
	local JUNIT5_TEST_SRC_DIR="documentation/src/test/java"
245
	local JUNIT5_TEST_RESOURCE_DIR="documentation/src/test/resources"
246
	local JUNIT5_TEST_RM=(
247
		$(usev !migration-support example/IgnoredTestsDemo.java)
248
		$(use !suite && echo \
249
			example/DocumentationTestSuite.java \
250
			example/SuiteDemo.java \
251
		)
252
		$(usev !vintage example/JUnit4Tests.java)
253

  
254
		# Need excluded module junit-platform-runner
255
		example/JUnitPlatformClassDemo.java
256
		example/JUnitPlatformSuiteDemo.java
257

  
258
		# Need excluded module junit-platform-testkit
259
		example/testkit/
260

  
261
		# Not necessary for the tests; some files even require extra dependency
262
		org/junit/api/tools/
263

  
264
		# Needs dev-java/hamcrest; no need to pull in extra dependency
265
		# as the examples already provide ample tests to run
266
		example/HamcrestAssertionsDemo.java
267

  
268
		# Makes an HTTP request and expects a certain response
269
		example/session/HttpTests.java
270
	)
63 271

  
64
	local CP=".:../resources:${S}/${PN}.jar:$(java-pkg_getjars ${JAVA_TEST_GENTOO_CLASSPATH})"
272
	pushd "${JUNIT5_TEST_SRC_DIR}" > /dev/null ||
273
		die "Failed to enter test source directory"
274
	rm -rv "${JUNIT5_TEST_RM[@]}" ||
275
		die "Failed to remove unneeded test sources"
276
	# Test sources expect the working directory to be 'documentation'
277
	sed -i -e "s|src/test/resources|${JUNIT5_TEST_RESOURCE_DIR}|g" \
278
		example/ParameterizedTestDemo.java ||
279
		die "Failed to update file paths in test sources"
280
	popd > /dev/null || die "Failed to leave test source directory"
281

  
282
	local test_dir="${T}/junit5_src_test"
283
	local example_classes="${test_dir}/classes"
284
	local test_classes="${test_dir}/test-classes"
285
	mkdir -p "${example_classes}" "${test_classes}" ||
286
		die "Failed to create test directories"
287

  
288
	local example_sources="${test_dir}/sources.lst"
289
	local test_sources="${test_dir}/test-sources.lst"
290
	find documentation/src/main/java -type f -name '*.java' > "${example_sources}" ||
291
		die "Failed to get a list of example sources"
292
	find documentation/src/test/java -type f -name '*.java' > "${test_sources}" ||
293
		die "Failed to get a list of test sources"
294

  
295
	ejavac -d "${example_classes}" -encoding "${JAVA_ENCODING}" \
296
		-classpath "$(junit5_gen_cp)" ${JAVAC_ARGS} \
297
		"@${example_sources}"
298

  
299
	local test_cp="${example_classes}:${JUNIT5_TEST_RESOURCE_DIR}:$(junit5_gen_cp)"
300
	ejavac -d "${test_classes}" -encoding "${JAVA_ENCODING}" \
301
		-classpath "${test_cp}" ${JAVAC_ARGS} \
302
		"@${test_sources}"
303

  
304
	set -- "$(java-config --java)" -classpath "${test_classes}:${test_cp}" \
305
		org.junit.platform.console.ConsoleLauncher \
306
		--disable-ansi-colors --fail-if-no-tests --scan-classpath \
307
		--include-classname='^(Test.*|.+[.$]Test.*|.*Tests?|.*Demo)$' \
308
		--exclude-tag="exclude"
309
	echo "${@}" >&2
310
	"${@}"
311
	local status="${?}"
312
	[[ "${status}" -eq 2 ]] && die "JUnit did not discover any tests"
313
	[[ "${status}" -eq 0 ]] || die "ConsoleLauncher failed"
314
}
315

  
316
junit5_module_install() {
317
	# It is OK to let java-pkg-simple_src_install call einstalldocs for
318
	# each module as long as each documentation file being installed
319
	# has a unique filename among _all_ modules; otherwise, some files
320
	# would overwrite other ones.
321
	if [[ -f README.md ]]; then
322
		mv -v README.md "README-${module}.md" ||
323
			die "Failed to rename ${module}'s README.md"
324
	fi
325
	java-pkg-simple_src_install
326
}
65 327

  
66
	ejavac -cp "${CP}" -d . $(find * -name "*.java")
67
	java -cp "${CP}" -Djava.awt.headless=true org.junit.runner.JUnitCore junit.tests.AllTests || die "Running junit failed"
328
src_install() {
329
	junit5_foreach_module junit5_module_install
330
	einstalldocs # For project-global documentation
331

  
332
	if use doc; then
333
		einfo "Installing Javadoc for all modules ..."
334
		local apidoc="target/api"
335
		java-pkg_dojavadoc "${apidoc}"
336
	fi
68 337
}
Thank you!