Skip to content

Commit

Permalink
Add Unit Tests for Indiware (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
johan12345 committed Aug 26, 2016
1 parent b387657 commit 9d40366
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 0 deletions.
1 change: 1 addition & 0 deletions parser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ apply plugin: 'java'

sourceCompatibility = 1.7
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'

dependencies {
compile 'joda-time:joda-time:2.8.1'
Expand Down
54 changes: 54 additions & 0 deletions parser/src/test/java/me/vertretungsplan/parser/BaseDemoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* substitution-schedule-parser - Java library for parsing schools' substitution schedules
* Copyright (c) 2016 Johan v. Forstner
*
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package me.vertretungsplan.parser;

import java.io.*;

public class BaseDemoTest {
/**
* Reads content from an InputStream into a string
*
* @param is InputStream to read from
* @return String content of the InputStream
*/
private static String convertStreamToString(InputStream is) throws IOException {
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
reader = new BufferedReader(new InputStreamReader(is));
}
StringBuilder sb = new StringBuilder();

String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}

protected String readResource(String filename) {
InputStream is = getClass().getResourceAsStream(filename);
if (is == null) return null;
try {
return convertStreamToString(is);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* substitution-schedule-parser - Java library for parsing schools' substitution schedules
* Copyright (c) 2016 Johan v. Forstner
*
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package me.vertretungsplan.parser;

import me.vertretungsplan.objects.Substitution;
import me.vertretungsplan.objects.SubstitutionScheduleData;
import me.vertretungsplan.objects.SubstitutionScheduleDay;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.parser.Parser;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class IndiwareDemoTest extends BaseDemoTest {
IndiwareParser parser;
private String xml;

@Before
public void setUp() throws JSONException {
xml = readResource("/indiware/indiware.xml");
SubstitutionScheduleData scheduleData = new SubstitutionScheduleData();
scheduleData.setData(new JSONObject());
parser = new IndiwareParser(scheduleData, null);
}

@Test
public void demoTest() {
SubstitutionScheduleDay schedule = parser.parseIndiwareDay(Jsoup.parse(xml, "", Parser.xmlParser()));
assertEquals(new LocalDate(2016, 8, 22), schedule.getDate());
assertEquals(new LocalDateTime(2016, 8, 19, 12, 50), schedule.getLastChange());
assertEquals(2, schedule.getMessages().size());
assertEquals("<b>Klassen mit Änderung:</b> bla", schedule.getMessages().get(0));
assertEquals("Erste Zeile.\nZweite Zeile", schedule.getMessages().get(1));
assertEquals(1, schedule.getSubstitutions().size());
Substitution subst = schedule.getSubstitutions().iterator().next();
assertEquals(2, subst.getClasses().size());
assertEquals("3", subst.getLesson());
assertEquals("Bio", subst.getSubject());
assertEquals("Sch", subst.getTeacher());
assertEquals("1234", subst.getRoom());
assertEquals("Mat", subst.getPreviousSubject());
assertEquals("Mül", subst.getPreviousTeacher());
assertEquals(null, subst.getDesc());
assertEquals("Vertretung", subst.getType());
}
}
58 changes: 58 additions & 0 deletions parser/src/test/java/me/vertretungsplan/parser/IndiwareTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* substitution-schedule-parser - Java library for parsing schools' substitution schedules
* Copyright (c) 2016 Johan v. Forstner
*
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package me.vertretungsplan.parser;

import org.junit.Test;

import java.util.regex.Matcher;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class IndiwareTest {
@Test
public void testSubstitutionPattern() {
Matcher matcher = IndiwareParser.substitutionPattern.matcher("für ETH Meh");
assertTrue(matcher.matches());
assertEquals(matcher.group(1), "ETH");
assertEquals(matcher.group(2), "Meh");
assertEquals(matcher.group(3), "");
}

@Test
public void testSubstitutionPatternWithDesc() {
Matcher matcher = IndiwareParser.substitutionPattern.matcher("für ETH Röh , Aufgaben Frau Röhling");
assertTrue(matcher.matches());
assertEquals(matcher.group(1), "ETH");
assertEquals(matcher.group(2), "Röh");
assertEquals(matcher.group(3), "Aufgaben Frau Röhling");
}

@Test
public void testCancelPattern() {
Matcher matcher = IndiwareParser.cancelPattern.matcher("FR Wen fällt aus");
assertTrue(matcher.matches());
assertEquals(matcher.group(1), "FR");
assertEquals(matcher.group(2), "Wen");
}

@Test
public void testSelfPattern() {
Matcher matcher = IndiwareParser.selfPattern.matcher("selbst.");
assertTrue(matcher.matches());
assertEquals(matcher.group(1), "");
}

@Test
public void testSelfPatternWithDesc() {
Matcher matcher = IndiwareParser.selfPattern.matcher("selbst., Aufgaben erteilt");
assertTrue(matcher.matches());
assertEquals(matcher.group(1), "Aufgaben erteilt");
}
}
30 changes: 30 additions & 0 deletions parser/src/test/resources/indiware/indiware.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="vplank.xsl"?>
<vp>
<kopf>
<titel>Montag, 22. August 2016 (A-Woche)</titel>
<schulname>Testschule Test</schulname>
<datum>19.08.2016, 12:50</datum>
<kopfinfo>
<aenderungk>bla</aenderungk>
</kopfinfo>
</kopf>
<fuss>
<fusszeile>
<fussinfo>Erste Zeile.</fussinfo>
</fusszeile>
<fusszeile>
<fussinfo>Zweite Zeile</fussinfo>
</fusszeile>
</fuss>
<haupt>
<aktion>
<klasse>5/1,5b</klasse>
<stunde>3</stunde>
<fach>Bio</fach>
<lehrer>Sch</lehrer>
<raum>1234</raum>
<info>für Mat Mül</info>
</aktion>
</haupt>
</vp>

0 comments on commit 9d40366

Please sign in to comment.