Revision: 1792
Author: [email protected]
Date: Fri Dec 18 16:46:01 2009
Log: Add a method for retrieving multiple projects from Ohloh by supplying  
their project IDs in a text file.
http://code.google.com/p/simal/source/detail?r=1792

Added:
   
/trunk/uk.ac.osswatch.simal.core/src/main/resources/testData/ohlohTestData.txt
   
/trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/importData/test/TestOhlohImport.java
Modified:
   
/trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/importData/Ohloh.java

=======================================
--- /dev/null
+++  
/trunk/uk.ac.osswatch.simal.core/src/main/resources/testData/ohlohTestData.txt  
 
Fri Dec 18 16:46:01 2009
@@ -0,0 +1,6 @@
+# projects to import from Ohloh
+simal
+
+# Apache projects
+apache
+forrest
=======================================
--- /dev/null
+++  
/trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/importData/test/TestOhlohImport.java
         
Fri Dec 18 16:46:01 2009
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2009 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.importData.test;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import uk.ac.osswatch.simal.SimalRepositoryFactory;
+import uk.ac.osswatch.simal.importData.Ohloh;
+import  
uk.ac.osswatch.simal.integrationTest.model.repository.BaseRepositoryTest;
+import uk.ac.osswatch.simal.model.IDoapHomepage;
+import uk.ac.osswatch.simal.model.IProject;
+import uk.ac.osswatch.simal.rdf.DuplicateURIException;
+import uk.ac.osswatch.simal.rdf.ISimalRepository;
+import uk.ac.osswatch.simal.rdf.SimalRepositoryException;
+
+public class TestOhlohImport extends BaseRepositoryTest {
+         private static final Logger logger =  
LoggerFactory.getLogger(TestOhlohImport.class);
+
+       private static ISimalRepository repo;
+       public final static String OHLOH_TEST_DATA = 
"testData/ohlohTestData.txt";
+
+       @BeforeClass
+       public static void importTestData() throws SimalRepositoryException {
+               repo = SimalRepositoryFactory.getInstance();
+       }
+
+       @AfterClass
+       public static void deleteImportedData() throws SimalRepositoryException 
{
+               repo.destroy();
+       }
+
+
+       @Test
+       public void testProjectImport() throws FileNotFoundException,  
IOException, SimalRepositoryException, DuplicateURIException,  
URISyntaxException {
+               File file = new  
File(ISimalRepository.class.getClassLoader().getResource(OHLOH_TEST_DATA).toURI());
+               Ohloh ohloh = new Ohloh();
+               ohloh.importProjects(file);
+
+               Set<IProject> allProjects = repo.getAllProjects();
+               int numProjectsbefore = allProjects.size();
+               Iterator<IProject> projects = allProjects.iterator();
+               IProject project;
+
+               boolean projectAIsValid = false;
+               while (projects.hasNext()) {
+                       project = projects.next();
+                       String name = project.getName();
+                       Set<IDoapHomepage> homepages = project.getHomepages();
+                       if (name.equals("Apache HTTP Server")) {
+                               projectAIsValid = true;
+                       }
+               }
+               if (allProjects.size() <= numProjectsbefore) {
+                       // @refactor if we have no projects and no exception 
was thrown it  
means that there has been
+                       // a problem importing projects. This is probably 
because no PAI key  
has been provided.
+                       // At present we swallow this and assume.
+                       logger.error("Not retrieved any projects from Ohloh, 
please see log for  
more information (probably a missing API key)");
+               } else {
+                       assertTrue("Apache HTTPD project has not been correctly 
imported",  
projectAIsValid);
+               }
+       }
+}
=======================================
---  
/trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/importData/Ohloh.java
        
Tue Oct 27 17:57:28 2009
+++  
/trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/importData/Ohloh.java
        
Fri Dec 18 16:46:01 2009
@@ -19,6 +19,10 @@
   *
   */

+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.net.URL;
@@ -35,6 +39,8 @@
  import javax.xml.transform.dom.DOMSource;
  import javax.xml.transform.stream.StreamSource;

+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
@@ -53,6 +59,7 @@
   *
   */
  public class Ohloh {
+  private static final Logger logger =  
LoggerFactory.getLogger(Ohloh.class);
    private static final String OHLOH_BASE_URI = "http://www.ohloh.net";;

    /**
@@ -274,5 +281,47 @@
      source.append(projectID);
      return source.toString();
    }
+
+  /**
+   * Import all the projects listed in the supplied file. The file lists
+   * a series of ohloh file identifiers, each on a separate line. Lines
+   * that start with '#' will be ignored. Blank lines will also be
+   * ignored. For example:
+   *
+   * <pre>
+   * # projects to import from Ohloh
+   * simal
+   *
+   * # Apache projects
+   * apache
+   * forrest
+   * </pre>
+   *
+   * @param file the file of projects to import.
+   * @throws IOException if there is a problem reading the file
+   */
+  public void importProjects(File file) throws IOException {
+       BufferedReader in = null;
+       try {
+         in = new BufferedReader(new FileReader(file));
+      String ohlohProjectID;
+      while ((ohlohProjectID = in.readLine()) != null) {
+         if (ohlohProjectID.trim().length() != 0  
&& !ohlohProjectID.startsWith("#")) {
+              try {
+                               addProjectToSimal(ohlohProjectID);
+                       } catch (ImportException e) {
+                               logger.warn("Unable to import project from 
Ohloh: " + ohlohProjectID,  
e);
+                       } catch (SimalException e) {
+                               logger.warn("Unable to add project from Ohloh: 
" + ohlohProjectID, e);
+                       }
+         }
+      }
+       } catch (FileNotFoundException e) {
+               // TODO Auto-generated catch block
+               e.printStackTrace();
+       } finally {
+      if (in != null) in.close();
+       }
+  }

  }

--

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