/* * 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/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ package complex.filter.detection.typeDetection; import com.sun.star.beans.PropertyValue; import com.sun.star.document.XTypeDetection; import com.sun.star.io.NotConnectedException; import com.sun.star.io.XInputStream; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.XInterface; import complexlib.ComplexTestCase; import java.io.File; import java.util.Iterator; import java.util.ArrayList; import util.utils; /** Check "TypeDetection" *
* This test will check the file type detection. This will be done by filling
* properties of a
*
*
*
*
*
*
* All these files will be copied by make file beside of
* MediaDescriptor
.
*
* In the test method checkByURLonly
the
* MediaDescriptor
was filled at once with the URL of a test
* file. At second it was filled with a XInputStream
from test
* file. In both subtests the returned file type must match with an expected
* type.
*
* In the test method checkPreselectedType
the
* MediaDescriptor
was filled with the URL of the test file and
* with the name of a type which should be used. The returned type of the
* TypeDetection
must match with an expected type.
*
* In the test method
checkPreselectedFilter
the
* MediaDescriptor
was filled with the URL of the test file and
* with the name of a filter which should be used. The returned type of the
* TypeDetection
must match with an expected type.
*
* In the test method
checkPreselectedDocService
the
* MediaDescriptor
was filled with the URL of the test file and
* with the name of a document service which should be used. The returned type
* of the TypeDetection
must match with an expected type.
*
*
* To get information which test file should support which type, filter and
* document service, this information was collect from configuration files:
*
*
*
* At fist there will be the typeDetection.props
typeDetection.props
. Here the following
* properties should be set (with example values):
*
* TestDocumentPath=file:///path/to/my/testdocuments
* placeHolder=%
* %SO6productname=StarOffice
* %SO6formatversion=6.0
*
* TestDocumentPath
: this is the path to your test documents. If
* you have grouped your documents ie. by writer, calc, ... then it should be
* the root path. To specify the particular sub folders you have to use
* csv.files
* files.csv
: In this file all test documents are listed.
* Syntax: fileAlias;fileURL;defaultURLFileType;StreamFileTypes
* Example:
*
* Writer6_1;Writer/Writer6.sxw;writer_StarOffice_XML_Writer;writer_StarOffice_XML_Writer
* text1;Writer/Text.txt:generic_Text:generic_Text
*
* The first example shows you the following:
* Writer6_1
is a free chosen name
* Writer/Writer6.sxw
is the document path. This will be assembled
* by TestDocumentPath
from typeDetection.props
.
* writer_StarOffice_XML_Writer
: this is the default file type of
* this file
*
* The second example displays two document types for
* XInputStream
(generic_Text
). This
* two document types are listed by a colon ':' as separator.
* This is needed because XInputStream can detect a text file as
* generic_Text.
*
*
* In this file you can choose a special
* filter to detect the document. This make sense ie. for csv-files: You can
* open csv files as Writer or as Calc. To check this case you have to specify
* in preselectedFilter.csv
csv.files
a fileAlias like ?csv_writer? and ?csv_calc? with
* the same fileURL and it's specific defaultFileType.
* The returned file type by TypeDetection
must equal to
* correspond defaultFileType
from csv.files
*
* Syntax: fileAlias;FilterName;FilterOptions;FilterData
* Example: Writer6_1;%SO6productname %SO6formatversion Textdokument;;
*
* The example shows the following:
* Writer6_1
is the same as in csv.files
* %SO6productname %SO6formatversion Textdokument
is the filter
* name which should be used. Here we have a special: %SO6productname
* %SO6formatversion
will be replaced by the equals of
* typeDetection.props
. The filter names depends on the Office
* name and version. So a future Office could called ?StarSuite 8?.
* FilterOptions
is not relevant for this filter. But ie. for csv
* filter this entry could be used to specify the separator of the csv file.
* FilterData
if filter needs some FilterData arguments you can
* specify it here
*
*
*
* In this file you can preselect the type
* preselectedType.csv
TypeDetection
should use.
* The returned file type by TypeDetection
must equal to the
* preselected file type.
* Note: If you try to use invalid types you will get a failed test because
* TypeDetection
tries to find out the type itself.
*
* Syntax: fileAlias;fileType
* Example: Writer6_1;writer_StarOffice_XML_Writer
*
* This example shows the following:
* Writer6_1
is the same as in csv.files
* writer_StarOffice_XML_Writer
is the file type which was used as
* parameter in MediaDescriptor
. This type must be returned from
* TypeDetection
*
*
*
In this file you can preselect a service name
* to detect the file type. The returned file type by
* serviceName.csv
TypeDetection
must equal to correspond
* defaultFileType
from csv.files
*
* Syntax: fileAlias;serviceName
* Example: Writer6_1;com.sun.star.text.FormatDetector
*
* This example shows the following:
* Writer6_1
is the same as in csv.files
* com.sun.star.text.FormatDetector
is the service name which was
* used as parameter in MediaDescriptor
.
*
*
* typeDetection.class
.
* @see com.sun.star.document.XTypeDetection
* @see com.sun.star.document.MediaDescriptor
*/
public class TypeDetection extends ComplexTestCase {
/**
* @member m_xDetection the object to test
* @member helper instacne of helper class
*/
static XTypeDetection m_xDetection;
static Helper helper = null;
/**
* A function to tell the framework, which test functions are available.
* @return All test methods.
*/
@Override
public String[] getTestMethodNames() {
return new String[]{"checkByURLonly",
"checkPreselectedType",
"checkPreselectedFilter",
"checkPreselectedDocService",
"checkStreamLoader",
"checkStreamLoader"};
}
/** Create the environment for following tests.
* Use either a component loader from desktop or
* from frame
* @throws Exception Exception
*/
public void before() throws Exception {
// create TypeDetection
XMultiServiceFactory xMSF = param.getMSF();
assure("Could not get XMultiServiceFactory", xMSF != null);
Object oInterface = xMSF.createInstance(
"com.sun.star.document.TypeDetection");
if (oInterface == null) {
failed("Service wasn't created") ;
}
XInterface oObj = (XInterface) oInterface ;
log.println("ImplName: "+utils.getImplName(oObj));
m_xDetection = UnoRuntime.queryInterface(XTypeDetection.class, oInterface);
IteratorMediaDescriptor
was filled with the URL of a file. The
* type
of the file is kown and must be returned by
* MediaDescriptor
*
* Syntax of files.csv:
* fileAlias;fileURL;fileType
*
*/
public void checkByURLonly() {
try{
log.println("### checkByURLonly() ###");
ArrayListTypeDedection
by URL the MediaDescriptor
* was filled at fist with the URL only, at second with XInputStream
* only. The TypeDetection
must return the expected value
* @param fileAlias the alias name of the test file
* @param fileURL the URL of the test file
* @param fileType the expected type of the test file
* @see com.sun.star.document.MediaDescriptor
*/
private void checkMediaDescriptorURL(
String fileAlias, String fileURL, String fileType){
PropertyValue[] MediaDescriptor = helper.createMediaDescriptor(
new String[] {"URL"},
new Object[] {fileURL});
log.println("check only by URL...");
String type = m_xDetection.queryTypeByDescriptor(
helper.createInOutPropertyValue(MediaDescriptor), true);
boolean fileTypeOK = helper.checkFileType(type, fileType);
assure("\nURL-test : " + fileAlias + ":\n\treturned type: '" + type +
"'\n\texpected type: '" + fileType + "'",fileTypeOK ,true);
}
/** Filles a MediaDescriptor with a XInputStream
of the test
* file given by URL.
* Then the MediaDescriptor was used as parameter for TypeDetection.
* The TypeDetection must return expected type
* @param fileAlias the alias name of the test file
* @param fileURL the URL of the test file
* @param fileType the expected type of the test file
* @see com.sun.star.document.MediaDescriptor
* @see com.sun.star.io.XInputStream
*/
private void checkMediaDescriptorXInputStream(
String fileAlias, String fileURL, String fileType){
XInputStream xStream = null;
try{
xStream = helper.getFileStream( fileURL );
} catch (NotConnectedException e) {
failed("Could not get XInputStream from file :'" + fileURL + "'",true);
return;
}
PropertyValue[] MediaDescriptor = helper.createMediaDescriptor(
new String[] {"InputStream"},
new Object[] {xStream});
log.println("check only by XInputStream...");
String type = m_xDetection.queryTypeByDescriptor(
helper.createInOutPropertyValue(MediaDescriptor), true);
boolean fileTypeOK = helper.checkFileType(type, fileType);
assure("\nXInputStream-test: " + fileAlias + ":\n\treturned type: '" + type +
"'\n\texpected type: '" + fileType + "'", fileTypeOK, true);
}
/**
* The MediaDescriptor
was filled with the URL of a file. The
* type
of the file is kown and must be returned by
* MediaDescriptor
*
* Syntax of files.csv:
* fileAlias;fileURL;fileType
*
*/
public void checkPreselectedType() {
try{
log.println("### checkPreselectedType() ###");
ArrayList