Skip to content

Commit

Permalink
Add unit test for OS SDK OSGi
Browse files Browse the repository at this point in the history
1. add unit test for OS SDK OSGi
2. refactor unit test for OS SDK jar to reuse resources


Signed-off-by: Shijie Zhang <szhang@opentext.com>
  • Loading branch information
Shijie Zhang committed May 27, 2016
1 parent a34b263 commit c23e381
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 100 deletions.
28 changes: 18 additions & 10 deletions build/birt-packages/birt-runtime-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,24 @@
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.eclipse.birt</groupId>
<artifactId>birt-runtime</artifactId>
<version>4.6.0-SNAPSHOT</version>
<type>zip</type>
<overWrite>true</overWrite>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/birt-runtime</outputDirectory>
<artifactItems>
<artifactItem>
<groupId>org.eclipse.birt</groupId>
<artifactId>birt-runtime</artifactId>
<version>4.6.0-SNAPSHOT</version>
<type>zip</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/birt-runtime</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>org.eclipse.birt</groupId>
<artifactId>birt-runtime-osgi</artifactId>
<version>4.6.0-SNAPSHOT</version>
<type>zip</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/birt-runtime-osgi</outputDirectory>
</artifactItem>
</artifactItems>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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( );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

0 comments on commit c23e381

Please sign in to comment.