Revision: 1864
Author: [email protected]
Date: Fri Mar  5 09:49:24 2010
Log: Added method to get obfuscated version of email address to prevent bot harvesting + unit test. ( issue 257)


http://code.google.com/p/simal/source/detail?r=1864

Added:
/trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/model/jena/TestInternetAddress.java
Modified:
/trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/model/IInternetAddress.java /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/model/jena/InternetAddress.java /trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/integrationTest/model/repository/BaseRepositoryTest.java

=======================================
--- /dev/null
+++ /trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/model/jena/TestInternetAddress.java Fri Mar 5 09:49:24 2010
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2010 University of Oxford
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package uk.ac.osswatch.simal.model.jena;
+
+import static junit.framework.Assert.*;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import uk.ac.osswatch.simal.SimalRepositoryFactory;
+import uk.ac.osswatch.simal.model.IInternetAddress;
+import uk.ac.osswatch.simal.model.IPerson;
+import uk.ac.osswatch.simal.model.ModelSupport;
+import uk.ac.osswatch.simal.rdf.ISimalRepository;
+import uk.ac.osswatch.simal.rdf.SimalRepositoryException;
+
+/**
+ * Test the class uk.ac.osswatch.simal.model.jena.InternetAddress, especially
+ * the method getObfuscatedAddress()
+ */
+public class TestInternetAddress {
+
+  protected static ISimalRepository repository;
+
+ private static final String TEST_OSS_WATCH_DOAP = "testData/ossWatchDOAP.xml";
+
+ private static final String TEST_DEVELOPER_SEE_ALSO = "http://people.apache.org/~rgardler/foaf.rdf.xml";;
+
+ private static final Map<String, String> EXPECTED_OBFUSCATED_EMAILS = new HashMap<String, String>();
+
+  @BeforeClass
+  public static void setUpBeforeClass() throws Exception {
+    repository = SimalRepositoryFactory
+        .getInstance(SimalRepositoryFactory.JENA);
+    if (!repository.isInitialised()) {
+      repository.initialise(null);
+    }
+
+ repository.addProject(ISimalRepository.class.getClassLoader().getResource(
+        TEST_OSS_WATCH_DOAP), ModelSupport.TEST_FILE_BASE_URL);
+    EXPECTED_OBFUSCATED_EMAILS.put("mailto:[email protected]";,
+        "mailto:rgardler [at] apache...org");
+    EXPECTED_OBFUSCATED_EMAILS.put("mailto:[email protected]";,
+        "mailto:ross.gardler [at] oucs...ox...ac...uk");
+  }
+
+  @Test
+  public void testInternetAddress() {
+    try {
+      IPerson developer = SimalRepositoryFactory.getPersonService()
+          .findBySeeAlso(TEST_DEVELOPER_SEE_ALSO);
+      if (developer != null) {
+        Set<IInternetAddress> allEmailAddresses = developer.getEmail();
+        for (IInternetAddress testAddress : allEmailAddresses) {
+          assertEquals("Obfuscated addresses don't match.",
+              EXPECTED_OBFUSCATED_EMAILS.get(testAddress.getAddress()),
+              testAddress.getObfuscatedAddress());
+        }
+      } else {
+        fail("Could not find right developer using seeAlso "
+            + TEST_DEVELOPER_SEE_ALSO);
+      }
+    } catch (SimalRepositoryException e) {
+      fail("Could not retreive test developer: " + e.getMessage());
+    }
+  }
+}
=======================================
--- /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/model/IInternetAddress.java Tue Nov 24 16:08:08 2009 +++ /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/model/IInternetAddress.java Fri Mar 5 09:49:24 2010
@@ -29,4 +29,11 @@
    */
   public String getAddress();

-}
+  /**
+   * Get the complete email address in a way that is not.
+   * directly machine-readable.
+   * @return
+   */
+  public String getObfuscatedAddress();
+
+}
=======================================
--- /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/model/jena/InternetAddress.java Thu Sep 18 14:53:18 2008 +++ /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/model/jena/InternetAddress.java Fri Mar 5 09:49:24 2010
@@ -30,6 +30,24 @@
   public InternetAddress(com.hp.hpl.jena.rdf.model.Resource resource) {
     super(resource);
   }
+
+  public String getObfuscatedAddress() {
+    String address = getURI();
+    if (address != null) {
+      int atPosition = address.indexOf("@");
+      if (atPosition > -1) {
+        StringBuffer obfuscatedAddress = new StringBuffer();
+
+        obfuscatedAddress.append(address.substring(0, atPosition));
+        obfuscatedAddress.append(" [at] ");
+ obfuscatedAddress.append(address.substring(atPosition + 1).replaceAll(
+            "[.]", "..."));
+        address = obfuscatedAddress.toString();
+      }
+    }
+
+    return address;
+  }

   public String getAddress() {
     return getURI();
=======================================
--- /trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/integrationTest/model/repository/BaseRepositoryTest.java Sun Jul 12 11:57:46 2009 +++ /trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/integrationTest/model/repository/BaseRepositoryTest.java Fri Mar 5 09:49:24 2010
@@ -132,7 +132,7 @@
project1 = SimalRepositoryFactory.getProjectService().findProjectBySeeAlso(TEST_PROJECT_URI); IPerson developer = SimalRepositoryFactory.getPersonService().findBySeeAlso("http://foo.org/~developer/#me";);
     testDeveloperID = developer.getUniqueSimalID();
-    testDeveloperEMail = developer.getEmail().toArray()[0].toString();
+ testDeveloperEMail = developer.getEmail().iterator().next().getAddress();

IPerson documentor = SimalRepositoryFactory.getPersonService().findBySeeAlso("http://foo.org/~documentor/#me";);
     String documentorID = documentor.getUniqueSimalID();

--
You received this message because you are subscribed to the Google Groups "Simal 
Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/simal-commits?hl=en.

Reply via email to