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.file.JmetrikCSVRecord; import com.itemanalysis.jmetrik.file.JmetrikFileReader; import com.itemanalysis.psychometrics.data.VariableAttributes; import com.itemanalysis.psychometrics.data.VariableName; import org.apache.commons.io.FileUtils; import org.junit.Test; import java.io.File; import java.io.IOException; import java.util.HashMap; import static org.junit.Assert.*; public class JmetrikFileReaderTest { @Test public void headerTest() { System.out.println("JmetrikFileReader: header test"); File f = FileUtils.toFile(this.getClass().getResource("/data/example-import-file.jmetrik")); HashMap<VariableName, VariableAttributes> variableAttributeMap = null; try (JmetrikFileReader reader = new JmetrikFileReader(f)) { reader.openConnection(); variableAttributeMap = reader.getVariableAttributes(); for (VariableName v : variableAttributeMap.keySet()) { System.out.println(variableAttributeMap.get(v).toString()); } } catch (IOException ex) { ex.printStackTrace(); } } @Test public void dataTest() { System.out.println("JmetrikFileReader: Reading data/example-import-file.jmetrik"); ClassLoader classLoader = getClass().getClassLoader(); File f = new File(classLoader.getResource("data/example-import-file.jmetrik").getFile()); String[] data = { "1001,F,1,A,B,C,0,10.1", "1002,F,1,B,C,A,1,11.2", "1003,M,1,A,B,A,0,12.12", "1004,M,1,A,B,C,0,14.3", "1005,M,0,B,B,C,1,15", "1006,F,0,A,B,C,1,10.0", "1007,M,0,B,A,C,0,21", "1008,F,0,A,B,C,1,10", "1009,M,0,A,B,D,0,19.5", "1010,M,0,C,B,A,1,21" }; VariableName sid = new VariableName("sid"); VariableName gender = new VariableName("gender"); VariableName item1 = new VariableName("item1"); VariableName item2 = new VariableName("item2"); VariableName item3 = new VariableName("item3"); VariableName item4 = new VariableName("item4"); VariableName score = new VariableName("score"); try (JmetrikFileReader reader = new JmetrikFileReader(f)) { reader.openConnection(); int index = 0; String[] s = null; JmetrikCSVRecord jmetrikCSVRecord = null; while (reader.hasNext()) { jmetrikCSVRecord = reader.next(); s = data[index].split(","); // System.out.println(jmetrikCSVRecord.toString()); assertTrue(s[0].equals(jmetrikCSVRecord.valueOfAsString(sid))); assertTrue(s[1].equals(jmetrikCSVRecord.valueOfAsString(gender))); assertTrue(s[3].equals(jmetrikCSVRecord.valueOfAsString(item1))); assertTrue(s[4].equals(jmetrikCSVRecord.valueOfAsString(item2))); assertTrue(s[5].equals(jmetrikCSVRecord.valueOfAsString(item3))); assertEquals("Item int test", Integer.parseInt(s[6]), jmetrikCSVRecord.valueOfAsInt(item4), 1e-15); assertEquals("Score test", Double.parseDouble(s[7]), jmetrikCSVRecord.valueOfAsDouble(score), 1e-15); index++; } assertEquals("Number of records test", data.length, index); assertEquals("Number of records test2", reader.getNumberOfRows(), index); } catch (IOException ex) { ex.printStackTrace(); } } @Test public void exam1Test() { System.out.println("JmetrikFileReader: Reading ./data/exam1.jmetrik"); File f = FileUtils.toFile(this.getClass().getResource("/data/exam1.jmetrik")); HashMap<VariableName, VariableAttributes> variableAttributeMap = null; try (JmetrikFileReader reader = new JmetrikFileReader(f)) { reader.openConnection(); variableAttributeMap = reader.getVariableAttributes(); // for(VariableName v : variableAttributeMap.keySet()){ // System.out.println(variableAttributeMap.get(v).printAttributes()); // } VariableName sid = new VariableName("id"); VariableName gender = new VariableName("gender"); VariableName score = new VariableName("sumscore"); JmetrikCSVRecord jmetrikCSVRecord = null; // while(reader.hasNext()){ // jmetrikCSVRecord = reader.next(); // System.out.print(jmetrikCSVRecord.valueOfAsString(sid) + " "); // System.out.print(jmetrikCSVRecord.valueOfAsString(gender) + " "); // System.out.println(jmetrikCSVRecord.valueOfAsDouble(score) + " "); // } } catch (IOException ex) { ex.printStackTrace(); } } @Test public void userHomeTest() { System.out.println("USER.DIR: " + System.getProperty("user.dir")); System.setProperty("user.dir", "C:\\Users\\Patrick\\Dropbox\\jmetrik\\cadence\\data"); System.out.println("USER.DIR: " + System.getProperty("user.dir")); System.out.println(); String prop = System.getProperty("user.home"); System.out.println("USER.HOME: " + prop); System.setProperty("user.home", "C:\\Users\\Patrick\\Dropbox\\jmetrik\\cadence\\data"); prop = System.getProperty("user.home"); System.out.println("USER.HOME: " + prop); } }