diff options
| author | Claire Lee | 2012-04-06 23:17:49 +0000 |
|---|---|---|
| committer | Claire Lee | 2012-04-06 23:17:49 +0000 |
| commit | 7b44869bba1513d5f3ee17bc6af0d65d2f04f686 (patch) | |
| tree | 8aeb2990333f27c204f85ef80d2870d5def6ac28 | |
| parent | 9e1a46513ed323a2d5505f0fe2ad2e9754698fdc (diff) | |
| download | org.eclipse.jubula.core-7b44869bba1513d5f3ee17bc6af0d65d2f04f686.tar.gz org.eclipse.jubula.core-7b44869bba1513d5f3ee17bc6af0d65d2f04f686.tar.xz org.eclipse.jubula.core-7b44869bba1513d5f3ee17bc6af0d65d2f04f686.zip | |
bug 372177: add support for Java Properties and CSV files as external
data source
Change-Id: If0b38643f6f8b27696bee62eb8a7e51d1265db85
9 files changed, 251 insertions, 13 deletions
diff --git a/org.eclipse.jubula.client.core/src/org/eclipse/jubula/client/core/businessprocess/ExternalTestDataBP.java b/org.eclipse.jubula.client.core/src/org/eclipse/jubula/client/core/businessprocess/ExternalTestDataBP.java index ef51fbf20..0550efb06 100644 --- a/org.eclipse.jubula.client.core/src/org/eclipse/jubula/client/core/businessprocess/ExternalTestDataBP.java +++ b/org.eclipse.jubula.client.core/src/org/eclipse/jubula/client/core/businessprocess/ExternalTestDataBP.java @@ -20,9 +20,11 @@ import java.util.Map; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.Validate; +import org.eclipse.jubula.client.core.businessprocess.importfilter.CSVImportFilter; import org.eclipse.jubula.client.core.businessprocess.importfilter.DataTable; import org.eclipse.jubula.client.core.businessprocess.importfilter.ExcelImportFilter; import org.eclipse.jubula.client.core.businessprocess.importfilter.IDataImportFilter; +import org.eclipse.jubula.client.core.businessprocess.importfilter.PropertiesImportFilter; import org.eclipse.jubula.client.core.businessprocess.importfilter.exceptions.DataReadException; import org.eclipse.jubula.client.core.businessprocess.importfilter.exceptions.NoSupportForLocaleException; import org.eclipse.jubula.client.core.i18n.Messages; @@ -83,6 +85,8 @@ public class ExternalTestDataBP { public ExternalTestDataBP() { m_filter = new ArrayList <IDataImportFilter> (); m_filter.add(new ExcelImportFilter()); + m_filter.add(new PropertiesImportFilter()); + m_filter.add(new CSVImportFilter()); } /** diff --git a/org.eclipse.jubula.client.core/src/org/eclipse/jubula/client/core/businessprocess/importfilter/CSVImportFilter.java b/org.eclipse.jubula.client.core/src/org/eclipse/jubula/client/core/businessprocess/importfilter/CSVImportFilter.java new file mode 100644 index 000000000..99fcc5578 --- /dev/null +++ b/org.eclipse.jubula.client.core/src/org/eclipse/jubula/client/core/businessprocess/importfilter/CSVImportFilter.java @@ -0,0 +1,132 @@ +/******************************************************************************* + * Copyright (c) 2012 MontaVista Software, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * MontaVista Software, Inc - Initial implementation of Java Properties file + * + *******************************************************************************/ +package org.eclipse.jubula.client.core.businessprocess.importfilter; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Locale; + +public class CSVImportFilter implements IDataImportFilter { + /** Define supported file extension for CSV file */ + private String[] fileExtensions = { "csv" }; //$NON-NLS-1$ + + public String[] getFileExtensions() { + return fileExtensions; + } + + /** + * parses a *.csv file and returns the data as DataTable structure + * + * @param dataDir + * directory for data files + * @param file + * data source File + * @param locale + * data source locale + * @return filled TestDataManager with new data + * @throws IOException + * error occurred while reading data source + * + */ + public DataTable parse(File dataDir, String file, Locale locale) + throws IOException { + /* + * Determine the path of the csv file and return an opened file + */ + final File dataFile = findDataFile(dataDir, file); + DataTable dataTable = null; + + try { + final BufferedReader reader = new BufferedReader(new FileReader( + dataFile)); + ArrayList<String[]> matrix = new ArrayList<String[]>(); + int numberOfPropertiesKeys = -1; + String strLine; + while ((strLine = reader.readLine()) != null) { + String[] properties = getProperties(strLine); + if (numberOfPropertiesKeys == -1) { + numberOfPropertiesKeys = properties.length; + } + if (properties.length == numberOfPropertiesKeys) + matrix.add(properties); + } + + if (matrix.size() > 0) { + dataTable = new DataTable(matrix.size(), numberOfPropertiesKeys); + for (int i = 0; i < matrix.size(); i++) { + String[] entries = matrix.get(i); + for (int j = 0; j < entries.length; j++) { + dataTable.updateDataEntry(i, j, + stripDoubleQuotes(entries[j])); + } + } + } + } catch (IOException e) { + throw e; + } + return dataTable; + } + + /** + * + * @param entry + * Text to be stripped + * @return text that has double-quote stripped + */ + private String stripDoubleQuotes(String entry) { + String processedStr = entry; + if (entry.startsWith("\"") && entry.endsWith("\"")) { //$NON-NLS-1$ //$NON-NLS-2$ + processedStr = entry.substring(1, entry.length() - 1); + } + return processedStr; + } + + /** + * + * @param line + * Line read from csv file + * @return An array of properties separated by comma + */ + private String[] getProperties(String line) { + String[] propertiesList = null; + propertiesList = line.split(","); //$NON-NLS-1$ + return propertiesList; + } + + /** + * Open a data file for reading + * + * @param dataDir + * the data directory + * @param file + * the filename + * @return an opened FileInputStream for the filename + * @throws FileNotFoundException + * guess when! + */ + private File findDataFile(File dataDir, String file) + throws FileNotFoundException { + File dataFile = new File(file); + File infile; + if (dataFile.isAbsolute()) { + infile = dataFile; + } else { + infile = new File(dataDir, file); + } + return infile; + } + +} diff --git a/org.eclipse.jubula.client.core/src/org/eclipse/jubula/client/core/businessprocess/importfilter/PropertiesImportFilter.java b/org.eclipse.jubula.client.core/src/org/eclipse/jubula/client/core/businessprocess/importfilter/PropertiesImportFilter.java new file mode 100644 index 000000000..e4941b839 --- /dev/null +++ b/org.eclipse.jubula.client.core/src/org/eclipse/jubula/client/core/businessprocess/importfilter/PropertiesImportFilter.java @@ -0,0 +1,102 @@ +/******************************************************************************* + * Copyright (c) 2012 MontaVista Software, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * MontaVista Software, Inc - Initial implementation of Java Properties file + * + *******************************************************************************/ +package org.eclipse.jubula.client.core.businessprocess.importfilter; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Enumeration; +import java.util.Locale; +import java.util.Properties; + + +public class PropertiesImportFilter implements IDataImportFilter { + /** Define supported extensions for properties files **/ + private String[] fileExtensions = { "properties" }; //$NON-NLS-1$ + + public String[] getFileExtensions() { + return fileExtensions; + } + + /** + * parses a *.properties file and returns the data as DataTable structure + * + * @param dataDir + * directory for data files + * @param file + * data source File + * @param locale + * data source locale + * @return filled TestDataManager with new data + * @throws IOException + * error occurred while reading data source + * + */ + public DataTable parse(File dataDir, String file, Locale locale) + throws IOException { + Properties testProperties = new Properties(); + /* Determine the path of the properties file and return an opened + * file */ + final FileInputStream dataFile = findDataFile(dataDir, file); + testProperties.load(dataFile); + + int numberOfPropertiesKeys = testProperties.size(); + /* + * Create a new 2 by y, where y is the number of keys in the properties + * file. + */ + DataTable dataTable = null; + if (numberOfPropertiesKeys > 0){ + dataTable = new DataTable(2, numberOfPropertiesKeys); + /* Insert keys on first row and corresponding value on the + * second role. Key/value pair is one-to-one + */ + Enumeration<Object> keys = testProperties.keys(); + int colIdx = 0; + while (keys.hasMoreElements()) { + String key = (String) keys.nextElement(); + /* Adding key */ + dataTable.updateDataEntry(0, colIdx, key); + /* Adding value */ + dataTable.updateDataEntry(1, colIdx, + testProperties.getProperty(key)); + /* Increment the column index */ + colIdx++; + } + } + return dataTable; + } + + /** + * Open a data file for reading + * + * @param dataDir + * the data directory + * @param file + * the filename + * @return an opened FileInputStream for the filename + * @throws FileNotFoundException + * guess when! + */ + private FileInputStream findDataFile(File dataDir, String file) + throws FileNotFoundException { + File dataFile = new File(file); + File infile; + if (dataFile.isAbsolute()) { + infile = dataFile; + } else { + infile = new File(dataDir, file); + } + return new FileInputStream(infile); + } +} diff --git a/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/controllers/propertysources/SpecTestCaseGUIPropertySource.java b/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/controllers/propertysources/SpecTestCaseGUIPropertySource.java index ebb1341cd..310bf4ff4 100644 --- a/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/controllers/propertysources/SpecTestCaseGUIPropertySource.java +++ b/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/controllers/propertysources/SpecTestCaseGUIPropertySource.java @@ -76,7 +76,7 @@ public class SpecTestCaseGUIPropertySource public static final String P_ELEMENT_DISPLAY_PARAM_LOCKED = Messages.SpecTestCaseGUIPropertySourceLockedParameters; - /** Constant for the String Excel Data File */ + /** Constant for the String External Data File */ public static final String P_ELEMENT_DISPLAY_DATEFILE = Messages.SpecTestCaseGUIPropertySourceTestCaseFileName; @@ -341,9 +341,9 @@ public class SpecTestCaseGUIPropertySource protected static final String DATA_SOURCE_CTDS = "TestDataSource.central"; //$NON-NLS-1$ /** - * <code>DATA_SOURCE_EXCEL</code> + * <code>DATA_SOURCE_EXTERNAL</code> */ - protected static final String DATA_SOURCE_EXCEL = "TestDataSource.excel"; //$NON-NLS-1$ + protected static final String DATA_SOURCE_EXTERNAL = "TestDataSource.external"; //$NON-NLS-1$ /** * <code>DATA_SOURCE_LOCAL</code> @@ -374,7 +374,7 @@ public class SpecTestCaseGUIPropertySource AbstractGuiNodePropertySource s) { super(s); getDataSource().add(DATA_SOURCE_LOCAL); - getDataSource().add(DATA_SOURCE_EXCEL); + getDataSource().add(DATA_SOURCE_EXTERNAL); getDataSource().add(DATA_SOURCE_CTDS); getDataSource().add(DATA_SOURCE_NONE); getDataSource().add(UNKOWN_DATA_SOURCE); @@ -407,7 +407,7 @@ public class SpecTestCaseGUIPropertySource */ protected String getDataSource(IParamNodePO node) { if (!StringUtils.isBlank(node.getDataFile())) { - return DATA_SOURCE_EXCEL; + return DATA_SOURCE_EXTERNAL; } else if (node.getReferencedDataCube() != null) { return DATA_SOURCE_CTDS; } else if (node.getParameterListSize() == 0) { diff --git a/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/i18n/messages.properties b/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/i18n/messages.properties index 87c4e6926..f6700331f 100644 --- a/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/i18n/messages.properties +++ b/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/i18n/messages.properties @@ -787,7 +787,7 @@ SpecTestCaseGUIPropertySourceParameter=Parameter SpecTestCaseGUIPropertySourceParameterName=Parameter Name SpecTestCaseGUIPropertySourceParameterType=Parameter Type SpecTestCaseGUIPropertySourceParameterValue=Parameter Value -SpecTestCaseGUIPropertySourceTestCaseFileName=Excel Data File +SpecTestCaseGUIPropertySourceTestCaseFileName=External Data File SpecTestCaseGUIPropertySourceTestCaseName=Test Case Name SpecTestCaseGUIPropertySourceTestCaseReferencedTestData=Central Test Data Set SpecTestCaseGUIPropertySourceTestdataCategory=Test Data diff --git a/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/provider/labelprovider/ParameterValueLabelProvider.java b/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/provider/labelprovider/ParameterValueLabelProvider.java index 3c1bc265e..7a3e794e6 100644 --- a/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/provider/labelprovider/ParameterValueLabelProvider.java +++ b/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/provider/labelprovider/ParameterValueLabelProvider.java @@ -79,7 +79,7 @@ public class ParameterValueLabelProvider extends LabelProvider } if (StringUtils.isNotEmpty(paramNode.getDataFile())) { - return IconConstants.EXCEL_DATA_IMAGE; + return IconConstants.EXTERNAL_DATA_IMAGE; } String property = controller.getProperty(); diff --git a/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/provider/labelprovider/decorators/ExternalDataDecorator.java b/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/provider/labelprovider/decorators/ExternalDataDecorator.java index a089645fa..2cc3f93c8 100644 --- a/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/provider/labelprovider/decorators/ExternalDataDecorator.java +++ b/org.eclipse.jubula.client.ui.rcp/src/org/eclipse/jubula/client/ui/rcp/provider/labelprovider/decorators/ExternalDataDecorator.java @@ -30,7 +30,7 @@ public class ExternalDataDecorator extends AbstractLightweightLabelDecorator { IParamNodePO pnpo = (IParamNodePO)element; if (!StringUtils.isEmpty(pnpo.getDataFile())) { decoration.addOverlay( - IconConstants.EXCEL_DATA_IMAGE_DESCRIPTOR); + IconConstants.EXTERNAL_DATA_IMAGE_DESCRIPTOR); } else if (pnpo.getReferencedDataCube() != null) { decoration.addOverlay( IconConstants.TDC_DECORATION_IMAGE_DESCRIPTOR); diff --git a/org.eclipse.jubula.client.ui/icons/externalData.gif b/org.eclipse.jubula.client.ui/icons/externalData.gif Binary files differnew file mode 100644 index 000000000..6ec9068e6 --- /dev/null +++ b/org.eclipse.jubula.client.ui/icons/externalData.gif diff --git a/org.eclipse.jubula.client.ui/src/org/eclipse/jubula/client/ui/constants/IconConstants.java b/org.eclipse.jubula.client.ui/src/org/eclipse/jubula/client/ui/constants/IconConstants.java index 39cb10bcb..e3355b97a 100644 --- a/org.eclipse.jubula.client.ui/src/org/eclipse/jubula/client/ui/constants/IconConstants.java +++ b/org.eclipse.jubula.client.ui/src/org/eclipse/jubula/client/ui/constants/IconConstants.java @@ -131,8 +131,8 @@ public class IconConstants { public static final Image OVERWRITTEN_DATA_IMAGE = Plugin.getImage("overwrittenData.gif"); //$NON-NLS-1$ /** complete data imageDescriptor */ public static final ImageDescriptor ERROR_IMAGE_DESCRIPTOR = Plugin.getImageDescriptor("incomplData.gif"); //$NON-NLS-1$ - /** excel data imageDescriptor */ - public static final ImageDescriptor EXCEL_DATA_IMAGE_DESCRIPTOR = Plugin.getImageDescriptor("excelData.gif"); //$NON-NLS-1$ + /** external data imageDescriptor */ + public static final ImageDescriptor EXTERNAL_DATA_IMAGE_DESCRIPTOR = Plugin.getImageDescriptor("externalData.gif"); //$NON-NLS-1$ /** greenDot imageDescriptor */ public static final ImageDescriptor GREEN_DOT_IMAGE_DESCRIPTOR = Plugin.getImageDescriptor("greenDot.gif"); //$NON-NLS-1$ /** redDot imageDescriptor */ @@ -145,9 +145,9 @@ public class IconConstants { public static final Image TECH_NAME_ERROR_IMAGE = Plugin.getImageDescriptor("techNameERR.gif").createImage(); //$NON-NLS-1$ /** yellowDot imageDescriptor */ public static final Image TECH_NAME_WARNING_IMAGE = Plugin.getImageDescriptor("techNameWARN.gif").createImage(); //$NON-NLS-1$ - /** excel data image */ - public static final Image EXCEL_DATA_IMAGE = - EXCEL_DATA_IMAGE_DESCRIPTOR.createImage(); + /** external data image */ + public static final Image EXTERNAL_DATA_IMAGE = + EXTERNAL_DATA_IMAGE_DESCRIPTOR.createImage(); /** new event handler dialog-image */ public static final Image NEW_EH_DIALOG_IMAGE = Plugin.getImage("newEventHandlerDialog.gif"); //$NON-NLS-1$ /** name for class path image */ |
