Java tutorial
/* * Copyright (c) 2014 Patrick Meyer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.itemanalysis.jmetrik.data; import com.itemanalysis.jmetrik.manager.ImportDataCommand; import com.itemanalysis.jmetrik.file.JmetrikFileImporter; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import org.apache.commons.io.FileUtils; import org.apache.commons.io.input.BOMInputStream; import org.junit.Test; import java.io.*; import java.util.Iterator; public class JmetrikFileImporterTest { @Test public void genericImportTest() { System.out.println("JmetrikFileImporterTest: Importing ./data/example-import-file.txt"); ClassLoader classLoader = getClass().getClassLoader(); File f = new File(classLoader.getResource("data/example-import-file.txt").getFile()); String fileName = f.getAbsolutePath(); ImportDataCommand command = new ImportDataCommand(); command.setDataFileOption(fileName); command.setDelimiter("comma"); String[] selectAllOptions = { "header", "overwrite" }; command.setOptionsOption(selectAllOptions); JmetrikFileImporter fileImporter = new JmetrikFileImporter(command); fileImporter.doInBackground(); } @Test public void exam1ImportTest() { System.out.println("JmetrikFileImporterTest: Importing ./data/exam1-raw-data.csv"); ClassLoader classLoader = getClass().getClassLoader(); File f = new File(classLoader.getResource("data/exam1-raw-data.csv").getFile()); String path = f.getParent(); File outputFile = new File(path + File.separator + "exam1.jmetrik"); try { outputFile.createNewFile(); } catch (IOException ex) { ex.printStackTrace(); } ImportDataCommand command = new ImportDataCommand(); command.setDataFileOption(f.getAbsolutePath()); command.setOutfileOption(outputFile.getAbsolutePath()); command.setDelimiter("comma"); String[] selectAllOptions = { "header", "overwrite" }; command.setOptionsOption(selectAllOptions); JmetrikFileImporter fileImporter = new JmetrikFileImporter(command); fileImporter.doInBackground(); } @Test public void readJmetrikFileTest() { System.out.println("JmetrikFileImporterTest: Reading *.jmetrik file"); CSVParser parser = null; Reader reader = null; try { File dataFile = FileUtils.toFile(this.getClass().getResource("/data/example-import-file.jmetrik")); reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8"); parser = new CSVParser(reader, CSVFormat.DEFAULT.withCommentMarker('#')); Iterator<CSVRecord> iter = parser.iterator(); CSVRecord temp = null; boolean readAttributes = false; boolean readData = false; int attCount = 0; while (iter.hasNext()) { temp = iter.next(); if ("VERSION".equals(temp.getComment())) { System.out.println("VERSION: " + temp.get(0)); } else if ("METADATA".equals(temp.getComment())) { System.out.println("CASES: " + temp.get(0)); } else if ("ATTRIBUTES".equals(temp.getComment())) { readAttributes = true; } else if ("DATA".equals(temp.getComment())) { readAttributes = false; readData = true; } if (readAttributes) { System.out.print("ATTRIBUTE-" + attCount + ": "); Iterator<String> innerIter = temp.iterator(); while (innerIter.hasNext()) { System.out.print(innerIter.next()); if (innerIter.hasNext()) { System.out.print(","); } } System.out.println(); attCount++; } if (readData) { Iterator<String> innerIter = temp.iterator(); while (innerIter.hasNext()) { System.out.print(innerIter.next()); if (innerIter.hasNext()) { System.out.print(","); } } System.out.println(); } } } catch (IOException ex) { ex.printStackTrace(); } finally { try { parser.close(); reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } @Test public void generalTest() { File f = new File("C:\\mystuff\\myfile.txt"); System.out.println(f.getName()); System.out.println(f.getAbsolutePath()); } }