Skip to content
Permalink
Browse files

Update unit tests for importers and add test data

  • Loading branch information
davidzof committed Nov 3, 2014
1 parent 7c079c1 commit 7a4f3449e2bf5c5f231aa264742b2362475e0f32

Large diffs are not rendered by default.

Binary file not shown.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

@@ -12,71 +12,143 @@
*
* You should have received a copy of the GNU General Public License
* along with Wattzap. If not, see <http://www.gnu.org/licenses/>.
*/
package com.wattzap.utils;

*/
package com.wattzap.utils;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import com.wattzap.model.UserPreferences;
import com.wattzap.model.dto.Telemetry;
import com.wattzap.model.dto.WorkoutData;
import com.wattzap.view.training.TrainingAnalysis;

public class GpxImporterTest {
@Test
public void FitLogImport() {
try {
LogManager.getRootLogger().setLevel(Level.INFO);
XMLReader xr = XMLReaderFactory.createXMLReader();
GpxImporter handler = new GpxImporter();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);

String file = "Croix-de-Fer-10may2014.gpx";
FileReader r = new FileReader(file);
xr.parse(new InputSource(r));

ArrayList<Telemetry> gpxData = handler.data;

FitlogImporter flHandler = new FitlogImporter();
xr.setContentHandler(flHandler);
xr.setErrorHandler(flHandler);
// System.exit(1);
file = "Croix-de-Fer-10may2014.fitlog";
r = new FileReader(file);
xr.parse(new InputSource(r));

ArrayList<Telemetry> fitData = flHandler.data;
long first = gpxData.get(0).getTime();
int count = 0;
for (Telemetry t : gpxData) {
long time = t.getTime() - first;
Telemetry fl = fitData.get(count);
if (time < fl.getTime()) {
t.setCadence(fl.getCadence());
t.setHeartRate(fl.getHeartRate());
} else {
t.setCadence(fl.getCadence());
t.setHeartRate(fl.getHeartRate());
count++;
}

}

WorkoutData d = TrainingAnalysis.analyze(gpxData);
d.setFtp(260);
System.out.println(d);
TcxWriter writer = new TcxWriter();
writer.save(gpxData, 0);
} catch (Exception e) {

}
}
}

public class GpxImporterTest {
@Test
public void ImportGPX() {
try {
LogManager.getRootLogger().setLevel(Level.INFO);
XMLReader xr = XMLReaderFactory.createXMLReader();
GpxImporter handler = new GpxImporter();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);

String file = "resources/test/colducoq-6jun2014.gpx";
FileReader r = new FileReader(file);
xr.parse(new InputSource(r));

ArrayList<Telemetry> gpxData = handler.data;
Telemetry last = gpxData.get(gpxData.size() - 1);
Assert.assertEquals(last.getDistanceKM(), 32.557, 0.001);
} catch (Exception e) {
e.printStackTrace();

}
}

@Test
public void ImportAndWriteTCX() {
try {
LogManager.getRootLogger().setLevel(Level.INFO);
XMLReader xr = XMLReaderFactory.createXMLReader();
GpxImporter handler = new GpxImporter();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);

String file = "resources/test/colducoq-6jun2014.gpx";
FileReader r = new FileReader(file);
xr.parse(new InputSource(r));

ArrayList<Telemetry> gpxData = handler.data;

TcxWriter writer = new TcxWriter();
writer.save(gpxData, 0);

XMLReader xrTcx = XMLReaderFactory.createXMLReader();
TcxImporter tcxHandler = new TcxImporter();
xr.setContentHandler(tcxHandler);
xr.setErrorHandler(tcxHandler);

Telemetry firstPoint = gpxData.get(0);

String fileName = UserPreferences.INSTANCE.getUserDataDirectory()
+ "/Workouts/"
+ TcxWriter.getWorkoutName(firstPoint.getTime());

r = new FileReader(fileName);
xr.parse(new InputSource(r));

ArrayList<Telemetry> tcxData = tcxHandler.data;
Assert.assertEquals(tcxData.size(), gpxData.size());
// go through point by point
for (int i = 0; i < tcxData.size(); i++) {
Telemetry gpxPoint = gpxData.get(i);
Telemetry tcxPoint = tcxData.get(i);
Assert.assertEquals(tcxPoint.getSpeedKMH(), gpxPoint.getSpeedKMH(), 0.1);
}

} catch (Exception e) {
e.printStackTrace();

}
}


//@Test
public void ImportGPXMergeFitLog() {
try {
LogManager.getRootLogger().setLevel(Level.INFO);
XMLReader xr = XMLReaderFactory.createXMLReader();
GpxImporter handler = new GpxImporter();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);

String file = "resources/test/colducoq-6jun2014.gpx";
FileReader r = new FileReader(file);
xr.parse(new InputSource(r));

ArrayList<Telemetry> gpxData = handler.data;

FitlogImporter flHandler = new FitlogImporter();
xr.setContentHandler(flHandler);
xr.setErrorHandler(flHandler);
file = "resources/test/colducoq-6jun2014.fitlog";
r = new FileReader(file);
xr.parse(new InputSource(r));

ArrayList<Telemetry> fitData = flHandler.data;
long first = gpxData.get(0).getTime();
int count = 0;
for (Telemetry t : gpxData) {
long time = t.getTime() - first;
Telemetry fl = fitData.get(count);
if (time < fl.getTime()) {
t.setCadence(fl.getCadence());
t.setHeartRate(fl.getHeartRate());
} else {
t.setCadence(fl.getCadence());
t.setHeartRate(fl.getHeartRate());
count++;
}

}

WorkoutData d = TrainingAnalysis.analyze(gpxData);
d.setFtp(250);
System.out.println(d);

} catch (Exception e) {
e.printStackTrace();

}
}
}
@@ -0,0 +1,134 @@
/* This file is part of Wattzap Community Edition.
*
* Wattzap Community Edtion 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.
*
* Wattzap Community Edition 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 Wattzap. If not, see <http://www.gnu.org/licenses/>.
*/
package com.wattzap.utils;

import org.junit.Test;

public class StringXORerTest {
private static final String cryptKey = "CAFEBABE";

@Test
public void encodeString() {
String s = StringXORer
.encode("<html>WattzAp Turbo Trainer Software<br/><br/>Version: 2.2.1<br/>Date: 7th October<br/>(c) 2014, All rights reserved<br/>",
cryptKey);
System.out.println(s);
s = StringXORer.decode(s, cryptKey);
System.out.println(s);

}

@Test
public void encodeRegistered() {
String s = StringXORer.encode("Your software is registered",
cryptKey);
System.out.println(s);
s = StringXORer.decode(s, cryptKey);
System.out.println(s);

}

@Test
public void encodeNotRegistered() {
String s = StringXORer.encode("Your software is not registered",
cryptKey);
System.out.println(s);
s = StringXORer.decode(s, cryptKey);
System.out.println(s);

}

@Test
public void encodeIncorrect() {
String s = StringXORer.encode("Incorrect registration key",
cryptKey);
System.out.println(s);
s = StringXORer.decode(s, cryptKey);
System.out.println(s);

}

@Test
public void error() {
String s = StringXORer.encode("Error in registration key",
cryptKey);
System.out.println(s);
s = StringXORer.decode(s, cryptKey);
System.out.println(s);

}

@Test
public void success() {
String s = StringXORer.encode("Your software has been registered successfully",
cryptKey);
System.out.println(s);
s = StringXORer.decode(s, cryptKey);
System.out.println(s);

}

@Test
public void eval() {
String s = StringXORer.encode("You have %d minutes left to evaluate this software<br/>",
cryptKey);
System.out.println(s);
s = StringXORer.decode(s, cryptKey);
System.out.println(s);

}

@Test
public void register() {
String s = StringXORer.encode("Register",
cryptKey);
System.out.println(s);
s = StringXORer.decode(s, cryptKey);
System.out.println(s);

}

@Test
public void about() {
String s = StringXORer.encode("About WattzAp",
cryptKey);
System.out.println(s);
s = StringXORer.decode(s, cryptKey);
System.out.println(s);

}

@Test
public void serial() {
String s = StringXORer.encode("Serial Number: ",
cryptKey);
System.out.println(s);
s = StringXORer.decode(s, cryptKey);
System.out.println(s);

}

@Test
public void debug() {
String s = StringXORer.encode("Debug",
cryptKey);
System.out.println(s);
s = StringXORer.decode(s, cryptKey);
System.out.println(s);

}

}
@@ -18,6 +18,9 @@
import java.io.FileReader;
import java.util.ArrayList;

import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
@@ -27,8 +30,32 @@
import com.wattzap.model.dto.WorkoutData;
import com.wattzap.view.training.TrainingAnalysis;

public class TcxImporterTest {
@Test
public class TcxImporterTest {

@Test
public void ImportTCX() {
try {
LogManager.getRootLogger().setLevel(Level.INFO);
XMLReader xr = XMLReaderFactory.createXMLReader();
TcxImporter handler = new TcxImporter();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);

String file = "resources/test/2014Apr21-183642.tcx";
FileReader r = new FileReader(file);
xr.parse(new InputSource(r));

ArrayList<Telemetry> gpxData = handler.data;
Telemetry last = gpxData.get(gpxData.size() - 1);
Assert.assertEquals(last.getDistanceKM(), 20.266, 0.001);
} catch (Exception e) {
e.printStackTrace();

}
}


//@Test
public void TcxImport() {
try {
XMLReader xr = XMLReaderFactory.createXMLReader();
@@ -42,8 +69,8 @@ public void TcxImport() {

ArrayList<Telemetry> data = handler.data;
Telemetry last = data.get(data.size() - 1);
if (last.getDistance() == 0) {
last.setDistance(handler.distance);// hack if no distance data
if (last.getDistanceMeters() == 0) {
last.setDistanceMeters(handler.distance);// hack if no distance data
}
for (Telemetry t : data) {
System.out.println(t.getPower());

0 comments on commit 7a4f344

Please sign in to comment.
You can’t perform that action at this time.