WorkbookEvaluator vs JUnit: WE seems to save state between test calls

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

WorkbookEvaluator vs JUnit: WE seems to save state between test calls

gallon.fizik@gmail.com
Hello, I have a simple test class:

import org.apache.poi.ss.formula.WorkbookEvaluator;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.functions.Function;
import org.junit.Test;

public class FunctionRegistratorIT1 {
    @Test
    public void testRegisterFunction1() {
        WorkbookEvaluator.registerFunction("TREND", new DummyExcelFunction());
    }

    @Test
    public void testRegisterFunction2() {
        WorkbookEvaluator.registerFunction("TREND", new DummyExcelFunction());
    }

    private class DummyExcelFunction implements Function {
        @Override
        public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
            return new NumberEval(0);
        }
    }
}

Oddly enough, the test being executed second fails with 'IllegalArgumentException: POI already implements TREND. You cannot POI's implementations of Excel functions'. If I comment one of the tests out, the other one passes. This behavior persists if the same function is registered in different tests across a test suite.

I expected that every @Test is run in isolation, but it seems that WorkbookEvaluator stores registered functions between invocations of different tests within a suite.

Although it is possible that it is probably related to JUnit rather than POI, perhaps someone here has already witnessed this behavior.

To sum up, the questions are:
* why does this happen?
* how can I have get several tests registering the same function names work?

Regards,
Vladislav

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: WorkbookEvaluator vs JUnit: WE seems to save state between test calls

Yegor Kozlov-4
UDFs are registered in a static map and JUnit does not reset  static
variables.
Use the @BeforeClass annotation to register your functions.  This can be a
problem if you have many functions, but if it is just one a or few  it
should be okay.
If @BeforeClass  is not an option then the only alternative is to reset
static variables via reflection or class loaded in @After.

Yegor

On Thu, Dec 21, 2017 at 5:36 PM, [hidden email] <
[hidden email]> wrote:

> Hello, I have a simple test class:
>
> import org.apache.poi.ss.formula.WorkbookEvaluator;
> import org.apache.poi.ss.formula.eval.NumberEval;
> import org.apache.poi.ss.formula.eval.ValueEval;
> import org.apache.poi.ss.formula.functions.Function;
> import org.junit.Test;
>
> public class FunctionRegistratorIT1 {
>     @Test
>     public void testRegisterFunction1() {
>         WorkbookEvaluator.registerFunction("TREND", new
> DummyExcelFunction());
>     }
>
>     @Test
>     public void testRegisterFunction2() {
>         WorkbookEvaluator.registerFunction("TREND", new
> DummyExcelFunction());
>     }
>
>     private class DummyExcelFunction implements Function {
>         @Override
>         public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int
> srcColumnIndex) {
>             return new NumberEval(0);
>         }
>     }
> }
>
> Oddly enough, the test being executed second fails with
> 'IllegalArgumentException: POI already implements TREND. You cannot POI's
> implementations of Excel functions'. If I comment one of the tests out, the
> other one passes. This behavior persists if the same function is registered
> in different tests across a test suite.
>
> I expected that every @Test is run in isolation, but it seems that
> WorkbookEvaluator stores registered functions between invocations of
> different tests within a suite.
>
> Although it is possible that it is probably related to JUnit rather than
> POI, perhaps someone here has already witnessed this behavior.
>
> To sum up, the questions are:
> * why does this happen?
> * how can I have get several tests registering the same function names
> work?
>
> Regards,
> Vladislav
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: WorkbookEvaluator vs JUnit: WE seems to save state between test calls

Greg Woolsey
You could also wrap your function registration in code that first looks to
see if it is already defined, using
WorkbookEvaluator.getSupportedFunctionNames().contains("TREND")

On Thu, Dec 21, 2017 at 7:35 AM Yegor Kozlov <[hidden email]> wrote:

> UDFs are registered in a static map and JUnit does not reset  static
> variables.
> Use the @BeforeClass annotation to register your functions.  This can be a
> problem if you have many functions, but if it is just one a or few  it
> should be okay.
> If @BeforeClass  is not an option then the only alternative is to reset
> static variables via reflection or class loaded in @After.
>
> Yegor
>
> On Thu, Dec 21, 2017 at 5:36 PM, [hidden email] <
> [hidden email]> wrote:
>
> > Hello, I have a simple test class:
> >
> > import org.apache.poi.ss.formula.WorkbookEvaluator;
> > import org.apache.poi.ss.formula.eval.NumberEval;
> > import org.apache.poi.ss.formula.eval.ValueEval;
> > import org.apache.poi.ss.formula.functions.Function;
> > import org.junit.Test;
> >
> > public class FunctionRegistratorIT1 {
> >     @Test
> >     public void testRegisterFunction1() {
> >         WorkbookEvaluator.registerFunction("TREND", new
> > DummyExcelFunction());
> >     }
> >
> >     @Test
> >     public void testRegisterFunction2() {
> >         WorkbookEvaluator.registerFunction("TREND", new
> > DummyExcelFunction());
> >     }
> >
> >     private class DummyExcelFunction implements Function {
> >         @Override
> >         public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int
> > srcColumnIndex) {
> >             return new NumberEval(0);
> >         }
> >     }
> > }
> >
> > Oddly enough, the test being executed second fails with
> > 'IllegalArgumentException: POI already implements TREND. You cannot POI's
> > implementations of Excel functions'. If I comment one of the tests out,
> the
> > other one passes. This behavior persists if the same function is
> registered
> > in different tests across a test suite.
> >
> > I expected that every @Test is run in isolation, but it seems that
> > WorkbookEvaluator stores registered functions between invocations of
> > different tests within a suite.
> >
> > Although it is possible that it is probably related to JUnit rather than
> > POI, perhaps someone here has already witnessed this behavior.
> >
> > To sum up, the questions are:
> > * why does this happen?
> > * how can I have get several tests registering the same function names
> > work?
> >
> > Regards,
> > Vladislav
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>
Reply | Threaded
Open this post in threaded view
|

Re: WorkbookEvaluator vs JUnit: WE seems to save state between test calls

gallon.fizik@gmail.com
Actually, this was my initial approach. It works perfect in the production application but still doesn't allow to register a function with the same name several times across a test suite (which should be legitimate).

Anyway, thanks for your reply

On 2017-12-21 18:13, Greg Woolsey <[hidden email]> wrote:
> You could also wrap your function registration in code that first looks to
> see if it is already defined, using
> WorkbookEvaluator.getSupportedFunctionNames().contains("TREND")
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: WorkbookEvaluator vs JUnit: WE seems to save state between test calls

gallon.fizik@gmail.com
In reply to this post by Yegor Kozlov-4
Thank you for the detailed explanation.

On 2017-12-21 16:35, Yegor Kozlov <[hidden email]> wrote:

> UDFs are registered in a static map and JUnit does not reset  static
> variables.
> Use the @BeforeClass annotation to register your functions.  This can be a
> problem if you have many functions, but if it is just one a or few  it
> should be okay.
> If @BeforeClass  is not an option then the only alternative is to reset
> static variables via reflection or class loaded in @After.
>
> Yegor
>
> On Thu, Dec 21, 2017 at 5:36 PM, [hidden email] <
> [hidden email]> wrote:
>
> > Hello, I have a simple test class:
> >
> > import org.apache.poi.ss.formula.WorkbookEvaluator;
> > import org.apache.poi.ss.formula.eval.NumberEval;
> > import org.apache.poi.ss.formula.eval.ValueEval;
> > import org.apache.poi.ss.formula.functions.Function;
> > import org.junit.Test;
> >
> > public class FunctionRegistratorIT1 {
> >     @Test
> >     public void testRegisterFunction1() {
> >         WorkbookEvaluator.registerFunction("TREND", new
> > DummyExcelFunction());
> >     }
> >
> >     @Test
> >     public void testRegisterFunction2() {
> >         WorkbookEvaluator.registerFunction("TREND", new
> > DummyExcelFunction());
> >     }
> >
> >     private class DummyExcelFunction implements Function {
> >         @Override
> >         public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int
> > srcColumnIndex) {
> >             return new NumberEval(0);
> >         }
> >     }
> > }
> >
> > Oddly enough, the test being executed second fails with
> > 'IllegalArgumentException: POI already implements TREND. You cannot POI's
> > implementations of Excel functions'. If I comment one of the tests out, the
> > other one passes. This behavior persists if the same function is registered
> > in different tests across a test suite.
> >
> > I expected that every @Test is run in isolation, but it seems that
> > WorkbookEvaluator stores registered functions between invocations of
> > different tests within a suite.
> >
> > Although it is possible that it is probably related to JUnit rather than
> > POI, perhaps someone here has already witnessed this behavior.
> >
> > To sum up, the questions are:
> > * why does this happen?
> > * how can I have get several tests registering the same function names
> > work?
> >
> > Regards,
> > Vladislav
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]