Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create parameter resolver for class path resources #503

Open
nipafx opened this issue Jun 28, 2021 · 9 comments
Open

Create parameter resolver for class path resources #503

nipafx opened this issue Jun 28, 2021 · 9 comments

Comments

@nipafx
Copy link
Member

nipafx commented Jun 28, 2021

An extension that makes it easy to read resources from src/test/resources as discussed in this tweet.

@Michael1993 Michael1993 changed the title Create parameter resolver fot class path resources Create parameter resolver for class path resources Jun 28, 2021
@nipafx
Copy link
Member Author

nipafx commented Jun 28, 2021

One possible approach is shown in this reply to the tweet:

@Test
@ResourceRead("sub/anton.txt")
void asContentLines(ResourceContentLines resource) {
	resource.getContent();
}

@Test
@ResourceRead("sub/anton.txt")
void asContentString(ResourceContentString resource) {
	resource.getContent();
}

Another approach would be to always annotate the parameter (I'll keep using ResourceRead for continuity; I'm not proposing it as the annotation's name):

@Test
void asContentString(@ResourceRead("sub/anton.txt") String content) {
	// ...
}

Since we then know which parameter to provide a value for (and thus its type), we can resolve a wide variety of types:

  • String by interpreting the entire file content as a single string
  • Stream<String> from Files::lines
  • any other collection of String by collecting to it
  • File or Path for the file itself
  • implicit conversion like Jupiter does for parameters
  • explicit conversion like Jupiter does with a @ConvertWith annotation
  • if we can determine the parametric type of a collection, we could even provide collections of T, where each line in the file is converted (as just described) to an instance of T

As to the annotation itself, it should probably offer the same attributes that the Files API offers when reading files (charset, [is there more?]).

@Michael1993
Copy link
Member

Michael1993 commented Jun 28, 2021

JUnit does not register extensions from parameter annotations. Meaning this:

@Test
void asContentString(@ResourceRead("sub/anton.txt") String content) {

does not work.

@nipafx
Copy link
Member Author

nipafx commented Jun 28, 2021

Ah right, I seem to forget this quite often... 🤦🏾 See linked issue ☝🏾 - maybe that can change.

@jbduncan
Copy link
Contributor

As a side note, we might want to name the annotation @ResourceRead something like @ClasspathResource (or @ModulePathResource?), because my ongoing work will introduce another concept which so far is also called "resource".

But perhaps that indicates that I need to improve the name of my work's own "resource" thingie instead!

@Bukama
Copy link
Member

Bukama commented Jun 29, 2021

String by interpreting the entire file content as a single string

Would really like to see this, to easily have the possibility to offer XML-Strings.

@Michael1993
Copy link
Member

We are just too slow, it seems.

@Bukama
Copy link
Member

Bukama commented Jul 12, 2021

Shall we ask him to integrate it into pioneer?

@nipafx nipafx linked a pull request Jul 25, 2021 that will close this issue
13 tasks
@nipafx nipafx removed a link to a pull request Jul 25, 2021
13 tasks
@nipafx
Copy link
Member Author

nipafx commented Jul 27, 2021

Hi @khmarbaise, we'd like to have something like this in Pioneer. Would you be interested to integrate your project here (i.e. republish an adapted version of the code in this project)? :)

@khmarbaise
Copy link

Hi @nipafx sure sure why not ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants