|
From: <ian...@us...> - 2007-07-06 13:50:17
|
Revision: 216
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=216&view=rev
Author: iansmith
Date: 2007-07-06 06:50:18 -0700 (Fri, 06 Jul 2007)
Log Message:
-----------
Few small changes. This is a safety checkin before attempting
a significant change.
Modified Paths:
--------------
spaces/trunk/src/com/ogoglio/templatesync/SyncTool.java
spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java
Modified: spaces/trunk/src/com/ogoglio/templatesync/SyncTool.java
===================================================================
--- spaces/trunk/src/com/ogoglio/templatesync/SyncTool.java 2007-07-06 02:24:44 UTC (rev 215)
+++ spaces/trunk/src/com/ogoglio/templatesync/SyncTool.java 2007-07-06 13:50:18 UTC (rev 216)
@@ -22,6 +22,7 @@
"usage SyncTool username password serviceURI";
public static final String ABORT_BAD_PW =
"username/password pair unable to authenticate to that service";
+ public static final String ABORT_DUP_TEMPLATE_NAMES = "Aborting: Found templates with the same name!";
private WebAPIAuthenticator theDecider=new WebAPIAuthenticator();
String cookie;
@@ -120,7 +121,7 @@
}
public Map getServerTemplateList(WebAPIClient client) throws IOException {
- System.out.println("Fetching server templates...");
+ System.out.println("[synctool] Fetching server templates...");
String user = client.getAuthDocument(true).getUsername();
TemplateDocument docs[] = client.getTemplateDocuments(user);
Map result=new HashMap();
@@ -129,9 +130,22 @@
abort(ABORT_NO_TEMPLATES_SERVER);
return null; //please don't mess with this
}
+ return copyTemplateNamesAndIdsToMap(docs, result);
+ }
+
+ private Map copyTemplateNamesAndIdsToMap(TemplateDocument[] docs, Map result) {
for (int i=0; i<docs.length; ++i) {
TemplateDocument doc=docs[i];
- result.put(doc.getDisplayName(),new Long(doc.getTemplateID()));
+ if (result.containsKey(doc.getDisplayName())) {
+ System.out.println("document "+i+" true path");
+ abort(ABORT_DUP_TEMPLATE_NAMES);
+ return null; //please don't mess with this
+ } else {
+ System.out.println("document "+i+" false path "+result.size());
+ String key = doc.getDisplayName();
+ Long value = new Long(doc.getTemplateID());
+ result.put(key,value);
+ }
}
return result;
}
Modified: spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java
===================================================================
--- spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java 2007-07-06 02:24:44 UTC (rev 215)
+++ spaces/trunk/src/com/ogoglio/templatesync/SyncToolSpec.java 2007-07-06 13:50:18 UTC (rev 216)
@@ -15,14 +15,14 @@
public class SyncToolSpec extends RMockTestCase {
- public SyncTool mock;
+ public SyncTool mockSyncTool;
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() throws URISyntaxException{
- mock=(SyncTool)mock(SyncTool.class);
+ mockSyncTool=(SyncTool)mock(SyncTool.class);
client = (WebAPIClient)mock(WebAPIClient.class,
new Object[]{null,new URI("http://transmutable.com"),"cookie-tastic"},"client");
}
@@ -37,34 +37,34 @@
beginSection(s.ordered("set of directories checked"));
{
- mock.findUsersTemplates();
+ mockSyncTool.findUsersTemplates();
modify().forward();
- mock.candidatesDirsFromCriticalDirs("cwd","home dir");
+ mockSyncTool.candidatesDirsFromCriticalDirs("cwd","home dir");
modify().args(is.NOT_NULL,is.NOT_NULL).returnValue(fakeDirList);
- mock.listTemplatesToSynchronize("points to a dir");
+ mockSyncTool.listTemplatesToSynchronize("points to a dir");
modify().args(is.NOT_NULL);
modify().multiplicity(expect.exactly(ITERS)).returnValue(null);
}
endSection();
startVerification();
- mock.findUsersTemplates();
+ mockSyncTool.findUsersTemplates();
}
public void checkWrongNumberOfArgs(int argsSupplied) throws IOException, URISyntaxException {
String[] args = new String[argsSupplied];
- mock.start(args);
+ mockSyncTool.start(args);
modify().args(is.instanceOf(String[].class));
modify().forward();
- mock.abort(SyncTool.ABORT_USAGE);
+ mockSyncTool.abort(SyncTool.ABORT_USAGE);
startVerification();
- mock.start(new String[0]);
+ mockSyncTool.start(new String[0]);
}
public void testNoArgsOnCmdLine() throws IOException, URISyntaxException {
checkWrongNumberOfArgs(0);
@@ -73,16 +73,16 @@
checkWrongNumberOfArgs(2);
}
- public void checkAuthorizationSequence(String cookieValue)
+ private void prepareAuthorizationSequence(String cookieValue)
throws IOException, URISyntaxException {
WebAPIAuthenticator authMock=(WebAPIAuthenticator)mock(WebAPIAuthenticator.class,"authMock");
beginSection(s.ordered("startup ordering of param and auth checks"));
{
- mock.start(CMD_LINE_ARGS);
+ mockSyncTool.start(CMD_LINE_ARGS);
modify().forward();
- mock.getAuthenticator();
+ mockSyncTool.getAuthenticator();
modify().returnValue(authMock);
authMock.authenticate(new URI(SVC), USER, PW);
@@ -91,52 +91,65 @@
endSection();
}
public void testBadUsernameOrPasswordForService() throws IOException, URISyntaxException {
- checkAuthorizationSequence(null);
+ prepareAuthorizationSequence(null);
- mock.abort(SyncTool.ABORT_BAD_PW);
+ mockSyncTool.abort(SyncTool.ABORT_BAD_PW);
startVerification();
- mock.start(CMD_LINE_ARGS);
+ mockSyncTool.start(CMD_LINE_ARGS);
}
//basically test that all the startup crap completes ok so we can get started testing that templates
//actually can sync
public void testSyncTemplateCalledOnceForEachTemplateName() throws IOException, URISyntaxException{
- List fakeMagicDirs= createFakeList(2,"dir");
List fakeTemplates = createFakeList(2,"templ");
- String COOKIE = "someCookieSoAuthOK";
-
- checkAuthorizationSequence(COOKIE);
- beginSection(s.ordered("order of looking in dirs makes sense"));
+ String COOKIE = prepareForAListOfDiskTemplates(fakeTemplates);
+
+ mockSyncTool.getServerTemplateList(null);
+ modify().args(is.instanceOf(WebAPIClient.class));
+
+ beginSection(s.ordered("templates called in order found"));
{
- 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.listTemplatesToSynchronize((String)fakeMagicDirs.get(0));
- modify().returnValue(null);
-
- 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);
+ mockSyncTool.syncTemplate((String)fakeTemplates.get(0),null);
modify().args(is.AS_RECORDED,is.instanceOf(WebAPIClient.class));
- mock.syncTemplate((String)fakeTemplates.get(1),null);
+ mockSyncTool.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));
+ mockSyncTool.start(CMD_LINE_ARGS);
+ assertThat(mockSyncTool.cookie,is.eq(COOKIE));
}
+
+ public void testAbortWhenNoTemplatesAreOnDisk() throws IOException, URISyntaxException {
+ prepareForAListOfDiskTemplates(null);
+ mockSyncTool.abort(SyncTool.ABORT_NO_TEMPLATES_DISK);
+
+ startVerification();
+ mockSyncTool.start(CMD_LINE_ARGS);
+ }
+
+ private String prepareForAListOfDiskTemplates(List fakeTemplates) throws IOException, URISyntaxException {
+ String authCookie = "someCookieSoAuthOK";
+ List fakeMagicDirs= createFakeList(2,"dir");
+
+ prepareAuthorizationSequence(authCookie);
+ mockSyncTool.findUsersTemplates();
+ modify().forward();
+ mockSyncTool.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);
+
+ mockSyncTool.listTemplatesToSynchronize((String)fakeMagicDirs.get(0));
+ modify().returnValue(null);
+
+ mockSyncTool.listTemplatesToSynchronize((String)fakeMagicDirs.get(1));
+ modify().returnValue(fakeTemplates);
+ return authCookie;
+ }
+
private void prepareForCallToListTemplates(WebAPIClient api,TemplateDocument[] templates)
throws IOException, URISyntaxException{
String USER = "some user";
@@ -149,7 +162,7 @@
authDoc.getUsername();
modify().returnValue(USER);
- mock.getServerTemplateList(api);
+ mockSyncTool.getServerTemplateList(api);
modify().args(is.instanceOf(WebAPIClient.class));
modify().forward();
@@ -157,34 +170,62 @@
modify().args(is.instanceOf(String.class)).returnValue(templates);
}
- public void testFailsWithNoTemplates() throws IOException,URISyntaxException {
+ public void testFailsWithNoServerTemplates() throws IOException,URISyntaxException {
prepareForCallToListTemplates(client,new TemplateDocument[]{});
- mock.abort(SyncTool.ABORT_NO_TEMPLATES_SERVER);
+ mockSyncTool.abort(SyncTool.ABORT_NO_TEMPLATES_SERVER);
startVerification();
- mock.getServerTemplateList(client);
+ mockSyncTool.getServerTemplateList(client);
}
+
+ private TemplateDocument makeTemplateMockAndPrepIt(String displayName, long id, String mockName,
+ boolean andDoPrep) {
+ TemplateDocument docMock=(TemplateDocument)mock(TemplateDocument.class,
+ new Object[] {new Long(id),displayName,"owner","description"},
+ mockName);
+ if (andDoPrep) {
+ prepareTemplateMockToBeQueried(docMock);
+ }
+ return docMock;
+ }
+ private void prepareTemplateMockToBeQueried(TemplateDocument doc) {
+ doc.getDisplayName();
+ modify().multiplicity(expect.exactly(2));
+ modify().forward();
+ doc.getTemplateID();
+ modify().forward();
+ }
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");
+ TemplateDocument templateMock = makeTemplateMockAndPrepIt(DISPLAY_NAME, ID,"templateMock",true);
- prepareForCallToListTemplates(client,new TemplateDocument[]{templateMock});
-
- templateMock.getDisplayName();
- modify().forward();
- templateMock.getTemplateID();
- modify().forward();
-
+ prepareForCallToListTemplates(client,new TemplateDocument[]{templateMock});
+
startVerification();
- Map map = mock.getServerTemplateList(client);
+ Map map = mockSyncTool.getServerTemplateList(client);
assertThat(map.size(),is.eq(1));
assertThat(map.containsKey(DISPLAY_NAME),is.TRUE);
assertThat(map.containsValue(new Long(ID)),is.TRUE);
}
+ public void testAbortIfTemplatesShareName() throws IOException, URISyntaxException {
+
+ TemplateDocument t1= makeTemplateMockAndPrepIt("one", 1,"t1",true);
+ TemplateDocument t2= makeTemplateMockAndPrepIt("two", 2,"t2",true);
+ TemplateDocument t3 = makeTemplateMockAndPrepIt("one", 3,"t3",false);
+ TemplateDocument t4 = makeTemplateMockAndPrepIt("four", 4,"t4",false);
+
+ t3.getDisplayName();
+ modify().forward();
+
+ prepareForCallToListTemplates(client,new TemplateDocument[]{t1,t2,t3,t4});
+ mockSyncTool.abort(SyncTool.ABORT_DUP_TEMPLATE_NAMES);
+
+ startVerification();
+ mockSyncTool.getServerTemplateList(client);
+
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|