View | Details | Raw Unified | Return to bug 51988
Collapse All | Expand All

(-)CSVDataSet.java (-49 / +49 lines)
Lines 100-173 Link Here
100
    public void iterationStart(LoopIterationEvent iterEvent) {
102
    public void iterationStart(LoopIterationEvent iterEvent) {
101
        FileServer server = FileServer.getFileServer();
103
        FileServer server = FileServer.getFileServer();
102
        final JMeterContext context = getThreadContext();
104
        final JMeterContext context = getThreadContext();
105
        String delim = getDelimiter();
106
        if (delim.startsWith("\\t")) { // $NON-NLS-1$
107
            delim = "\t";// Make it easier to enter a Tab // $NON-NLS-1$
108
        } else if (delim.length() == 0) {
109
            log.warn("Empty delimiter converted to ','");
110
            delim = ",";
111
        }
103
        if (vars == null) {
112
        if (vars == null) {
104
            String _fileName = getFilename();
113
            String _fileName = getFilename();
105
            String mode = getShareMode();
114
            String mode = getShareMode();
106
            int modeInt = CSVDataSetBeanInfo.getShareModeAsInt(mode);
115
            int modeInt = CSVDataSetBeanInfo.getShareModeAsInt(mode);
107
            switch(modeInt){
116
            switch (modeInt) {
108
                case CSVDataSetBeanInfo.SHARE_ALL:
117
            case CSVDataSetBeanInfo.SHARE_ALL:
109
                    alias = _fileName;
118
                alias = _fileName;
110
                    break;
119
                break;
111
                case CSVDataSetBeanInfo.SHARE_GROUP:
120
            case CSVDataSetBeanInfo.SHARE_GROUP:
112
                    alias = _fileName+"@"+System.identityHashCode(context.getThreadGroup());
121
                alias = _fileName + "@" + System.identityHashCode(context.getThreadGroup());
113
                    break;
122
                break;
114
                case CSVDataSetBeanInfo.SHARE_THREAD:
123
            case CSVDataSetBeanInfo.SHARE_THREAD:
115
                    alias = _fileName+"@"+System.identityHashCode(context.getThread());
124
                alias = _fileName + "@" + System.identityHashCode(context.getThread());
116
                    break;
125
                break;
117
                default:
126
            default:
118
                    alias = _fileName+"@"+mode; // user-specified key
127
                alias = _fileName + "@" + mode; // user-specified key
119
                    break;
128
                break;
120
            }
129
            }
121
            final String names = getVariableNames();
130
            final String names = getVariableNames();
122
            if (names == null || names.length()==0) {
131
            if (names == null || names.length() == 0) {
123
                String header = server.reserveFile(_fileName, getFileEncoding(), alias, true);
132
                String header = server.reserveFile(_fileName, getFileEncoding(), alias, true);
124
                try {
133
                try {
125
                    vars = CSVSaveService.csvSplitString(header, getDelimiter().charAt(0));
134
                    vars = CSVSaveService.csvSplitString(header, delim.charAt(0));
126
                    firstLineIsNames = true;
135
                    firstLineIsNames = true;
127
                } catch (IOException e) {
136
                } catch (IOException e) {
128
                    log.warn("Could not split CSV header line",e);
137
                    log.warn("Could not split CSV header line", e);
129
                }
138
                }
130
            } else {
139
            } else {
131
                server.reserveFile(_fileName, getFileEncoding(), alias);
140
                server.reserveFile(_fileName, getFileEncoding(), alias);
132
                vars = JOrphanUtils.split(names, ","); // $NON-NLS-1$
141
                vars = JOrphanUtils.split(names, ","); // $NON-NLS-1$
133
            }
142
            }
134
        }
143
        }
135
            String delim = getDelimiter();
144
        // TODO: fetch this once as per vars above?
136
            if (delim.equals("\\t")) { // $NON-NLS-1$
145
        JMeterVariables threadVars = context.getVariables();
137
                delim = "\t";// Make it easier to enter a Tab // $NON-NLS-1$
146
        String line = null;
138
            } else if (delim.length()==0){
147
        try {
139
                log.warn("Empty delimiter converted to ','");
148
            line = server.readLine(alias, getRecycle(), firstLineIsNames);
140
                delim=",";
149
        } catch (IOException e) { // treat the same as EOF
141
            }
150
            log.error(e.toString());
142
            // TODO: fetch this once as per vars above?
151
        }
143
            JMeterVariables threadVars = context.getVariables();
152
        if (line != null) {// i.e. not EOF
144
            String line = null;
145
            try {
153
            try {
146
                line = server.readLine(alias, getRecycle(), firstLineIsNames);
154
                String[] lineValues = getQuotedData() ?
147
            } catch (IOException e) { // treat the same as EOF
155
                        CSVSaveService.csvSplitString(line, delim.charAt(0))
148
                log.error(e.toString());
156
                        : JOrphanUtils.split(line, delim, false);
149
            }
157
                for (int a = 0; a < vars.length && a < lineValues.length; a++) {
150
            if (line!=null) {// i.e. not EOF
158
                    threadVars.put(vars[a], lineValues[a]);
151
                try {
152
                    String[] lineValues = getQuotedData() ?
153
                            CSVSaveService.csvSplitString(line, delim.charAt(0))
154
                            : JOrphanUtils.split(line, delim, false);
155
                            for (int a = 0; a < vars.length && a < lineValues.length; a++) {
156
                                threadVars.put(vars[a], lineValues[a]);
157
                            }
158
                } catch (IOException e) { // Should only happen for quoting errors
159
                   log.error("Unexpected error splitting '"+line+"' on '"+delim.charAt(0)+"'");
160
                }
159
                }
161
                // TODO - report unused columns?
160
            } catch (IOException e) { // Should only happen for quoting errors
162
                // TODO - provide option to set unused variables ?
161
                log.error("Unexpected error splitting '" + line + "' on '" + delim.charAt(0) + "'");
163
            } else {
164
                if (getStopThread()) {
165
                    throw new JMeterStopThreadException("End of file detected");
166
                }
167
                for (int a = 0; a < vars.length ; a++) {
168
                    threadVars.put(vars[a], EOFVALUE);
169
                }
170
            }
162
            }
163
            // TODO - report unused columns?
164
            // TODO - provide option to set unused variables ?
165
        } else {
166
            if (getStopThread()) {
167
                throw new JMeterStopThreadException("End of file detected");
168
            }
169
            for (int a = 0; a < vars.length ; a++) {
170
                threadVars.put(vars[a], EOFVALUE);
171
            }
172
        }
171
    }
173
    }
172
174
173
    /**
175
    /**

Return to bug 51988