1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package mod._scripting; 25 26 import java.io.File; 27 import java.io.FileReader; 28 import java.io.BufferedReader; 29 import java.io.IOException; 30 31 import java.util.ArrayList; 32 import java.util.HashMap; 33 import java.util.StringTokenizer; 34 35 import lib.TestEnvironment; 36 import lib.Parameters; 37 38 public class TestDataLoader { 39 TestDataLoader()40 private TestDataLoader() { 41 } 42 setupData(TestEnvironment tEnv, String name)43 public static void setupData(TestEnvironment tEnv, String name) { 44 String filename = 45 util.utils.getFullTestDocName("testdata"); 46 File testdatadir = new File(filename); 47 File f = new File(testdatadir, name + ".csv"); 48 49 if (!f.exists()) 50 return; 51 52 BufferedReader in; 53 54 try { 55 in = new BufferedReader(new FileReader(f)); 56 57 String s, previous, current; 58 ArrayList list = new ArrayList(11); 59 60 if ((s = in.readLine()) != null) { 61 StringTokenizer st = new StringTokenizer(s, ";"); 62 63 current = previous = st.nextToken(); 64 list.add(getParameters(st)); 65 } 66 else { 67 return; 68 } 69 70 while ((s = in.readLine()) != null) { 71 StringTokenizer st = new StringTokenizer(s, ";"); 72 73 current = st.nextToken(); 74 75 if (!current.equals(previous)) { 76 tEnv.addObjRelation(previous, list); 77 previous = current; 78 list = new ArrayList(11); 79 } 80 81 list.add(getParameters(st)); 82 } 83 84 tEnv.addObjRelation(previous, list); 85 } 86 catch (IOException ioe) { 87 } 88 } 89 getParameters(StringTokenizer st)90 private static Parameters getParameters(StringTokenizer st) { 91 String separator = "="; 92 HashMap map = new HashMap(5); 93 94 while (st.hasMoreTokens()) { 95 String pair = st.nextToken(); 96 StringTokenizer tokens = new StringTokenizer(pair, separator); 97 98 String name; 99 String value; 100 101 if (tokens.countTokens() < 2) 102 continue; 103 104 name = tokens.nextToken(); 105 if (tokens.countTokens() == 1) 106 value = tokens.nextToken(); 107 else { 108 StringBuffer buf = new StringBuffer(tokens.nextToken()); 109 while (tokens.hasMoreTokens()) 110 buf.append(separator).append(tokens.nextToken()); 111 value = buf.toString(); 112 } 113 114 map.put(name, value); 115 } 116 117 return new Parameters(map); 118 } 119 } 120