diff options
Diffstat (limited to 'src/org/openoffice/tools/tests/TypeManagerTest.java')
-rw-r--r-- | src/org/openoffice/tools/tests/TypeManagerTest.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/org/openoffice/tools/tests/TypeManagerTest.java b/src/org/openoffice/tools/tests/TypeManagerTest.java new file mode 100644 index 0000000..2037c02 --- /dev/null +++ b/src/org/openoffice/tools/tests/TypeManagerTest.java @@ -0,0 +1,120 @@ +package org.openoffice.tools.tests; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileInputStream; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openoffice.tools.types.TypeManager; + +import com.sun.star.container.NoSuchElementException; +import com.sun.star.reflection.XTypeDescription; + +/** + * This test class assumes a <code>ooo.install.dir</code> Java property is set and + * points to the directory of an installed OOo 3.x. + * + * @author cbosdonnat + * + */ +public class TypeManagerTest { + + private static final String SEARCHED_TYPE = "com.sun.star.lang.XTypeProvider"; + private static final String SEARCHED_TYPE_ERR = "some.dummy.type"; + + private static TypeManager sTestedManager; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + String installDir = System.getProperty( "ooo.install.dir" ); + + sTestedManager = new TypeManager( installDir ); + sTestedManager.initRegistries( new String[]{ + getUreRegistryUrl( installDir ) + } ); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + sTestedManager.dispose(); + } + + @Test + public void testSearchTypeValid() { + try { + XTypeDescription result = sTestedManager.searchType( SEARCHED_TYPE ); + assertEquals( "Actual description doesn't match", SEARCHED_TYPE, result.getName() ); + } catch ( NoSuchElementException e ) { + fail( "This element should be found" ); + } + } + + @Test( expected=NoSuchElementException.class ) + public void testSearchTypeIncorrect() throws Exception { + sTestedManager.searchType( SEARCHED_TYPE_ERR ); + } + + private static String getUreRegistryUrl( String pInstallDir ) { + String regUrl = null; + + try { + File install = new File( pInstallDir ); + File basis = getPortableLink( "basis-link", install ); + File ure = getPortableLink( "ure-link", basis ); + + String sep = System.getProperty( "file.separator" ); + File reg = new File( ure, "share" + sep + "misc" + sep + "types.rdb" ); + + if ( reg.canRead() ) { + regUrl = "file://" + reg.toURI().toURL().getPath(); + } + + } catch ( Exception e ) { + } + + return regUrl; + } + + /** + * Get the file object for the link defined as a child of a folder. + * + * On Windows platform, the link relative location is specified as the content of + * a file named after the link name. On Unix-based systems symbolic links are + * supported. + * + * <p>This method has been adapted from the ooeclipse integration code, class + * <code>org.openoffice.ide.eclipse.core.OOo.OOo3PathMapper</code></p> + * + * @param pName the name of the symbolic link + * @param pParent the parent directory file + * + * @return the file representing the link target or <code>null</code> + * + */ + private static File getPortableLink(String pName, File pParent) { + File link = null; + + File linkFile = new File(pParent, pName); + if ( System.getProperty( "os.name" ).toLowerCase().startsWith( "win" ) ) { + // Read the content of the file to get the true folder + try { + FileInputStream is = new FileInputStream(linkFile); + byte[] buf = new byte[is.available()]; + is.read(buf); + + String relativePath = new String(buf); + linkFile = new File(pParent, relativePath); + link = linkFile; + } catch (Exception e) { + // the returned link is null to show the error + } + } else { + link = linkFile; + } + + return link; + } +} |