javaunohelper: migrate ComponentBase_Test to JUnit

Use -XX:MaxGCPauseMillis=50 in conjunction with Thread.sleep(51) to wait 51ms
after a GC call, instead of waiting 10s.

http://docs.oracle.com/javase/1.5.0/docs/guide/vm/gc-ergonomics.html

$ make JunitTest_juh

Change-Id: Id2cdada0e493fd450aab5dbec164502e0173857f
Reviewed-on: https://gerrit.libreoffice.org/11593
Reviewed-by: David Ostrovsky <David.Ostrovsky@gmx.de>
Tested-by: David Ostrovsky <David.Ostrovsky@gmx.de>
This commit is contained in:
Robert Antoni Buj i Gelonch
2014-09-22 16:31:56 +02:00
committed by David Ostrovsky
parent ed01da53ff
commit 2d8348d298
4 changed files with 74 additions and 88 deletions

View File

@@ -0,0 +1,45 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
$(eval $(call gb_JunitTest_JunitTest,juh))
$(eval $(call gb_JunitTest_set_defs,juh,\
$$(DEFS) \
-XX:MaxGCPauseMillis=50 \
))
$(eval $(call gb_JunitTest_use_jars,juh,\
ridl \
jurt \
))
$(eval $(call gb_JunitTest_use_jar_classset,juh,ridljar))
$(eval $(call gb_JunitTest_add_sourcefiles,juh,\
javaunohelper/com/sun/star/comp/helper/Bootstrap \
javaunohelper/com/sun/star/comp/helper/BootstrapException \
javaunohelper/com/sun/star/comp/helper/ComponentContext \
javaunohelper/com/sun/star/comp/helper/ComponentContextEntry \
javaunohelper/com/sun/star/lib/uno/helper/ComponentBase \
javaunohelper/com/sun/star/lib/uno/helper/InterfaceContainer \
javaunohelper/com/sun/star/lib/uno/helper/MultiTypeInterfaceContainer \
javaunohelper/com/sun/star/lib/uno/helper/WeakAdapter \
javaunohelper/com/sun/star/lib/uno/helper/WeakBase \
javaunohelper/test/com/sun/star/comp/helper/ComponentContext_Test \
javaunohelper/test/com/sun/star/lib/uno/helper/AWeakBase \
javaunohelper/test/com/sun/star/lib/uno/helper/ComponentBase_Test \
javaunohelper/test/com/sun/star/lib/uno/helper/ProxyProvider \
))
$(eval $(call gb_JunitTest_add_classes,juh,\
com.sun.star.comp.helper.ComponentContext_Test \
com.sun.star.lib.uno.helper.ComponentBase_Test \
))
# vim:set noet sw=4 ts=4:

View File

@@ -1,31 +0,0 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
$(eval $(call gb_JunitTest_JunitTest,juh_ComponentContext))
$(eval $(call gb_JunitTest_use_jars,juh_ComponentContext,\
ridl \
jurt \
))
$(eval $(call gb_JunitTest_use_jar_classset,juh_ComponentContext,ridljar))
$(eval $(call gb_JunitTest_add_sourcefiles,juh_ComponentContext,\
javaunohelper/com/sun/star/comp/helper/Bootstrap \
javaunohelper/com/sun/star/comp/helper/BootstrapException \
javaunohelper/com/sun/star/comp/helper/ComponentContext \
javaunohelper/com/sun/star/comp/helper/ComponentContextEntry \
javaunohelper/test/com/sun/star/comp/helper/ComponentContext_Test \
))
$(eval $(call gb_JunitTest_add_classes,juh_ComponentContext,\
com.sun.star.comp.helper.ComponentContext_Test \
))
# vim:set noet sw=4 ts=4:

View File

@@ -17,7 +17,7 @@ $(eval $(call gb_Module_add_targets,javaunohelper,\
)) ))
$(eval $(call gb_Module_add_subsequentcheck_targets,javaunohelper,\ $(eval $(call gb_Module_add_subsequentcheck_targets,javaunohelper,\
JunitTest_juh_ComponentContext \ JunitTest_juh \
)) ))
ifneq ($(DISABLE_DYNLOADING),TRUE) ifneq ($(DISABLE_DYNLOADING),TRUE)

View File

@@ -26,6 +26,11 @@ import com.sun.star.lang.XEventListener;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Before;
import org.junit.Test;
public class ComponentBase_Test public class ComponentBase_Test
{ {
private static final Logger logger = Logger.getLogger(ComponentBase_Test.class.getName()); private static final Logger logger = Logger.getLogger(ComponentBase_Test.class.getName());
@@ -36,25 +41,30 @@ public class ComponentBase_Test
Object proxyObj3TypeProv; Object proxyObj3TypeProv;
Object proxyObj2TypeProv; Object proxyObj2TypeProv;
/** Creates a new instance of ComponentBase_Test */ /** Class variables are initialized before each Test method */
public ComponentBase_Test() @Before public void setUp() throws Exception
{ {
obj1= new AWeakBase(); obj1= new AWeakBase();
obj2= new AWeakBase(); obj2= new AWeakBase();
obj3= new AWeakBase(); obj3= new AWeakBase();
proxyObj1Weak1= ProxyProvider.createProxy(obj1, XWeak.class); proxyObj1Weak1= ProxyProvider.createProxy(obj1, XWeak.class);
proxyObj3Weak1= ProxyProvider.createProxy(obj3, XWeak.class); proxyObj3Weak1= ProxyProvider.createProxy(obj3, XWeak.class);
proxyObj3Weak2= ProxyProvider.createProxy(obj3, XWeak.class); proxyObj3Weak2= ProxyProvider.createProxy(obj3, XWeak.class);
assertNotNull(proxyObj1Weak1);
assertNotNull(proxyObj3Weak1);
assertNotNull(proxyObj3Weak2);
proxyObj2TypeProv= ProxyProvider.createProxy(obj2, XTypeProvider.class); proxyObj2TypeProv= ProxyProvider.createProxy(obj2, XTypeProvider.class);
proxyObj3TypeProv= ProxyProvider.createProxy(obj3, XTypeProvider.class); proxyObj3TypeProv= ProxyProvider.createProxy(obj3, XTypeProvider.class);
assertNotNull(proxyObj2TypeProv);
assertNotNull(proxyObj3TypeProv);
} }
public boolean dispose() @Test public void test_dispose() throws Exception
{ {
logger.log(Level.INFO, "Testing ComponentBase"); logger.log(Level.INFO, "Testing ComponentBase: test_dispose()");
ComponentBase comp= new ComponentBase(); ComponentBase comp= new ComponentBase();
boolean r[]= new boolean[50];
int i= 0;
logger.log(Level.FINE, "addEventListener"); logger.log(Level.FINE, "addEventListener");
comp.addEventListener(obj1); comp.addEventListener(obj1);
@@ -68,73 +78,35 @@ public class ComponentBase_Test
obj3.nDisposingCalled = 0; obj3.nDisposingCalled = 0;
comp.dispose(); comp.dispose();
r[i++]= obj1.nDisposingCalled == 1; assertEquals(obj1.nDisposingCalled, 1);
r[i++]= obj2.nDisposingCalled == 1; assertEquals(obj2.nDisposingCalled, 1);
r[i++]= obj3.nDisposingCalled == 1; assertEquals(obj3.nDisposingCalled, 1);
logger.log(Level.FINE, "Adding a listener after dispose, causes a immediate call to the listerner."); logger.log(Level.FINE, "Adding a listener after dispose, causes a immediate call to the listerner.");
obj1.nDisposingCalled= 0; obj1.nDisposingCalled= 0;
comp.addEventListener(obj1); comp.addEventListener(obj1);
r[i++]= obj1.nDisposingCalled == 1; assertEquals(obj1.nDisposingCalled, 1);
logger.log(Level.FINE, "Calling dispose again must not notify the listeners again."); logger.log(Level.FINE, "Calling dispose again must not notify the listeners again.");
obj1.nDisposingCalled= 0; obj1.nDisposingCalled= 0;
obj2.nDisposingCalled= 0; obj2.nDisposingCalled= 0;
obj3.nDisposingCalled= 0; obj3.nDisposingCalled= 0;
comp.dispose(); // already disposed; comp.dispose(); // already disposed;
r[i++]= obj1.nDisposingCalled == 0; assertEquals(obj1.nDisposingCalled, 0);
boolean bOk= true;
for (int c= 0; c < i; c++)
bOk= bOk && r[c];
logger.log(Level.INFO, bOk ? "Ok" : "Failed");
return bOk;
} }
public boolean test_finalize() @Test public void test_finalize() throws Exception
{ {
logger.log(Level.INFO, "Testing ComponentBase"); logger.log(Level.INFO, "Testing ComponentBase: test_finalize()");
ComponentBase comp= new ComponentBase(); ComponentBase comp= new ComponentBase();
boolean r[]= new boolean[50];
int i= 0;
obj1.nDisposingCalled = 0; obj1.nDisposingCalled = 0;
comp.addEventListener(obj1); comp.addEventListener(obj1);
comp= null; comp= null;
logger.log(Level.FINE, "Waiting 10s"); System.gc();
for(int c= 0; c < 100; c++) System.runFinalization();
{ logger.log(Level.FINE, "Waiting 51ms (-XX:MaxGCPauseMillis=50)");
try Thread.sleep(51);
{ assertEquals(obj1.nDisposingCalled, 1);
Thread.sleep(100);
System.gc();
System.runFinalization();
}catch (InterruptedException ie)
{
}
}
r[i++]= obj1.nDisposingCalled == 1;
boolean bOk= true;
for (int c= 0; c < i; c++)
bOk= bOk && r[c];
logger.log(Level.INFO, bOk ? "Ok" : "Failed");
return bOk;
}
public static void main(String[] args)
{
ComponentBase_Test test= new ComponentBase_Test();
boolean r[]= new boolean[50];
int i= 0;
r[i++]= test.dispose();
r[i++]= test.test_finalize();
boolean bOk= true;
for (int c= 0; c < i; c++)
bOk= bOk && r[c];
logger.log(Level.INFO, bOk ? "No errors." : "Errors occurred!");
System.exit( bOk ? 0: -1 );
} }
} }