Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ARSnova Backend
Manage
Activity
Members
Labels
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Privacy
Imprint
Contact
Snippets
Groups
Projects
Show more breadcrumbs
Paul-Christian Volkmer
ARSnova Backend
Commits
a8b4ef43
Commit
a8b4ef43
authored
11 years ago
by
Christoph Thelen
Browse files
Options
Downloads
Patches
Plain Diff
Added easier to use CouchDB view
parent
84d5a26f
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/de/thm/arsnova/dao/NovaView.java
+86
-0
86 additions, 0 deletions
src/main/java/de/thm/arsnova/dao/NovaView.java
src/test/java/de/thm/arsnova/dao/NovaViewTest.java
+86
-0
86 additions, 0 deletions
src/test/java/de/thm/arsnova/dao/NovaViewTest.java
with
172 additions
and
0 deletions
src/main/java/de/thm/arsnova/dao/NovaView.java
0 → 100644
+
86
−
0
View file @
a8b4ef43
/*
* 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
""
;
}
}
This diff is collapsed.
Click to expand it.
src/test/java/de/thm/arsnova/dao/NovaViewTest.java
0 → 100644
+
86
−
0
View file @
a8b4ef43
/*
* 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
());
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment