From c23e3819c77bc149ccfd67c3bf50b54340d83cd2 Mon Sep 17 00:00:00 2001 From: Shijie Zhang Date: Fri, 27 May 2016 15:05:01 -0700 Subject: [PATCH] Add unit test for OS SDK OSGi 1. add unit test for OS SDK OSGi 2. refactor unit test for OS SDK jar to reuse resources Signed-off-by: Shijie Zhang --- build/birt-packages/birt-runtime-test/pom.xml | 28 +++-- .../eclipse/birt/sdk/BaseTestTemplate.java | 110 ++++++++++++++++++ .../org/eclipse/birt/sdk/RuntimeOSGiTest.java | 31 +++++ .../org/eclipse/birt/sdk/RuntimeTest.java | 101 ++-------------- 4 files changed, 170 insertions(+), 100 deletions(-) create mode 100644 build/birt-packages/birt-runtime-test/src/test/java/org/eclipse/birt/sdk/BaseTestTemplate.java create mode 100644 build/birt-packages/birt-runtime-test/src/test/java/org/eclipse/birt/sdk/RuntimeOSGiTest.java diff --git a/build/birt-packages/birt-runtime-test/pom.xml b/build/birt-packages/birt-runtime-test/pom.xml index 7bb9c9d7509..68fd6ec2f24 100644 --- a/build/birt-packages/birt-runtime-test/pom.xml +++ b/build/birt-packages/birt-runtime-test/pom.xml @@ -36,16 +36,24 @@ unpack - - - org.eclipse.birt - birt-runtime - 4.6.0-SNAPSHOT - zip - true - - - ${project.build.directory}/birt-runtime + + + org.eclipse.birt + birt-runtime + 4.6.0-SNAPSHOT + zip + true + ${project.build.directory}/birt-runtime + + + org.eclipse.birt + birt-runtime-osgi + 4.6.0-SNAPSHOT + zip + true + ${project.build.directory}/birt-runtime-osgi + + true true diff --git a/build/birt-packages/birt-runtime-test/src/test/java/org/eclipse/birt/sdk/BaseTestTemplate.java b/build/birt-packages/birt-runtime-test/src/test/java/org/eclipse/birt/sdk/BaseTestTemplate.java new file mode 100644 index 00000000000..e65a43742b6 --- /dev/null +++ b/build/birt-packages/birt-runtime-test/src/test/java/org/eclipse/birt/sdk/BaseTestTemplate.java @@ -0,0 +1,110 @@ +/******************************************************************************* + * Copyright (c) 2013 Actuate Corporation. + * All rights reserved. + *******************************************************************************/ +package org.eclipse.birt.sdk; + +import java.io.File; +import java.io.FilenameFilter; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.junit.Assert; +import org.junit.Test; + +/** + * + */ + +public abstract class BaseTestTemplate +{ + + protected Class mainClass; + protected ClassLoader loader; + + @Test + public void testMain( ) throws Exception + { + String output = "./target/output.html"; + new File( output ).delete( ); + int result = run( new String[]{"-o", output, "-m", "RunAndRender", + "./target/birt-runtime/ReportEngine/samples/hello_world.rptdesign"} ); + Assert.assertEquals( 0, result ); + Assert.assertTrue( new File( output ).exists( ) ); + Assert.assertTrue( new String( Files.readAllBytes( Paths.get( output ) ), StandardCharsets.UTF_8 ) + .contains( "If you can see this report, it means that the BIRT viewer is installed correctly." ) ); + } + + @Test + public void testTable( ) throws Exception + { + String output = "./target/table.html"; + new File( output ).delete( ); + int result = run( new String[]{"-o", output, "-m", "RunAndRender", "./src/test/resources/table.rptdesign"} ); + Assert.assertEquals( 0, result ); + Assert.assertTrue( new File( output ).exists( ) ); + //USA's customer count is 36 + Assert.assertTrue( + new String( Files.readAllBytes( Paths.get( output ) ), StandardCharsets.UTF_8 ).contains( "36" ) ); + } + + @Test + public void testXtab( ) throws Exception + { + String output = "./target/xtab.html"; + new File( output ).delete( ); + int result = run( new String[]{"-o", output, "-m", "RunAndRender", "./src/test/resources/xtab.rptdesign"} ); + Assert.assertEquals( 0, result ); + Assert.assertTrue( new File( output ).exists( ) ); + //USA's customer count is 36 + Assert.assertTrue( + new String( Files.readAllBytes( Paths.get( output ) ), StandardCharsets.UTF_8 ).contains( "36" ) ); + } + + @Test + public void testChart( ) throws Exception + { + String output = "./target/chart.html"; + new File( output ).delete( ); + int result = run( new String[]{"-o", output, "-m", "RunAndRender", "./src/test/resources/chart.rptdesign"} ); + Assert.assertEquals( 0, result ); + Assert.assertTrue( new File( output ).exists( ) ); + //there is a svg image output as type="image/svg+xml" + Assert.assertTrue( new String( Files.readAllBytes( Paths.get( output ) ), StandardCharsets.UTF_8 ) + .contains( "image/svg+xml" ) ); + } + + protected File[] listJars( String folder ) + { + return new File( folder ).listFiles( new FilenameFilter( ) { + + @Override + public boolean accept( File dir, String name ) + { + if ( name.endsWith( ".jar" ) ) + { + return true; + } + return false; + } + + } ); + } + + protected ClassLoader createClassLoader( String folder ) throws MalformedURLException + { + File[] jarFiles = listJars( folder ); + URL[] urls = new URL[jarFiles.length]; + for ( int i = 0; i < jarFiles.length; i++ ) + { + urls[i] = jarFiles[i].toURI( ).toURL( ); + } + return new URLClassLoader( urls ); + } + + public abstract int run( String[] args ) throws Exception; +} diff --git a/build/birt-packages/birt-runtime-test/src/test/java/org/eclipse/birt/sdk/RuntimeOSGiTest.java b/build/birt-packages/birt-runtime-test/src/test/java/org/eclipse/birt/sdk/RuntimeOSGiTest.java new file mode 100644 index 00000000000..c0f55d8ca3e --- /dev/null +++ b/build/birt-packages/birt-runtime-test/src/test/java/org/eclipse/birt/sdk/RuntimeOSGiTest.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2013 Actuate Corporation. + * All rights reserved. + *******************************************************************************/ +package org.eclipse.birt.sdk; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +/** + * + */ + +public class RuntimeOSGiTest extends BaseTestTemplate +{ + public int run( String[] args ) throws Exception + { + if ( mainClass == null ) + { + System.setProperty( "BIRT_HOME", new File("./target/birt-runtime-osgi/ReportEngine").getAbsolutePath() ); + loader = createClassLoader( "./target/birt-runtime-osgi/ReportEngine/lib" ); //$NON-NLS-1$ + mainClass = loader.loadClass( "org.eclipse.birt.report.engine.api.ReportRunner" ); //$NON-NLS-1$ + } + Constructor constructor = mainClass.getConstructor( String[].class ); + Object runner = constructor.newInstance( new Object[]{args} ); + Method execute = mainClass.getMethod( "execute", null ); + Object result = execute.invoke( runner, null ); + return ( (Integer) result ).intValue( ); + } +} diff --git a/build/birt-packages/birt-runtime-test/src/test/java/org/eclipse/birt/sdk/RuntimeTest.java b/build/birt-packages/birt-runtime-test/src/test/java/org/eclipse/birt/sdk/RuntimeTest.java index c741a238933..3a73678b5de 100644 --- a/build/birt-packages/birt-runtime-test/src/test/java/org/eclipse/birt/sdk/RuntimeTest.java +++ b/build/birt-packages/birt-runtime-test/src/test/java/org/eclipse/birt/sdk/RuntimeTest.java @@ -13,104 +13,25 @@ import java.nio.file.Paths; import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; -public class RuntimeTest +public class RuntimeTest extends BaseTestTemplate { - - private Class mainClass; - + public int run( String[] args ) throws Exception - { - if ( mainClass == null ) - { - ClassLoader loader = createClassLoader( "./target/birt-runtime/ReportEngine/lib" ); - mainClass = loader.loadClass( "org.eclipse.birt.report.engine.api.ReportRunner" ); - } + { + if ( mainClass == null ) + { + System.clearProperty( "BIRT_HOME" ); + loader = createClassLoader( "./target/birt-runtime/ReportEngine/lib" ); //$NON-NLS-1$ + mainClass = loader.loadClass( "org.eclipse.birt.report.engine.api.ReportRunner" ); //$NON-NLS-1$ + } Constructor constructor = mainClass.getConstructor( String[].class ); Object runner = constructor.newInstance( new Object[]{args} ); - Method execute = mainClass.getMethod( "execute", null ); + Method execute = mainClass.getMethod( "execute", null ); Object result = execute.invoke( runner, null ); return ( (Integer) result ).intValue( ); } - @Test - public void testMain( ) throws Exception - { - String output = "./target/output.html"; - new File( output ).delete( ); - int result = run( new String[]{"-o", output, "-m", "RunAndRender", - "./target/birt-runtime/ReportEngine/samples/hello_world.rptdesign"} ); - Assert.assertEquals( 0, result ); - Assert.assertTrue( new File( output ).exists( ) ); - Assert.assertTrue( new String( Files.readAllBytes( Paths.get( output ) ), StandardCharsets.UTF_8 ) - .contains( "If you can see this report, it means that the BIRT viewer is installed correctly." ) ); - } - - @Test - public void testTable( ) throws Exception - { - String output = "./target/table.html"; - new File( output ).delete( ); - int result = run( new String[]{"-o", output, "-m", "RunAndRender", "./src/test/resources/table.rptdesign"} ); - Assert.assertEquals( 0, result ); - Assert.assertTrue( new File( output ).exists( ) ); - //USA's customer count is 36 - Assert.assertTrue( - new String( Files.readAllBytes( Paths.get( output ) ), StandardCharsets.UTF_8 ).contains( "36" ) ); - } - - @Test - public void testXtab( ) throws Exception - { - String output = "./target/xtab.html"; - new File( output ).delete( ); - int result = run( new String[]{"-o", output, "-m", "RunAndRender", "./src/test/resources/xtab.rptdesign"} ); - Assert.assertEquals( 0, result ); - Assert.assertTrue( new File( output ).exists( ) ); - //USA's customer count is 36 - Assert.assertTrue( - new String( Files.readAllBytes( Paths.get( output ) ), StandardCharsets.UTF_8 ).contains( "36" ) ); - } - - @Test - public void testChart( ) throws Exception - { - String output = "./target/chart.html"; - new File( output ).delete( ); - int result = run( new String[]{"-o", output, "-m", "RunAndRender", "./src/test/resources/chart.rptdesign"} ); - Assert.assertEquals( 0, result ); - Assert.assertTrue( new File( output ).exists( ) ); - //there is a svg image output as type="image/svg+xml" - Assert.assertTrue( new String( Files.readAllBytes( Paths.get( output ) ), StandardCharsets.UTF_8 ) - .contains( "image/svg+xml" ) ); - } - - private File[] listJars( String folder ) - { - return new File( folder ).listFiles( new FilenameFilter( ) { - - @Override - public boolean accept( File dir, String name ) - { - if ( name.endsWith( ".jar" ) ) - { - return true; - } - return false; - } - - } ); - } - - private ClassLoader createClassLoader( String folder ) throws MalformedURLException - { - File[] jarFiles = listJars( folder ); - URL[] urls = new URL[jarFiles.length]; - for ( int i = 0; i < jarFiles.length; i++ ) - { - urls[i] = jarFiles[i].toURI( ).toURL( ); - } - return new URLClassLoader( urls ); - } }