diff --git a/src/main/java/de/thm/arsnova/dao/NovaView.java b/src/main/java/de/thm/arsnova/dao/NovaView.java new file mode 100644 index 0000000000000000000000000000000000000000..b73bb9ee8521ce896dff2182bb9463c84aa65d20 --- /dev/null +++ b/src/main/java/de/thm/arsnova/dao/NovaView.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2012 THM webMedia + * + * This file is part of ARSnova. + * + * ARSnova 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. + * + * ARSnova 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 de.thm.arsnova.dao; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +import com.fourspaces.couchdb.View; + +public class NovaView extends View { + + public NovaView(String fullname) { + super(fullname); + } + + @Override + public void setStartKey(String key) { + this.startKey = quote(key); + } + + @Override + public void setEndKey(String key) { + this.endKey = quote(key); + } + + public void setStartKey(String... keys) { + this.startKey = toJsonArray(keys); + } + + public void setEndKey(String... keys) { + this.endKey = toJsonArray(keys); + } + + @Override + public void setKey(String key) { + this.key = quote(key); + } + + public void setKey(String... keys) { + this.key = toJsonArray(keys); + } + + private String toJsonArray(String[] strings) { + StringBuilder sb = new StringBuilder(); + for (String str : strings) { + if (str.equals("{}")) { + sb.append(str + ","); + } else { + sb.append("\"" + str + "\"" + ","); + } + } + sb.replace(sb.length() - 1, sb.length(), ""); // remove final comma + sb.insert(0, "["); + sb.append("]"); + return encode(sb.toString()); + } + + private String quote(String string) { + return encode("\"" + string + "\""); + } + + private String encode(String string) { + try { + return URLEncoder.encode(string, "UTF-8"); + } catch (UnsupportedEncodingException e) { + // Since we're using 'UTF-8', this should Exception should never occur. + } + return ""; + } +} diff --git a/src/test/java/de/thm/arsnova/dao/NovaViewTest.java b/src/test/java/de/thm/arsnova/dao/NovaViewTest.java new file mode 100644 index 0000000000000000000000000000000000000000..93595a6ab463f471eb51675039f619eddec1712e --- /dev/null +++ b/src/test/java/de/thm/arsnova/dao/NovaViewTest.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2012 THM webMedia + * + * This file is part of ARSnova. + * + * ARSnova 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. + * + * ARSnova 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 de.thm.arsnova.dao; + +import static org.junit.Assert.*; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +import org.junit.Test; + +public class NovaViewTest { + + @Test + public void setKeyShouldAcceptSingleArgument() { + NovaView v = new NovaView(null); + v.setKey("foo"); + assertEncodedEquals("key", "\"foo\"", v.getQueryString()); + } + + @Test + public void setKeyShouldAcceptMultipleArgument() { + NovaView v = new NovaView(null); + v.setKey("foo", "bar", "baz"); + assertEncodedEquals("key", "[\"foo\",\"bar\",\"baz\"]", v.getQueryString()); + } + + @Test + public void setStartKeyShouldAcceptSingleArgument() { + NovaView v = new NovaView(null); + v.setStartKey("foo"); + assertEncodedEquals("startkey", "\"foo\"", v.getQueryString()); + } + + @Test + public void setEndKeyShouldAcceptSingleArgument() { + NovaView v = new NovaView(null); + v.setEndKey("foo"); + assertEncodedEquals("endkey", "\"foo\"", v.getQueryString()); + } + + @Test + public void setStartKeyShouldAcceptMultipleArgument() { + NovaView v = new NovaView(null); + v.setStartKey("foo", "bar", "baz"); + assertEncodedEquals("startkey", "[\"foo\",\"bar\",\"baz\"]", v.getQueryString()); + } + + @Test + public void setEndKeyShouldAcceptMultipleArgument() { + NovaView v = new NovaView(null); + v.setEndKey("foo", "bar", "baz"); + assertEncodedEquals("endkey", "[\"foo\",\"bar\",\"baz\"]", v.getQueryString()); + } + + @Test + public void keysShouldSupportEmptyObject() { + NovaView v = new NovaView(null); + v.setKey("foo", "bar", "{}"); + assertEncodedEquals("key", "[\"foo\",\"bar\",{}]", v.getQueryString()); + } + + private void assertEncodedEquals(String key, String expected, String actual) { + try { + assertEquals(key + "=" + URLEncoder.encode(expected, "UTF-8"), actual); + } catch (UnsupportedEncodingException e) { + fail(e.getLocalizedMessage()); + } + } +}