add unit tests, closes #12

fix for datetime timezone calculation
This commit is contained in:
akaessens 2020-07-29 19:38:39 +02:00
parent dbde1188dc
commit 8a1f5f8c53
4 changed files with 206 additions and 22 deletions

View File

@ -0,0 +1,106 @@
package com.akdev.nofbeventscraper;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.Test;
import org.junit.runner.RunWith;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static org.junit.Assert.assertEquals;
@RunWith(AndroidJUnit4.class)
public class MainActivityUnitTest {
@Test
public void TestSubdomainUrl() {
Instrumentation mInstrumentation = getInstrumentation();
// We register our interest in the activity
Instrumentation.ActivityMonitor monitor = mInstrumentation.addMonitor(MainActivity.class.getName(), null, false);
// We launch it
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(mInstrumentation.getTargetContext(), MainActivity.class.getName());
mInstrumentation.startActivitySync(intent);
MainActivity mainActivity = (MainActivity) getInstrumentation().waitForMonitor(monitor);
// We register our interest in the next activity from the sequence in this use case
mInstrumentation.removeMonitor(monitor);
final String exp = "https://m.facebook.com/events/261145401687844";
String url = "https://www.facebook.com/events/261145401687844";
String act = mainActivity.checkURI(url);
assertEquals(exp, act);
url = "https://de-de.facebook.com/events/261145401687844";
act = mainActivity.checkURI(url);
assertEquals(exp, act);
url = "https://m.facebook.com/events/261145401687844";
act = mainActivity.checkURI(url);
assertEquals(exp, act);
url = "https://www.facebook.com/events/261145401687844/?active_tab=discussion";
act = mainActivity.checkURI(url);
assertEquals(exp, act);
}
@Test
public void TestTimeToEpoch() {
Instrumentation mInstrumentation = getInstrumentation();
// We register our interest in the activity
Instrumentation.ActivityMonitor monitor = mInstrumentation.addMonitor(MainActivity.class.getName(), null, false);
// We launch it
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(mInstrumentation.getTargetContext(), MainActivity.class.getName());
mInstrumentation.startActivitySync(intent);
MainActivity mainActivity = (MainActivity) getInstrumentation().waitForMonitor(monitor);
// We register our interest in the next activity from the sequence in this use case
mInstrumentation.removeMonitor(monitor);
String in = "2020-07-29T12:00:00+00:00";
Long exp = new Long(1596024000);
exp = exp* 1000;
Long act = mainActivity.convertTimeToEpoch(in);
assertEquals(exp, act);
in = "2020-07-29T12:00:00+02:00";
exp = new Long(1596016800);
exp = exp* 1000;
act = mainActivity.convertTimeToEpoch(in);
assertEquals(exp, act);
in = "1970-01-01T00:00:00+00:00";
exp = new Long(0);
exp = exp* 1000;
act = mainActivity.convertTimeToEpoch(in);
assertEquals(exp, act);
in = "1970-01-01T02:00:00+02:00";
exp = new Long(0);
exp = exp* 1000;
act = mainActivity.convertTimeToEpoch(in);
assertEquals(exp, act);
}
}

View File

@ -0,0 +1,68 @@
package com.akdev.nofbeventscraper;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
@RunWith(AndroidJUnit4.class)
public class ScraperUnitTest {
@Test
public void TestLocation() {
FbScraper scraper = new FbScraper(null, "");
String exp = "Deutschland";
String json = "{'@type': 'Place', 'name': 'Deutschland'}";
String act = scraper.fixLocation(json);
assertEquals(exp, act);
exp = "Example name, Example Street 1, 12345 Example city";
json = "{'@type': 'Place', 'name': 'Example name', 'address': {'@type': 'PostalAddress', 'addressCountry': 'DE', 'addressLocality': 'Example city', 'postalCode': '12345', 'streetAddress': 'Example Street 1'}}";
act = scraper.fixLocation(json);
assertEquals(exp, act);
exp = "";
json = "";
act = scraper.fixLocation(json);
assertEquals(exp, act);
}
@Test
public void TestTimezone() {
FbScraper scraper = new FbScraper(null, "");
String exp = "2020-10-23T05:00:00+02:00";
String in = "2020-10-23T05:00:00+0200";
String act = scraper.fixTimezone(in);
assertEquals(exp, act);
exp = "";
in = "";
act = scraper.fixTimezone(in);
assertEquals(exp, act);
}
@Test
public void TestLinks() {
FbScraper scraper = new FbScraper(null, "");
String in = "foo @[152580919265:274:MagentaMusik 360] bar";
String exp = "foo m.facebook.com/152580919265 (MagentaMusik 360) bar";
String act = scraper.fixLinks(in);
assertEquals(exp, act);
in = "foo @[152580919265:274:MagentaMusik 360] bar @[666666666666:274:NoOfTheBeast]";
exp = "foo m.facebook.com/152580919265 (MagentaMusik 360) bar m.facebook.com/666666666666 (NoOfTheBeast)";
act = scraper.fixLinks(in);
assertEquals(exp, act);
}
}

View File

@ -30,7 +30,7 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
this.main = main;
}
private String fixLocation(String location_json) {
protected String fixLocation(String location_json) {
String name = "";
@ -63,7 +63,7 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
}
}
private String fixTimezone(String time_in) {
protected String fixTimezone(String time_in) {
try {
@ -77,7 +77,7 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
}
}
private String fixLinks(String description_in) {
protected String fixLinks(String description_in) {
try {
// @[152580919265:274:MagentaMusik 360] -> m.facebook.com/152580919265
return description_in.replaceAll("@\\[([0-9]{10,}):[0-9]{3}:([^\\]]*)\\]", "m.facebook.com/$1 ($2)");

View File

@ -1,9 +1,7 @@
package com.akdev.nofbeventscraper;
import android.app.AlertDialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
@ -21,7 +19,9 @@ import android.widget.Toast;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;
public class MainActivity extends AppCompatActivity {
@ -85,8 +85,8 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onClick(View view) {
try {
Long start_epoch = convertTimeToEpoch(field_event_start);
Long end_epoch = convertTimeToEpoch(field_event_end);
Long start_epoch = convertTimeToEpoch(field_event_start.getText().toString());
Long end_epoch = convertTimeToEpoch(field_event_end.getText().toString());
String name = parseField(field_event_name);
String location = parseField(field_event_location);
@ -149,12 +149,11 @@ public class MainActivity extends AppCompatActivity {
}
private Long convertTimeToEpoch (TextInputEditText field) {
Long convertTimeToEpoch(String time_str) {
try {
String time_str = field.getText().toString();
ZonedDateTime datetime = ZonedDateTime.parse(time_str, DateTimeFormatter.ISO_DATE_TIME);
LocalDateTime datetime = LocalDateTime.parse(time_str, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
return datetime.atZone(ZoneId.of("UTC")).toEpochSecond() * 1000;
return datetime.toEpochSecond() * 1000;
} catch (Exception e)
{
e.printStackTrace();
@ -169,16 +168,14 @@ public class MainActivity extends AppCompatActivity {
try {
double d = Double.parseDouble(strNum);
} catch (NumberFormatException e) {
e.printStackTrace();
//e.printStackTrace();
return false;
}
return true;
}
public void startScraping() {
String checkURI(String str)
{
try {
String str = field_uri_input.getText().toString();
// check for a valid uri
new URL(str).toURI();
@ -190,7 +187,7 @@ public class MainActivity extends AppCompatActivity {
// find event id
String[] separated = str.split("/");
for (int i = 0; i< separated.length; i++) {
for (int i = 0; i < separated.length; i++) {
if (separated[i].length() > 8 && isNumeric(separated[i])) {
eventId = separated[i];
break;
@ -200,17 +197,30 @@ public class MainActivity extends AppCompatActivity {
{
throw new Exception();
}
}
else {
} else {
throw new Exception();
}
String input_uri = "https://m.facebook.com/events/"+ eventId;
field_uri_input.setText(input_uri);
String input_uri = "https://m.facebook.com/events/" + eventId;
str = input_uri;
}
catch (Exception e) {
e.printStackTrace();
clear(true);
toast("Invalid URL");
str = "";
}
return str;
}
public void startScraping() {
String str = checkURI(field_uri_input.getText().toString());
field_uri_input.setText(str);
try {
FbScraper scraper = new FbScraper(this, field_uri_input.getText().toString());
scraper.execute();
}
catch (Exception e) {
e.printStackTrace();