From 580c0bed80a0b417cd7c773114df6c2517fff8e0 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Mon, 5 Mar 2018 09:02:00 +0000 Subject: [PATCH] Fix ConnectionManager and add test for LoginCommand --- .../wikidata/editing/ConnectionManager.java | 10 +++---- .../wikidata/commands/LoginCommandTest.java | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 extensions/wikidata/tests/src/org/openrefine/wikidata/commands/LoginCommandTest.java diff --git a/extensions/wikidata/src/org/openrefine/wikidata/editing/ConnectionManager.java b/extensions/wikidata/src/org/openrefine/wikidata/editing/ConnectionManager.java index ad26bbb5c..c63a5b2ea 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/editing/ConnectionManager.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/editing/ConnectionManager.java @@ -54,13 +54,10 @@ public class ConnectionManager { private PreferenceStore prefStore; private ApiConnection connection; - private static class ConnectionManagerHolder { - - private static final ConnectionManager instance = new ConnectionManager(); - } + private static final ConnectionManager instance = new ConnectionManager(); public static ConnectionManager getInstance() { - return ConnectionManagerHolder.instance; + return instance; } private ConnectionManager() { @@ -78,7 +75,6 @@ public class ConnectionManager { obj.put("password", password); array.put(obj); prefStore.put(PREFERENCE_STORE_KEY, array); - // TODO save preferences (find out how) } catch (JSONException e) { e.printStackTrace(); } @@ -108,7 +104,7 @@ public class ConnectionManager { public JSONObject getStoredCredentials() { JSONArray array = (JSONArray) prefStore.get(PREFERENCE_STORE_KEY); - if (array.length() > 0) { + if (array != null && array.length() > 0) { try { return array.getJSONObject(0); } catch (JSONException e) { diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/LoginCommandTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/LoginCommandTest.java new file mode 100644 index 000000000..ae7f8cf58 --- /dev/null +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/commands/LoginCommandTest.java @@ -0,0 +1,27 @@ +package org.openrefine.wikidata.commands; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import javax.servlet.ServletException; + +import org.json.JSONException; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class LoginCommandTest extends CommandTest { + + @BeforeMethod + public void SetUp() + throws JSONException { + command = new LoginCommand(); + } + + @Test + public void testNoCredentials() throws ServletException, IOException { + command.doPost(request, response); + + assertEquals("{\"logged_in\":false,\"username\":null}", writer.toString()); + } +}