[ 
https://issues.apache.org/jira/browse/SOLR-15815?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17448145#comment-17448145
 ] 

Chris M. Hostetter commented on SOLR-15815:
-------------------------------------------

{quote}The only downside I see is that rules defined this way would trigger for 
all tests (even if they don't use the rule).
{quote}
That would be the point of using a rule like this though – if you declare it in 
your test class, it means you want any test method to be "wrapped" in this 
AutoClosable's lifecycle – so that if test method (now or added in the future) 
causes an ERROR to be logged, you want that test method to fail – unless the 
test logic goes out of it's way to "expect" that specific log message by 
popping it off the queue.

If you want ERROR log messages to be ignored from most tests, you just wouldn't 
use the rule – and if there was a single test method you wanted to inspect the 
logs for, that test method could create a  {{LogListener}} directly.
----
If we made {{CloseableResourceRule}} "lazy init" only if it's {{get()}} method 
is called, that would kind of defaet the point of this initial use case for 
capturing log messages – because every test method would have to go out of it's 
way to call {{get()}} before doing any logic (or if you wanted to use it as a 
{{@ClassRule}} to also catch any ERROR logs from {{@Before*}} / {{@After*}} or 
other {{@Rule}} logic then ... i dunno what you'd do? )

I suppose for completeness we could add a static 
{{CloseableResourceRule.none()}} type factory method, and a {{set()}} method 
... so that you could do stuff like this...
{code:java}
@Rule public static CloseableResourceRule<X> xxx = CloseableResourceRule.none();
@Rule public static CloseableResourceRule<Y> yyy = 
CloseableResourceRule.create(() -> new Y());

public void testA() {
  // yyy.get() will already return a Y which will be closed by the yyy rule 
after testA finishes
  // xxx.get() will return null

  // do stuff
}
public void testB() {
  // yyy.get() will already return a Y which will be closed by the yyy rule 
after testB finishes
  xxx.set(new X());
  // now xxx.get() will return the X we just created
  // and when the test is finished, the xxx rule will close our X instance

 // do stuff
}
{code}
...but i'm not sure how useful that would really be?

> Add TestRule support for ErrorLogMuter & LogListener
> ----------------------------------------------------
>
>                 Key: SOLR-15815
>                 URL: https://issues.apache.org/jira/browse/SOLR-15815
>             Project: Solr
>          Issue Type: Sub-task
>            Reporter: Chris M. Hostetter
>            Priority: Major
>         Attachments: SOLR-15815.patch
>
>
> sibling sub-tasks implement log muting & inspection helpers that work by 
> implementing AutoClosable, as discussed in parent task we should also expose 
> this functionality in a TestRule



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to