|
From: <ian...@us...> - 2007-07-06 02:24:42
|
Revision: 215
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=215&view=rev
Author: iansmith
Date: 2007-07-05 19:24:44 -0700 (Thu, 05 Jul 2007)
Log Message:
-----------
First successful use of mocks with the WebAPIClient. Can
run tests of the template sync tool <B>without</B> having
the server up. Woo-hoo!
Modified Paths:
--------------
spaces/trunk/build.xml
spaces/trunk/src/com/ogoglio/client/WebAPIClient.java
spaces/trunk/src/com/ogoglio/templatesync/SyncTool.java
spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java
spaces/trunk/src/com/ogoglio/templatesync/SyncToolTest.java
spaces/trunk/src/com/ogoglio/xml/TemplateDocument.java
Modified: spaces/trunk/build.xml
===================================================================
--- spaces/trunk/build.xml 2007-07-05 20:40:07 UTC (rev 214)
+++ spaces/trunk/build.xml 2007-07-06 02:24:44 UTC (rev 215)
@@ -82,6 +82,7 @@
<formatter type="brief" usefile="false" />
<test name="com.ogoglio.templatesync.TemplateSyncTestSuite" />
<test name="com.ogoglio.OgoglioTestSuite" />
+
<jvmarg value="-Demma.coverage.out.file=${coverage.dir}/coverage.emma" />
<jvmarg value="-Demma.coverage.out.merge=false" />
</junit>
Modified: spaces/trunk/src/com/ogoglio/client/WebAPIClient.java
===================================================================
--- spaces/trunk/src/com/ogoglio/client/WebAPIClient.java 2007-07-05 20:40:07 UTC (rev 214)
+++ spaces/trunk/src/com/ogoglio/client/WebAPIClient.java 2007-07-06 02:24:44 UTC (rev 215)
@@ -59,9 +59,15 @@
private AuthDocument authDocument = null;
private AccountDocument accountDocument = null;
-
+ private static final URI NO_SPACE = null; /* constant representing that we want no space connection */
+
+ public WebAPIClient(URI serviceURI, String authCookie) throws IOException {
+ this(NO_SPACE,serviceURI,authCookie);
+ }
public WebAPIClient(URI spaceURI, URI serviceURI, String authCookie) throws IOException {
- ArgumentUtils.assertNotNull(spaceURI);
+ //seems that we have to allow the null spaceURI to indicate that
+ //the client has no interest in a space
+ //ArgumentUtils.assertNotNull(spaceURI);
this.spaceURI = spaceURI;
ArgumentUtils.assertNotNull(serviceURI);
this.serviceURI = serviceURI;
@@ -806,5 +812,7 @@
public URI getSettingURI(String key) {
return WebAPIUtil.appendToURI(getSettingsURI(), key + "/");
}
-
+ public String toString() {
+ return "<WebAPI:"+serviceURI+">";
+ }
}
Modified: spaces/trunk/src/com/ogoglio/templatesync/SyncTool.java
===================================================================
--- spaces/trunk/src/com/ogoglio/templatesync/SyncTool.java 2007-07-05 20:40:07 UTC (rev 214)
+++ spaces/trunk/src/com/ogoglio/templatesync/SyncTool.java 2007-07-06 02:24:44 UTC (rev 215)
@@ -5,20 +5,30 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import com.ogoglio.client.WebAPIAuthenticator;
+import com.ogoglio.client.WebAPIClient;
+import com.ogoglio.xml.TemplateDocument;
public class SyncTool {
- public static final String ABORT_NO_SPACES =
- "Aborting: Checked CWD and $HOME for \"spaces\" and \".spaces\" but found none.";
+ public static final String ABORT_NO_TEMPLATES_DISK =
+ "Aborting: Checked local disk in CWD and $HOME for \"templates\" and \".templates\" but found none.";
+ public static final String ABORT_NO_TEMPLATES_SERVER =
+ "Aborting: Checked server and you don't have any templates.";
public static final String ABORT_USAGE =
"usage SyncTool username password serviceURI";
public static final String ABORT_BAD_PW =
"username/password pair unable to authenticate to that service";
private WebAPIAuthenticator theDecider=new WebAPIAuthenticator();
+ String cookie;
+ Map serverTemplateNamesToIds;
+ //Troubling: Can't test this the way I want to because the OS calls main and there is no
+ //Troubling: no way to introduce things in-between the two
public static void main(String[] args) {
SyncTool self = new SyncTool();
try {
@@ -29,7 +39,7 @@
}
}
- public List listSpacesToSynchronize(String dir) {
+ public List listTemplatesToSynchronize(String dir) {
File f = new File(dir);
List result = new ArrayList();
@@ -53,14 +63,14 @@
public List candidatesDirsFromCriticalDirs(String path1, String path2) {
List result = new ArrayList();
- result.add(path1 + File.separatorChar + "spaces");
- result.add(path1 + File.separatorChar + ".spaces");
- result.add(path2 + File.separatorChar + "spaces");
- result.add(path2 + File.separatorChar + ".spaces");
+ result.add(path1 + File.separatorChar + "templates");
+ result.add(path1 + File.separatorChar + ".templates");
+ result.add(path2 + File.separatorChar + "templates");
+ result.add(path2 + File.separatorChar + ".templates");
return result;
}
- public List findUsersSpaces() {
+ public List findUsersTemplates() {
List result=null;
String cwd=System.getProperty("user.dir");
@@ -68,7 +78,7 @@
List candidateList = candidatesDirsFromCriticalDirs(cwd, home);
for (int i=0; i<candidateList.size();++i) {
- result=listSpacesToSynchronize((String)candidateList.get(i));
+ result=listTemplatesToSynchronize((String)candidateList.get(i));
if (result!=null) {
return result;
}
@@ -76,29 +86,60 @@
return result;
}
+ //Troubling: One really can't test this properly because you can't mock System
public void abort(String msg) {
System.out.println(msg);
System.exit(1);
}
- public void start(String[] argv) throws IOException, URISyntaxException{
+ public void start(String[] argv) throws IOException, URISyntaxException{
+ URI serviceURI;
+
if (argv.length!=3) {
abort(ABORT_USAGE);
return; //if you don't understand why this is here, don't mess with it
- }
- String cookie = getAuthenticator().authenticate(new URI(argv[2]), argv[0], argv[1]);
+ }
+ serviceURI = new URI(argv[2]);
+ cookie = getAuthenticator().authenticate(serviceURI, argv[0], argv[1]);
if (cookie==null) {
abort(ABORT_BAD_PW);
return; //if you don't understand why this is here, don't mess with it
}
- List spaceNames = findUsersSpaces();
- if (spaceNames==null) {
- abort(ABORT_NO_SPACES);
+ List templateNames = findUsersTemplates();
+ if (templateNames==null) {
+ abort(ABORT_NO_TEMPLATES_DISK);
return; //if you don't understand why this is here, don't mess with it
}
+ WebAPIClient client = new WebAPIClient(serviceURI,cookie);
+ serverTemplateNamesToIds = getServerTemplateList(client);
+
+ for (int i=0; i<templateNames.size();++i) {
+ syncTemplate((String)templateNames.get(i),client);
+ }
}
+ public Map getServerTemplateList(WebAPIClient client) throws IOException {
+ System.out.println("Fetching server templates...");
+ String user = client.getAuthDocument(true).getUsername();
+ TemplateDocument docs[] = client.getTemplateDocuments(user);
+ Map result=new HashMap();
+
+ if (docs.length==0) {
+ abort(ABORT_NO_TEMPLATES_SERVER);
+ return null; //please don't mess with this
+ }
+ for (int i=0; i<docs.length; ++i) {
+ TemplateDocument doc=docs[i];
+ result.put(doc.getDisplayName(),new Long(doc.getTemplateID()));
+ }
+ return result;
+ }
+
+ public void syncTemplate(String name,WebAPIClient client) {
+
+ }
+
public WebAPIAuthenticator getAuthenticator() {
return theDecider;
}
Modified: spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java
===================================================================
--- spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java 2007-07-05 20:40:07 UTC (rev 214)
+++ spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java 2007-07-06 02:24:44 UTC (rev 215)
@@ -5,19 +5,26 @@
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
import com.agical.rmock.extension.junit.RMockTestCase;
import com.ogoglio.client.WebAPIAuthenticator;
+import com.ogoglio.client.WebAPIClient;
+import com.ogoglio.xml.AuthDocument;
+import com.ogoglio.xml.TemplateDocument;
public class SyncToolSpec extends RMockTestCase {
- public SyncTool mock; //we use this in every spec
-
+ public SyncTool mock;
+ public WebAPIClient client;
+
public static final String SVC="http://transmutable.gov",USER="doofus",PW="somepw";
public static final String[] CMD_LINE_ARGS = { USER, PW, SVC};
- public void setUp() {
+ public void setUp() throws URISyntaxException{
mock=(SyncTool)mock(SyncTool.class);
+ client = (WebAPIClient)mock(WebAPIClient.class,
+ new Object[]{null,new URI("http://transmutable.com"),"cookie-tastic"},"client");
}
private List createFakeList(int numberOfFakeItems,String prefix) {
List result = new ArrayList();
@@ -30,20 +37,20 @@
beginSection(s.ordered("set of directories checked"));
{
- mock.findUsersSpaces();
+ mock.findUsersTemplates();
modify().forward();
mock.candidatesDirsFromCriticalDirs("cwd","home dir");
modify().args(is.NOT_NULL,is.NOT_NULL).returnValue(fakeDirList);
- mock.listSpacesToSynchronize("points to a dir");
+ mock.listTemplatesToSynchronize("points to a dir");
modify().args(is.NOT_NULL);
modify().multiplicity(expect.exactly(ITERS)).returnValue(null);
}
endSection();
startVerification();
- mock.findUsersSpaces();
+ mock.findUsersTemplates();
}
public void checkWrongNumberOfArgs(int argsSupplied) throws IOException, URISyntaxException {
@@ -92,32 +99,92 @@
mock.start(CMD_LINE_ARGS);
}
- //basically test that all the startup crap completes ok so we can get started testing that spaces
+ //basically test that all the startup crap completes ok so we can get started testing that templates
//actually can sync
- public void testSyncSpaceCalledOnceForEachSpaceName() throws IOException, URISyntaxException{
+ public void testSyncTemplateCalledOnceForEachTemplateName() throws IOException, URISyntaxException{
List fakeMagicDirs= createFakeList(2,"dir");
- List fakeSpaces = createFakeList(2,"space");
+ List fakeTemplates = createFakeList(2,"templ");
+ String COOKIE = "someCookieSoAuthOK";
- checkAuthorizationSequence("someCookieSoAuthOK");
-
+ checkAuthorizationSequence(COOKIE);
beginSection(s.ordered("order of looking in dirs makes sense"));
{
- mock.findUsersSpaces();
+ mock.findUsersTemplates();
modify().forward();
mock.candidatesDirsFromCriticalDirs("really CWD", "really $HOME");
modify().args(is.instanceOf(String.class).and(is.NOT_NULL),is.instanceOf(String.class).and(is.NOT_NULL));
modify().returnValue(fakeMagicDirs);
- mock.listSpacesToSynchronize((String)fakeMagicDirs.get(0));
+ mock.listTemplatesToSynchronize((String)fakeMagicDirs.get(0));
modify().returnValue(null);
- mock.listSpacesToSynchronize((String)fakeMagicDirs.get(1));
- modify().returnValue(fakeSpaces);
+ mock.listTemplatesToSynchronize((String)fakeMagicDirs.get(1));
+ modify().returnValue(fakeTemplates);
+
+ mock.getServerTemplateList(null);
+ modify().args(is.instanceOf(WebAPIClient.class));
+
+ mock.syncTemplate((String)fakeTemplates.get(0),null);
+ modify().args(is.AS_RECORDED,is.instanceOf(WebAPIClient.class));
+ mock.syncTemplate((String)fakeTemplates.get(1),null);
+ modify().args(is.AS_RECORDED,is.instanceOf(WebAPIClient.class));
}
endSection();
startVerification();
mock.start(CMD_LINE_ARGS);
+ assertThat(mock.cookie,is.eq(COOKIE));
}
+
+ private void prepareForCallToListTemplates(WebAPIClient api,TemplateDocument[] templates)
+ throws IOException, URISyntaxException{
+ String USER = "some user";
+
+ AuthDocument authDoc = (AuthDocument)mock(AuthDocument.class,"authDoc");
+
+ api.getAuthDocument(true);
+ modify().returnValue(authDoc);
+
+ authDoc.getUsername();
+ modify().returnValue(USER);
+
+ mock.getServerTemplateList(api);
+ modify().args(is.instanceOf(WebAPIClient.class));
+ modify().forward();
+
+ api.getTemplateDocuments("someuser");
+ modify().args(is.instanceOf(String.class)).returnValue(templates);
+
+ }
+ public void testFailsWithNoTemplates() throws IOException,URISyntaxException {
+
+ prepareForCallToListTemplates(client,new TemplateDocument[]{});
+ mock.abort(SyncTool.ABORT_NO_TEMPLATES_SERVER);
+
+ startVerification();
+ mock.getServerTemplateList(client);
+ }
+ public void testMapCreatedByListingTemplates() throws IOException,URISyntaxException {
+ String DISPLAY_NAME="myTemplate";
+ long ID=82872;
+
+ TemplateDocument templateMock = (TemplateDocument)mock(TemplateDocument.class,
+ new Object[] {new Long(ID),DISPLAY_NAME,"owner","description"},
+ "templateMock");
+
+ prepareForCallToListTemplates(client,new TemplateDocument[]{templateMock});
+
+ templateMock.getDisplayName();
+ modify().forward();
+ templateMock.getTemplateID();
+ modify().forward();
+
+ startVerification();
+ Map map = mock.getServerTemplateList(client);
+ assertThat(map.size(),is.eq(1));
+ assertThat(map.containsKey(DISPLAY_NAME),is.TRUE);
+ assertThat(map.containsValue(new Long(ID)),is.TRUE);
+ }
+
}
Modified: spaces/trunk/src/com/ogoglio/templatesync/SyncToolTest.java
===================================================================
--- spaces/trunk/src/com/ogoglio/templatesync/SyncToolTest.java 2007-07-05 20:40:07 UTC (rev 214)
+++ spaces/trunk/src/com/ogoglio/templatesync/SyncToolTest.java 2007-07-06 02:24:44 UTC (rev 215)
@@ -16,8 +16,8 @@
tool = new SyncTool();
}
public void testFindSpacesDirOnBogusDirectory_unit(){
- assertThat(tool.listSpacesToSynchronize("/foo"),is.NULL);
- assertThat(tool.listSpacesToSynchronize("/bar"),is.NULL);
+ assertThat(tool.listTemplatesToSynchronize("/foo"),is.NULL);
+ assertThat(tool.listTemplatesToSynchronize("/bar"),is.NULL);
}
public void testFindSpacesDirOnGoodDirectoryGivesAListOfDirs_unit() {
@@ -38,8 +38,8 @@
}
String path = f.getPath();
- assertThat(tool.listSpacesToSynchronize(path),is.instanceOf(List.class));
- List result = tool.listSpacesToSynchronize(path);
+ assertThat(tool.listTemplatesToSynchronize(path),is.instanceOf(List.class));
+ List result = tool.listTemplatesToSynchronize(path);
assertThat(result.size(),is.eq(2));
assertThat(result.contains(dir1),is.TRUE);
@@ -58,8 +58,8 @@
List choices = tool.candidatesDirsFromCriticalDirs(FART,BARF);
assertThat(choices.size(),is.eq(4));
- assertThat(choices.get(0),is.eq(FART+File.separatorChar+"spaces")); //prefer no dot
- assertThat(choices.get(3),is.eq(BARF+File.separatorChar+".spaces"));
+ assertThat(choices.get(0),is.eq(FART+File.separatorChar+"templates")); //prefer no dot
+ assertThat(choices.get(3),is.eq(BARF+File.separatorChar+".templates"));
}
public void testBadURIFormat() throws IOException, URISyntaxException{
@@ -70,7 +70,6 @@
expectThatExceptionThrown(is.instanceOf(IOException.class));
tool.getAuthenticator().authenticate(new URI("http://transmutable.com"), "", "no");
tool.getAuthenticator().authenticate(new URI("http://transmutable.com"), null, "no");
-
}
}
Modified: spaces/trunk/src/com/ogoglio/xml/TemplateDocument.java
===================================================================
--- spaces/trunk/src/com/ogoglio/xml/TemplateDocument.java 2007-07-05 20:40:07 UTC (rev 214)
+++ spaces/trunk/src/com/ogoglio/xml/TemplateDocument.java 2007-07-06 02:24:44 UTC (rev 215)
@@ -35,6 +35,10 @@
this(templateRecord.getTemplateID(), templateRecord.getDisplayName(), templateRecord.getOwnerUsername(), templateRecord.getDescription());
}
+ //for convenience of people who are creating this dynamically via the mock system
+ public TemplateDocument(Long templateID, String displayName, String ownerUsername, String description) {
+ this(templateID.longValue(),displayName,ownerUsername,description);
+ }
public TemplateDocument(long templateID, String displayName, String ownerUsername, String description) {
data = new XMLElement(NAME);
data.setAttribute(TEMPLATE_ID, templateID);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|