Search in sources :

Example 1 with Functor

use of org.apache.jorphan.reflect.Functor in project jmeter by apache.

the class CSVSaveService method getSampleSaveConfiguration.

/**
 * Parse a CSV header line
 *
 * @param headerLine
 *            from CSV file
 * @param filename
 *            name of file (for log message only)
 * @return config corresponding to the header items found or null if not a
 *         header line
 */
public static SampleSaveConfiguration getSampleSaveConfiguration(String headerLine, String filename) {
    // Try
    String[] parts = splitHeader(headerLine, _saveConfig.getDelimiter());
    // default
    // delimiter
    String delim = null;
    if (parts == null) {
        Perl5Matcher matcher = JMeterUtils.getMatcher();
        PatternMatcherInput input = new PatternMatcherInput(headerLine);
        Pattern pattern = JMeterUtils.getPatternCache().getPattern(// $NON-NLS-1$
        "\\w+((\\W)\\w+)?(\\2\\w+)*(\\2\"\\w+\")*", // last entries may be quoted strings
        Perl5Compiler.READ_ONLY_MASK);
        if (matcher.matches(input, pattern)) {
            delim = matcher.getMatch().group(2);
            // now validate the
            parts = splitHeader(headerLine, delim);
        // result
        }
    }
    if (parts == null) {
        // failed to recognise the header
        return null;
    }
    // We know the column names all exist, so create the config
    SampleSaveConfiguration saveConfig = new SampleSaveConfiguration(false);
    int varCount = 0;
    for (String label : parts) {
        if (isVariableName(label)) {
            varCount++;
        } else {
            Functor set = headerLabelMethods.get(label);
            set.invoke(saveConfig, new Boolean[] { Boolean.TRUE });
        }
    }
    if (delim != null) {
        if (log.isWarnEnabled()) {
            log.warn("Default delimiter '{}' did not work; using alternate '{}' for reading {}", _saveConfig.getDelimiter(), delim, filename);
        }
        saveConfig.setDelimiter(delim);
    }
    saveConfig.setVarCount(varCount);
    return saveConfig;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) SampleSaveConfiguration(org.apache.jmeter.samplers.SampleSaveConfiguration) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) Functor(org.apache.jorphan.reflect.Functor)

Example 2 with Functor

use of org.apache.jorphan.reflect.Functor in project jmeter by apache.

the class ObjectTableSorterTest method createModelAndSorter.

@BeforeEach
public void createModelAndSorter() {
    String[] headers = { "key", "value", "object" };
    Functor[] readFunctors = { new Functor("getKey"), new Functor("getValue"), new Functor("getValue") };
    Functor[] writeFunctors = { null, null, null };
    Class<?>[] editorClasses = { String.class, Integer.class, Object.class };
    ObjectTableModel model = new ObjectTableModel(headers, readFunctors, writeFunctors, editorClasses);
    sorter = new ObjectTableSorter(model);
    List<Map.Entry<String, Integer>> data = asList(b2(), a3(), d4(), c1());
    data.forEach(model::addRow);
}
Also used : SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) Functor(org.apache.jorphan.reflect.Functor) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with Functor

use of org.apache.jorphan.reflect.Functor in project jmeter by apache.

the class ObjectTableModel method setValueAt.

/**
 * {@inheritDoc}
 */
@Override
public void setValueAt(Object cellValue, int row, int col) {
    if (row < objects.size()) {
        Object value = objects.get(row);
        if (col < writeFunctors.size()) {
            Functor setMethod = writeFunctors.get(col);
            if (setMethod != null) {
                setMethod.invoke(value, new Object[] { cellValue });
                super.fireTableDataChanged();
            }
        } else if (headers.size() == 1) {
            objects.set(row, cellValue);
        }
    }
}
Also used : Functor(org.apache.jorphan.reflect.Functor)

Example 4 with Functor

use of org.apache.jorphan.reflect.Functor in project jmeter by apache.

the class ObjectTableModel method checkFunctors.

/**
 * Check all registered functors.
 * <p>
 * <b>** only for use in unit test code **</b>
 * </p>
 *
 * @param _value - an instance of the table model row data item
 * (if null, use the class passed to the constructor).
 *
 * @param caller - class of caller.
 *
 * @return false if at least one Functor cannot be found.
 */
@SuppressWarnings("deprecation")
public boolean checkFunctors(Object _value, Class<?> caller) {
    Object value;
    if (_value == null && objectClass != null) {
        try {
            value = objectClass.getDeclaredConstructor().newInstance();
        } catch (ReflectiveOperationException e) {
            log.error("Cannot create instance of class {}", objectClass.getName(), e);
            return false;
        }
    } else {
        value = _value;
    }
    boolean status = true;
    for (int i = 0; i < getColumnCount(); i++) {
        Functor setMethod = writeFunctors.get(i);
        if (setMethod != null && !setMethod.checkMethod(value, getColumnClass(i))) {
            status = false;
            log.warn("{} is attempting to use nonexistent {}", caller.getName(), setMethod);
        }
        Functor getMethod = readFunctors.get(i);
        if (getMethod != null && !getMethod.checkMethod(value)) {
            status = false;
            log.warn("{} is attempting to use nonexistent {}", caller.getName(), getMethod);
        }
    }
    return status;
}
Also used : Functor(org.apache.jorphan.reflect.Functor)

Example 5 with Functor

use of org.apache.jorphan.reflect.Functor in project jmeter by apache.

the class ObjectTableModel method getValueAt.

/**
 * {@inheritDoc}
 */
@Override
public Object getValueAt(int row, int col) {
    log.debug("Getting row value");
    Object value = objects.get(row);
    if (headers.size() == 1 && col >= readFunctors.size()) {
        return value;
    }
    Functor getMethod = readFunctors.get(col);
    if (getMethod != null && value != null) {
        return getMethod.invoke(value);
    }
    return null;
}
Also used : Functor(org.apache.jorphan.reflect.Functor)

Aggregations

Functor (org.apache.jorphan.reflect.Functor)10 ObjectTableModel (org.apache.jorphan.gui.ObjectTableModel)3 BorderLayout (java.awt.BorderLayout)2 JCheckBox (javax.swing.JCheckBox)2 JPanel (javax.swing.JPanel)2 GridLayout (java.awt.GridLayout)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)1 BorderFactory (javax.swing.BorderFactory)1 Box (javax.swing.Box)1 BoxLayout (javax.swing.BoxLayout)1 JButton (javax.swing.JButton)1 JTable (javax.swing.JTable)1 TableModelEvent (javax.swing.event.TableModelEvent)1 TableModelListener (javax.swing.event.TableModelListener)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Argument (org.apache.jmeter.config.Argument)1 Arguments (org.apache.jmeter.config.Arguments)1