You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Stephen Colebourne <sc...@joda.org> on 2024/09/23 15:47:12 UTC

JPMS whitebox testing issues

Hi all,
Apologies for the long email, but whitebox testing is a complex topic.
I know not everyone agrees with whitebox testing, but it is a major
factor in how many projects are written, and IMO Maven should fully
and easily support it.

A "whitebox mode" test has two module-info.java files, both with the
same module name. The goal is to support unit testing with full access
to the internals of the module as in the "good ole days" while
ensuring that the test occurs on the module-path (test failures can
occur on the module-path which do not occur on the class-path, and
vice versa):

root
- pom.xml
- src/main/java
-- module-info.java
-- mypackage
--- Main,java
- src/main/resources
-- mypackage
--- main.txt
- src/test/java
-- module-info.java
-- mypackage
--- Test,java

The main module-info would be something like:
  module mymodule {
    exports mypackage;
  }

The test module-info would be something like:
  open module mymodule {
    // duplicate contents of main module-info
    exports mypackage;
    // any additional test dependencies/services
    requires transitive org.junit.jupiter;
  }

The good news is that a setup like this mostly works today in Maven
(tested with v3.9.9 and Java 21).
However there are three key issues:

The way Maven currently operates is:

1) In `testCompile`, the test source code is compiled using:
  --module-path target/test-classes:target/classes
  --patch-module mymodule=src/main/java

2) In `surefire`, the tests are run with:
--module-path target/test-classes:target/classes:<< dependencies >>
--class-path << surefire jar files >>

What actually happens is (I think) very surprising. TestCompile
compiles BOTH the main and test source code into
`target/test-classes`:
target/classes contains
- module-info.class (from src/main/java)
- mypackage/Main.class
- mypackage/main.txt
target/test-classes contains
- module-info.class (from src/test/java)
- mypackage/Main.class
- mypackage/Test.class

This results in three issues:

A) The code in `src/main/java` is compiled twice, which is
unnecessary, surprising and slows things down

B) There is no need to refer to `target/classes` on the module-path,
as it is not used in TestCompile or Surefire.

C) Any resource files (src/main/resources) are UNAVAILABLE to
Surefire, which results in the tests failing, eg.
`Main.class.getResourceAsStream("main.txt")` will return null. This is
because Surefire sees the code in target/test-classes as the complete
definition of the module, and as shown above `main.txt` is not present
in `target/test-classes`.

-----
Option 1:
Do nothing. End users can fix the resource file issue by adding this
to the pom.xml:
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <configuration>
    <argLine>--patch-module mymodule=src/main/resources;</argLine>
   </configuration>
  </plugin>

This causes Surefire to pickup the resource folder and patch it into the module.

Option 2:
Automate the patch-module described above. When Surefire runs in
"whitebox mode", the --patch-module outlined above should be
automatically generated and added to the command line. Care will be
needed not to clash with any patch-module the user adds manually.

This fixes issue C.

Option 3:
Properly address the issue. maven-compiler-plugin already recognises
that there are two module-info.java files and checks the names. If
they are different it sets up "blackbox mode". IMO,
maven-compiler-plugin needs an official "whitebox mode".
The correct approach, as far as I can determine, is for TestCompile to
do the following:
  --module-path target/test-classes
  --patch-module mymodule=target/classes
This avoids the main source code being compiled twice - it is patched
in instead.

The same change needs to be applied to Surefire
  --module-path target/test-classes:<< dependencies >>
  --patch-module mymodule=target/classes

This fixes issues A , B and C. (The resource files in target/classes
are patched in alongside the .class files).
Note that the java command line generates a warning about duplicate
module-info.class files, but that is expected in this case,and can be
ignored.

---
So, what do people think? Is there appetite to raise an issue to get
option 2 or 3 done?

thanks for reading this far
Stephen Colebourne

PS. IDEs don't like the two module-info.java files of whitebox
testing, but that's OK - Maven isn't beholden to IDEs. It also turns
out there is an easy workaround for IDEs (which I can describe in
another email, as this one is too long)
PPS. I know about the suggested module-info.test file - lets not get
into that discussion here

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Martin Desruisseaux <ma...@geomatys.com>.
Hello Stephen

Just a very quick reply, as I have to go. But I will try to provide more 
details tomorrow.

I agree with the problem that you describe, and a massive amount of work 
is ongoing for improving Maven support of JPMS, including whitebox 
testing. The Maven compiler plugin has already been fully rewritten in a 
separated repository [1] but not yet merged on the Maven repository. One 
of the change is to get ride of Plexus compiler and uses directly the 
javax.tools compiler API, in part because this API provides specialized 
methods for JPMS.

While having two module-info is still supported (I think, it would need 
extensive testing), I'm more inclined in having only one module-info in 
the main source code and letting the compiler and JUnit generates 
automatically the --patch-modules, --add-exports, --add-reads, etc. 
options. We will try to do that in documented and predictable way, as 
one goal of this rewrite is to avoid as much "black magic" as we can.

This work will require Maven 4. Some changes are in progress in support 
for that. One of the change is that Maven 4 gives full control to the 
developer about whether to put a dependency on the class-path or 
module-path. This part is already in Maven core, but not yet honored by 
plugins, except the compiler prototype. Another work in progress right 
now is proposing to change the way that we declare the sources in Maven, 
for supporting both module source hierarchy and (as a side effect) more 
easily multi-release projects.

I will try to give more details tomorrow. If you were available for 
testing and feedback, that would be greatly appreciated. However at this 
time, only the compiler plugin has been rewritten. We will also need to 
update Surefire, but this work hasn't started yet.

     Martin

[1]https://github.com/Geomatys/maven-compiler-plugin/commits/beta2

Re: JPMS whitebox testing issues

Posted by Stephen Colebourne <sc...@joda.org>.
On Wed, 2 Oct 2024 at 11:29, Martin Desruisseaux
<ma...@geomatys.com> wrote:
> Hello Stephen
> Thanks for testing.
>
> Le 2024-10-02 à 00 h 30, Stephen Colebourne a écrit :
>
> > At present, the compile phase is asked to compile module-info.java,
> > but the target/classes directory does not contain module-info.class.
> > I've no idea where it is going I'm afraid.
> >
> Is module-info missing for both test and main, or only for test?

Feel free to try the branch yourself. After mvn clean compile, there
was no file target/classes/module-info.class despite it being listed
as an input to javac.

Stephen

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Martin Desruisseaux <ma...@geomatys.com>.
Le 2024-10-12 à 00 h 06, Gary Gregory a écrit :

> That's quite the false equivalence: Lambas and records don't force you 
> to hack JVM start scripts with --add-exports, --add-opens, 
> --add-modules, --add-reads, and --patch-module...
>
We are not supposed to add any such option to a start script. They 
should be used during tests only, or as temporary workaround for 
patching a bug. Maybe your configuration is a mix of modular and 
non-modular JARs, with the non-modular ones missing in the module-info? 
(because some consider that relying on automatic module naming is a bad 
practice).

     Martin



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Gary Gregory <ga...@gmail.com>.
On Fri, Oct 11, 2024 at 5:45 PM Martin Desruisseaux <
martin.desruisseaux@geomatys.com> wrote:

> Le 2024-10-11 à 20 h 21, Gary Gregory a écrit :
> >
> > I do not need JPMS and I've never had a request for features around it
> > at work. Our customers care about our products' features and couldn't
> > care less about JPMS.
> >
> We could also said that "customers couldn't care less about lambdas", or
> "about records", etc. JPMS is for developers. The benefit for users is
> only indirect.
>

That's quite the false equivalence: Lambas and records don't force you to
hack JVM start scripts with --add-exports, --add-opens, --add-modules,
--add-reads, and --patch-module...

Gary


>
> > It would be nice if the JVM had a --stop-complaining-about-jpms
> >
> Migrating from a non-JPMS to a JPMS environment is a little bit
> equivalent to migrating from a Unix environment on which we were root,
> to a Unix environment on which we are an ordinary user with more limited
> authorizations. Asking for a --stop-complaining-about-jpms option is
> equivalent to asking "I want to be root again". The world is becoming
> more concerned about security, it will require some adjustment on our side.
>
>      Martin
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: JPMS whitebox testing issues

Posted by Martin Desruisseaux <ma...@geomatys.com>.
Le 2024-10-11 à 20 h 21, Gary Gregory a écrit :
>
> I do not need JPMS and I've never had a request for features around it 
> at work. Our customers care about our products' features and couldn't 
> care less about JPMS.
>
We could also said that "customers couldn't care less about lambdas", or 
"about records", etc. JPMS is for developers. The benefit for users is 
only indirect.


> It would be nice if the JVM had a --stop-complaining-about-jpms
>
Migrating from a non-JPMS to a JPMS environment is a little bit 
equivalent to migrating from a Unix environment on which we were root, 
to a Unix environment on which we are an ordinary user with more limited 
authorizations. Asking for a --stop-complaining-about-jpms option is 
equivalent to asking "I want to be root again". The world is becoming 
more concerned about security, it will require some adjustment on our side.

     Martin



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Gary Gregory <ga...@gmail.com>.
On Fri, Oct 11, 2024 at 2:17 PM Martin Desruisseaux <
martin.desruisseaux@geomatys.com> wrote:

> Le 2024-10-11 à 20 h 02, Gary Gregory a écrit :
> >
> > JPMS feels like something to workaround, not with :(
> >
> This is the perception that I hope to change. I happily work with JPMS
> everyday, but had to move from Maven to Gradle and tweak Gradle a lot of
> that purpose. Let revisit that evaluation after Maven compiler,
> surefire, jar and exec have been updated.
>

I do not need JPMS and I've never had a request for features around it at
work. Our customers care about our products' features and couldn't care
less about JPMS. It would be nice if the JVM had a
--stop-complaining-about-jpms so I would not even need something like
Moditect.

Gary


>
>      Martin
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: JPMS whitebox testing issues

Posted by Martin Desruisseaux <ma...@geomatys.com>.
Le 2024-10-11 à 20 h 02, Gary Gregory a écrit :
>
> JPMS feels like something to workaround, not with :(
>
This is the perception that I hope to change. I happily work with JPMS 
everyday, but had to move from Maven to Gradle and tweak Gradle a lot of 
that purpose. Let revisit that evaluation after Maven compiler, 
surefire, jar and exec have been updated.

     Martin



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Gary Gregory <ga...@gmail.com>.
FWIW,

In Apache Commons, I've circumvented dealing with JPMS directly in sources
by using the Moditect plugin like this:
https://github.com/apache/commons-parent/blob/72331421cd9388c0cacf6486c2f7a4748cb2b7ce/pom.xml#L1778-L1830

JPMS feels like something to workaround, not with :(

Gary


On Fri, Oct 11, 2024 at 4:22 AM Stephen Colebourne <sc...@joda.org>
wrote:

> On Wed, 2 Oct 2024 at 11:29, Martin Desruisseaux
> <ma...@geomatys.com> wrote:
> > The problem with module-info in both tests and main is that it works
> > awkward. The previous compiler plugin had to took the project structure
> > upside-down: the test code was considered the main code, and the main
> > code was considered the patch. I just checked by compiling and running a
> > dummy project with `java` and `javac` directly (without Maven) and I
> > confirm: even if a `module-info` is present in the test output, it is
> > ignored by the `java` command (the `module-info` of the main code has
> > precedence) unless we pretend that the test are the main code and the
> > main code are patching the tests. The behavior of `javac` is a little
> > bit more complicated, but basically we also have to put things upside
> > down to make it works.
> >
> > If nevertheless we still want to support having `module-info` in both
> > main and test, I found a trick this morning for getting `javac` to
> > compile the test `module-info` without putting the project structure
> > upside-down. But it is undocumented and I don't know how reliable it
> > would be. I can implement this trick (or try other tricks) if this is
> > important, but I think that we should stop recommending the approach of
> > overwriting `module-info`, or recommend to use it in last resort only.
> > The alternative (compiler options) is indeed tedious, but this is
> > precisely a major goal of this JPMS work to make that task easier than
> > it currently is.
>
> This issue remains hugely painful not just in Maven, but across Java.
> It is a key part of why JPMS adoption is so painful.
>
> The key principle here is that tests for a Maven module should be
> located *within* the same Maven module of the main source code.
> Forcing developers to write a separate Maven module just for tests is
> not an acceptable solution.
>
> 1) Classpath testing with one (main) module-info.java file.
> IIUC, this is what you are planning for maven-compiler-plugin and
> maven-surefire-plugin to support.
> It tests with the main code on the modulepath and the test code on the
> classpath.
> While this is fine for many use cases, sometimes you really need to
> run the tests on the modulepath, so this is not sufficient by itself.
>
> 2) Blackbox testing with two module-info.java files.
> This is supported by IntelliJ and Gradle IIUC. Maven must support this IMO.
>
> For maven-compiler-plugin, Blackbox tests are fine AFAICT. All the
> files in src/test/java are in a different package, and the
> module-info.java file refers to a different module name, so there are
> no clashes. The maven-surefire-plugin would just put the main module
> and the test module on the modulepath, and everything ought to be
> fine.
>
> I believe Gradle recommends src/integrationTest for blackbox tests,
> and I think that convention makes a lot of sense. It offers up the
> potential for a single Maven module to contain both whitebox and
> blackbox tests, which is incredibly powerful.
>
> 3) Whitebox testing with two module-info.java files.
> This has yet to achieve an agreed approach in the community, despite
> JPMS being out for many years.
>
> To work effectively, it might be beneficial for the module-info file
> to be named module-info-whitebox.java, or alternatively be in
> src/whitebox.
>
> Option A) "Parse and Patch"
> maven-compiler-plugin would find the whitebox module-info source file,
> parse it directly and convert it to command line options based on
> --patch-module.
> maven-compiler-plugin would then compile the test classes excluding
> the whitebox module-info source file using the derived command line
> options.
> maven-surefire-plugin would use the same derived command line options.
>
> Option B) "Merge and Recompile"
> maven-compiler-plugin would compile the test classes by including both
> src/main/java and src/test/java as inputs, excluding the
> src/main/java/module-info.java file
> maven-surefire-plugin would run only with target/test-classes, not
> target/classes
>
> Option C) "Copy before Compile"
> maven-compiler-plugin would first copy target/classes to
> target/test-classes excluding target/classes/module-info.class
> maven-compiler-plugin then compiles src/test/java to
> target/test-classes, without using target/classes as an input
> maven-surefire-plugin would run only with target/test-classes, not
> target/classes
>
> I believe Option A is more desirable, and more in the spirit of JPMS,
> and more in the spirit of Maven.
>
> I understand that you want to make defining patch-module directives
> easier in pom.xml. While that's a lovely idea, I think it is
> fundamentally flawed in usability. There is no connection in the mind
> of a developer between module-info directives and the matching command
> line arguments. By contrast developers know exactly how to setup the
> module-info.java file.
>
> The only real difference I'm proposing is that the patch-module
> directives are not defined in pom.xml, but are instead defined in a
> java-like source file, eg module-info-whitebox.java. Think of it as a
> Maven config file which happens to look like a java source file and it
> really isn't that scary.
>
> ( I think it makes sense for this to be part of maven-compiler-plugin,
> but it could also be a separate plugin that runs before
> maven-compiler-plugin, or a separate goal. For example, it could be
> thought of as a converter/compiler from module-info-whitebox.java to
> module-info-whitebox.javac.args)
>
> Stephen
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

Re: JPMS whitebox testing issues

Posted by Tamás Cservenák <ta...@cservenak.net>.
Just to chime in re new Maven4 scopes:
https://github.com/apache/maven/blob/master/api/maven-api-core/src/main/java/org/apache/maven/api/DependencyScope.java#L92

T

On Mon, Oct 14, 2024 at 12:36 PM Martin Desruisseaux
<ma...@geomatys.com> wrote:
>
> Le 2024-10-14 à 10 h 45, Stephen Colebourne a écrit :
>
> > So you've defined a GAV for Java modules like java.desktop?
> >
> Good point. I didn't, but in the proposed approach this is something
> that would need to be done (with version number omitted I guess).
> Alternatively, it can also be considered another case where module-info
> duplication is needed, or just rely on the classical way of adding
> command-line options.
>
>
> > Happy to test - I remain unconvinced that an approach based on command
> > line flags derived from existing Maven info is sufficient.
> >
> Maybe not all case. But I think that it can cover a lot of cases, and
> makes other cases easier by reducing the number of remaining options
> that developers need to provide. I have some urgent work to finish in
> the next couple of weeks, but hope to be back on the compiler plugin
> after that.
>
>      Martin
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Martin Desruisseaux <ma...@geomatys.com>.
Le 2024-10-14 à 10 h 45, Stephen Colebourne a écrit :

> So you've defined a GAV for Java modules like java.desktop?
>
Good point. I didn't, but in the proposed approach this is something 
that would need to be done (with version number omitted I guess). 
Alternatively, it can also be considered another case where module-info 
duplication is needed, or just rely on the classical way of adding 
command-line options.


> Happy to test - I remain unconvinced that an approach based on command 
> line flags derived from existing Maven info is sufficient.
>
Maybe not all case. But I think that it can cover a lot of cases, and 
makes other cases easier by reducing the number of remaining options 
that developers need to provide. I have some urgent work to finish in 
the next couple of weeks, but hope to be back on the compiler plugin 
after that.

     Martin



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Stephen Colebourne <sc...@joda.org>.
On Mon, 14 Oct 2024 at 09:14, Martin Desruisseaux
<ma...@geomatys.com> wrote:
> > Problem 2: Extra dependencies
> > One of my projects depends on java.desktop in testing, but not in
> > prod. Since this cannot be expressed in <dependencies>, it cannot be
> > automatically derived as described above
>
> As mentioned in my previous email, it is expressed by a dependency with
> <scope>test</scope>. The new compiler plugin already supports that. This
> is handled in the same way as any other test dependencies such as JUnit.

So you've defined a GAV for Java modules like java.desktop?

> The most productive way to resolve those questions may be to have a
> working prototype and checks which issues are still valid on that
> prototype. However some issues such as problem 6 are not compiler
> issues, but Surefire issues. Do we need to have the full stack tested
> before validating the approach?

Happy to test - I remain unconvinced that an approach based on command
line flags derived from existing Maven info is sufficient.

Stephen

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Nils Breunese <ni...@breun.nl>.
Guillaume Nodet <gn...@apache.org> wrote:

> Fwiw, the Maven 4 API defines a few new scopes, in particular the scopes
> named compile-only and test-only, see
> 
> https://github.com/apache/maven/blob/30dc81227bf9c36d36f8549d22ab4c40bce60e77/api/maven-api-core/src/main/java/org/apache/maven/api/DependencyScope.java#L82

Nice to see some additional dependency scopes in the Maven 4 API! I must say though that I find the meaning of `test-only` (‘test compile only, not test runtime’) not very intuitive. `test-compile-only` would make more sense to me for the test-counterpart of `compile-only`. `test` already is what I’d expect from a scope called `test-only`.

Nils.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Guillaume Nodet <gn...@apache.org>.
Le lun. 14 oct. 2024 à 10:18, Martin Desruisseaux <
martin.desruisseaux@geomatys.com> a écrit :

> Hello Stephen
>
> Le 2024-10-13 à 22 h 57, Stephen Colebourne a écrit :
>
> > Problem 1:Services
> > Already a known problem, as there is no command line flag for
> > services. This cannot be automatically derived as described above
>
> Already answered in my previous email: this is one of the few cases
> where duplicated module-info.java are necessary, and it can be supported
> with hacks.
>
>
> > Problem 2: Extra dependencies
> > One of my projects depends on java.desktop in testing, but not in
> > prod. Since this cannot be expressed in <dependencies>, it cannot be
> > automatically derived as described above
>
> As mentioned in my previous email, it is expressed by a dependency with
> <scope>test</scope>. The new compiler plugin already supports that. This
> is handled in the same way as any other test dependencies such as JUnit.
> If we mean a dependency needed only at test runtime (not test compile
> time), we may need to introduce a new type of dependency.
>

Fwiw, the Maven 4 API defines a few new scopes, in particular the scopes
named compile-only and test-only, see

https://github.com/apache/maven/blob/30dc81227bf9c36d36f8549d22ab4c40bce60e77/api/maven-api-core/src/main/java/org/apache/maven/api/DependencyScope.java#L82


>
>
> > Problem 3: Extra packages
> > One of my projects has a package in the tests that is not present in
> > the main code. Classes in that package need to be exported (to another
> > module) in order for the tests to pass. This cannot be automatically
> > derived as described above
>
> The old way to specify additional compiler options is still available.
> So it is possible to add explicit --add-exports flags there. If
> developers need to add only special cases such as this one instead of
> all --add-exports flags, it is still a better situation than the current
> one.
>
>
> > Problem 4: Export to all
> > Problem 3 involved exporting an additional package to a specific
> > module, which is possible via command line flags. However, there are
> > no doubt cases where the additional package needs to be exported to
> > all module, something which cannot be done via a command line flag.
>
> For testing purpose only? If you mean using a different module-info for
> tests than the module-info for the main code when testing from a
> different Maven module, it seems to not be possible at all, unless we
> put the project upside-down (test become main code and main code become
> patch), or unless we temporarily replace de module-info.class file. This
> is doable if there is really a need for that.
>
>
> > Problem 5: Optional dependencies
> > Various of my projects have optional module dependencies (requires
> > static). It is desirable to test both with and without the optional
> > dependency (multiple runs of surefire). If the optional dependency is
> > always added, that could be problematic.
>
> Yes, we will need a mechanism for controlling that. But the problem is
> the same with the current plugins.
>
>
> > Problem 6: Opening for reflection
> > As mentioned in your previous email, some packages may need to be
> > opened for testing.
>
> Yes, as said before this is one of the points where the mechanism is yet
> to be determined.
>
> In summary, in this list of problems, I think that 2 or them should
> already works. 2 other require hacks, even with the current plugins, so
> the proposed work would hopefully be at least an improvements. The last
> 2 are known open questions, but not blocking as their solution would be
> some new configuration options somewhere.
>
> The most productive way to resolve those questions may be to have a
> working prototype and checks which issues are still valid on that
> prototype. However some issues such as problem 6 are not compiler
> issues, but Surefire issues. Do we need to have the full stack tested
> before validating the approach?
>

Not sure we need, but it would be nice, once Maven 4.0.0-beta-5 is out,
to progress on the stack migration...


>
>      Martin
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

-- 
------------------------
Guillaume Nodet

Re: JPMS whitebox testing issues

Posted by Martin Desruisseaux <ma...@geomatys.com>.
Hello Stephen

Le 2024-10-13 à 22 h 57, Stephen Colebourne a écrit :

> Problem 1:Services
> Already a known problem, as there is no command line flag for
> services. This cannot be automatically derived as described above

Already answered in my previous email: this is one of the few cases 
where duplicated module-info.java are necessary, and it can be supported 
with hacks.


> Problem 2: Extra dependencies
> One of my projects depends on java.desktop in testing, but not in
> prod. Since this cannot be expressed in <dependencies>, it cannot be
> automatically derived as described above

As mentioned in my previous email, it is expressed by a dependency with 
<scope>test</scope>. The new compiler plugin already supports that. This 
is handled in the same way as any other test dependencies such as JUnit. 
If we mean a dependency needed only at test runtime (not test compile 
time), we may need to introduce a new type of dependency.


> Problem 3: Extra packages
> One of my projects has a package in the tests that is not present in
> the main code. Classes in that package need to be exported (to another
> module) in order for the tests to pass. This cannot be automatically
> derived as described above

The old way to specify additional compiler options is still available. 
So it is possible to add explicit --add-exports flags there. If 
developers need to add only special cases such as this one instead of 
all --add-exports flags, it is still a better situation than the current 
one.


> Problem 4: Export to all
> Problem 3 involved exporting an additional package to a specific
> module, which is possible via command line flags. However, there are
> no doubt cases where the additional package needs to be exported to
> all module, something which cannot be done via a command line flag.

For testing purpose only? If you mean using a different module-info for 
tests than the module-info for the main code when testing from a 
different Maven module, it seems to not be possible at all, unless we 
put the project upside-down (test become main code and main code become 
patch), or unless we temporarily replace de module-info.class file. This 
is doable if there is really a need for that.


> Problem 5: Optional dependencies
> Various of my projects have optional module dependencies (requires
> static). It is desirable to test both with and without the optional
> dependency (multiple runs of surefire). If the optional dependency is
> always added, that could be problematic.

Yes, we will need a mechanism for controlling that. But the problem is 
the same with the current plugins.


> Problem 6: Opening for reflection
> As mentioned in your previous email, some packages may need to be
> opened for testing.

Yes, as said before this is one of the points where the mechanism is yet 
to be determined.

In summary, in this list of problems, I think that 2 or them should 
already works. 2 other require hacks, even with the current plugins, so 
the proposed work would hopefully be at least an improvements. The last 
2 are known open questions, but not blocking as their solution would be 
some new configuration options somewhere.

The most productive way to resolve those questions may be to have a 
working prototype and checks which issues are still valid on that 
prototype. However some issues such as problem 6 are not compiler 
issues, but Surefire issues. Do we need to have the full stack tested 
before validating the approach?

     Martin



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Stephen Colebourne <sc...@joda.org>.
On Fri, 11 Oct 2024 at 10:43, Martin Desruisseaux
<ma...@geomatys.com> wrote:
> > 1) Classpath testing with one (main) module-info.java file. IIUC, this
> > is what you are planning for maven-compiler-plugin and
> > maven-surefire-plugin to support. It tests with the main code on the
> > modulepath and the test code on the classpath.
> >
> No, this is not what I plan to do. My plan is to test everything on the
> module path (for modular projects), with a single module-info specified
> in the main code. The test-only dependencies are handled by
> --add-modules and --add-reads options added automatically by Maven based
> on what is declared in POM <dependencies> section (so developers have
> control). The test classes in the same packages as the main classes are
> handled by --path-modules options added automatically by Maven. JUnit
> execution needs --add-opens options to be added by automatically by
> Maven too (the way to keep that part under developer's control has not
> been determined yet). So there is usually no need to overwrite the
> module-info in the test code.
>
> There are a few cases where overwriting module-info in test code may be
> necessary. For example, I'm not aware of any Java option for adding or
> removing services (the "provides" clause in module-info). So this is a
> case where overwriting module-info may be necessary, but it should be a
> minority of cases, to use in last resort only. And as said above, for
> those cases, hacks are still possible.

I understand what you are trying to do, but I think it will be
insufficient in a lot more cases than you think it will. Plus, it
suffers from a cliff-edge problem. As soon as it doesn't work
developers are faced with a hard problem to solve.

Of the projects I've converted so far, I think only 1 out of 4 would
work with the mechanism you propose.

Problem 1:Services
Already a known problem, as there is no command line flag for
services. This cannot be automatically derived as described above

Problem 2: Extra dependencies
One of my projects depends on java.desktop in testing, but not in
prod. Since this cannot be expressed in <dependencies>, it cannot be
automatically derived as described above

Problem 3: Extra packages
One of my projects has a package in the tests that is not present in
the main code. Classes in that package need to be exported (to another
module) in order for the tests to pass. This cannot be automatically
derived as described above

Problem 4: Export to all
Problem 3 involved exporting an additional package to a specific
module, which is possible via command line flags. However, there are
no doubt cases where the additional package needs to be exported to
all module, something which cannot be done via a command line flag.

Problem 5: Optional dependencies
Various of my projects have optional module dependencies (requires
static). It is desirable to test both with and without the optional
dependency (multiple runs of surefire). If the optional dependency is
always added, that could be problematic.

Problem 6: Opening for reflection
As mentioned in your previous email, some packages may need to be
opened for testing.

> > 3) Whitebox testing with two module-info.java files. This has yet to
> > achieve an agreed approach in the community, despite JPMS being out
> > for many years.
> If the two module-info files declares the same module name with the
> intend that the test one overwrite the main one, we can do with a hack
> different than the current one (without putting the project
> upside-down). But it should be a deprecated practice, to use in last
> resort only. For the majority of cases, we don't need two module-info
> for whitebox testing with everything on the module path (see point 1 above).

Hopefully my point is coming across. It can't be a deprecated
practice, because it will be significantly more common than you think.
In fact, I suspect that most projects have difficult use cases like
I've described.

As such, I think the opposite approach should be taken - that
specifying a whitebox module-info.java file should be a normal, fully
supported thing. (Great if Maven can do it automatically for the
simple cases, but there needs to be an obvious alternative for the
non-simple cases).

FWIW, I don't think the approach of putting the tests ahead of the
main code ("upside down") is a bad one at all. Given my experience, I
simply don't think command line flags are sufficient to meet the
requirements users of Maven will have.

Stephen

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Martin Desruisseaux <ma...@geomatys.com>.
Le 2024-10-11 à 10 h 21, Stephen Colebourne a écrit :
>>
>> (…snip…, was about module-info duplicated in main and tests)
>>
> This issue remains hugely painful not just in Maven, but across Java. 
> It is a key part of why JPMS adoption is so painful.
>
I don't think that adoption is painful because of JPMS, since I think 
that there is good security reasons for the way that it works. I believe 
adoption is painful because Maven and Gradle are not helping. I hope 
that the new compiler plugin will demonstrate that (I didn't had the 
time to work on it during the last month, sorry for that).


> The key principle here is that tests for a Maven module should be 
> located *within* the same Maven module of the main source code. 
> Forcing developers to write a separate Maven module just for tests is 
> not an acceptable solution.
>
Yes, we agree on that. But it doesn't change with JPMS, there is no need 
to put the tests in a separated module. Overwriting a module-info in the 
tests should be a deprecated practice, as there is better alternatives 
(again, with Maven's help for making that easy). If nevertheless a 
developer really wants to overwrite the module-info, it is possible with 
better hacks than what the current plugin does (e.g. we could 
temporarily move the main module-info).


> 1) Classpath testing with one (main) module-info.java file. IIUC, this 
> is what you are planning for maven-compiler-plugin and 
> maven-surefire-plugin to support. It tests with the main code on the 
> modulepath and the test code on the classpath.
>
No, this is not what I plan to do. My plan is to test everything on the 
module path (for modular projects), with a single module-info specified 
in the main code. The test-only dependencies are handled by 
--add-modules and --add-reads options added automatically by Maven based 
on what is declared in POM <dependencies> section (so developers have 
control). The test classes in the same packages as the main classes are 
handled by --path-modules options added automatically by Maven. JUnit 
execution needs --add-opens options to be added by automatically by 
Maven too (the way to keep that part under developer's control has not 
been determined yet). So there is usually no need to overwrite the 
module-info in the test code.

There are a few cases where overwriting module-info in test code may be 
necessary. For example, I'm not aware of any Java option for adding or 
removing services (the "provides" clause in module-info). So this is a 
case where overwriting module-info may be necessary, but it should be a 
minority of cases, to use in last resort only. And as said above, for 
those cases, hacks are still possible.


> 2) Blackbox testing with two module-info.java files. This is supported 
> by IntelliJ and Gradle IIUC. Maven must support this IMO.
>
If the two module-info files declare a different module name and there 
is no package name collision, indeed this would be a good think to add. 
I will put that on my "TODO" list for a future version.


> I believe Gradle recommends src/integrationTest for blackbox tests, 
> and I think that convention makes a lot of sense. It offers up the 
> potential for a single Maven module to contain both whitebox and 
> blackbox tests, which is incredibly powerful.
>
Indeed, I agree we should support something like that out-of-the-box.


> 3) Whitebox testing with two module-info.java files. This has yet to 
> achieve an agreed approach in the community, despite JPMS being out 
> for many years.
>
If the two module-info files declares the same module name with the 
intend that the test one overwrite the main one, we can do with a hack 
different than the current one (without putting the project 
upside-down). But it should be a deprecated practice, to use in last 
resort only. For the majority of cases, we don't need two module-info 
for whitebox testing with everything on the module path (see point 1 above).


> I understand that you want to make defining patch-module directives 
> easier in pom.xml. While that's a lovely idea, I think it is 
> fundamentally flawed in usability. There is no connection in the mind 
> of a developer between module-info directives and the matching command 
> line arguments. By contrast developers know exactly how to setup the 
> module-info.java file.
>
I'm not proposing to add a new syntax in pom.xml for patch-module 
directives. Every time that a test directory has the same packages as 
the main code, a patch-module directive must be added. The rule is clear 
and simple because otherwise the JVM refuses to launch. So patch 
directives are added automatically with no need for the developer to 
think about that. For the --add-modules and --add-reads directives, this 
is also automatic with clear rules based on the scope of dependencies. 
The only directives at this time for which I do not yet have a rule that 
I can map to existing Maven concepts is --add-opens.

     Martin


Re: JPMS whitebox testing issues

Posted by Stephen Colebourne <sc...@joda.org>.
On Wed, 2 Oct 2024 at 11:29, Martin Desruisseaux
<ma...@geomatys.com> wrote:
> The problem with module-info in both tests and main is that it works
> awkward. The previous compiler plugin had to took the project structure
> upside-down: the test code was considered the main code, and the main
> code was considered the patch. I just checked by compiling and running a
> dummy project with `java` and `javac` directly (without Maven) and I
> confirm: even if a `module-info` is present in the test output, it is
> ignored by the `java` command (the `module-info` of the main code has
> precedence) unless we pretend that the test are the main code and the
> main code are patching the tests. The behavior of `javac` is a little
> bit more complicated, but basically we also have to put things upside
> down to make it works.
>
> If nevertheless we still want to support having `module-info` in both
> main and test, I found a trick this morning for getting `javac` to
> compile the test `module-info` without putting the project structure
> upside-down. But it is undocumented and I don't know how reliable it
> would be. I can implement this trick (or try other tricks) if this is
> important, but I think that we should stop recommending the approach of
> overwriting `module-info`, or recommend to use it in last resort only.
> The alternative (compiler options) is indeed tedious, but this is
> precisely a major goal of this JPMS work to make that task easier than
> it currently is.

This issue remains hugely painful not just in Maven, but across Java.
It is a key part of why JPMS adoption is so painful.

The key principle here is that tests for a Maven module should be
located *within* the same Maven module of the main source code.
Forcing developers to write a separate Maven module just for tests is
not an acceptable solution.

1) Classpath testing with one (main) module-info.java file.
IIUC, this is what you are planning for maven-compiler-plugin and
maven-surefire-plugin to support.
It tests with the main code on the modulepath and the test code on the
classpath.
While this is fine for many use cases, sometimes you really need to
run the tests on the modulepath, so this is not sufficient by itself.

2) Blackbox testing with two module-info.java files.
This is supported by IntelliJ and Gradle IIUC. Maven must support this IMO.

For maven-compiler-plugin, Blackbox tests are fine AFAICT. All the
files in src/test/java are in a different package, and the
module-info.java file refers to a different module name, so there are
no clashes. The maven-surefire-plugin would just put the main module
and the test module on the modulepath, and everything ought to be
fine.

I believe Gradle recommends src/integrationTest for blackbox tests,
and I think that convention makes a lot of sense. It offers up the
potential for a single Maven module to contain both whitebox and
blackbox tests, which is incredibly powerful.

3) Whitebox testing with two module-info.java files.
This has yet to achieve an agreed approach in the community, despite
JPMS being out for many years.

To work effectively, it might be beneficial for the module-info file
to be named module-info-whitebox.java, or alternatively be in
src/whitebox.

Option A) "Parse and Patch"
maven-compiler-plugin would find the whitebox module-info source file,
parse it directly and convert it to command line options based on
--patch-module.
maven-compiler-plugin would then compile the test classes excluding
the whitebox module-info source file using the derived command line
options.
maven-surefire-plugin would use the same derived command line options.

Option B) "Merge and Recompile"
maven-compiler-plugin would compile the test classes by including both
src/main/java and src/test/java as inputs, excluding the
src/main/java/module-info.java file
maven-surefire-plugin would run only with target/test-classes, not
target/classes

Option C) "Copy before Compile"
maven-compiler-plugin would first copy target/classes to
target/test-classes excluding target/classes/module-info.class
maven-compiler-plugin then compiles src/test/java to
target/test-classes, without using target/classes as an input
maven-surefire-plugin would run only with target/test-classes, not
target/classes

I believe Option A is more desirable, and more in the spirit of JPMS,
and more in the spirit of Maven.

I understand that you want to make defining patch-module directives
easier in pom.xml. While that's a lovely idea, I think it is
fundamentally flawed in usability. There is no connection in the mind
of a developer between module-info directives and the matching command
line arguments. By contrast developers know exactly how to setup the
module-info.java file.

The only real difference I'm proposing is that the patch-module
directives are not defined in pom.xml, but are instead defined in a
java-like source file, eg module-info-whitebox.java. Think of it as a
Maven config file which happens to look like a java source file and it
really isn't that scary.

( I think it makes sense for this to be part of maven-compiler-plugin,
but it could also be a separate plugin that runs before
maven-compiler-plugin, or a separate goal. For example, it could be
thought of as a converter/compiler from module-info-whitebox.java to
module-info-whitebox.javac.args)

Stephen

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Martin Desruisseaux <ma...@geomatys.com>.
Hello Stephen

Thanks for testing.

Le 2024-10-02 à 00 h 30, Stephen Colebourne a écrit :

> At present, the compile phase is asked to compile module-info.java, 
> but the target/classes directory does not contain module-info.class. 
> I've no idea where it is going I'm afraid.
>
Is module-info missing for both test and main, or only for test?


>> the current prototype does not allow anymore to have module-info in 
>> both main and test. The latter is replaced by automatically generated 
>> compiler options.
>>
> I don't believe this is the right direction to go. The command line 
> flags are difficult to understand, and connect to the matching meaning 
> in module-info.java. By contrast, simply specifying a testing 
> module-info.java directly expresses what needs to be expressed. 
> Furthermore, IntelliJ already supports BlackBox testing in 
> src/test/java, plus it is the recommended approach on the Maven 
> website, so I don't think it is viable to remove that.
>
The problem with module-info in both tests and main is that it works 
awkward. The previous compiler plugin had to took the project structure 
upside-down: the test code was considered the main code, and the main 
code was considered the patch. I just checked by compiling and running a 
dummy project with `java` and `javac` directly (without Maven) and I 
confirm: even if a `module-info` is present in the test output, it is 
ignored by the `java` command (the `module-info` of the main code has 
precedence) unless we pretend that the test are the main code and the 
main code are patching the tests. The behavior of `javac` is a little 
bit more complicated, but basically we also have to put things upside 
down to make it works.

If nevertheless we still want to support having `module-info` in both 
main and test, I found a trick this morning for getting `javac` to 
compile the test `module-info` without putting the project structure 
upside-down. But it is undocumented and I don't know how reliable it 
would be. I can implement this trick (or try other tricks) if this is 
important, but I think that we should stop recommending the approach of 
overwriting `module-info`, or recommend to use it in last resort only. 
The alternative (compiler options) is indeed tedious, but this is 
precisely a major goal of this JPMS work to make that task easier than 
it currently is.


> As a separate point, it is very hard to know what the plugin is doing. 
> AFAIK, the only approach to finding out what is going on is to use -X 
> which produces far too many logs.
>
Indeed, the current beta-2 compiler generates a `javac` file somewhere 
in the `target` directory with the arguments that it used, but only if 
the compilation fails or if Maven was launched with the `--debug` flag. 
My rational was that the developer may not be interested in details when 
the compilation is successful. But we can of course revisit that policy. 
Otherwise, you can ignore the verbose logs of -X and just look for that 
`javac` file (I forgot the exact name) in `target`.

(And as a side note, I agree that Maven is too verbose, even in 
non-debug mode).


>>    * In project dependencies, add <type>modular-jar</type> for forcing
>>      the compiler to put the JAR dependency on the module path instead of
>>      trying to guess.
>>    * Conversely, use <type>classpath-jar</type> for forcing the compiler
>>      to put the JAR dependency on the class-path.
>
> I can't see many people wanting to set these. They are also 
> restricting. (I believe use of these setting would mean that I cannot 
> be used to run the same tests three times, as I am currently doing 
> with Maven 3)
>
The current Maven 3 behavior is still supported: <type>jar</type> 
(without specifying "modular-jar" or "classpath-jar") behaves as today, 
with the same heuristic rules. The "jar" type is still the default, so 
being specific is an "opt-in" feature.


>> Regarding white-box testing in a modular project:
>>    * The plugin uses the --patch-module option for specifying the
>>      directories of test classes to the Java compiler.
>>    * The plugin adds the following options for test dependencies that are
>>      not present in the main dependencies (typically JUnit):
>>        o --add-modules and --add-reads for compile-time dependencies.
>>        o --add-modules only for runtime-time dependencies (typically
>>          services to be discovered with java.util.ServiceLoader).
>>        o --add-reads ALL-UNNAMED if at least one compile-time test
>>          dependency is not modular.
> You also need to ensure that
> - src/main/resources and src/test/resources are correctly patched
> - the module-info.java `requires static` directive is correctly followed.

Yes, but those last points apply more to the surefire plugin than the 
compiler one. We did not started the work on surefire yet.

     Martin


Re: JPMS whitebox testing issues

Posted by Stephen Colebourne <sc...@joda.org>.
On Tue, 24 Sept 2024 at 16:13, Martin Desruisseaux
<ma...@geomatys.com> wrote:
> If you are willing to test the development regarding JPMS, there is the
> steps:
>
>   * Download Maven 4.0.0-beta4
>   * Use Maven 4 for building the master branch of
>     https://github.com/apache/maven-plugin-tools
>   * Use Maven 4 for building
>     https://github.com/apache/maven-plugin-testing/pull/44. Use "mvn
>     install -Denforcer.skip".
>   * Use Maven 4 for building
>     https://github.com/Geomatys/maven-compiler-plugin/tree/beta2. Use
>     "mvn install -Denforcer.skip".
>   * Set the compiler version to "4.0.0-beta-2-SNAPSHOT" in your project.

See https://github.com/JodaOrg/joda-money/tree/maventest

At present, the compile phase is asked to compile module-info.java,
but the target/classes directory does not contain module-info.class.
I've no idea where it is going I'm afraid.

>     (note: I was wrong
>     yesterday, the current prototype does not allowed anymore to have
>     module-info in both main and test. The latter is replaced by
>     automatically generated compiler options).

I don't believe this is the right direction to go. The command line
flags are difficult to understand, and connect to the matching meaning
in module-info.java. By contrast, simply specifying a testing
module-info.java directly expresses what needs to be expressed.
Furthermore, IntelliJ already supports BlackBox testing in
src/test/java, plus it is the recommended approach on the Maven
website, so I don't think it is viable to remove that.

As a separate point, it is very hard to know what the plugin is doing.
AFAIK, the only approach to finding out what is going on is to use -X
which produces far too many logs.


>   * In project dependencies, add <type>modular-jar</type> for forcing
>     the compiler to put the JAR dependency on the module path instead of
>     trying to guess.
>   * Conversely, use <type>classpath-jar</type> for forcing the compiler
>     to put the JAR dependency on the class-path.

I can't see many people wanting to set these. They are also
restricting. (I believe use of these setting would mean that I cannot
be used to run the same tests three times, as I am currently doing
with Maven 3)


> Regarding white-box testing in a modular project:
>   * The plugin uses the --patch-module option for specifying the
>     directories of test classes to the Java compiler.
>   * The plugin adds the following options for test dependencies that are
>     not present in the main dependencies (typically JUnit):
>       o --add-modules and --add-reads for compile-time dependencies.
>       o --add-modules only for runtime-time dependencies (typically
>         services to be discovered with java.util.ServiceLoader).
>       o --add-reads ALL-UNNAMED if at least one compile-time test
>         dependency is not modular.

You also need to ensure that
- src/main/resources and src/test/resources are correctly patched
- the module-info.java `requires static` directive is correctly followed.

Stephen

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Martin Desruisseaux <ma...@geomatys.com>.
Hello again Stephen

If you are willing to test the development regarding JPMS, there is the 
steps:

  * Download Maven 4.0.0-beta4
  * Use Maven 4 for building the master branch of
    https://github.com/apache/maven-plugin-tools
  * Use Maven 4 for building
    https://github.com/apache/maven-plugin-testing/pull/44. Use "mvn
    install -Denforcer.skip".
  * Use Maven 4 for building
    https://github.com/Geomatys/maven-compiler-plugin/tree/beta2. Use
    "mvn install -Denforcer.skip".
  * Set the compiler version to "4.0.0-beta-2-SNAPSHOT" in your project.
  * See
    https://github.com/Geomatys/maven-compiler-plugin/wiki/Incompatible-changes
    necessary changes in project configuration (note: I was wrong
    yesterday, the current prototype does not allowed anymore to have
    module-info in both main and test. The latter is replaced by
    automatically generated compiler options).

Below are the new features. They are not all tested extensively. If they 
are not working as expected, I would like to know so we can fix them. 
Note: the following apply to the compiler plugin only. Other plugins 
have not yet been updated.

  * In project dependencies, add <type>modular-jar</type> for forcing
    the compiler to put the JAR dependency on the module path instead of
    trying to guess.
  * Conversely, use <type>classpath-jar</type> for forcing the compiler
    to put the JAR dependency on the class-path.
  * The default (<type>jar</type>) tries to guess with the same
    heuristic as the current compiler (Maven 3), but I would recommend
    to be explicit instead.
  * Use <type>modular-processor</type> or
    <type>classpath-processor</type> for adding the dependency on the
    annotation processor module-path or class-path. This is in
    replacement for the previous compiler configuration: dependencies
    are managed in a more unified way (same will apply for taglet,
    doclet, etc.).

Regarding white-box testing in a modular project:

  * The plugin uses the --patch-module option for specifying the
    directories of test classes to the Java compiler.
  * The plugin adds the following options for test dependencies that are
    not present in the main dependencies (typically JUnit):
      o --add-modules and --add-reads for compile-time dependencies.
      o --add-modules only for runtime-time dependencies (typically
        services to be discovered with java.util.ServiceLoader).
      o --add-reads ALL-UNNAMED if at least one compile-time test
        dependency is not modular.

Planed for Surefire (not yet done):

  * The plugin will adds the same --add-reads and --add-modules options
    as the compiler, maybe using shared Maven code for ensuring consistency.
  * The plugin will adds --add-exports module/package=junit (or whatever
    test engine is used) options for non-exported packages that have
    test classes.

Above will be ajusted according experience and feedbacks.

     Martin


Re: JPMS whitebox testing issues

Posted by Stephen Colebourne <sc...@joda.org>.
For anyone following along who wants to get get whitebox testing
working, here is the changes I made:

https://github.com/JodaOrg/joda-money/pull/143/files#diff-9c5fb3d1b7e3b0f54bc5c4182965c4fe1f9023d449017cece3005d3f90e8e4d8R126

These changes cause Maven to test Joda-Money three times:
- once with main and test on the classpath
- once with main on the modulepath and tests on the classpath
- once with main and tests on the modulepath

This uses an extra module-info.java file for testing. This is managed
in such a way that IDEs are not messed up.

The code is tested with Java 21 and Maven 3.8.8/3.9.9.

Stephen


On Mon, 23 Sept 2024 at 16:47, Stephen Colebourne <sc...@joda.org> wrote:
>
> Hi all,
> Apologies for the long email, but whitebox testing is a complex topic.
> I know not everyone agrees with whitebox testing, but it is a major
> factor in how many projects are written, and IMO Maven should fully
> and easily support it.
>
> A "whitebox mode" test has two module-info.java files, both with the
> same module name. The goal is to support unit testing with full access
> to the internals of the module as in the "good ole days" while
> ensuring that the test occurs on the module-path (test failures can
> occur on the module-path which do not occur on the class-path, and
> vice versa):
>
> root
> - pom.xml
> - src/main/java
> -- module-info.java
> -- mypackage
> --- Main,java
> - src/main/resources
> -- mypackage
> --- main.txt
> - src/test/java
> -- module-info.java
> -- mypackage
> --- Test,java
>
> The main module-info would be something like:
>   module mymodule {
>     exports mypackage;
>   }
>
> The test module-info would be something like:
>   open module mymodule {
>     // duplicate contents of main module-info
>     exports mypackage;
>     // any additional test dependencies/services
>     requires transitive org.junit.jupiter;
>   }
>
> The good news is that a setup like this mostly works today in Maven
> (tested with v3.9.9 and Java 21).
> However there are three key issues:
>
> The way Maven currently operates is:
>
> 1) In `testCompile`, the test source code is compiled using:
>   --module-path target/test-classes:target/classes
>   --patch-module mymodule=src/main/java
>
> 2) In `surefire`, the tests are run with:
> --module-path target/test-classes:target/classes:<< dependencies >>
> --class-path << surefire jar files >>
>
> What actually happens is (I think) very surprising. TestCompile
> compiles BOTH the main and test source code into
> `target/test-classes`:
> target/classes contains
> - module-info.class (from src/main/java)
> - mypackage/Main.class
> - mypackage/main.txt
> target/test-classes contains
> - module-info.class (from src/test/java)
> - mypackage/Main.class
> - mypackage/Test.class
>
> This results in three issues:
>
> A) The code in `src/main/java` is compiled twice, which is
> unnecessary, surprising and slows things down
>
> B) There is no need to refer to `target/classes` on the module-path,
> as it is not used in TestCompile or Surefire.
>
> C) Any resource files (src/main/resources) are UNAVAILABLE to
> Surefire, which results in the tests failing, eg.
> `Main.class.getResourceAsStream("main.txt")` will return null. This is
> because Surefire sees the code in target/test-classes as the complete
> definition of the module, and as shown above `main.txt` is not present
> in `target/test-classes`.
>
> -----
> Option 1:
> Do nothing. End users can fix the resource file issue by adding this
> to the pom.xml:
>   <plugin>
>    <groupId>org.apache.maven.plugins</groupId>
>    <artifactId>maven-surefire-plugin</artifactId>
>    <configuration>
>     <argLine>--patch-module mymodule=src/main/resources;</argLine>
>    </configuration>
>   </plugin>
>
> This causes Surefire to pickup the resource folder and patch it into the module.
>
> Option 2:
> Automate the patch-module described above. When Surefire runs in
> "whitebox mode", the --patch-module outlined above should be
> automatically generated and added to the command line. Care will be
> needed not to clash with any patch-module the user adds manually.
>
> This fixes issue C.
>
> Option 3:
> Properly address the issue. maven-compiler-plugin already recognises
> that there are two module-info.java files and checks the names. If
> they are different it sets up "blackbox mode". IMO,
> maven-compiler-plugin needs an official "whitebox mode".
> The correct approach, as far as I can determine, is for TestCompile to
> do the following:
>   --module-path target/test-classes
>   --patch-module mymodule=target/classes
> This avoids the main source code being compiled twice - it is patched
> in instead.
>
> The same change needs to be applied to Surefire
>   --module-path target/test-classes:<< dependencies >>
>   --patch-module mymodule=target/classes
>
> This fixes issues A , B and C. (The resource files in target/classes
> are patched in alongside the .class files).
> Note that the java command line generates a warning about duplicate
> module-info.class files, but that is expected in this case,and can be
> ignored.
>
> ---
> So, what do people think? Is there appetite to raise an issue to get
> option 2 or 3 done?
>
> thanks for reading this far
> Stephen Colebourne
>
> PS. IDEs don't like the two module-info.java files of whitebox
> testing, but that's OK - Maven isn't beholden to IDEs. It also turns
> out there is an easy workaround for IDEs (which I can describe in
> another email, as this one is too long)
> PPS. I know about the suggested module-info.test file - lets not get
> into that discussion here

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Ceki Gulcu <ce...@qos.ch.INVALID>.
I forgot to mention that the original mail from Stephen is quite 
informative and I thank him for that.

On 10/10/2024 22:49, Ceki Gulcu wrote:
> 
> Hi Stephen,
> 
> Thank you for broaching the issue of whitebox testing with JMPS. I think 
> that it is an important topic.
> 
> I should also like to mention for a related feature.
> 
> If JMPS project "Bee" depends on JMPS project "Insect", is often useful 
> to use the insect-tests.jar in the tests of project "Bee". Perhaps if 
> the whitebox testing issue with JMPS is settled on, then depending on 
> tests-jars can be solved as well.
> 
> Best regards,
> 

-- 
Ceki Gülcü

Sponsoring SLF4J/logback/reload4j at https://github.com/sponsors/qos-ch


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: JPMS whitebox testing issues

Posted by Ceki Gulcu <ce...@qos.ch.INVALID>.
Hi Stephen,

Thank you for broaching the issue of whitebox testing with JMPS. I think 
that it is an important topic.

I should also like to mention for a related feature.

If JMPS project "Bee" depends on JMPS project "Insect", is often useful 
to use the insect-tests.jar in the tests of project "Bee". Perhaps if 
the whitebox testing issue with JMPS is settled on, then depending on 
tests-jars can be solved as well.

Best regards,

-- 
Ceki Gülcü

Sponsoring SLF4J/logback/reload4j at https://github.com/sponsors/qos-ch


On 23/09/2024 17:47, Stephen Colebourne wrote:
> Hi all,
> Apologies for the long email, but whitebox testing is a complex topic.
> I know not everyone agrees with whitebox testing, but it is a major
> factor in how many projects are written, and IMO Maven should fully
> and easily support it.
> 
> A "whitebox mode" test has two module-info.java files, both with the
> same module name. The goal is to support unit testing with full access
> to the internals of the module as in the "good ole days" while
> ensuring that the test occurs on the module-path (test failures can
> occur on the module-path which do not occur on the class-path, and
> vice versa):
> 
> root
> - pom.xml
> - src/main/java
> -- module-info.java
> -- mypackage
> --- Main,java
> - src/main/resources
> -- mypackage
> --- main.txt
> - src/test/java
> -- module-info.java
> -- mypackage
> --- Test,java
> 
> The main module-info would be something like:
>    module mymodule {
>      exports mypackage;
>    }
> 
> The test module-info would be something like:
>    open module mymodule {
>      // duplicate contents of main module-info
>      exports mypackage;
>      // any additional test dependencies/services
>      requires transitive org.junit.jupiter;
>    }
> 
> The good news is that a setup like this mostly works today in Maven
> (tested with v3.9.9 and Java 21).
> However there are three key issues:
> 
> The way Maven currently operates is:
> 
> 1) In `testCompile`, the test source code is compiled using:
>    --module-path target/test-classes:target/classes
>    --patch-module mymodule=src/main/java
> 
> 2) In `surefire`, the tests are run with:
> --module-path target/test-classes:target/classes:<< dependencies >>
> --class-path << surefire jar files >>
> 
> What actually happens is (I think) very surprising. TestCompile
> compiles BOTH the main and test source code into
> `target/test-classes`:
> target/classes contains
> - module-info.class (from src/main/java)
> - mypackage/Main.class
> - mypackage/main.txt
> target/test-classes contains
> - module-info.class (from src/test/java)
> - mypackage/Main.class
> - mypackage/Test.class
> 
> This results in three issues:
> 
> A) The code in `src/main/java` is compiled twice, which is
> unnecessary, surprising and slows things down
> 
> B) There is no need to refer to `target/classes` on the module-path,
> as it is not used in TestCompile or Surefire.
> 
> C) Any resource files (src/main/resources) are UNAVAILABLE to
> Surefire, which results in the tests failing, eg.
> `Main.class.getResourceAsStream("main.txt")` will return null. This is
> because Surefire sees the code in target/test-classes as the complete
> definition of the module, and as shown above `main.txt` is not present
> in `target/test-classes`.
> 
> -----
> Option 1:
> Do nothing. End users can fix the resource file issue by adding this
> to the pom.xml:
>    <plugin>
>     <groupId>org.apache.maven.plugins</groupId>
>     <artifactId>maven-surefire-plugin</artifactId>
>     <configuration>
>      <argLine>--patch-module mymodule=src/main/resources;</argLine>
>     </configuration>
>    </plugin>
> 
> This causes Surefire to pickup the resource folder and patch it into the module.
> 
> Option 2:
> Automate the patch-module described above. When Surefire runs in
> "whitebox mode", the --patch-module outlined above should be
> automatically generated and added to the command line. Care will be
> needed not to clash with any patch-module the user adds manually.
> 
> This fixes issue C.
> 
> Option 3:
> Properly address the issue. maven-compiler-plugin already recognises
> that there are two module-info.java files and checks the names. If
> they are different it sets up "blackbox mode". IMO,
> maven-compiler-plugin needs an official "whitebox mode".
> The correct approach, as far as I can determine, is for TestCompile to
> do the following:
>    --module-path target/test-classes
>    --patch-module mymodule=target/classes
> This avoids the main source code being compiled twice - it is patched
> in instead.
> 
> The same change needs to be applied to Surefire
>    --module-path target/test-classes:<< dependencies >>
>    --patch-module mymodule=target/classes
> 
> This fixes issues A , B and C. (The resource files in target/classes
> are patched in alongside the .class files).
> Note that the java command line generates a warning about duplicate
> module-info.class files, but that is expected in this case,and can be
> ignored.
> 
> ---
> So, what do people think? Is there appetite to raise an issue to get
> option 2 or 3 done?
> 
> thanks for reading this far
> Stephen Colebourne
> 
> PS. IDEs don't like the two module-info.java files of whitebox
> testing, but that's OK - Maven isn't beholden to IDEs. It also turns
> out there is an easy workaround for IDEs (which I can describe in
> another email, as this one is too long)
> PPS. I know about the suggested module-info.test file - lets not get
> into that discussion here
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org