From c1b8000273d4d11f26185580f920382383cf9a82 Mon Sep 17 00:00:00 2001 From: sasinda Date: Sun, 19 Aug 2012 22:41:28 +0530 Subject: [PATCH] More Tests ;-) FileStore tests. --- .../data/fileStore/DocumentFaoImpSer.java | 32 +- .../data/fileStore/FSDocumentReference.java | 5 +- .../data/fileStore/FileStoreException.java | 62 +++ .../org/xwiki/android/data/fileStore/README | 7 + .../xwiki/android/rest/ral/RaoException.java | 25 +- .../android/rest/ral/XmlDocumentRao.java | 180 +++++--- .../rest/ral/algo/DocUpdateStrategy.java | 44 +- .../xwiki/android/rest/reference/Link.java | 3 +- .../transformation/DocLaunchPadForXML.java | 6 +- .../DocumentDismantler_XML.java | 4 +- .../android/rest/transformation/RefactorNote | 6 +- .../transformation/RestModelTransformer.java | 57 ++- .../xwiki/android/xmodel/entity/Comment.java | 405 ++++++++++-------- .../xwiki/android/xmodel/entity/Document.java | 6 +- .../android/xmodel/entity/HistoryRecord.java | 35 +- .../org/xwiki/android/xmodel/entity/Tag.java | 2 +- .../xmodel/xobjects/XBooleanProperty.java | 8 + .../android/xmodel/xobjects/XComment.java | 6 + .../xmodel/xobjects/XDateProperty.java | 11 +- .../xmodel/xobjects/XDoubleProperty.java | 6 + .../xmodel/xobjects/XFloatProperty.java | 6 + .../xmodel/xobjects/XIntegerProperty.java | 6 + .../xmodel/xobjects/XLongProperty.java | 4 + .../xmodel/xobjects/XStringProperty.java | 8 + .../xwiki/android/xmodel/xobjects/XTag.java | 3 +- .../xmodel/xobjects/XTextAreaProperty.java | 6 + .../xwiki/android/rest/CommentResources.java | 9 + .../org/xwiki/android/rest/HttpConnector.java | 189 +++++++- .../core/test/properties/TestConstants.java | 35 +- .../data/filestore/test/DocumentFaoTest.java | 120 ++++++ .../rest/ral/test/TestDocumentRaoCreate.java | 51 ++- .../ral/test/TestDocumentRaoRetreive.java | 2 +- .../rest/ral/test/TestDocumentRaoUpdate.java | 6 +- .../entity/test/CommentModelStateTest.java | 88 ++++ .../entity/test/DocumentModelStateTest.java | 50 +++ .../xwiki/android/xmodel/entity/test/README | 2 + 36 files changed, 1156 insertions(+), 339 deletions(-) create mode 100644 xwiki-android-core/src/org/xwiki/android/data/fileStore/FileStoreException.java create mode 100644 xwiki-android-core/src/org/xwiki/android/data/fileStore/README create mode 100644 xwiki-android-test-core/src/org/xwiki/android/data/filestore/test/DocumentFaoTest.java create mode 100644 xwiki-android-test-core/src/org/xwiki/android/xmodel/entity/test/CommentModelStateTest.java create mode 100644 xwiki-android-test-core/src/org/xwiki/android/xmodel/entity/test/DocumentModelStateTest.java create mode 100644 xwiki-android-test-core/src/org/xwiki/android/xmodel/entity/test/README diff --git a/xwiki-android-core/src/org/xwiki/android/data/fileStore/DocumentFaoImpSer.java b/xwiki-android-core/src/org/xwiki/android/data/fileStore/DocumentFaoImpSer.java index 538087c..93bcb6a 100644 --- a/xwiki-android-core/src/org/xwiki/android/data/fileStore/DocumentFaoImpSer.java +++ b/xwiki-android-core/src/org/xwiki/android/data/fileStore/DocumentFaoImpSer.java @@ -30,12 +30,12 @@ class DocumentFaoImpSer implements DocumentFao { - private final String TAG="Document FAO"; + private static final String TAG="Document FAO"; private XWikiApplicationContext ctx; private final File FSDIR; - private static final String tag="DocumentFao";//loggin tag + public DocumentFaoImpSer(XWikiApplicationContext ctx, FileStoreManager mngr) { @@ -50,15 +50,14 @@ public FSDocumentReference save(Document doc, String tag) String wikiName = doc.getWikiName(); String spaceName = doc.getSpaceName(); String pageName = doc.getPageName(); - String dirPath = FSDIR.getAbsolutePath() + wikiName + "/" + spaceName + "/" + pageName; + String dirPath = FSDIR.getAbsolutePath() +"/"+ wikiName + "/" + spaceName + "/" + pageName; String filePath=dirPath+"/doc.ser"; f = new File(filePath); boolean success=true; FSDocumentReference fsref = new FSDocumentReference(); // doc ref data - DocumentReference docref = doc.getDocumentReference(); - docref.setServerName(ctx.getUserSession().getRealm()); + DocumentReference docref = doc.getDocumentReference(); fsref.copyfrom(docref); // fs data fsref.setTag(tag); @@ -81,11 +80,14 @@ public FSDocumentReference save(Document doc, String tag) if(!lst.isEmpty()){ fsref= lst.get(0); Log.d(TAG,"Doc already saved "+ fsref.getPageName()); - return fsref; + if(!(fsref.getTag().equals(tag))){ + throw new FileStoreException.UnsupportedOperation("["+tag+"!="+fsref.tag+"] Try to save an already saved document with a different tag. We can have only one tag"); + } + }else{ throw new RuntimeException("error in logic"); } - } catch (SQLException e1) { + } catch (SQLException e1) { Log.e(TAG, "error while retreiving already saved doc's doc ref"); } } @@ -222,7 +224,21 @@ public boolean delete(FSDocumentReference ref) dao.delete(ref); } em.close(); - File f = ref.getFile(); + + File f = ref.getFile(); + if(f==null){ + String wikiName=ref.getWikiName(); + String spaceName=ref.getSpaceName(); + String pageName=ref.getPageName(); + + if(wikiName==null || spaceName==null || pageName==null){ + throw new IllegalArgumentException("at least specify wiki,space,page coordinates. If not giving a direct file in fsref"); + } + String dirPath=FSDIR.getAbsolutePath() +"/"+ wikiName + "/" + spaceName + "/" + pageName; + String filePath=dirPath+"/doc.ser"; + f=new File(filePath); + } + return f.delete(); } catch (SQLException e) { Log.e(TAG, e.getMessage()); diff --git a/xwiki-android-core/src/org/xwiki/android/data/fileStore/FSDocumentReference.java b/xwiki-android-core/src/org/xwiki/android/data/fileStore/FSDocumentReference.java index 021739b..d480ebc 100644 --- a/xwiki-android-core/src/org/xwiki/android/data/fileStore/FSDocumentReference.java +++ b/xwiki-android-core/src/org/xwiki/android/data/fileStore/FSDocumentReference.java @@ -37,8 +37,9 @@ public void setFile(File f) } public File getFile() - { - if(file==null){ + { + if(file==null ){ + if(filePath==null)return null; file=new File(filePath); } return this.file; diff --git a/xwiki-android-core/src/org/xwiki/android/data/fileStore/FileStoreException.java b/xwiki-android-core/src/org/xwiki/android/data/fileStore/FileStoreException.java new file mode 100644 index 0000000..68e1aaf --- /dev/null +++ b/xwiki-android-core/src/org/xwiki/android/data/fileStore/FileStoreException.java @@ -0,0 +1,62 @@ +package org.xwiki.android.data.fileStore; + +public class FileStoreException extends RuntimeException +{ + + public FileStoreException() + { + super(); + } + + public FileStoreException(String detailMessage, Throwable throwable) + { + super(detailMessage, throwable); + } + + public FileStoreException(String detailMessage) + { + super(detailMessage); + } + + + + public static class DuplicateKey extends FileStoreException + { + + public DuplicateKey() + { + super(); + } + + public DuplicateKey(String detailMessage, Throwable throwable) + { + super(detailMessage, throwable); + } + + public DuplicateKey(String detailMessage) + { + super(detailMessage); + } + } + + public static class UnsupportedOperation extends FileStoreException{ + + public UnsupportedOperation() + { + super(); + } + + public UnsupportedOperation(String detailMessage, Throwable throwable) + { + super(detailMessage, throwable); + } + + public UnsupportedOperation(String detailMessage) + { + super(detailMessage); + } + + + } + +} diff --git a/xwiki-android-core/src/org/xwiki/android/data/fileStore/README b/xwiki-android-core/src/org/xwiki/android/data/fileStore/README new file mode 100644 index 0000000..f1b2408 --- /dev/null +++ b/xwiki-android-core/src/org/xwiki/android/data/fileStore/README @@ -0,0 +1,7 @@ +This is intended to be lightweight mimic of a document oriented Database. + +Issues: +The File Store needs more specification. +Suggest +key= wikiName, spaceName, pageName, localVersin(new field) +What if user connects to 2 XWiki servers with same coordintates as above but 2 different Xwikis? \ No newline at end of file diff --git a/xwiki-android-core/src/org/xwiki/android/rest/ral/RaoException.java b/xwiki-android-core/src/org/xwiki/android/rest/ral/RaoException.java index 3421141..cd47364 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/ral/RaoException.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/ral/RaoException.java @@ -6,6 +6,7 @@ * @author xwiki gsoc 2012 For exceptional situations where client is doing some thing wrong due to lack of knowledge of * the remote servers state.Ex: Call to create a document that already exsist in the remote server. */ +//TODO: PLEASE SEE THE SUB EXCEPTION TYPES. Make Raos throw them at specific points. public class RaoException extends Exception { @@ -42,7 +43,7 @@ public RaoException(Throwable throwable) super(throwable); // TODO Auto-generated constructor stub } - + public RaoException(RestException rex) { super(rex); @@ -69,7 +70,25 @@ public void setCode(int code) { this.code = code; } - - + + public class DuplicateDocument extends RaoException + { + + } + + public class DocumentNotFound extends RaoException + { + + } + + /** + * when updating a object in a document. Object not found thrown for set obj ops + * + * @author xwiki gsoc 2012 + */ + public class ObjectNotFound extends RaoException + { + + } } diff --git a/xwiki-android-core/src/org/xwiki/android/rest/ral/XmlDocumentRao.java b/xwiki-android-core/src/org/xwiki/android/rest/ral/XmlDocumentRao.java index 49fea0d..465678e 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/ral/XmlDocumentRao.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/ral/XmlDocumentRao.java @@ -29,6 +29,7 @@ import org.xwiki.android.rest.rpc.XWikiAPI; import org.xwiki.android.rest.transformation.DocLaunchPadForXML; import org.xwiki.android.rest.transformation.DocumentDismantler_XML; +import org.xwiki.android.rest.transformation.RestModelTransformer; import org.xwiki.android.xmodel.entity.Attachment; import org.xwiki.android.xmodel.entity.Document; import org.xwiki.android.xmodel.xobjects.XSimpleObject; @@ -173,68 +174,51 @@ public Index(int first, int last) } List newCmnts = pad.getNewComments(); - List edCmnts = pad.getEditedComments(); - Comparator comparator = new Comparator() - { - @Override - public int compare(Object object1, Object object2) - { - // we are sure that edited Comments coming as Objects get there number prop filled to comment - // id; - return object1.getNumber() - object2.getNumber(); - } - }; - Collections.sort(edCmnts, comparator); - // start order comment upload. Try create the setComment(cmnt with id 1) to match the same id in server. - int svrCmntId=-1; - int used = 0; // how many new comments used - int avail = newCmnts.size(); - int lastId = -1; - for (int i = 0; i < edCmnts.size(); i++) { - Object ec = edCmnts.get(i); - int thisId = ec.getNumber(); - if (thisId > lastId + 1) { - int needed = thisId - lastId-1; - lastId = thisId; - if (used < avail) { - int start = used; - int end = Math.min(avail, used + needed); - for (int j = used; j < end; j++) { - Comment cnw=newCmnts.get(j); - int tmpId=cnw.getId(); - cnw.setId(++svrCmntId); - api.addPageComment(wikiName, spaceName, pageName, cnw); - - for(Comment c:newCmnts){ - if(c.getReplyTo()!=null && c.getReplyTo()==tmpId){ - c.setReplyTo(svrCmntId); - } - } + List edCmnts = pad.getEditedComments(); + + List cmnts=new LinkedList(newCmnts); + + if(edCmnts.size()>0){ + RestModelTransformer transformer=new RestModelTransformer(); + Comparator comparator=new Comparator() + { + @Override + public int compare(Comment c1, Comment c2) + { + return c1.getId()-c2.getId(); } - used += needed; - } - api.addObject(wikiName, spaceName, pageName, ec); - svrCmntId++; - } else { - lastId=thisId; - api.addObject(wikiName, spaceName, pageName, ec); - svrCmntId++; + }; + Collections.sort(edCmnts, comparator); + + Comment ec; + for(int i=0 ; icollection=edCmnts.subList(i, edCmnts.size()); + cmnts.addAll(collection); + break; + } + } + } + for(int i=0; i0){ + RestModelTransformer transformer=new RestModelTransformer(); + Comparator comparator=new Comparator() + { + @Override + public int compare(Comment c1, Comment c2) + { + return c1.getId()-c2.getId(); + } + }; + Collections.sort(edCmnts, comparator); + // start order comment upload. Try create the setComment(cmnt with id 1) to match the same id in server. + + for (int i = 0; i < edCmnts.size(); i++) { + Comment ec = edCmnts.get(i); + int thisId = ec.getId(); + if (thisId > lastId + 1) { + int needed = thisId - lastId-1; + lastId = thisId; + if (used < avail) { + int start = used; + int end = Math.min(avail, used + needed); + for (int j = used; j < end; j++) { + Comment cnw=newCmnts.get(j); + int tmpId=cnw.getId(); + cnw.setId(++svrCmntId); + api.addPageComment(wikiName, spaceName, pageName, cnw); + for(Comment c:newCmnts){ + if(c.getReplyTo()!=null && c.getReplyTo()==tmpId){ + c.setReplyTo(svrCmntId); + } + } + for(Comment c:edCmnts){ + if(c.getReplyTo()!=null && c.getReplyTo()==tmpId){ + c.setReplyTo(svrCmntId); + } + } + } + + + } + Object eo = transformer.toObject(ec); + api.addObject(wikiName, spaceName, pageName,eo ); + if(used+needed>avail){//if avail newComments = pad.getNewComments(); + List editedComments = pad.getEditedComments(); + for (Comment comment : newComments) {//TODO: Update op cannot achieve replyto s, unless rest layer is upgraded. try { - api.addPageComment(wikiName, spaceName, pageName, comment); - numNwCmnt++; + Comment c = rpc.commentOperations(wikiName, spaceName, pageName).addPageCommentForResult(comment); + for(Comment rply:newComments){ + if(rply.replyTo!=null && rply.replyTo==comment.replyTo){//cause both cmnts id,replyTo can be null. + rply.setReplyTo(comment.id); + } + } + for(Comment rply:editedComments){ + if(rply.replyTo!=null && rply.replyTo==comment.replyTo){//cause both cmnts id,replyTo can be null. + rply.setReplyTo(comment.id); + } + } + numNwCmnt++; } catch (RestException e1) { // TODO Auto-generated catch block throw new RaoException(e1); @@ -89,11 +103,13 @@ public Document update(Document d) throws RestConnectionException, RaoException // edited comments // need to conver to objs. Direct editing is not currently supported in // xwiki restful api. - for (Object cmntObj : pad.getEditedComments()) { + RestModelTransformer transformer =new RestModelTransformer(); + for (Comment cmnt : pad.getEditedComments()) { + Object cmntObj=transformer.toObject(cmnt); String objectClassname = cmntObj.getClassName(); int objectNumber = cmntObj.getNumber(); try { - api.updateObject(wikiName, spaceName, pageName, objectClassname, objectNumber, cmntObj); + rpc.updateObject(wikiName, spaceName, pageName, objectClassname, objectNumber, cmntObj); numEdCmnt++; } catch (RestException e1) { // TODO Auto-generated catch block @@ -109,7 +125,7 @@ public Document update(Document d) throws RestConnectionException, RaoException String objectClassname = ss[0]; String objectNumber = ss[1]; try { - api.deleteObject(wikiName, spaceName, pageName, objectClassname, objectNumber); + rpc.deleteObject(wikiName, spaceName, pageName, objectClassname, objectNumber); numDelCmnt++; } catch (RestException e1) { // TODO Auto-generated catch block @@ -121,7 +137,7 @@ public Document update(Document d) throws RestConnectionException, RaoException Tags tags = pad.getTags(); if (tags != null) { try { - api.setTags(wikiName, spaceName, pageName, tags); + rpc.setTags(wikiName, spaceName, pageName, tags); numTags = tags.getTags().size(); } catch (RestException e1) { // TODO Auto-generated catch block @@ -134,7 +150,7 @@ public Document update(Document d) throws RestConnectionException, RaoException for (Attachment a : pad.getAttatchmentsToupload()) { try { - api.putPageAttachment(wikiName, spaceName, pageName, a.getFile().getAbsolutePath(), a.getName()); + rpc.putPageAttachment(wikiName, spaceName, pageName, a.getFile().getAbsolutePath(), a.getName()); numEdNwAtch++; } catch (RestException e1) { // TODO Auto-generated catch block @@ -146,7 +162,7 @@ public Document update(Document d) throws RestConnectionException, RaoException for (String s : pad.getDeletedAttachments()) { try { - api.deletePageAttachment(wikiName, spaceName, pageName, s); + rpc.deletePageAttachment(wikiName, spaceName, pageName, s); numDelAtch++; } catch (RestException e1) { // TODO Auto-generated catch block diff --git a/xwiki-android-core/src/org/xwiki/android/rest/reference/Link.java b/xwiki-android-core/src/org/xwiki/android/rest/reference/Link.java index 0458ea6..33170cf 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/reference/Link.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/reference/Link.java @@ -3,7 +3,8 @@ import java.io.Serializable; /** - * @author xwiki gsoc 2012 http://www.xwiki.org/rel/wikis The representation containing the list of virtual wikis. + * @author xwiki gsoc 2012 + * http://www.xwiki.org/rel/wikis The representation containing the list of virtual wikis. * http://www.xwiki.org/rel/spaces The representation containing the list of spaces in a wiki. * http://www.xwiki.org/rel/pages The representation containing the list of pages in a space. * http://www.xwiki.org/rel/translation The representation containing a translation of a page. diff --git a/xwiki-android-core/src/org/xwiki/android/rest/transformation/DocLaunchPadForXML.java b/xwiki-android-core/src/org/xwiki/android/rest/transformation/DocLaunchPadForXML.java index 2d46924..f42feb8 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/transformation/DocLaunchPadForXML.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/transformation/DocLaunchPadForXML.java @@ -27,7 +27,7 @@ public class DocLaunchPadForXML List deletedObjects; // locally List newComments; - List editedComments;//modification of comments sup for obj only. + List editedComments;//modification of comments sup for obj only. List deletedComments; Tags tags; @@ -41,7 +41,7 @@ public DocLaunchPadForXML() editedObjects = new Hashtable(); deletedObjects = new ArrayList(); newComments=new ArrayList(); - editedComments=new ArrayList(); + editedComments=new ArrayList(); deletedComments=new ArrayList(); attatchmentsToupload=new ArrayList(); @@ -112,7 +112,7 @@ public List getNewComments() return newComments; } - public List getEditedComments() + public List getEditedComments() { return editedComments; } diff --git a/xwiki-android-core/src/org/xwiki/android/rest/transformation/DocumentDismantler_XML.java b/xwiki-android-core/src/org/xwiki/android/rest/transformation/DocumentDismantler_XML.java index d71f572..ee9ab69 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/transformation/DocumentDismantler_XML.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/transformation/DocumentDismantler_XML.java @@ -97,8 +97,8 @@ public DocLaunchPadForXML convertDocument(Document doc) // updates List editedComments = doc.getAllEditedComments(); for (Comment ec : editedComments) { - Object ores=translator.toObject(ec); - pad.editedComments.add(ores); + org.xwiki.android.resources.Comment c=translator.toComment(ec); + pad.editedComments.add(c); } // deletes Set deletedComments = doc.getDeletedCommentSet(); diff --git a/xwiki-android-core/src/org/xwiki/android/rest/transformation/RefactorNote b/xwiki-android-core/src/org/xwiki/android/rest/transformation/RefactorNote index 99873d4..49c3711 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/transformation/RefactorNote +++ b/xwiki-android-core/src/org/xwiki/android/rest/transformation/RefactorNote @@ -1,4 +1,8 @@ consider: apply Builder pattern with a Director. So, which REST model to use can be identified from the config.xml and convert accordingly. -*** This is currently achieved by creating a separate ReSTfulManager for each ReST scheme we use. (performance efficient) \ No newline at end of file +*** This is currently achieved by creating a separate ReSTfulManager for each ReST scheme we use. (performance efficient) + + + + diff --git a/xwiki-android-core/src/org/xwiki/android/rest/transformation/RestModelTransformer.java b/xwiki-android-core/src/org/xwiki/android/rest/transformation/RestModelTransformer.java index 570c325..4c314aa 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/transformation/RestModelTransformer.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/transformation/RestModelTransformer.java @@ -1,36 +1,63 @@ package org.xwiki.android.rest.transformation; +import java.util.ArrayList; + import org.xwiki.android.resources.Comment; import org.xwiki.android.resources.Object; +import org.xwiki.android.resources.Property; import org.xwiki.android.resources.Tags; +import org.xwiki.android.resources.Attribute; + import org.xwiki.android.xmodel.xobjects.XComment; /** * transforms XModel Objects to XModel 1st class entities and vice versa. + * * @author xwiki gsoc 2012 - * */ public class RestModelTransformer { - public static Object toObject(Comment c){ - XComment xc=new XComment(); - xc.setAuthor(c.getAuthor()); - xc.setComment(c.getText()); - //xc.setDate(c.getDate()); - //TODO - return new XModelTranslator_XML().toObject(xc); + public Object toObject(Comment c) + { + Object o=new Object(); + + o.setClassName("XWiki.XWikiComments"); + + Property id,author,date,comment,replyto = null; + + id=newProperty("id", c.id+""); + author=newProperty("author", c.author); + date=newProperty("date", c.getDate()); + comment=newProperty("comment", c.text); + if(c.replyTo!=null){ + replyto=newProperty("replyto", c.replyTo.toString()); + } + + return o.withProperties(id,author,date,comment,replyto); } - - public static Object toObject(Tags ts){ + + public Object toObject(Tags ts) + { throw new UnsupportedOperationException("//TODO"); } - - public static Tags toTags(Object o){ - throw new UnsupportedOperationException("//TODO"); + + public Tags toTags(Object o) + { + throw new UnsupportedOperationException("//TODO"); + } + + public Comment toComment(Object o) + { + throw new UnsupportedOperationException("//TODO"); } - public static Comment toComment(Object o){ - throw new UnsupportedOperationException("//TODO"); + private Property newProperty(String name, String value){ + if(value==null)return null; + Property p=new Property(); + p.setName(name); + p.setValue(value); + p.attributes=new ArrayList(); + return p; } } diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Comment.java b/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Comment.java index e175b71..821c9a2 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Comment.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Comment.java @@ -9,186 +9,229 @@ public class Comment extends XWikiResource { - - int id = -1; // -1 to mean null - String author; - Date date; - String text; - int replyTo = -1; // -1 to mean null - String highlight; - - Document ownerDoc; - List replies; - // object associated to this comment. - XComment xobj; - - public Comment() - { - replies = new ArrayList(); - } - - public Comment(String text) - { - this(); - this.text = text; - } - - /** - * - * This method also adds the reply comment to the parent comment's owning - * document if it is owned by a document. - * - * @return true if added. false if already contained the comment. - */ - public boolean addReplyComment(Comment rply) - { - if (!replies.contains(rply)) { - replies.add(rply); - if (ownerDoc != null) { - ownerDoc.addComment(rply); - } - rply.replyTo = this.id; - return true; - } else { - return false; - } - } - - /** - * when the id of this comment is changed refresh the change to replyTo - * fields of direct reply comments. - */ - private void refreshChildrenReplyToID() - { - for (Comment rply : this.getReplies()) { - rply.setReplyTo(this.getId()); - } - } - - public List getReplies() - { - return replies; - } - - public void setReplies(List replies) - { - this.replies = replies; - } - - - // - //getter setteres - // - - public int getId() - { - return id; - } - - public void setId(int id) - { - if (ownerDoc != null) { - throw new IllegalStateException("You cannot alter the id of a comment after it is owned by a document"); - } - this.id = id; - refreshChildrenReplyToID(); - } - - public String getAuthor() - { - return author; - } - - public void setAuthor(String author) - { - this.author = author; - } - - public Date getDate() - { - return date; - } - - public void setDate(Date date) - { - this.date = date; - } - - public String getText() - { - return text; - } - - public void setText(String text) - { - this.text = text; - } - - public int getReplyTo() - { - return replyTo; - } - - public void setReplyTo(int replyTo) - { - this.replyTo = replyTo; - } - - public String getHighlight() - { - return highlight; - } - - public void setHighlight(String highlight) - { - this.highlight = highlight; - } - - // package - - Document getDocument() - { - return ownerDoc; - } - - void setOwner(Document ownerDoc) - { - this.ownerDoc = ownerDoc; - } - - // private - - private XComment getXObject() - { - if (xobj == null) { - xobj = new XComment(); - - // ! xobj.setId(""+id);// dont set.Not the same. - xobj.setNumber(id); - xobj.setAuthor(author); - xobj.setDate(date); - xobj.setComment(text); - xobj.setReplyto(replyTo); - - } - return xobj; - } - - private void setXObject(XComment xobj) - { - this.xobj = xobj; - } - - @Override - public boolean equals(Object o) - { - if (!(o instanceof XComment)) { - return false; - } else { - Comment c = (Comment) o; - return c.getId() == this.id && c.text == this.text; - } - - } + + private int id = -1; // -1 to mean null + private String author; + private Date date; + private String text; + private int replyTo = -1; // -1 to mean null + private String highlight; + + private Document ownerDoc; + private List replies; + + //non bean props. + // object associated to this comment. + private XComment xobj; + boolean isaReply; + + public Comment() + { + replies = new ArrayList(); + } + + public Comment(String text) + { + this(); + this.text = text; + } + + public void replyTo(Comment c){ + c.addReplyComment(this); + isaReply=true; + } + + /** + * This method also adds the reply comment to the parent comment's owning document if it is owned by a document. + * + * @return true if added. false if already contained the comment. + */ + public boolean addReplyComment(Comment rply) + { + if (this == rply) + throw new IllegalArgumentException("Comment cannot reply to itself"); + rply.isaReply=true; + if (!replies.contains(rply)) { + replies.add(rply); + if (ownerDoc != null) { + ownerDoc.addComment(rply); + } + rply.setReplyTo(this.id); + return true; + } else { + return false; + } + + } + + + /** + * when the id of this comment is changed refresh the change to replyTo fields of direct reply comments. + */ + private void refreshChildrenReplyToID() + { + for (Comment rply : this.getReplies()) { + rply.setReplyTo(this.getId()); + } + } + + public List getReplies() + { + return replies; + } + + public void setReplies(List replies) + { + for (Comment c : replies) { + addReplyComment(c); + } + } + + // + // getter setteres + // + + public int getId() + { + return id; + } + + public void setId(int id) + { + if (ownerDoc != null) { + throw new IllegalStateException("You cannot alter the id of a comment after it is owned by a document"); + } + this.id = id; + refreshChildrenReplyToID(); + } + + public String getAuthor() + { + return author; + } + + public void setAuthor(String author) + { + this.author = author; + } + + public Date getDate() + { + return date; + } + + public void setDate(Date date) + { + this.date = date; + } + + public String getText() + { + return text; + } + + public void setText(String text) + { + this.text = text; + } + + public int getReplyTo() + { + return replyTo; + } + + public void setReplyTo(int replyTo) + { + if(replyTo!=-1){ + if (this.id == -1) { + this.replyTo = replyTo; + } else if (this.id >= 0) { + if (replyTo < -1) { + if ((-10 - replyTo) > id) { + String msg = + "this cmnts id is " + id + " but we reply to " + replyTo + " which is the " + (-10 - replyTo) + + "th new comment which obviously gets added after this comment"; + throw new IllegalStateException(msg); + } + } else if (replyTo >= 0) { + if (!(replyTo < id)) { + throw new IllegalStateException("cannot reply to a comment that is after this."); + } + } else {// replyto=-1 + if (this.ownerDoc != null) { + int newCmnts = ownerDoc.getAllNewComments().size(); + if (newCmnts >= id) { + throw new IllegalStateException( + "replying to a comment that will obviously be added after this comment. there are " + + newCmnts + " already added to Document. They are enough to fill the gap. this.id is:" + + id); + } + } + } + }else{//this.id<-1 i.e. already associated to document. + if(!(id(); + objects = new HashMap(); newObjects = new ArrayList(); - editedObjects = new Hashtable(); + editedObjects = new HashMap(); deletedObjects = new ArrayList(); comments = new HashMap(); @@ -89,7 +89,7 @@ public Document(String wikiName, String spaceName, String pageName) editedComments = new ArrayList(); deletedCommetns = new ArrayList(); - attatchments = new Hashtable(); + attatchments = new HashMap(); newAttachments = new ArrayList(); editedAttachments = new ArrayList(); deletedAttachments = new ArrayList(); diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/entity/HistoryRecord.java b/xwiki-android-core/src/org/xwiki/android/xmodel/entity/HistoryRecord.java index bffbec9..1aeda11 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/entity/HistoryRecord.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/entity/HistoryRecord.java @@ -1,25 +1,28 @@ package org.xwiki.android.xmodel.entity; + +import java.io.Serializable; + /** * equivelent to history summary. * @author xwiki gsoc 2012 * */ -public class HistoryRecord +public class HistoryRecord implements Serializable { - + + /** + + + xwiki:Blog.test2 + xwiki + Blog + test2 + 17.1 + 17 + 1 + 2012-08-18T12:06:43+05:30 + XWiki.Admin + + **/ } -/** - - -xwiki:Blog.test2 -xwiki -Blog -test2 -17.1 -17 -1 -2012-08-18T12:06:43+05:30 -XWiki.Admin - -**/ \ No newline at end of file diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Tag.java b/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Tag.java index 28f38d1..eac9711 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Tag.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Tag.java @@ -3,7 +3,7 @@ public class Tag extends XWikiResource { - private static final long serialVersionUID = 3416603502937027872L; + //private static final long serialVersionUID = 3416603502937027872L; private String name; // private XTag xobj; diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XBooleanProperty.java b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XBooleanProperty.java index ca2ad09..0929c86 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XBooleanProperty.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XBooleanProperty.java @@ -14,6 +14,14 @@ public XBooleanProperty() { super("com.xpn.xwiki.objects.classes.BooleanClass"); } + + + public XBooleanProperty(Boolean value) + { + this(); + this.value = value; + } + @Override public void setValue(Boolean val) diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XComment.java b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XComment.java index 74b6994..ce509fe 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XComment.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XComment.java @@ -10,6 +10,12 @@ public XComment() super("XWiki.XWikiComments"); } + public XComment(String comment) + { + this(); + setComment(comment); + } + String getAuthor() { XStringProperty prop = (XStringProperty) fields.get("author"); diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XDateProperty.java b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XDateProperty.java index 2480b2e..a4969d3 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XDateProperty.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XDateProperty.java @@ -15,7 +15,16 @@ public class XDateProperty extends XPropertyBase public XDateProperty() { super("com.xpn.xwiki.objects.classes.DateClass"); - } + } + + + public XDateProperty(Date value) + { + this(); + this.value = value; + } + + @Override public void setValue(Date date) diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XDoubleProperty.java b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XDoubleProperty.java index f41ae0d..a54c266 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XDoubleProperty.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XDoubleProperty.java @@ -9,6 +9,12 @@ public XDoubleProperty() { fields.put("numberType","double"); } + + public XDoubleProperty(Double val) + { + this(); + this.val=val; + } @Override public void setValue(Double val) diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XFloatProperty.java b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XFloatProperty.java index 1607895..c802682 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XFloatProperty.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XFloatProperty.java @@ -10,6 +10,12 @@ public XFloatProperty() //type="com.xpn.xwiki.objects.classes.NumberClass";// in super() fields.put("numberType","float"); } + + public XFloatProperty(Float val) + { + this(); + this.val=val; + } @Override public void setValue(Float val) diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XIntegerProperty.java b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XIntegerProperty.java index fa7f483..14bad26 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XIntegerProperty.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XIntegerProperty.java @@ -8,6 +8,12 @@ public XIntegerProperty() { fields.put("numberType", "integer"); } + + public XIntegerProperty(Integer val) + { + this(); + this.val=val; + } @Override public void setValue(Integer val) diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XLongProperty.java b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XLongProperty.java index 8efce70..899d14c 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XLongProperty.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XLongProperty.java @@ -8,6 +8,10 @@ public XLongProperty() { fields.put("numberType", "long"); } + + public XLongProperty(Long val){ + this.val=val; + } @Override public void setValue(Long val) diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XStringProperty.java b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XStringProperty.java index 6cd12a3..d6cbb94 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XStringProperty.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XStringProperty.java @@ -12,6 +12,14 @@ public XStringProperty() super("com.xpn.xwiki.objects.classes.StringClass"); } + + public XStringProperty(String value) + { + super("com.xpn.xwiki.objects.classes.StringClass"); + this.value=value; + + } + // special attr diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XTag.java b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XTag.java index 37c2ffe..fd218cb 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XTag.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XTag.java @@ -11,7 +11,8 @@ public XTag() { super("XWiki.TagClass"); } - + + public void addTag(String tag) { XStaticListProperty prop = (XStaticListProperty) fields.get("category"); diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XTextAreaProperty.java b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XTextAreaProperty.java index 979524a..a3a5139 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XTextAreaProperty.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XTextAreaProperty.java @@ -14,6 +14,12 @@ public XTextAreaProperty() { super("com.xpn.xwiki.objects.classes.TextAreaClass"); } + + public XTextAreaProperty(String value) + { + super("com.xpn.xwiki.objects.classes.TextAreaClass"); + this.value=value; + } @Override public String toString() diff --git a/xwiki-android-rest/src/org/xwiki/android/rest/CommentResources.java b/xwiki-android-rest/src/org/xwiki/android/rest/CommentResources.java index faa1cbe..bcec14c 100644 --- a/xwiki-android-rest/src/org/xwiki/android/rest/CommentResources.java +++ b/xwiki-android-rest/src/org/xwiki/android/rest/CommentResources.java @@ -165,6 +165,15 @@ public String addPageComment(Comment comment) throws RestConnectionException, Re return super.postRequest(Uri, buildXmlComment(comment)); } + + public Comment addPageCommentForResult(Comment comment) throws RestConnectionException, RestException + { + String uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/comments"; + + return super.postForResource(uri, comment, Comment.class); + } /** * Parse xml into a Comments object diff --git a/xwiki-android-rest/src/org/xwiki/android/rest/HttpConnector.java b/xwiki-android-rest/src/org/xwiki/android/rest/HttpConnector.java index f83e477..7dafb6a 100644 --- a/xwiki-android-rest/src/org/xwiki/android/rest/HttpConnector.java +++ b/xwiki-android-rest/src/org/xwiki/android/rest/HttpConnector.java @@ -25,10 +25,12 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.StringWriter; import java.net.SocketTimeoutException; import java.net.URI; import java.net.URISyntaxException; +import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpHost; import org.apache.http.HttpRequest; @@ -55,6 +57,11 @@ import org.apache.http.params.HttpParams; import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; +import org.apache.http.util.EntityUtils; +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; +import org.xwiki.android.resources.Comment; +import org.xwiki.android.resources.Resource; import android.util.Log; @@ -72,6 +79,10 @@ public class HttpConnector * custom response code for client connection timeout */ public static final int RESP_CODE_CLIENT_CON_TIMEOUT=21408; + + private static final String TAG = HttpConnector.class.getSimpleName(); + + /** * URI of the Remote XWiki instance @@ -254,6 +265,149 @@ public String deleteRequest(String Uri) throws RestConnectionException, RestExce return "error"; } + + + + + /** + * + * @param uri + * @param res The resource to be posted to server. Ex {@link Comment} + * @param retType The type of Resource to be extracted from the response to post request + * @return + * @throws RestConnectionException + * @throws RestException + */ + public T postForResource(String uri, Resource res, Class retType) + throws RestConnectionException, RestException + { + String content = toXmlString(res); + HttpResponse response = post(uri, content); + if (retType != null) { + return buildResource(retType, response.getEntity()); + } else { + return null; + } + } + + public T putForResource(String uri, Resource res, Class retType) + throws RestConnectionException, RestException + { + String content = toXmlString(res); + HttpResponse response = put(uri, content); + if (retType != null) { + return buildResource(retType, response.getEntity()); + } else { + return null; + } + } + + /** + * Perform HTTP Post method execution and return its Response + * + * @param uri URL of XWiki RESTful API call. + * @param content content to be posted to the server + * @return status code of the Post method execution + * @throws RestConnectionException + * @throws RestException + */ + HttpResponse post(String uri, String content) throws RestConnectionException, RestException + { + + HttpPost request = new HttpPost(); + + try { + URI requestUri = new URI(uri); + request.setURI(requestUri); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + Log.d(TAG, "POST URL :" + uri); + + try { + Log.d("Post content", "content=" + content); + StringEntity se = new StringEntity(content, "UTF-8"); + + se.setContentType("application/xml"); + // se.setContentType("text/plain"); + request.setEntity(se); + request.setHeader("Content-Type", "application/xml;charset=UTF-8"); + + HttpResponse response = client.execute(request); + Log.d(TAG, response.getStatusLine().toString()); + + validate(response.getStatusLine().getStatusCode()); + return response; + + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + throw new RestConnectionException(e); + } + return null; + } + + /** + * Perform HTTP Put method execution and return its Response + * + * @param uri URL of XWiki RESTful API call. + * @param content content to be posted to the server + * @return status code of the Put method execution + * @throws RestConnectionException + * @throws RestException + */ + public HttpResponse put(String uri, String content) throws RestConnectionException, RestException + { + + HttpPut request = new HttpPut(); + + try { + Log.d(TAG, "Request URL :" + uri); + System.out.println(uri); + URI requestUri = new URI(uri); + request.setURI(requestUri); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + try { + Log.d(TAG, "Put content=" + content); + StringEntity se = new StringEntity(content, "UTF-8"); + + se.setContentType("application/xml"); + // se.setContentType("text/plain"); + request.setEntity(se); + request.setHeader("Content-Type", "application/xml;charset=UTF-8"); + + HttpResponse response = client.execute(request); + Log.d(TAG, response.getStatusLine().toString()); + // close the stream to releas resources. //TODO: [ignore since test utils.]ideally this should be closed + // after content is read (if needed) by requester. So move it to PageOps etc. + validate(response.getStatusLine().getStatusCode()); + return response; + + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + throw new RestConnectionException(e); + } + + return null; + } + + + + + + + + + /** * Checks whether user credentials are valid in the provided remote XWiki @@ -278,7 +432,7 @@ public int checkLogin(String username, String password, String Url) throws RestC String Uri; int responseCode = 0; - Uri = "http://" + Url + "/xwiki/xmlrpc/"; + Uri = "http://" + Url + "/xwiki/rest/"; try { requestUri = new URI(Uri); @@ -573,6 +727,39 @@ private void validate(int code) throws RestException{ } } + /** + * Methods taken from the inspiration of rpc.XMLRestCient. + * these methods don't suite here if we name this as a Http Connector. + * @param res + * @return + */ + + private String toXmlString(Resource res) + { + Serializer serializer = new Persister(); + StringWriter result = new StringWriter(); + + try { + serializer.write(res, result); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return result.toString(); + } + + private T buildResource(Class ofType, HttpEntity from) + { + Serializer serializer = new Persister(); + T res = null; + try { + res = serializer.read(ofType, EntityUtils.toString(from)); + from.consumeContent(); // from.finish(). + } catch (Exception e) { + Log.e(TAG, e.getMessage()); + } + return res; + } } diff --git a/xwiki-android-test-core/src/org/xwiki/android/core/test/properties/TestConstants.java b/xwiki-android-test-core/src/org/xwiki/android/core/test/properties/TestConstants.java index b6aa3f4..024ac31 100644 --- a/xwiki-android-test-core/src/org/xwiki/android/core/test/properties/TestConstants.java +++ b/xwiki-android-test-core/src/org/xwiki/android/core/test/properties/TestConstants.java @@ -4,34 +4,35 @@ public class TestConstants { public static final int SERVER_INDEX; public static final String HOST = "10.0.2.2"; - public static final int PORT = 8080; //actual port is port+server_index - public static final String SCHEME="http://"; - public static final String SERVER_URL; //SERVER NAME set in static block. + public static final int PORT = 8080; // actual port is port+server_index + public static final String SCHEME = "http://"; + public static final String SERVER_URL; // SERVER NAME set in static block. public static final String USERNAME = "Admin"; public static final String PASSWORD = "admin"; - //external test fixture. + // external test fixture. public static final String WIKI_NAME = "xwiki"; public static final String SPACE_NAME = "Blog"; public static final String PAGE_NAME = "test2"; public static final String PAGE_VERSION = "1.1"; - public static final String CREATE_PAGE_NAME ="CreatePage"; //used for create method tests. because page needss to be deleted for creation test 01 will create CreatePage-1 - public static final String UPDATE_PAGE_NAME ="UpdatePage"; - - + public static final String CREATE_PAGE_NAME = "CreatePage"; // used for create method tests. because page needss to + // be deleted for creation test 01 will create + // CreatePage-1 + public static final String UPDATE_PAGE_NAME = "UpdatePage"; + public static final String COMMENT_ID = "0"; public static final String COMMENT_TEXT = "hi"; public static final String COMMENT_REPLY_ID = "1"; public static final int COMMENT_REPLY_REPLY_TO = 0; - public static final String COMMENT_PAGE_HISTORY_VERSION="5.1"; + public static final String COMMENT_PAGE_HISTORY_VERSION = "5.1"; public static final String COMMENT_REPLY_TEXT = "huy"; - public static final String LANGUAGE = "fr"; - public static final String TRANSLATION_VERSION="2.1"; + public static final String TRANSLATION_VERSION = "2.1"; - public static final String CLASS_NAME = "Blog.BlogPostClass"; + public static final String OBJECT_CLASS_NAME_1 = "Blog.BlogPostClass"; + public static final String OBJECT_CLASS_NAME_2 = "Blog.BlogCategoryClass"; public static final int OBJECT_NUMBER = 0; public static final String OBJECT_PROPERTY_NAME = "content"; public static final String OBJECT_PROPERTY_VALUE = "test blog content"; @@ -45,19 +46,13 @@ public class TestConstants public static final String ATTACHMENT_PATH = "./"; public static final String ATTACHMENT_VERSION = "1.1"; public static final String ATTACHMENT_PAGE_HISTORY_VERSION = "9.1"; - + static { SERVER_INDEX = new Integer(System.getProperty("xwikiExecutionIndex", "0")); SERVER_URL = HOST + ":" + (PORT + SERVER_INDEX); - if(HOST.contains("localhost")){ + if (HOST.contains("localhost")) { System.err.println("localhost on android means devices interface card!!!"); } } - - - - - - } diff --git a/xwiki-android-test-core/src/org/xwiki/android/data/filestore/test/DocumentFaoTest.java b/xwiki-android-test-core/src/org/xwiki/android/data/filestore/test/DocumentFaoTest.java new file mode 100644 index 0000000..3a6495c --- /dev/null +++ b/xwiki-android-test-core/src/org/xwiki/android/data/filestore/test/DocumentFaoTest.java @@ -0,0 +1,120 @@ +package org.xwiki.android.data.filestore.test; + +import java.io.File; +import java.util.List; + +import org.xwiki.android.context.XWikiApplicationContext; +import org.xwiki.android.core.test.properties.TestConstants; +import org.xwiki.android.data.fileStore.DocumentFao; +import org.xwiki.android.data.fileStore.FSDocumentReference; +import org.xwiki.android.data.fileStore.FileStoreException; +import org.xwiki.android.data.fileStore.FileStoreManager; +import org.xwiki.android.xmodel.blog.XBlogPost; +import org.xwiki.android.xmodel.entity.Comment; +import org.xwiki.android.xmodel.entity.Document; +import org.xwiki.android.xmodel.xobjects.XSimpleObject; + +import android.database.SQLException; +import android.test.AndroidTestCase; + +public class DocumentFaoTest extends AndroidTestCase +{ + + String wikiName, spaceName, pageName, objectClassname; + int objectNumber; + FileStoreManager fm; + DocumentFao fao; + + Document doc; + XBlogPost xblg; + Comment c; + + @Override + protected void setUp() throws Exception + { + wikiName = TestConstants.WIKI_NAME; + spaceName = TestConstants.SPACE_NAME; + pageName = TestConstants.PAGE_NAME; + objectClassname = TestConstants.OBJECT_CLASS_NAME_1; + objectNumber = TestConstants.OBJECT_NUMBER; + + XWikiApplicationContext ctx = XWikiApplicationContext.getInstance(); + fm = ctx.getFileStoreManager(); + fao = fm.getDocumentFao(); + + doc = new Document(wikiName, spaceName, pageName); + xblg = new XBlogPost(); + xblg.setContent("hi"); + c = new Comment("hi"); + XSimpleObject xso = new XSimpleObject(objectClassname) + { + }; + + doc.addComment(c); + xso.setNumber(objectNumber); + doc.setObject(xso); + xblg.setNumber(0); + doc.setObject(xblg); + + } + + public void testSave_01() + { + + FSDocumentReference ref = fao.save(doc, "sas"); + // verify + assertNotNull(ref.getFile()); + + } + + public void testSave_02_saveTwice() + { + + FSDocumentReference ref = fao.save(doc, "sas"); + FSDocumentReference ref2 = fao.save(doc, "sas"); + assertEquals(ref.getFile(), ref2.getFile()); + + } + + public void testSave_03_saveTwice() + { + boolean ex = false; + + FSDocumentReference ref = fao.save(doc, "sas"); + try { + FSDocumentReference ref2 = fao.save(doc, "exception now!"); // this is the current behaviour of save method. + // Delete this test case if you need to change behaviour :-). Make sure to do a manual functional test after + // semantics change.Should not affect anyway ;-). No support yet for multiple versions and other + // fs locations. + } catch (FileStoreException.UnsupportedOperation e) { + ex = true; + } + assertTrue(ex); + } + + public void testList_afterSave() + { + fao.save(doc, "sas"); + List lst = fao.listByTag("sas"); + + assertNotNull(lst); + assertTrue(lst.size() > 0); + + } + + public void testLoad_afterSave() + { + fao.save(doc, "sas"); + List lst = fao.listByTag("sas"); + Document ldoc = fao.load(lst.get(0).getFile()); + assertTrue(ldoc.getAllComments().size() > 0); + } + + public void testDelete() + { + fao.save(doc, "sas"); + FSDocumentReference fsref=new FSDocumentReference(); + fsref.copyfrom(doc.getDocumentReference()); + assertTrue(fao.delete(fsref)); + } +} diff --git a/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoCreate.java b/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoCreate.java index 510510f..fa8a127 100644 --- a/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoCreate.java +++ b/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoCreate.java @@ -29,6 +29,7 @@ import org.xwiki.android.xmodel.entity.Document; import org.xwiki.android.xmodel.entity.Tag; import org.xwiki.android.xmodel.xobjects.XSimpleObject; +import org.xwiki.android.xmodel.xobjects.XTextAreaProperty; import android.app.Application; import android.content.Context; @@ -42,7 +43,7 @@ public class TestDocumentRaoCreate extends AndroidTestCase { private static final String TAG = TestDocumentRaoCreate.class.getSimpleName(); // test org.xwiki.android.test.fixture.teardown.env parameters. - String serverUrl, username, password, wikiName, spaceName, pageName, attachmentName; + String serverUrl, username, password, wikiName, spaceName, pageName, attachmentName, objClsName1, objClsName2; static int count = 1; // tested apis @@ -75,7 +76,8 @@ public void setUp() throws Exception spaceName = TestConstants.SPACE_NAME; pageName = TestConstants.CREATE_PAGE_NAME+"-"+count; attachmentName = TestConstants.ATTACHMENT_NAME; - + objClsName1=TestConstants.OBJECT_CLASS_NAME_1; + objClsName2=TestConstants.OBJECT_CLASS_NAME_2; rm = new XmlRESTFulManager(serverUrl, username, password); api = rm.getRestConnector(); @@ -162,13 +164,14 @@ public void testCreate02() throws Throwable public void testCreate03() throws Throwable { boolean success; - so1 = new XSimpleObject("Blog.BlogClass") + so1 = new XSimpleObject(objClsName1) { }; - so2 = new XSimpleObject("Blog.BlogCategoryClass") + so2 = new XSimpleObject(objClsName2) { }; so2.setNumber(1); + so2.setProperty("content", new XTextAreaProperty("hi")); doc.addObject(so1); doc.setObject(so2); @@ -183,7 +186,7 @@ public void testCreate03() throws Throwable success = api.existsPage(wikiName, spaceName, pageName); if (success) { boolean crite1 = api.existsObject(wikiName, spaceName, pageName, so1.getClassName(), 0); - boolean crite2 = api.existsObject(wikiName, spaceName, pageName, so1.getClassName(), 0); + boolean crite2 = api.existsObject(wikiName, spaceName, pageName, so2.getClassName(), 0); success = crite1 & crite2; } } catch (RestException e) { @@ -333,9 +336,45 @@ public void testCreate_07_WithCmnts_SequenceCheck2() throws Throwable assertEquals("6", clst.get(3).text); } + } + + + public void testCreate_08_WithCmnts_wieredReplyTos() throws Throwable + { + + c1 = new Comment("0"); + c2 = new Comment("1"); + c3 = new Comment("2"); + c3.setId(2); //here the set id is changed + c4 = new Comment("3"); + c4.setId(3); + + c2.addReplyComment(c1); + c4.addReplyComment(c3); + + doc.addComment(c1, true); + doc.setComment(c3); + doc.setComment(c4); + + rao.create(doc); + + boolean success = api.existsPage(wikiName, spaceName, pageName); + assertTrue(success); + if (success) { + Comments cmnts = api.getPageComments(wikiName, spaceName, pageName); + List clst = cmnts.comments; + assertEquals(4, clst.size()); + int replyto = clst.get(2).replyTo; + assertEquals(1, replyto); + replyto=clst.get(3).replyTo; + assertEquals(0, replyto); + + assertEquals("6", clst.get(3).text); + } + } - public void testCreate_08_WithAttachment() throws Throwable + public void testCreate_09_WithAttachment() throws Throwable { assertNotNull("No file to upload as attachement. Set it up in setup method" ,af1); Attachment a=new Attachment(attachmentName, af1); diff --git a/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoRetreive.java b/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoRetreive.java index f9db7eb..4e99b7c 100644 --- a/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoRetreive.java +++ b/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoRetreive.java @@ -48,7 +48,7 @@ protected void setUp() throws Exception wikiName = TestConstants.WIKI_NAME; spaceName = TestConstants.SPACE_NAME; pageName = TestConstants.PAGE_NAME; - objectClassname = TestConstants.CLASS_NAME; + objectClassname = TestConstants.OBJECT_CLASS_NAME_1; objectNumber = TestConstants.OBJECT_NUMBER; propertyName = TestConstants.OBJECT_PROPERTY_NAME; diff --git a/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoUpdate.java b/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoUpdate.java index f8cdaa9..a20aee3 100644 --- a/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoUpdate.java +++ b/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoUpdate.java @@ -112,7 +112,7 @@ public void testUpdate_01() throws Throwable } - public void testUpdate_02() throws Throwable + public void testUpdate_02_withComments() throws Throwable { boolean success = true; @@ -133,6 +133,8 @@ public void testUpdate_02() throws Throwable assertTrue(numCmntsAfter == numCmntsBefore+1); } - //public void testUp + public void testUpdate_03_withComments(){ + + } } diff --git a/xwiki-android-test-core/src/org/xwiki/android/xmodel/entity/test/CommentModelStateTest.java b/xwiki-android-test-core/src/org/xwiki/android/xmodel/entity/test/CommentModelStateTest.java new file mode 100644 index 0000000..dd49d9b --- /dev/null +++ b/xwiki-android-test-core/src/org/xwiki/android/xmodel/entity/test/CommentModelStateTest.java @@ -0,0 +1,88 @@ +package org.xwiki.android.xmodel.entity.test; + +import org.xwiki.android.xmodel.entity.Comment; +import org.xwiki.android.xmodel.entity.Document; +import org.xwiki.android.xmodel.xobjects.XComment; + +import android.test.AndroidTestCase; + +public class CommentModelStateTest extends AndroidTestCase +{ + + String wikiName, spaceName, pageName; + + public void testCommentNormalAddSet(){ + Document doc=new Document(wikiName, spaceName, pageName); + Comment c=new Comment(); + Comment c2=new Comment(); + Comment c3=new Comment(); + c3.setId(2); + c.addReplyComment(c2); + c.addReplyComment(c3); + + doc.addComment(c, true); + doc.addComment(c2); + doc.addComment(c3); + + + + } + + public void testCommentNormalAddSet_02(){ + Document doc=new Document(wikiName, spaceName, pageName); + Comment c=new Comment(); + Comment c2=new Comment(); + Comment c3=new Comment(); + c3.setId(5); + c.addReplyComment(c2); + c.addReplyComment(c3); + + doc.addComment(c, true); + doc.addComment(c2); + doc.addComment(c3); + + + + } + + public void testCommentIllegalReplies(){ + Document doc=new Document(wikiName, spaceName, pageName); + Comment c=new Comment(); + Comment c2=new Comment(); + + doc.addComment(c);//c id=-11 + doc.addComment(c2); //c2 id=-12 + assertEquals(2, doc.getAllNewComments().size()); + + boolean success=false; + try{ + c2.addReplyComment(c); + }catch(IllegalStateException e){ + success=true; + } + assertTrue(success); + } + + public void testCommentIllegalReplies_02(){ + boolean success=false; + Document doc=new Document(wikiName, spaceName, pageName); + Comment c=new Comment(); + Comment c2=new Comment(); + Comment c3=new Comment(); + c3.setId(0); + c2.replyTo(c);//ok + c3.replyTo(c);//illegal here. Identified when adding to doc. + + try{ + doc.addComment(c, true); + doc.addComment(c2); + doc.addComment(c3); + }catch(IllegalStateException e){ + success=true; + } + assertTrue(success); + + + } + +} diff --git a/xwiki-android-test-core/src/org/xwiki/android/xmodel/entity/test/DocumentModelStateTest.java b/xwiki-android-test-core/src/org/xwiki/android/xmodel/entity/test/DocumentModelStateTest.java new file mode 100644 index 0000000..7365506 --- /dev/null +++ b/xwiki-android-test-core/src/org/xwiki/android/xmodel/entity/test/DocumentModelStateTest.java @@ -0,0 +1,50 @@ +package org.xwiki.android.xmodel.entity.test; + +import org.xwiki.android.xmodel.blog.XBlogPost; +import org.xwiki.android.xmodel.entity.Attachment; +import org.xwiki.android.xmodel.entity.Comment; +import org.xwiki.android.xmodel.entity.Document; + +import android.test.AndroidTestCase; + +public class DocumentModelStateTest extends AndroidTestCase +{ + + private String wikiName, spaceName, pageName; + + public void testAddSetStateManipulation(){ + + Document doc=new Document(wikiName, spaceName, pageName); + + XBlogPost xblgpst1=new XBlogPost(); + XBlogPost xblgpst2=new XBlogPost(); + + Comment c1=new Comment(); + Comment c2=new Comment(); + +// Attachment a1=new Attachment(); +// Attachment a2=new Attachment(); + + xblgpst1.setNumber(0); + xblgpst1.setEdited(false); + doc.setObject(xblgpst1); + xblgpst2.setNew(false); + doc.addObject(xblgpst1); + + c1.setId(0); + c1.setEdited(false); + c2.setId(1); + c2.setNew(false); + doc.setComment(c1); + doc.addComment(c2); + + assertEquals(0, doc.getAllNewObjects().size()); + assertEquals(0, doc.getAllEditedObjects().size()); + assertEquals(0, doc.getAllNewComments().size()); + assertEquals(0, doc.getAllEditedComments().size()); + + + + } + +} diff --git a/xwiki-android-test-core/src/org/xwiki/android/xmodel/entity/test/README b/xwiki-android-test-core/src/org/xwiki/android/xmodel/entity/test/README new file mode 100644 index 0000000..fde3221 --- /dev/null +++ b/xwiki-android-test-core/src/org/xwiki/android/xmodel/entity/test/README @@ -0,0 +1,2 @@ +Testing non android dependant xmodel. +If xmodel is seperated to a J2SE module take these to that module as well. \ No newline at end of file