| 1 | |
package br.mia.test.util; |
| 2 | |
|
| 3 | |
import java.awt.Toolkit; |
| 4 | |
import java.awt.datatransfer.Clipboard; |
| 5 | |
import java.awt.datatransfer.DataFlavor; |
| 6 | |
import java.awt.datatransfer.Transferable; |
| 7 | |
import java.awt.datatransfer.UnsupportedFlavorException; |
| 8 | |
import java.io.BufferedReader; |
| 9 | |
import java.io.File; |
| 10 | |
import java.io.FileInputStream; |
| 11 | |
import java.io.FileReader; |
| 12 | |
import java.io.IOException; |
| 13 | |
import java.sql.SQLException; |
| 14 | |
import java.util.ArrayList; |
| 15 | |
import java.util.Iterator; |
| 16 | |
import java.util.LinkedHashMap; |
| 17 | |
import java.util.List; |
| 18 | |
import java.util.Properties; |
| 19 | |
|
| 20 | |
import javax.swing.JComboBox; |
| 21 | |
import javax.swing.JFileChooser; |
| 22 | |
import javax.swing.JTable; |
| 23 | |
|
| 24 | |
import org.apache.log4j.Logger; |
| 25 | |
|
| 26 | |
import br.mia.test.model.CasoUso; |
| 27 | |
import br.mia.test.model.Filtro; |
| 28 | |
import br.mia.test.model.Fluxo; |
| 29 | |
import br.mia.test.model.Link; |
| 30 | |
import br.mia.test.model.RegraNegocio; |
| 31 | |
import br.mia.test.model.RegraNegocioStep; |
| 32 | |
import br.mia.test.model.Step; |
| 33 | |
import br.mia.test.model.TestDataValor; |
| 34 | |
import br.mia.test.model.algoritmo.Algoritmo; |
| 35 | 0 | import br.mia.test.model.dao.DAOBussinessRule; |
| 36 | 0 | import br.mia.test.testcase.CasosTeste; |
| 37 | 0 | import br.mia.test.testcase.DadosTeste; |
| 38 | |
import br.mia.test.view.Global; |
| 39 | 0 | |
| 40 | 0 | public class Util |
| 41 | 0 | { |
| 42 | 0 | |
| 43 | 0 | public static Algoritmo retornaAlgoritmoCasosTeste(CasoUso useCase) |
| 44 | 0 | throws InstantiationException, IllegalAccessException, |
| 45 | 0 | ClassNotFoundException, SQLException, IOException { |
| 46 | 0 | Algoritmo alg = new Algoritmo(); |
| 47 | 0 | alg.limpaArvore(useCase); |
| 48 | 0 | alg.retornaCenarios((Fluxo) useCase.getFlows().get(0), 0, |
| 49 | 0 | new CasosTeste(), 0, useCase); |
| 50 | 0 | ArrayList<CasosTeste> casosTeste = alg.getCasosDeTeste(); |
| 51 | |
|
| 52 | 0 | for (Object object : casosTeste) { |
| 53 | 0 | CasosTeste casoTeste = (CasosTeste) object; |
| 54 | 0 | |
| 55 | 0 | alg.applyData(casoTeste, 0, new CasosTeste(), useCase, true); |
| 56 | 0 | |
| 57 | 0 | } |
| 58 | 0 | |
| 59 | 0 | return alg; |
| 60 | 0 | } |
| 61 | 0 | |
| 62 | 0 | public static Logger getLogger() { |
| 63 | 0 | return Logger.getLogger("testkase"); |
| 64 | |
} |
| 65 | 0 | |
| 66 | 0 | public static String getTestCaseName(CasoUso useCase, int id, |
| 67 | 0 | CasosTeste testCase) throws IOException { |
| 68 | 0 | String regra = Util.getPropriedade("testcasename"); |
| 69 | 0 | String nome; |
| 70 | 0 | |
| 71 | 0 | if (regra != null) { |
| 72 | 0 | if (regra.contains("FN")) { |
| 73 | |
|
| 74 | 0 | StringBuffer nomeFluxo = new StringBuffer(); |
| 75 | 0 | for (Fluxo flow : testCase.getFlows()) { |
| 76 | 0 | String nomeReduzido = flow.getName().substring(0, 2); |
| 77 | 0 | |
| 78 | 0 | if (!(nomeFluxo.toString().contains(nomeReduzido))) { |
| 79 | 0 | |
| 80 | 0 | nomeFluxo.append(nomeReduzido); |
| 81 | 0 | } |
| 82 | 0 | } |
| 83 | 0 | nome = useCase.getName() + nomeFluxo.toString() + "-CT"; |
| 84 | 0 | |
| 85 | 0 | } else { |
| 86 | 0 | nome = useCase.getName() + "-CT" + id; |
| 87 | 0 | } |
| 88 | 0 | if (regra.contains("DT")) { |
| 89 | 0 | nome += (new java.util.Date()).toString(); |
| 90 | 0 | } |
| 91 | 0 | |
| 92 | 0 | } else { |
| 93 | 0 | nome = useCase.getName() + "-CT" + id; |
| 94 | 0 | |
| 95 | 0 | } |
| 96 | |
|
| 97 | 0 | return nome; |
| 98 | 0 | } |
| 99 | |
|
| 100 | |
public static File[] getFiles() { |
| 101 | |
|
| 102 | 0 | JFileChooser fc = new JFileChooser(); |
| 103 | 0 | fc.setMultiSelectionEnabled(true); |
| 104 | 0 | fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); |
| 105 | 0 | int returnVal = fc.showOpenDialog(Global.getMidpane()); |
| 106 | 0 | if (returnVal == JFileChooser.APPROVE_OPTION) { |
| 107 | 0 | |
| 108 | 0 | return fc.getSelectedFiles(); |
| 109 | 0 | } else { |
| 110 | 0 | return null; |
| 111 | 0 | } |
| 112 | |
} |
| 113 | 0 | |
| 114 | 0 | public static String readFile(File file) throws IOException { |
| 115 | 0 | BufferedReader in = new BufferedReader(new FileReader(file)); |
| 116 | |
|
| 117 | 0 | StringBuffer sb = new StringBuffer(); |
| 118 | 0 | String line = ""; |
| 119 | 0 | |
| 120 | 0 | while ((line = in.readLine()) != null) { |
| 121 | 0 | sb.append(line); |
| 122 | 0 | } |
| 123 | 0 | |
| 124 | 0 | return sb.toString(); |
| 125 | 0 | } |
| 126 | 0 | |
| 127 | 0 | public static ArrayList<String> readFileArray(File file) throws IOException { |
| 128 | 0 | BufferedReader in = new BufferedReader(new FileReader(file)); |
| 129 | 0 | |
| 130 | 0 | ArrayList<String> sb = new ArrayList<String>(); |
| 131 | 0 | String line = ""; |
| 132 | 0 | |
| 133 | 0 | while ((line = in.readLine()) != null) { |
| 134 | 0 | sb.add(line); |
| 135 | 0 | } |
| 136 | 0 | |
| 137 | 0 | return sb; |
| 138 | 0 | } |
| 139 | 0 | |
| 140 | 0 | public static ArrayList<TestDataValor> getValores(File file) |
| 141 | 0 | throws IOException { |
| 142 | 0 | |
| 143 | 0 | ArrayList<TestDataValor> array = new ArrayList<TestDataValor>(); |
| 144 | 0 | |
| 145 | 0 | ArrayList<String> valoresString = readFileArray(file); |
| 146 | |
|
| 147 | 0 | for (String linha : valoresString) { |
| 148 | 0 | |
| 149 | 0 | String termos[] = linha.split(","); |
| 150 | 0 | |
| 151 | 0 | TestDataValor valor = new TestDataValor(); |
| 152 | 0 | valor.setValor(termos[0]); |
| 153 | 0 | valor.setResultadoEsperado(termos[1]); |
| 154 | 0 | valor.setClasse(termos[2]); |
| 155 | 0 | |
| 156 | 0 | array.add(valor); |
| 157 | 0 | |
| 158 | 0 | } |
| 159 | 0 | return array; |
| 160 | 0 | |
| 161 | 0 | } |
| 162 | 0 | |
| 163 | 0 | public static String getPropriedade(String propriedade) throws IOException { |
| 164 | 0 | File file = new java.io.File("config.properties"); |
| 165 | 0 | |
| 166 | 0 | FileInputStream iof = new FileInputStream(file); |
| 167 | 0 | |
| 168 | 0 | Properties properties = new Properties(); |
| 169 | 0 | |
| 170 | 0 | properties.load(iof); |
| 171 | 0 | return properties.getProperty(propriedade); |
| 172 | 0 | } |
| 173 | 0 | |
| 174 | |
public JComboBox comboDados(CasoUso useCase) { |
| 175 | 0 | return new JComboBox(Util.getDataUseCase(useCase).toArray()); |
| 176 | 0 | } |
| 177 | 0 | |
| 178 | 0 | public void recarregaCombo(JComboBox combo, Object[] array) { |
| 179 | 0 | if (combo != null) { |
| 180 | 0 | combo.removeAllItems(); |
| 181 | 0 | Object[] aux = (Object[]) array; |
| 182 | 0 | |
| 183 | 0 | for (Object objeto : aux) { |
| 184 | 0 | combo.addItem(objeto); |
| 185 | 0 | } |
| 186 | 0 | |
| 187 | 0 | } else { |
| 188 | 0 | combo = new JComboBox(array); |
| 189 | 0 | } |
| 190 | 0 | combo.setVisible(false); |
| 191 | 0 | combo.revalidate(); |
| 192 | 0 | combo.setVisible(true); |
| 193 | 0 | combo.repaint(); |
| 194 | 0 | } |
| 195 | 0 | |
| 196 | 0 | public static DadosTeste getDataTable(JTable datas, CasoUso useCase) { |
| 197 | 0 | return (DadosTeste) getDataUseCase(useCase).get(datas.getSelectedRow()); |
| 198 | 0 | } |
| 199 | 0 | |
| 200 | |
public static void zeraIdArrayValores(List<TestDataValor> valores1) { |
| 201 | 0 | for (Iterator<TestDataValor> iterator = valores1.iterator(); iterator |
| 202 | 0 | .hasNext();) { |
| 203 | 0 | TestDataValor type = (TestDataValor) iterator.next(); |
| 204 | 0 | type.setId(0); |
| 205 | 0 | } |
| 206 | 0 | } |
| 207 | 0 | |
| 208 | 0 | public static void zeraIdUseCase(CasoUso useCase) { |
| 209 | 0 | useCase.setId(0); |
| 210 | 0 | List<Fluxo> flows = useCase.getFlows(); |
| 211 | 0 | ArrayList<DadosTeste> datas = Util.getDataUseCase(useCase); |
| 212 | 0 | List<RegraNegocio> regras = useCase.getRegras(); |
| 213 | 0 | for (Iterator<Fluxo> iterator = flows.iterator(); iterator.hasNext();) { |
| 214 | 0 | Fluxo flow = (Fluxo) iterator.next(); |
| 215 | 0 | flow.setIdFlow(0); |
| 216 | 0 | List<Step> steps = flow.getSteps(); |
| 217 | 0 | for (Iterator<Step> iterator1 = steps.iterator(); iterator1 |
| 218 | 0 | .hasNext();) { |
| 219 | 0 | Step step = (Step) iterator1.next(); |
| 220 | 0 | step.setId(0); |
| 221 | 0 | ArrayList<Link> links = (ArrayList<Link>) step.getLinks(); |
| 222 | 0 | for (Iterator<Link> iterator4 = links.iterator(); iterator4 |
| 223 | 0 | .hasNext();) { |
| 224 | 0 | Link link = (Link) iterator4.next(); |
| 225 | 0 | link.setId(0); |
| 226 | 0 | } |
| 227 | 0 | for (Filtro filtro : step.getFiltros()) { |
| 228 | 0 | filtro.setId(0); |
| 229 | |
} |
| 230 | 0 | ArrayList<DadosTeste> testDatas = (ArrayList<DadosTeste>) step |
| 231 | 0 | .getDatas(); |
| 232 | 0 | for (Iterator<DadosTeste> iterator5 = testDatas.iterator(); iterator5 |
| 233 | 0 | .hasNext();) { |
| 234 | 0 | DadosTeste data = (DadosTeste) iterator5.next(); |
| 235 | 0 | data.setId(0); |
| 236 | 0 | } |
| 237 | 0 | |
| 238 | 0 | } |
| 239 | 0 | } |
| 240 | 0 | for (Iterator<DadosTeste> iterator2 = datas.iterator(); iterator2 |
| 241 | 0 | .hasNext();) { |
| 242 | 0 | DadosTeste data = (DadosTeste) iterator2.next(); |
| 243 | 0 | data.setId(0); |
| 244 | 0 | List<TestDataValor> valores = data.getValues(); |
| 245 | 0 | for (Iterator<TestDataValor> iterator3 = valores.iterator(); iterator3 |
| 246 | 0 | .hasNext();) { |
| 247 | 0 | TestDataValor valor = (TestDataValor) iterator3.next(); |
| 248 | 0 | valor.setId(0); |
| 249 | 0 | } |
| 250 | 0 | } |
| 251 | 0 | if (regras != null) { |
| 252 | 0 | for (RegraNegocio regraNegocio : regras) { |
| 253 | 0 | regraNegocio.setId(0); |
| 254 | 0 | for (RegraNegocioStep step : regraNegocio.getSteps()) { |
| 255 | 0 | step.setId(0); |
| 256 | 0 | } |
| 257 | 0 | |
| 258 | 0 | } |
| 259 | |
} |
| 260 | 0 | |
| 261 | 0 | } |
| 262 | |
|
| 263 | |
public static void zeraIdArraySteps(ArrayList<Step> valores) { |
| 264 | 0 | for (Iterator<Step> iterator = valores.iterator(); iterator.hasNext();) { |
| 265 | 0 | Step type = (Step) iterator.next(); |
| 266 | 0 | type.setId(0); |
| 267 | 0 | } |
| 268 | 0 | } |
| 269 | |
|
| 270 | 0 | public static String getClipboardContents() |
| 271 | 0 | throws UnsupportedFlavorException, IOException { |
| 272 | 0 | String result = ""; |
| 273 | 0 | Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); |
| 274 | |
|
| 275 | 0 | Transferable contents = clipboard.getContents(null); |
| 276 | 0 | boolean hasTransferableText = (contents != null) |
| 277 | 0 | && contents.isDataFlavorSupported(DataFlavor.stringFlavor); |
| 278 | 0 | if (hasTransferableText) { |
| 279 | 0 | |
| 280 | 0 | result = (String) contents.getTransferData(DataFlavor.stringFlavor); |
| 281 | 0 | |
| 282 | 0 | } |
| 283 | 0 | return result; |
| 284 | |
} |
| 285 | 0 | |
| 286 | 0 | public static DadosTeste getDataCombo(JComboBox combo, CasoUso useCase) { |
| 287 | 0 | return (DadosTeste) getDataUseCase(useCase).get( |
| 288 | 0 | combo.getSelectedIndex()); |
| 289 | 0 | } |
| 290 | 0 | |
| 291 | 0 | public static TestDataValor getDataValor(JTable datas, JTable valores, |
| 292 | 0 | CasoUso useCase) { |
| 293 | 0 | return getDataTable(datas, useCase).getValues().get( |
| 294 | 0 | valores.getSelectedRow()); |
| 295 | 0 | } |
| 296 | 0 | |
| 297 | 0 | public static TestDataValor getDataValor(JTable datas, int index, |
| 298 | 0 | CasoUso useCase) { |
| 299 | 0 | return getDataTable(datas, useCase).getValues().get(index); |
| 300 | |
} |
| 301 | 0 | |
| 302 | |
public static void putData(DadosTeste data, CasoUso useCase) { |
| 303 | 0 | putData(useCase, data); |
| 304 | 0 | } |
| 305 | |
|
| 306 | 0 | public static void putData(CasoUso useCase, DadosTeste data) { |
| 307 | 0 | |
| 308 | 0 | useCase.getDataMapaGlobal().put( |
| 309 | 0 | data.getName() + useCase.getName() + useCase.getId(), data); |
| 310 | 0 | } |
| 311 | |
|
| 312 | |
public static void removeData(DadosTeste data, CasoUso useCase) { |
| 313 | 0 | removeData(useCase, data); |
| 314 | 0 | } |
| 315 | |
|
| 316 | |
public static void removeData(CasoUso useCase, DadosTeste data) { |
| 317 | 0 | useCase.getDataMapaGlobal().remove( |
| 318 | 0 | data.getName() + useCase.getName() + useCase.getId()); |
| 319 | |
|
| 320 | 0 | } |
| 321 | 0 | |
| 322 | |
public static DadosTeste getData(int index, CasoUso useCase) { |
| 323 | 0 | return (DadosTeste) getDataUseCase(useCase).get(index); |
| 324 | |
} |
| 325 | 0 | |
| 326 | |
public static ArrayList<DadosTeste> getDataUseCase(CasoUso useCase) { |
| 327 | |
|
| 328 | 0 | return new ArrayList<DadosTeste>(useCase.getDataMapaGlobal().values()); |
| 329 | 0 | } |
| 330 | 0 | |
| 331 | 0 | public static RegraNegocio getBussinessRule(int id, CasoUso useCase) { |
| 332 | |
|
| 333 | 0 | List<RegraNegocio> array = useCase.getRegras(); |
| 334 | 0 | return (RegraNegocio) array.get(id); |
| 335 | |
} |
| 336 | 0 | |
| 337 | 0 | @SuppressWarnings("unchecked") |
| 338 | 0 | public static ArrayList<Object> toArray(LinkedHashMap mapa) { |
| 339 | |
|
| 340 | 0 | ArrayList<Object> array = new ArrayList<Object>(mapa.values()); |
| 341 | 0 | return array; |
| 342 | |
} |
| 343 | |
|
| 344 | |
public static void recarga(CasoUso useCase) throws InstantiationException, |
| 345 | |
IllegalAccessException, ClassNotFoundException, SQLException, |
| 346 | |
IOException { |
| 347 | |
|
| 348 | 0 | DAOBussinessRule daoRule = new DAOBussinessRule(); |
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | 0 | } |
| 353 | |
|
| 354 | |
public static void insereRegra(CasoUso useCase, RegraNegocio rule) { |
| 355 | 0 | if (!(useCase.getRuleMapaGlobal().containsKey(rule.getName() |
| 356 | 0 | + useCase.getName()))) { |
| 357 | 0 | useCase.getRuleMapaGlobal().put(rule.getName() + useCase.getName(), |
| 358 | 0 | rule); |
| 359 | |
} |
| 360 | 0 | } |
| 361 | |
|
| 362 | |
public static void zeraIdArrayRegraNegocioSteps( |
| 363 | |
List<RegraNegocioStep> valores1) { |
| 364 | 0 | for (Iterator<RegraNegocioStep> iterator = valores1.iterator(); iterator |
| 365 | 0 | .hasNext();) { |
| 366 | 0 | RegraNegocioStep type = (RegraNegocioStep) iterator.next(); |
| 367 | 0 | type.setId(0); |
| 368 | |
} |
| 369 | |
|
| 370 | 0 | } |
| 371 | |
|
| 372 | |
} |