Author: batosai
Date: 2008-09-04 14:23:27 +0000 (Thu, 04 Sep 2008)
New Revision: 22408
Added:
trunk/apps/WoT/src/plugins/WoT/IdentityTest.java
Modified:
trunk/apps/WoT/src/plugins/WoT/Identity.java
trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java
Log:
Changed Identity.id to String as unit tests demonstrated that querying on
byte[] didn't work.
Modified: trunk/apps/WoT/src/plugins/WoT/Identity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/Identity.java 2008-09-04 12:41:26 UTC
(rev 22407)
+++ trunk/apps/WoT/src/plugins/WoT/Identity.java 2008-09-04 14:23:27 UTC
(rev 22408)
@@ -27,7 +27,7 @@
public class Identity {
@SuppressWarnings("unused")
- private byte[] id;
+ private String id;
private FreenetURI requestURI;
private Date lastChange;
@@ -45,7 +45,10 @@
props = new HashMap<String, String>();
contexts = new ArrayList<String>();
contexts.add(context);
- id = getRequestURI().getRoutingKey();
+
+ int start = getRequestURI().toString().indexOf(',');
+ int end = getRequestURI().toString().indexOf(',', start + 1);
+ id = getRequestURI().toString().substring(start, end);
}
public Identity (String requestURI, String nickName, String
publishTrustList, String context) throws InvalidParameterException,
MalformedURLException {
@@ -55,7 +58,8 @@
@SuppressWarnings("unchecked")
- public static Identity getById (ObjectContainer db, byte[] id) throws
DuplicateIdentityException, UnknownIdentityException {
+ public static Identity getById (ObjectContainer db, String id) throws
DuplicateIdentityException, UnknownIdentityException {
+
Query query = db.query();
query.constrain(Identity.class);
query.descend("id").constrain(id);
@@ -66,12 +70,15 @@
return result.next();
}
- public static Identity getByURI (ObjectContainer db, FreenetURI uri)
throws UnknownIdentityException, DuplicateIdentityException {
- return getById(db, uri.getRoutingKey());
+ public static Identity getByURI (ObjectContainer db, String uri) throws
UnknownIdentityException, DuplicateIdentityException {
+ int start = uri.indexOf(',');
+ int end = uri.indexOf(',', start + 1);
+
+ return getById(db, uri.substring(start,end));
}
- public static Identity getByURI (ObjectContainer db, String uri) throws
UnknownIdentityException, DuplicateIdentityException, MalformedURLException {
- return getByURI(db, new FreenetURI(uri));
+ public static Identity getByURI (ObjectContainer db, FreenetURI uri)
throws UnknownIdentityException, DuplicateIdentityException {
+ return getByURI(db, uri.toString());
}
Added: trunk/apps/WoT/src/plugins/WoT/IdentityTest.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/IdentityTest.java
(rev 0)
+++ trunk/apps/WoT/src/plugins/WoT/IdentityTest.java 2008-09-04 14:23:27 UTC
(rev 22408)
@@ -0,0 +1,81 @@
+/**
+ * This code is part of WoT, a plugin for Freenet. It is distributed
+ * under the GNU General Public License, version 2 (or at your option
+ * any later version). See http://www.gnu.org/ for details of the GPL.
+ */
+package plugins.WoT;
+
+import java.io.File;
+import java.net.MalformedURLException;
+
+import com.db4o.Db4o;
+import com.db4o.ObjectContainer;
+import com.db4o.ObjectSet;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Julien Cornuwel (batosai at freenetproject.org)
+ */
+public class IdentityTest extends TestCase {
+
+ private Identity identity;
+ private ObjectContainer db;
+
+ public IdentityTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ identity = new Identity("USK at
MF2Vc6FRgeFMZJ0s2l9hOop87EYWAydUZakJzL0OfV8,fQeN-RMQZsUrDha2LCJWOMFk1-EiXZxfTnBT8NEgY00,AQACAAE/WoT/0",
+ "Seed Identity",
+ "true",
+ "boostrap");
+
+ db = Db4o.openFile("test.db4o");
+ db.store(identity);
+ }
+
+ protected void tearDown() throws Exception {
+ db.close();
+ new File("test.db4o").delete();
+ }
+
+ public void testIdentityStored() {
+ ObjectSet<Identity> result = db.queryByExample(Identity.class);
+ assertEquals(result.size(), 1);
+ }
+
+ public void testGetByURI() throws MalformedURLException,
UnknownIdentityException, DuplicateIdentityException {
+ assertNotNull(Identity.getByURI(db, "USK at
MF2Vc6FRgeFMZJ0s2l9hOop87EYWAydUZakJzL0OfV8,fQeN-RMQZsUrDha2LCJWOMFk1-EiXZxfTnBT8NEgY00,AQACAAE/WoT/0"));
+ }
+
+ public void testContexts() throws InvalidParameterException {
+ assertFalse(identity.hasContext("foo"));
+ identity.addContext("test", db);
+ assertTrue(identity.hasContext("test"));
+ identity.removeContext("test", db);
+ assertFalse(identity.hasContext("test"));
+ }
+
+ public void testProperties() {
+ try {
+ identity.setProp("foo", "bar", db);
+ } catch (InvalidParameterException e) {}
+
+ try {
+ assertTrue(identity.getProp("foo").equals("bar"));
+ } catch (InvalidParameterException e) { fail(); }
+
+ try {
+ identity.removeProp("foo", db);
+ } catch (InvalidParameterException e) { fail(); }
+
+ try {
+ identity.getProp("foo");
+ fail();
+ } catch (InvalidParameterException e) {}
+ }
+
+}
Modified: trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java
===================================================================
--- trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java 2008-09-04 12:41:26 UTC
(rev 22407)
+++ trunk/apps/WoT/src/plugins/WoT/OwnIdentity.java 2008-09-04 14:23:27 UTC
(rev 22408)
@@ -60,7 +60,7 @@
@SuppressWarnings("unchecked")
- public static OwnIdentity getById (ObjectContainer db, byte[] id)
throws UnknownIdentityException, DuplicateIdentityException {
+ public static OwnIdentity getById (ObjectContainer db, String id)
throws UnknownIdentityException, DuplicateIdentityException {
Query query = db.query();
query.constrain(OwnIdentity.class);
query.descend("id").constrain(id);
@@ -71,14 +71,18 @@
return result.next();
}
+ public static OwnIdentity getByURI (ObjectContainer db, String uri)
throws UnknownIdentityException, DuplicateIdentityException {
+ int start = uri.indexOf(',');
+ int end = uri.indexOf(',', start + 1);
+
+ return getById(db, uri.substring(start,end));
+
+ }
+
public static OwnIdentity getByURI (ObjectContainer db, FreenetURI uri)
throws UnknownIdentityException, DuplicateIdentityException {
- return getById(db, uri.getRoutingKey());
+ return getByURI(db, uri.toString());
}
- public static OwnIdentity getByURI (ObjectContainer db, String uri)
throws UnknownIdentityException, DuplicateIdentityException,
MalformedURLException {
- return getByURI(db, new FreenetURI(uri));
- }
-
public void exportToXML(ObjectContainer db, OutputStream os) throws
ParserConfigurationException, TransformerConfigurationException,
TransformerException, FileNotFoundException, IOException, Db4oIOException,
DatabaseClosedException, InvalidParameterException {