diff --git a/.gitignore b/.gitignore index 58c3a45..0eae072 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ # Build -target -*/target -target-eclipse +target/ +target-eclipse/ # IDEA *.iml diff --git a/pom.xml b/pom.xml index f339655..321fadf 100644 --- a/pom.xml +++ b/pom.xml @@ -22,72 +22,187 @@ --> - 4.0.0 - - org.xwiki.commons - xwiki-commons - 4.1.2 - - org.xwiki.android - xwiki-android - 1.0-SNAPSHOT - XWiki Android - Parent POM - pom - XWiki Android - Parent POM - - true - + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*Test.java + + once + + + + + + xwikiExecutionDirectory + ${xwikiExecutionDirectory} + + + xwikiExecutionIndex + ${xwikiExecutionIndex} + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + unzip-application + test-compile + + unpack + + + + + org.xwiki.enterprise + xwiki-enterprise-jetty-${xwiki.db} + ${xwiki.xe.version} + zip + + + ${xwikiExecutionDirectory}/../ + + ${xwiki.test.skipUnpack} + + + + + + + + + + + + true + - true - - - - xwiki-rest-model - xwiki-android-rest - xwiki-android-core - xwiki-android-components - xwiki-android-client - - xwiki-android-tests-instrumentation - - - - - com.google.android - android - 2.1.2 - provided - - - - - src - - - - com.jayway.maven.plugins.android.generation2 - android-maven-plugin - 3.3.0 - - - ${env.ANDROID_HOME} - 2.1 - - true - true - true - - - true - - - - - maven-compiler-plugin - 2.3.2 - - - - + true + + + + 8080 + 8079 + 6666 + 4444 + + + http://localhost + + false + + false + ${project.parent.basedir}/target/test-server/xwiki-enterprise-jetty-${xwiki.db}-${xwiki.xe.version} + 0 + + + + + hsqldb + + + true + + + 4.1.3 + hsqldb + org.hsqldb + hsqldb + 2.2.8 + + + + \ No newline at end of file diff --git a/xwiki-android-client/AndroidManifest.xml b/xwiki-android-client/AndroidManifest.xml index 2e433fc..e846ac8 100644 --- a/xwiki-android-client/AndroidManifest.xml +++ b/xwiki-android-client/AndroidManifest.xml @@ -20,7 +20,7 @@ android:icon="@drawable/icon" android:label="@string/app_name" > diff --git a/xwiki-android-client/project.properties b/xwiki-android-client/project.properties index ed15f33..ef59822 100644 --- a/xwiki-android-client/project.properties +++ b/xwiki-android-client/project.properties @@ -12,7 +12,8 @@ # Project target. target=android-7 - -android.library.reference.3=../xwiki-android-core android.library.reference.1=../xwiki-android-components -android.library.reference.2=../xwiki-android-rest +android.library.reference.2=../xwiki-android-core +android.library.reference.3=../xwiki-android-rest + + diff --git a/xwiki-android-components/project.properties b/xwiki-android-components/project.properties index caec599..3993eb0 100644 --- a/xwiki-android-components/project.properties +++ b/xwiki-android-components/project.properties @@ -14,3 +14,5 @@ android.library=true # Project target. target=android-7 android.library.reference.1=../xwiki-android-core +android.library.reference.2=../xwiki-android-rest + diff --git a/xwiki-android-components/res/values/strings.xml b/xwiki-android-components/res/values/strings.xml index 80ac9d4..52af792 100644 --- a/xwiki-android-components/res/values/strings.xml +++ b/xwiki-android-components/res/values/strings.xml @@ -1,6 +1,6 @@ - Hello World, Main! + Hello World, org.xwiki.android.test.org.xwiki.test.integration.Main! Xwiki Components No items xwikiNavigatorListClick diff --git a/xwiki-android-core/pom.xml b/xwiki-android-core/pom.xml index 887a553..7f8e9dd 100644 --- a/xwiki-android-core/pom.xml +++ b/xwiki-android-core/pom.xml @@ -33,6 +33,7 @@ xwiki-android-core apklib + 1.0-SNAPSHOT XWiki android core lib diff --git a/xwiki-android-core/project.properties b/xwiki-android-core/project.properties index 8dffb0e..62f1e5f 100644 --- a/xwiki-android-core/project.properties +++ b/xwiki-android-core/project.properties @@ -14,3 +14,4 @@ target=android-7 android.library=true android.library.reference.1=../xwiki-android-rest + diff --git a/xwiki-android-core/src/org/xwiki/android/context/XWikiApplicationContext.java b/xwiki-android-core/src/org/xwiki/android/context/XWikiApplicationContext.java index deac8ff..62e396c 100644 --- a/xwiki-android-core/src/org/xwiki/android/context/XWikiApplicationContext.java +++ b/xwiki-android-core/src/org/xwiki/android/context/XWikiApplicationContext.java @@ -105,7 +105,8 @@ public void updateToAuthenticatedState(User user) public static XWikiApplicationContext getInstance() { - return currContext; + if(currContext==null) throw new RuntimeException("please set XWikiApplicationContext in android manifest * doc.set("XOMyClass/0",o1) * the create method adds the same object twice to the remote page. * @param doc + * @return created document extracted from server. */ @Override public Document create(Document doc) throws RestConnectionException ,RaoException; + /** + * check wether the doc exists on server + * @param dref + * @return + */ + public boolean exists(DocumentReference dref) throws RestConnectionException,RaoException;; + /** * default semantics * diff --git a/xwiki-android-core/src/org/xwiki/android/rest/ral/RESTfulManager.java b/xwiki-android-core/src/org/xwiki/android/rest/ral/RESTfulManager.java index 45ff583..c35372f 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/ral/RESTfulManager.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/ral/RESTfulManager.java @@ -1,7 +1,11 @@ package org.xwiki.android.rest.ral; +import java.util.Properties; + import org.xwiki.android.resources.Space; import org.xwiki.android.resources.Wiki; +import org.xwiki.android.rest.RestConnection; +import org.xwiki.android.rest.XWikiAPI; import org.xwiki.android.xmodel.entity.Document; public interface RESTfulManager @@ -11,4 +15,9 @@ public interface RESTfulManager SpaceRao newSpaceRao(); WikiRao newWikiRao(); + + RestConnection getConnection(); + + + } diff --git a/xwiki-android-core/src/org/xwiki/android/rest/ral/Rao.java b/xwiki-android-core/src/org/xwiki/android/rest/ral/Rao.java index 2e2a7db..1ba04a2 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/ral/Rao.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/ral/Rao.java @@ -16,6 +16,8 @@ interface Rao { T create(T resrc) throws RestConnectionException,RaoException; + + boolean exists(T resrc) throws RestConnectionException,RaoException; T retreive(T resrc) throws RestConnectionException,RaoException; 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 4a146cb..3421141 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 @@ -1,5 +1,7 @@ package org.xwiki.android.rest.ral; +import org.xwiki.android.rest.RestException; + /** * @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. @@ -7,6 +9,9 @@ public class RaoException extends Exception { + private RestException cause; + private int code; + public RaoException() { super(); @@ -19,6 +24,13 @@ public RaoException(String detailMessage, Throwable throwable) // TODO Auto-generated constructor stub } + public RaoException(String detailMessage, RestException rex) + { + super(detailMessage, rex); + this.cause = rex; + this.code = rex.getCode(); + } + public RaoException(String detailMessage) { super(detailMessage); @@ -30,5 +42,34 @@ public RaoException(Throwable throwable) super(throwable); // TODO Auto-generated constructor stub } + + public RaoException(RestException rex) + { + super(rex); + this.cause = rex; + this.code = rex.getCode(); + } + + public RestException getCause() + { + return cause; + } + + public void setCause(RestException cause) + { + this.cause = cause; + } + + public int getCode() + { + return code; + } + + public void setCode(int code) + { + this.code = code; + } + + } diff --git a/xwiki-android-core/src/org/xwiki/android/rest/ral/Verifier.java b/xwiki-android-core/src/org/xwiki/android/rest/ral/Verifier.java new file mode 100644 index 0000000..b50bdd0 --- /dev/null +++ b/xwiki-android-core/src/org/xwiki/android/rest/ral/Verifier.java @@ -0,0 +1,33 @@ +package org.xwiki.android.rest.ral; + +import org.xwiki.android.xmodel.entity.Document; + +import android.util.Log; + +/** + * + * Verifiere to check parameters passed to each ral method. + * + */ +class Verifier +{ + + + + private static final String TAG ="RAL Verifier"; + + /** + * + * @param doc + * @return verification result. success=true + */ + static boolean verifyDocumentForCreate(Document doc){ + boolean title=false; //verify title set + title=(doc.getTitle()!=null & !doc.getTitle().equals("")); + if(!title){ + Log.w(TAG, "doc must have a title. For XWiki version 3.x and below."); + } + return title; + + } +} 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 ccc7159..4fecbc1 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 @@ -11,12 +11,13 @@ import org.xwiki.android.context.UserSession; import org.xwiki.android.context.XWikiApplicationContext; +import org.xwiki.android.resources.Comment; import org.xwiki.android.resources.Object; import org.xwiki.android.resources.Page; import org.xwiki.android.rest.RestConnectionException; import org.xwiki.android.rest.RestException; -import org.xwiki.android.rest.XWikiRestConnector; -import org.xwiki.android.rest.XWikiRestfulAPI; +import org.xwiki.android.rest.XWikiRestConnecion; +import org.xwiki.android.rest.XWikiAPI; import org.xwiki.android.rest.ral.algo.DocRetreiveStrategy; import org.xwiki.android.rest.ral.algo.DocUpdateStrategy; import org.xwiki.android.rest.ral.algo.IDocRetreiveStrategy; @@ -44,14 +45,14 @@ class XmlDocumentRao implements DocumentRao private static int PAGE = DocumentDismantler_XML.PAGE; private static int OBJECTS = DocumentDismantler_XML.OBJECTS; private static int ALL = DocumentDismantler_XML.ALL; - private XWikiRestfulAPI api; + private XWikiAPI api; private IDocRetreiveStrategy retStr; private IDocUpdateStragegy updStr; public XmlDocumentRao(String serverUrl, String username, String password) { - api = new XWikiRestConnector(serverUrl, username, password); + api = new XWikiRestConnecion(serverUrl, username, password); retStr = new DocRetreiveStrategy(username, password, serverUrl); updStr = new DocUpdateStrategy(serverUrl, username, password); // consider IoC to app context @@ -63,6 +64,9 @@ public XmlDocumentRao(String serverUrl, String username, String password) @Override public Document create(Document doc) throws RestConnectionException, RaoException { + Verifier.verifyDocumentForCreate(doc);//just logs warn msgs if verification failed. Runtime exceptions will be thrown + //when server responds with errors. + // TODO: make advanced algo.Put it in ral.algo package. Here we use simple non parellal way to create doc. DocumentDismantler_XML con = new DocumentDismantler_XML(ALL); DocLaunchPadForXML pad = con.convertDocument(doc); @@ -92,7 +96,7 @@ public Index(int first, int last) } Map indexes=new HashMap(10);//for < 10 classes of objs. - Map edObjs = pad.getEditedObjects(); + Map edObjs = pad.getEditedObjects(); //objects added using set method. explicitly palced at smplObject.number A:for(String k: edObjs.keySet()){ String ss[]=k.split("/"); @@ -101,7 +105,7 @@ public Index(int first, int last) Index i= indexes.get(clsName); if(i==null){//new class obList.add(o); - int first=obList.size(); + int first=obList.size()-1; int last=first; indexes.put(clsName, new Index(first,last)); }else{ @@ -114,7 +118,7 @@ public Index(int first, int last) continue A; } } - obList.add(last+1,edObjs.get(k)); + obList.add(last+1,o); i.last++; } } @@ -126,7 +130,7 @@ public Index(int first, int last) Index i=indexes.get(clsName); if(i==null){//this is new object obList.add(o); - int first=obList.size(); + int first=obList.size()-1; int last=first; indexes.put(clsName, new Index(first, last)); }else{ @@ -154,7 +158,14 @@ public Index(int first, int last) //upload objects. for (Object object : obList) { api.addObject(wikiName, spaceName, pageName, object); - } + } + + List newCmnts=pad.getNewComments(); + List edCmnts=pad.getEditedComments(); + + + + return null; // TODO RET CREATED DOC. NEED TO ADD CUSTOMIZATION CODE WETHER TO RET OR NOT. } } catch (RestException e) { @@ -164,6 +175,28 @@ public Index(int first, int last) } } + + @Override + public boolean exists(Document doc) throws RestConnectionException, RaoException + { + String wikiName = doc.getWikiName(); + String spaceName = doc.getSpaceName(); + String pageName = doc.getPageName(); + boolean exists; + try { + exists=api.existsPage(wikiName, spaceName, pageName); + return exists; + } catch (RestException e) { + throw new RaoException(e); + } + } + + @Override + public boolean exists(DocumentReference dref) throws RestConnectionException, RaoException + { + // TODO Auto-generated method stub + return false; + } @Override public Document retreive(Document doc) throws RestConnectionException, RaoException @@ -224,4 +257,8 @@ public void delete(Document resrc) throws RestConnectionException, RaoException delete(resrc.getDocumentReference()); } + + + + } diff --git a/xwiki-android-core/src/org/xwiki/android/rest/ral/XmlRESTFulManager.java b/xwiki-android-core/src/org/xwiki/android/rest/ral/XmlRESTFulManager.java index b9e785d..f3d73cf 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/ral/XmlRESTFulManager.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/ral/XmlRESTFulManager.java @@ -1,7 +1,12 @@ package org.xwiki.android.rest.ral; +import java.util.Properties; + import org.xwiki.android.resources.Space; import org.xwiki.android.resources.Wiki; +import org.xwiki.android.rest.RestConnection; +import org.xwiki.android.rest.XWikiAPI; +import org.xwiki.android.rest.XWikiRestConnecion; import org.xwiki.android.xmodel.entity.Document; public class XmlRESTFulManager implements RESTfulManager @@ -15,6 +20,22 @@ public XmlRESTFulManager(String serverUrl, String username, String password) this.username = username; this.password = password; } + + /** + * + * @param username + * @param password + * @param props additional properties. Like props to support OAuth when XWiki Servers support them and etc. + * + */ + + public XmlRESTFulManager(String serverUrl, String username, String password, Properties props) + { + this.serverUrl = serverUrl; + this.username = username; + this.password = password; + throw new UnsupportedOperationException("impl in future, when need arise for additional props for connection. Like OAuth etc..."); + } public DocumentRao newDocumentRao() { @@ -35,4 +56,12 @@ public WikiRao newWikiRao() } + @Override + public RestConnection getConnection() + { + return new XWikiRestConnecion(serverUrl,username,password); + } + + + } diff --git a/xwiki-android-core/src/org/xwiki/android/rest/ral/algo/DocRetreiveStrategy.java b/xwiki-android-core/src/org/xwiki/android/rest/ral/algo/DocRetreiveStrategy.java index d9b2479..845f4a5 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/ral/algo/DocRetreiveStrategy.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/ral/algo/DocRetreiveStrategy.java @@ -13,8 +13,8 @@ import org.xwiki.android.resources.Tags; import org.xwiki.android.rest.RestConnectionException; import org.xwiki.android.rest.RestException; -import org.xwiki.android.rest.XWikiRestConnector; -import org.xwiki.android.rest.XWikiRestfulAPI; +import org.xwiki.android.rest.XWikiRestConnecion; +import org.xwiki.android.rest.XWikiAPI; import org.xwiki.android.rest.ral.RaoException; import org.xwiki.android.rest.reference.DocumentReference; import org.xwiki.android.rest.transformation.DocumentBuilder; @@ -27,14 +27,14 @@ public class DocRetreiveStrategy implements IDocRetreiveStrategy String username; String password; String url; - XWikiRestfulAPI api; + XWikiAPI api; public DocRetreiveStrategy(String username, String password, String serverUrl) { this.username = username; this.password = password; this.url = serverUrl; - api=new XWikiRestConnector(url, username, password); + api=new XWikiRestConnecion(url, username, password); } @Override diff --git a/xwiki-android-core/src/org/xwiki/android/rest/ral/algo/DocUpdateStrategy.java b/xwiki-android-core/src/org/xwiki/android/rest/ral/algo/DocUpdateStrategy.java index 3712421..1a2df91 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/ral/algo/DocUpdateStrategy.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/ral/algo/DocUpdateStrategy.java @@ -8,8 +8,8 @@ import org.xwiki.android.resources.Tags; import org.xwiki.android.rest.RestConnectionException; import org.xwiki.android.rest.RestException; -import org.xwiki.android.rest.XWikiRestConnector; -import org.xwiki.android.rest.XWikiRestfulAPI; +import org.xwiki.android.rest.XWikiRestConnecion; +import org.xwiki.android.rest.XWikiAPI; import org.xwiki.android.rest.ral.RaoException; import org.xwiki.android.rest.transformation.DocLaunchPadForXML; import org.xwiki.android.rest.transformation.DocumentDismantler_XML; @@ -22,12 +22,12 @@ public class DocUpdateStrategy implements IDocUpdateStragegy { private static final String TAG = "DocUpdate"; - XWikiRestfulAPI api; + XWikiAPI api; DocumentDismantler_XML dismantler; public DocUpdateStrategy(String serverUrl, String username, String password) { - api = new XWikiRestConnector(serverUrl, username, password); + api = new XWikiRestConnecion(serverUrl, username, password); dismantler = new DocumentDismantler_XML(); } @@ -51,7 +51,7 @@ public Document update(Document d) throws RestConnectionException, RaoException String ss[] = key.split("/"); Object ores = e.getValue(); String objectClassname = ss[0]; - String objectNumber = ss[1]; + int objectNumber = new Integer(ss[1]); try { api.updateObject(wikiName, spaceName, pageName, objectClassname, objectNumber, ores); numEdObj++; // after the api op. If exception happens no ++ happens. @@ -89,11 +89,11 @@ 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 comment : pad.getEditedComments()) { - String objectClassname = comment.getClassName(); - String objectNumber = comment.getNumber() + ""; + for (Object cmntObj : pad.getEditedComments()) { + String objectClassname = cmntObj.getClassName(); + int objectNumber = cmntObj.getNumber(); try { - api.updateObject(wikiName, spaceName, pageName, objectClassname, objectNumber, comment); + api.updateObject(wikiName, spaceName, pageName, objectClassname, objectNumber, cmntObj); numEdCmnt++; } catch (RestException e1) { // TODO Auto-generated catch block diff --git a/xwiki-android-core/src/org/xwiki/android/rest/reference/DocumentReference.java b/xwiki-android-core/src/org/xwiki/android/rest/reference/DocumentReference.java index cb0ea5e..01cd647 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/reference/DocumentReference.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/reference/DocumentReference.java @@ -80,7 +80,7 @@ public void setPageName(String pageName) @Override public String getURL() { - return getAuthority() + "/xwiki/rest/wikis/" + wikiName + "/spaces" + spaceName + "/pages" + pageName; + return getAuthority() + "/xwiki/xmlrpc/wikis/" + wikiName + "/spaces" + spaceName + "/pages" + pageName; } } 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 4a7068c..2d46924 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 @@ -1,9 +1,11 @@ package org.xwiki.android.rest.transformation; import java.util.ArrayList; +import java.util.HashSet; import java.util.Hashtable; import java.util.List; import java.util.Map; +import java.util.Set; import org.xwiki.android.resources.Attachment; import org.xwiki.android.resources.Comment; @@ -30,8 +32,8 @@ public class DocLaunchPadForXML Tags tags; - List attatchmentsToupload;//new and update both. Same rest method. - List deletedAttachments; + List attatchmentsToupload;//new and update both. Same xmlrpc method. + Set deletedAttachments; public DocLaunchPadForXML() { @@ -43,7 +45,7 @@ public DocLaunchPadForXML() deletedComments=new ArrayList(); attatchmentsToupload=new ArrayList(); - deletedAttachments=new ArrayList(); + deletedAttachments=new HashSet(); } @@ -135,7 +137,7 @@ public List getAttatchmentsToupload( return attatchmentsToupload; } - public List getDeletedAttachments() + public Set getDeletedAttachments() { return deletedAttachments; } diff --git a/xwiki-android-core/src/org/xwiki/android/rest/transformation/DocumentBuilder_XML.java b/xwiki-android-core/src/org/xwiki/android/rest/transformation/DocumentBuilder_XML.java index 6dcb2c9..b41d37b 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/transformation/DocumentBuilder_XML.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/transformation/DocumentBuilder_XML.java @@ -106,7 +106,7 @@ private DocumentBuilder initWithPage(Page p) } /* (non-Javadoc) - * @see org.xwiki.android.rest.transformation.DocumentBuilder#withObject(org.xwiki.android.resources.Object) + * @see org.xwiki.android.xmlrpc.transformation.DocumentBuilder#withObject(org.xwiki.android.resources.Object) */ @Override public DocumentBuilder withObject(Object res) @@ -146,7 +146,7 @@ public DocumentBuilder withObject(Object res) } /* (non-Javadoc) - * @see org.xwiki.android.rest.transformation.DocumentBuilder#withObjectSummary(org.xwiki.android.resources.ObjectSummary) + * @see org.xwiki.android.xmlrpc.transformation.DocumentBuilder#withObjectSummary(org.xwiki.android.resources.ObjectSummary) */ @Override public DocumentBuilder withObjectSummary(ObjectSummary res) @@ -171,7 +171,7 @@ public DocumentBuilder withObjectSummary(ObjectSummary res) } /* (non-Javadoc) - * @see org.xwiki.android.rest.transformation.DocumentBuilder#withComments(org.xwiki.android.resources.Comments) + * @see org.xwiki.android.xmlrpc.transformation.DocumentBuilder#withComments(org.xwiki.android.resources.Comments) */ @Override public DocumentBuilder withComments(Comments cmnts) @@ -183,7 +183,7 @@ public DocumentBuilder withComments(Comments cmnts) } /* (non-Javadoc) - * @see org.xwiki.android.rest.transformation.DocumentBuilder#withComment(org.xwiki.android.resources.Comment) + * @see org.xwiki.android.xmlrpc.transformation.DocumentBuilder#withComment(org.xwiki.android.resources.Comment) */ @Override public DocumentBuilder withComment(Comment cmntRes) @@ -201,7 +201,7 @@ public DocumentBuilder withComment(Comment cmntRes) } /* (non-Javadoc) - * @see org.xwiki.android.rest.transformation.DocumentBuilder#withTags(org.xwiki.android.resources.Tags) + * @see org.xwiki.android.xmlrpc.transformation.DocumentBuilder#withTags(org.xwiki.android.resources.Tags) */ @Override public DocumentBuilder withTags(Tags tags) @@ -214,7 +214,7 @@ public DocumentBuilder withTags(Tags tags) } /* (non-Javadoc) - * @see org.xwiki.android.rest.transformation.DocumentBuilder#withAttachments(org.xwiki.android.resources.Attachments) + * @see org.xwiki.android.xmlrpc.transformation.DocumentBuilder#withAttachments(org.xwiki.android.resources.Attachments) */ @Override public DocumentBuilder withAttachments(Attachments attachments) @@ -226,7 +226,7 @@ public DocumentBuilder withAttachments(Attachments attachments) } /* (non-Javadoc) - * @see org.xwiki.android.rest.transformation.DocumentBuilder#withAttachment(org.xwiki.android.resources.Attachment) + * @see org.xwiki.android.xmlrpc.transformation.DocumentBuilder#withAttachment(org.xwiki.android.resources.Attachment) */ @Override public DocumentBuilder withAttachment(Attachment attachment) @@ -367,7 +367,7 @@ public DocumentBuilder withAttachment(Attachment attachment) // } // /* (non-Javadoc) - * @see org.xwiki.android.rest.transformation.DocumentBuilder#build() + * @see org.xwiki.android.xmlrpc.transformation.DocumentBuilder#build() */ @Override public Document build() 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 35d3195..d71f572 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 @@ -77,7 +77,7 @@ public DocLaunchPadForXML convertDocument(Document doc) pad.editedObjects.put(en.getKey(),ores ); } // mark deleted objects. - List delObjs = doc.getAllDeletedObjects(); + Set delObjs = doc.getDeletedObjects(); for (String objKey : delObjs) { pad.deletedObjects.add(objKey); } @@ -97,13 +97,11 @@ public DocLaunchPadForXML convertDocument(Document doc) // updates List editedComments = doc.getAllEditedComments(); for (Comment ec : editedComments) { - Object ores = new Object(); - ores.setClassName("XWiki.XWikiComments"); - ores.setNumber(ec.getId()); + Object ores=translator.toObject(ec); pad.editedComments.add(ores); } // deletes - List deletedComments = doc.getAllDeletedComments(); + Set deletedComments = doc.getDeletedCommentSet(); for (int id : deletedComments) { String key = "XWiki.XWikiComments"; key += "/" + id; @@ -112,7 +110,7 @@ public DocLaunchPadForXML convertDocument(Document doc) } if ((parts & TAGS) == TAGS) { - // new,update,del all in one rest method. + // new,update,del all in one xmlrpc method. Tags tgsres = null; if (doc.getAllNewTags() != null && !doc.getAllNewTags().isEmpty()) { tgsres = translator.toTags(doc.getTags()); @@ -126,7 +124,7 @@ public DocLaunchPadForXML convertDocument(Document doc) edlist.addAll(newlist); pad.attatchmentsToupload = edlist; // deleted ones - pad.deletedAttachments = doc.getAllDeletedAttachments(); + pad.deletedAttachments = doc.getDeletedAttachments(); } // "ALL", its automatically done as 31=1+2+... return pad; diff --git a/xwiki-android-core/src/org/xwiki/android/rest/transformation/XModelTranslator_XML.java b/xwiki-android-core/src/org/xwiki/android/rest/transformation/XModelTranslator_XML.java index 7990668..c4d0dd8 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/transformation/XModelTranslator_XML.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/transformation/XModelTranslator_XML.java @@ -97,33 +97,55 @@ public Object toObject(XSimpleObject xso) XProperty xp = entry.getValue(); String pName = entry.getKey(); // transfer fields to resource.Property - Property p = new Property(); - p.setName(pName); - p.setType(xp.getType()); - p.setValue(xp.toString()); - - Map attrs = xp.getAllAttributes(); - Set> atrEntrySet = attrs.entrySet(); - - List attrList = new ArrayList(); - for (Entry atrEn : atrEntrySet) { - Attribute atr = new Attribute(); - atr.setName(atrEn.getKey()); - atr.setValue(atrEn.getValue().toString()); - attrList.add(atr); - } - plist.add(p.withAttributes(attrList)); + if(xp!=null){ + Property p = new Property(); + p.setName(pName); + p.setType(xp.getType()); + p.setValue(xp.toString()); + + Map attrs = xp.getAllAttributes(); + Set> atrEntrySet = attrs.entrySet(); + List attrList = new ArrayList(); + for (Entry atrEn : atrEntrySet) { + Attribute atr = new Attribute(); + atr.setName(atrEn.getKey()); + atr.setValue(atrEn.getValue().toString()); + attrList.add(atr); + } + plist.add(p.withAttributes(attrList)); + } } return obj.withProperties(plist); } + + public Object toObject(org.xwiki.android.xmodel.entity.Comment c){ + XComment xc=new XComment(); + xc.setNumber((c.getId())); + xc.setAuthor(c.getAuthor()); + xc.setDate(c.getDate()); + xc.setComment(c.getText()); + xc.setReplyto(c.getReplyTo()); + return toObject(xc); + } + + public Object toObject(Comment c){ + XComment xc=new XComment(); + xc.setAuthor(c.getAuthor()); + xc.setComment(c.getText()); + //xc.setDate(c.getDate()); + //TODO + return toObject(xc); + } public Comment toComment(org.xwiki.android.xmodel.entity.Comment c) { Comment cres = new Comment(); cres.id = c.getId(); cres.author = c.getAuthor(); - cres.date = c.getDate().toGMTString(); + if(c.getDate()!=null){ + cres.date = c.getDate().toGMTString(); + } cres.highlight = c.getHighlight(); cres.text = c.getText(); cres.replyTo = c.getReplyTo(); @@ -131,22 +153,9 @@ public Comment toComment(org.xwiki.android.xmodel.entity.Comment c) } - public Object toCommentObject(org.xwiki.android.xmodel.entity.Comment c){ - XComment xc=new XComment(); - xc.setAuthor(c.getAuthor()); - xc.setComment(c.getText()); - //TODO - return toObject(xc); - } + - public Object convertToCommentObject(Comment c){ - XComment xc=new XComment(); - xc.setAuthor(c.getAuthor()); - xc.setComment(c.getText()); - //xc.setDate(c.getDate()); - //TODO - return toObject(xc); - } + public Tags toTags(List tags) @@ -180,10 +189,19 @@ public Attachment toAttachment(org.xwiki.android.xmodel.entity.Attachment a) atres.setPageVersion(a.getVersion()); atres.setMimeType(a.getMimeType()); atres.setAuthor(a.getAuthor()); - atres.setDate(a.getDate().toGMTString()); - atres.setXwikiAbsoluteUrl(a.getXwikiAbsoluteUrl().toString()); - atres.setXwikiRelativeUrl(a.getXwikiAbsoluteUrl().toString()); + if(atres.getDate()!=null){ + atres.setDate(a.getDate().toGMTString()); + } + if(a.getXwikiAbsoluteUrl()!=null){ + atres.setXwikiAbsoluteUrl(a.getXwikiAbsoluteUrl().toString()); + } + if(a.getXwikiRelativeUrl()!=null){ + atres.setXwikiRelativeUrl(a.getXwikiAbsoluteUrl().toString()); + } return null; } } + +//TODO overload above methods with ex: toComment(Comment c, boolean minimal) where minimal true means +// only essential data to create a comment in the server are put to resources.Comment DTO. diff --git a/xwiki-android-core/src/org/xwiki/android/rest/transformation/XObjectFactory.java b/xwiki-android-core/src/org/xwiki/android/rest/transformation/XObjectFactory.java index 5c4e821..2ac9d75 100644 --- a/xwiki-android-core/src/org/xwiki/android/rest/transformation/XObjectFactory.java +++ b/xwiki-android-core/src/org/xwiki/android/rest/transformation/XObjectFactory.java @@ -16,7 +16,7 @@ public class XObjectFactory private static Hashtable> xobjMap; static { xobjMap = new Hashtable>(20); - // Include code here to map XSimple Objects to the rest model objects by className. (keep alphabetic ordering + // Include code here to map XSimple Objects to the xmlrpc model objects by className. (keep alphabetic ordering // for clarity) // xobj in app within minutes diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Attachment.java b/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Attachment.java index 8659b07..bffcf78 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Attachment.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Attachment.java @@ -21,6 +21,20 @@ public class Attachment extends Resource private URL xwikiAbsoluteUrl; // local private File file; + + public Attachment() + { + + } + + + public Attachment(String name, File file) + { + super(); + this.name = name; + this.file = file; + } + public String getId() { 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 1d16cb8..b82ae9c 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 @@ -1,5 +1,6 @@ package org.xwiki.android.xmodel.entity; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -8,112 +9,187 @@ public class Comment extends Resource { - List links; - int id; - String author; - Date date; - String text; - int replyTo; - String highlight; - //object associated to this comment. - XComment xobj; - public List getLinks() - { - return links; - } - public void addLink(Link link) - { - links.add(link); - } - public void removeLink(int index){ - links.remove(index); - } - public void clearLinks(){ - links.clear(); - } - - - public int getId() - { - return id; - } - public void setId(int id) - { - this.id = id; - } - 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; - } - 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; - } - - } - + List links; + 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){ + rply.replyTo=this.id;//setting id is done by document. If this cmnt is owned by a doc then it is + ownerDoc.addComment(rply); + } + return true; + }else{ + return false; + } + } + + public List getReplies() + { + return replies; + } + + public void setReplies(List replies) + { + this.replies = replies; + } + + public List getLinks() + { + return links; + } + + public void addLink(Link link) + { + links.add(link); + } + + public void removeLink(int index) + { + links.remove(index); + } + + public void clearLinks() + { + links.clear(); + } + + 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; + } + + 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; + } + + } + } diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Document.java b/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Document.java index ee87e77..388e58a 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Document.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/entity/Document.java @@ -1,9 +1,12 @@ package org.xwiki.android.xmodel.entity; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.Hashtable; import java.util.List; import java.util.Map; +import java.util.Set; import org.xwiki.android.resources.Attachments; import org.xwiki.android.rest.reference.DocumentReference; @@ -27,7 +30,7 @@ public class Document extends DocumentBase private Map objects; // key= ClassName/number - private List comments; // key = int index in the list + private Map comments; // key = int index in the list private Map attatchments;// key = resource id. ex: xwiki:Blog.BlogPost1@mypic -->key is: mypic @@ -63,7 +66,7 @@ public Document(String wikiName, String spaceName, String pageName) editedObjects = new Hashtable(); deletedObjects = new ArrayList(); - comments = new ArrayList(); + comments = new HashMap(); newComments=new ArrayList(); editedComments=new ArrayList(); deletedCommetns=new ArrayList(); @@ -97,6 +100,17 @@ public XSimpleObject getObject(String key) } return obj; } + + public XSimpleObject getObject(String className, int number) + { + String key=className+"/"+number; + XSimpleObject obj = objects.get(key); + if(obj!=null){ + obj.setEdited(true); + } + return obj; + } + /** * Update a existing object in the doc. The update is done if the Object.isAltered returns true. This property is @@ -105,6 +119,7 @@ public XSimpleObject getObject(String key) * @param key * @param object */ + @Deprecated public void setObject(String key, XSimpleObject object) { String keyprefix=object.getClassName(); @@ -127,6 +142,29 @@ public void setObject(String key, XSimpleObject object) editedObjects.put(key, object); } objects.put(key, object); + } + /** + * + * @param object + * @return key to refer this obj. + */ + public String setObject(XSimpleObject object) + { + if(object.getNumber()==-1){ + throw new IllegalArgumentException("object's number must be set"); + } + if(object.getClassName().equals("")){ + throw new IllegalArgumentException("object's class name must not be empty"); + } + //end validation + + String key=object.getClassName()+"/"+object.getNumber(); + + if (object.isEdited()) {//may remove this line. Since set is done because XObj is edited. + editedObjects.put(key, object); + } + objects.put(key, object); + return key; } private int _addNum = 0; @@ -169,29 +207,91 @@ public Comment getComment(int id){ * @return id of the new comment. */ public int addComment(Comment cmnt){ - comments.add(cmnt); - cmnt.setId(comments.size()-1); - newComments.add(cmnt); - return comments.size()-1; + if(!newComments.contains(cmnt)&&!comments.containsKey(cmnt.getId())){ + newComments.add(cmnt); + int id=-newComments.size()-10; + comments.put(id,cmnt); + cmnt.setId(id); + cmnt.setOwner(this); + return id; + }else{ + return cmnt.getId(); + } } + + /** + * + * @param cmnt + * @param cascade if true adds all the reply comments as well to the document. + * @return int[0]=parent comments id, int[1] number of comments added + */ + public int[] addComment(Comment prntCmnt, boolean cascade){ + int pid=addComment(prntCmnt); + int[] rslts=new int[2]; + rslts[0]=pid; + rslts[1]=1; + if(cascade){ + for(Comment rply:prntCmnt.getReplies()){ + int[] recRslt=addComment(rply,true); + rslts[1]+=recRslt[1]; + } + } + return rslts; + } + + @Deprecated //TODO remove soon public void setComment(int id, Comment cmnt){ - comments.set(id, cmnt); + if(id<0){ + throw new IllegalArgumentException("comment id should be gt or eq to 0, \n If you are seting a comment that was added with add method no need to set again"); + } + comments.put(id, cmnt); if(!editedComments.contains(cmnt)){ editedComments.add(cmnt); - } + } + if(cmnt.getDocument()!=null){ + if(this!=cmnt.getDocument())throw new IllegalStateException("comment is already owned by another doc"); + }else{ + cmnt.setId(id); + cmnt.setOwner(this); + } + } + + public void setComment(Comment cmnt){ + int id=cmnt.getId(); + if(id<0){ + throw new IllegalArgumentException("comment id should be gt or eq to 0, \n If you are seting a comment that was added with add method no need to set again"); + } + comments.put(id, cmnt); + if(!editedComments.contains(cmnt)){ + editedComments.add(cmnt); + } + if(cmnt.getDocument()!=null){ + if(this!=cmnt.getDocument())throw new IllegalStateException("comment is already owned by another doc"); + }else{ + cmnt.setId(id); + cmnt.setOwner(this); + } + + } + + public void deleteComment(int id){ comments.remove(id); Comment cmnt=new Comment(); cmnt.setId(id); - int i=newComments.indexOf(cmnt); - int j=editedComments.indexOf(cmnt); - if(i>0){ - newComments.remove(i); - } - if(j>0){ - editedComments.remove(j); - } + if(id<0){ + int i=newComments.indexOf(cmnt); + if(i>=0){ + newComments.remove(i); + } + }else{ + int j=editedComments.indexOf(cmnt); + if(j>=0){ + editedComments.remove(j); + } + } + deletedCommetns.add(id); } @@ -280,7 +380,7 @@ public List getChildrenDocuments() return children; } - public List getAllComments() + public Map getAllComments() { return comments; } @@ -335,22 +435,40 @@ public List getAllEditedAttachments() - public List getAllDeletedObjects() + public List listObjectDeletions() { return deletedObjects; } - public List getAllDeletedComments() + public Set getDeletedObjects(){ + Set s=new HashSet(); + s.addAll(deletedObjects); + return s; + } + + public List listCommentDeletions() { return deletedCommetns; } + public Set getDeletedCommentSet(){ + Set s=new HashSet(); + s.addAll(deletedCommetns); + return s; + } + - public List getAllDeletedAttachments(){ + public List listAttachmentDeletions(){ return deletedAttachments; - } + } + + public Set getDeletedAttachments(){ + Set s=new HashSet(); + s.addAll(deletedObjects); + return s; + } - public List getAllDeletedTags(){ + public List listTagDeletions(){ return deletedTags; } diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/entity/DocumentBase.java b/xwiki-android-core/src/org/xwiki/android/xmodel/entity/DocumentBase.java index 72172fd..2c0bcee 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/entity/DocumentBase.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/entity/DocumentBase.java @@ -66,7 +66,7 @@ public abstract class DocumentBase extends Resource // other fields protected DocumentReference docRef; - protected boolean offlineMode; + public DocumentBase(String wikiName, String spaceName, String pageName) { @@ -92,22 +92,7 @@ public void setDocumentReference(DocumentReference docRef) this.docRef = docRef; } - /** - * if in offline mode the create(), update ... methods will use the save method to save a local copy. The delete - * method will mark the document for deletion. The sync service will take the responsibilities after internet - * connection is available. * - * - * @param val - */ - public void setOfflineMode(boolean val) - { - this.offlineMode = val; - } - - public boolean isOfflineMode() - { - return offlineMode; - } + // /** // * valid for only Documents retreived from server. 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 bcf1adf..4a822c7 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 @@ -106,6 +106,7 @@ public void setpAuthor(XStringProperty author) { fields.put("author", author); } + public XTextAreaProperty getpComment() { return (XTextAreaProperty) fields.get("comment"); diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XPropertyBase.java b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XPropertyBase.java index b754aba..7802337 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XPropertyBase.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XPropertyBase.java @@ -148,7 +148,11 @@ public String getValidationRegExp() @Override public void setAttribute(String name, Object val) throws IllegalArgumentException { - if(name.equals("number")){ + if(val==null){ + throw new IllegalArgumentException("cannot set null attribute"); + } + + if(name.equals("number")){ if(!(val instanceof Integer)){ //now it should be string. Other wise class cast exception. try{ diff --git a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XSimpleObject.java b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XSimpleObject.java index e85b737..4c0d228 100644 --- a/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XSimpleObject.java +++ b/xwiki-android-core/src/org/xwiki/android/xmodel/xobjects/XSimpleObject.java @@ -40,13 +40,7 @@ public XSimpleObject(String className) } - /** - * same as get className when called from a extended type. Ex: XBlogPost.getType() eq new XblogPost().getClassName(); - */ - public static String getType() - { - return "XSimpleObject"; - } + //get set pairs. diff --git a/xwiki-android-rest/res/values/strings.xml b/xwiki-android-rest/res/values/strings.xml index 4545ba3..7697541 100644 --- a/xwiki-android-rest/res/values/strings.xml +++ b/xwiki-android-rest/res/values/strings.xml @@ -1,6 +1,6 @@ - Hello World, Main! + Hello World, org.xwiki.android.test.org.xwiki.test.integration.Main! AndroidRest searchButtonClick 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 8d77c40..faa1cbe 100644 --- a/xwiki-android-rest/src/org/xwiki/android/rest/CommentResources.java +++ b/xwiki-android-rest/src/org/xwiki/android/rest/CommentResources.java @@ -74,8 +74,8 @@ public CommentResources(String URLprefix, String wikiName, String spaceName, Str /** * @return Comments object of the page - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ public Comments getPageComments() throws RestConnectionException, RestException { @@ -86,13 +86,29 @@ public Comments getPageComments() throws RestConnectionException, RestException return buildComments(super.getResponse(Uri)); } + public boolean exists(int commentId) throws RestConnectionException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/comments/" + commentId; + int code = headRequest(Uri); + if (code == 200) { + return true; + } + if (code == 404) { + return false; + } else { + throw new RestException(code); + } + } + /** * @param commentId ID of the comment in the page * @return comment of a page selected using comment ID - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ - public Comment getPageComment(String commentId) throws RestConnectionException, RestException + public Comment getPageComment(int commentId) throws RestConnectionException, RestException { String Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName @@ -104,8 +120,8 @@ public Comment getPageComment(String commentId) throws RestConnectionException, /** * @param version version of the page * @return list of comments as a Comments object in a specific page history version - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ public Comments getPageCommentsInHistory(String version) throws RestConnectionException, RestException { @@ -120,10 +136,11 @@ public Comments getPageCommentsInHistory(String version) throws RestConnectionEx * @param version version of the page * @param commentId ID of the comment in the page * @return Comment in a specific page history version - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ - public Comments getPageCommentsInHistory(String version, String commentId) throws RestConnectionException, RestException + public Comments getPageCommentsInHistory(String version, String commentId) throws RestConnectionException, + RestException { String Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName @@ -137,8 +154,8 @@ public Comments getPageCommentsInHistory(String version, String commentId) throw * * @param comment Comment object to be added * @return status of the HTTP post request - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ public String addPageComment(Comment comment) throws RestConnectionException, RestException { 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 672167e..f83e477 100644 --- a/xwiki-android-rest/src/org/xwiki/android/rest/HttpConnector.java +++ b/xwiki-android-rest/src/org/xwiki/android/rest/HttpConnector.java @@ -139,7 +139,7 @@ public int headRequest(String Uri) throws RestConnectionException{ setCredentials(); request.setURI(requestUri); - Log.d("Request URL", Uri); + Log.d("HEAD Request URL", Uri); try { response = client.execute(request); @@ -180,7 +180,7 @@ public String getResponse(String Uri) throws RestConnectionException, RestExcept setCredentials(); request.setURI(requestUri); - Log.d("Request URL", Uri); + Log.d("GET Request URL", Uri); try { response = client.execute(request); @@ -234,7 +234,7 @@ public String deleteRequest(String Uri) throws RestConnectionException, RestExce setCredentials(); request.setURI(requestUri); - Log.d("Request URL", Uri); + Log.d("DELETE Request URL", Uri); try { @@ -278,7 +278,7 @@ public int checkLogin(String username, String password, String Url) throws RestC String Uri; int responseCode = 0; - Uri = "http://" + Url + "/xwiki/rest/"; + Uri = "http://" + Url + "/xwiki/xmlrpc/"; try { requestUri = new URI(Uri); @@ -292,7 +292,7 @@ public int checkLogin(String username, String password, String Url) throws RestC setCredentials(); request.setURI(requestUri); - Log.d("Request URL", Uri); + Log.d("GET Login Request URL", Uri); try { response = client.execute(request); @@ -340,7 +340,7 @@ public String postRequest(String Uri, String content) throws RestConnectionExcep setCredentials(); request.setURI(requestUri); - Log.d("Request URL", Uri); + Log.d("POST Request URL", Uri); try { Log.d("Post content", "content=" + content); @@ -392,7 +392,7 @@ public String putRequest(String Uri, String content) throws RestConnectionExcept setCredentials(); request.setURI(requestUri); - Log.d("Request URL", Uri); + Log.d("PUT Request URL", Uri); try { Log.d("Put content", "content=" + content); @@ -443,7 +443,7 @@ public InputStream getResponseAttachment(String Uri) throws RestConnectionExcept setCredentials(); request.setURI(requestUri); - Log.d("Request URL", Uri); + Log.d("GET Request URL", Uri); try { response = client.execute(request); @@ -488,7 +488,7 @@ public String putRaw(String Uri, String filePath) throws RestConnectionException setCredentials(); request.setURI(requestUri); - Log.d("Request URL", Uri); + Log.d("PUT Request URL", Uri); try { diff --git a/xwiki-android-rest/src/org/xwiki/android/rest/ObjectResources.java b/xwiki-android-rest/src/org/xwiki/android/rest/ObjectResources.java index 803b9a7..ac306f8 100644 --- a/xwiki-android-rest/src/org/xwiki/android/rest/ObjectResources.java +++ b/xwiki-android-rest/src/org/xwiki/android/rest/ObjectResources.java @@ -73,10 +73,27 @@ public ObjectResources(String URLprefix, String wikiName, String spaceName, Stri this.pageName = pageName; } + public boolean exists(String objectClassname, String objectNumber) throws RestConnectionException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/objects/" +objectClassname + "/" + objectNumber; + + int code = headRequest(Uri); + if (code == 200) { + return true; + } + if (code == 404) { + return false; + } else { + throw new RestException(code); + } + } + /** * @return list of all the Objects in a page - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ public Objects getAllObjects() throws RestConnectionException, RestException { @@ -93,8 +110,8 @@ public Objects getAllObjects() throws RestConnectionException, RestException * * @param object Object object to be added to the page * @return status of th HTTP post request - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ public String addObject(Object object) throws RestConnectionException, RestException { @@ -109,8 +126,8 @@ public String addObject(Object object) throws RestConnectionException, RestExcep /** * @param objectClassname name of the class * @return list of objects in a specific class of a page - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ public Objects getObjectsInClassname(String objectClassname) throws RestConnectionException, RestException { @@ -126,8 +143,8 @@ public Objects getObjectsInClassname(String objectClassname) throws RestConnecti * @param objectClassname name of the class in the page * @param objectNumber number of the object in the specified class * @return selected Object object in the page - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ public Object getObject(String objectClassname, String objectNumber) throws RestConnectionException, RestException { @@ -146,10 +163,11 @@ public Object getObject(String objectClassname, String objectNumber) throws Rest * @param objectNumber number of the object in the class * @param object Object object to be updated in the page * @return status of the HTTP put request - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ - public String updateObject(String objectClassname, String objectNumber, Object object) throws RestConnectionException, RestException + public String updateObject(String objectClassname, String objectNumber, Object object) + throws RestConnectionException, RestException { String Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName @@ -165,10 +183,11 @@ public String updateObject(String objectClassname, String objectNumber, Object o * @param objectClassname name of the class in the page * @param objectNumber number of the object in the class * @return status of HTTP resonce in delete request - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ - public String deleteObject(String objectClassname, String objectNumber) throws RestConnectionException, RestException + public String deleteObject(String objectClassname, String objectNumber) throws RestConnectionException, + RestException { String Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName @@ -182,10 +201,11 @@ public String deleteObject(String objectClassname, String objectNumber) throws R * @param objectClassname class name of the object in a page * @param objectNumber number of the object in the class * @return list of properties as a Properties object in a object - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ - public Properties getObjectProperties(String objectClassname, String objectNumber) throws RestConnectionException, RestException + public Properties getObjectProperties(String objectClassname, String objectNumber) throws RestConnectionException, + RestException { String Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName @@ -202,10 +222,11 @@ public Properties getObjectProperties(String objectClassname, String objectNumbe * @param objectNumber number of the object in the class * @param property Property object to be added to the Object * @return status of the HTTP put request - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ - public String addObjectProperty(String objectClassname, String objectNumber, Property property) throws RestConnectionException, RestException + public String addObjectProperty(String objectClassname, String objectNumber, Property property) + throws RestConnectionException, RestException { String Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName @@ -219,10 +240,11 @@ public String addObjectProperty(String objectClassname, String objectNumber, Pro * @param objectNumber number of the object in the class * @param propertyName name of the required property * @return Specific property of a object in a page - * @throws RestConnectionException - * @throws RestException + * @throws RestConnectionException + * @throws RestException */ - public Property getObjectProperty(String objectClassname, String objectNumber, String propertyName) throws RestConnectionException, RestException + public Property getObjectProperty(String objectClassname, String objectNumber, String propertyName) + throws RestConnectionException, RestException { String Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName diff --git a/xwiki-android-rest/src/org/xwiki/android/rest/Requests.java b/xwiki-android-rest/src/org/xwiki/android/rest/Requests.java index eafa63a..5a036a7 100644 --- a/xwiki-android-rest/src/org/xwiki/android/rest/Requests.java +++ b/xwiki-android-rest/src/org/xwiki/android/rest/Requests.java @@ -47,7 +47,7 @@ /** * All the XWiki Android RESTful requests for getting, putting and posting are included here. Method names starting from - * "request" will return objects XWiki Android REST model. Currently using xwiki-rest-model-simplexml. Method names + * "request" will return objects XWiki Android REST model. Currently using xwiki-xmlrpc-model-simplexml. Method names * starting from "add" will add XWiki Android REST model objects to the XWiki Server. */ @Deprecated @@ -98,7 +98,7 @@ public Requests(String URLprefix, String username, String password) } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#setAuthentication(java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#setAuthentication(java.lang.String, java.lang.String) */ public void setAuthentication(String username, String password) { @@ -130,7 +130,7 @@ private String convertToUTF(String keyword) } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestSearch(java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestSearch(java.lang.String, java.lang.String) */ public SearchResults requestSearch(String wikiName, String keyword) { @@ -157,7 +157,7 @@ public SearchResults requestSearch(String wikiName, String keyword) } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestSearch(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestSearch(java.lang.String, java.lang.String, java.lang.String) */ public SearchResults requestSearch(String wikiName, String spaceName, String keyword) @@ -184,7 +184,7 @@ public SearchResults requestSearch(String wikiName, String spaceName, String key } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestWikis() + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestWikis() */ public Wikis requestWikis() @@ -208,7 +208,7 @@ public Wikis requestWikis() } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestSpaces(java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestSpaces(java.lang.String) */ public Spaces requestSpaces(String wikiName) @@ -231,7 +231,7 @@ public Spaces requestSpaces(String wikiName) } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestAllPages(java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestAllPages(java.lang.String, java.lang.String) */ public Pages requestAllPages(String wikiName, String spaceName) @@ -255,7 +255,7 @@ public Pages requestAllPages(String wikiName, String spaceName) } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPage(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPage(java.lang.String, java.lang.String, java.lang.String) */ public Page requestPage(String wikiName, String spaceName, String pageName) @@ -281,7 +281,7 @@ public Page requestPage(String wikiName, String spaceName, String pageName) } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#addPage(java.lang.String, java.lang.String, java.lang.String, org.xwiki.android.resources.Page) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#addPage(java.lang.String, java.lang.String, java.lang.String, org.xwiki.android.resources.Page) */ public String addPage(String wikiName, String spaceName, String pageName, Page page) @@ -307,7 +307,7 @@ public String addPage(String wikiName, String spaceName, String pageName, Page p } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#deletePage(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#deletePage(java.lang.String, java.lang.String, java.lang.String) */ public String deletePage(String wikiName, String spaceName, String pageName) @@ -333,7 +333,7 @@ public String deletePage(String wikiName, String spaceName, String pageName) } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#deletePage(java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#deletePage(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ public String deletePage(String wikiName, String spaceName, String pageName, String language) @@ -360,7 +360,7 @@ public String deletePage(String wikiName, String spaceName, String pageName, Str } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageHistory(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageHistory(java.lang.String, java.lang.String, java.lang.String) */ @@ -387,7 +387,7 @@ public History requestPageHistory(String wikiName, String spaceName, String page } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageInVersionHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageInVersionHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -414,7 +414,7 @@ public Page requestPageInVersionHistory(String wikiName, String spaceName, Strin } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageChildren(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageChildren(java.lang.String, java.lang.String, java.lang.String) */ @@ -442,7 +442,7 @@ public Pages requestPageChildren(String wikiName, String spaceName, String pageN } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageTags(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageTags(java.lang.String, java.lang.String, java.lang.String) */ @@ -469,7 +469,7 @@ public Tags requestPageTags(String wikiName, String spaceName, String pageName) } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#addPageTag(java.lang.String, java.lang.String, java.lang.String, org.xwiki.android.resources.Tag) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#addPageTag(java.lang.String, java.lang.String, java.lang.String, org.xwiki.android.resources.Tag) */ @@ -497,7 +497,7 @@ public String addPageTag(String wikiName, String spaceName, String pageName, Tag } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestWikiTags(java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestWikiTags(java.lang.String) */ @@ -522,7 +522,7 @@ public Tags requestWikiTags(String wikiName) } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageComments(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageComments(java.lang.String, java.lang.String, java.lang.String) */ @@ -549,7 +549,7 @@ public Comments requestPageComments(String wikiName, String spaceName, String pa } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#addPageComment(java.lang.String, java.lang.String, java.lang.String, org.xwiki.android.resources.Comment) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#addPageComment(java.lang.String, java.lang.String, java.lang.String, org.xwiki.android.resources.Comment) */ @@ -578,7 +578,7 @@ public String addPageComment(String wikiName, String spaceName, String pageName, } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageComments(java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageComments(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -593,7 +593,7 @@ public Comment requestPageComments(String wikiName, String spaceName, String pag commentresources.setAuthenticaion(username, password); } try { - return commentresources.getPageComment(commentId); + return commentresources.getPageComment(new Integer(commentId)); } catch (RestConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -605,7 +605,7 @@ public Comment requestPageComments(String wikiName, String spaceName, String pag } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageCommentsInHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageCommentsInHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -632,7 +632,7 @@ public Comments requestPageCommentsInHistory(String wikiName, String spaceName, } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageCommentsInHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageCommentsInHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -660,7 +660,7 @@ public Comments requestPageCommentsInHistory(String wikiName, String spaceName, } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestAllPageAttachments(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestAllPageAttachments(java.lang.String, java.lang.String, java.lang.String) */ @@ -688,7 +688,7 @@ public Attachments requestAllPageAttachments(String wikiName, String spaceName, } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageAttachment(java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageAttachment(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -716,7 +716,7 @@ public InputStream requestPageAttachment(String wikiName, String spaceName, Stri } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#addPageAttachment(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#addPageAttachment(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -745,7 +745,7 @@ public String addPageAttachment(String wikiName, String spaceName, String pageNa } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#deletePageAttachment(java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#deletePageAttachment(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -773,7 +773,7 @@ public String deletePageAttachment(String wikiName, String spaceName, String pag } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageAttachmentsInHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageAttachmentsInHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -801,7 +801,7 @@ public Attachments requestPageAttachmentsInHistory(String wikiName, String space } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageAttachmentsInHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageAttachmentsInHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -830,7 +830,7 @@ public InputStream requestPageAttachmentsInHistory(String wikiName, String space } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageAttachmentsInAttachmentHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageAttachmentsInAttachmentHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -859,7 +859,7 @@ public Attachments requestPageAttachmentsInAttachmentHistory(String wikiName, St } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageAttachmentsInAttachmentHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageAttachmentsInAttachmentHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -888,7 +888,7 @@ public InputStream requestPageAttachmentsInAttachmentHistory(String wikiName, St } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestWikiClasses(java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestWikiClasses(java.lang.String) */ @@ -913,7 +913,7 @@ public Classes requestWikiClasses(String wikiName) } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestWikiClasses(java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestWikiClasses(java.lang.String, java.lang.String) */ @@ -939,7 +939,7 @@ public Class requestWikiClasses(String wikiName, String classname) } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestAllPageTranslations(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestAllPageTranslations(java.lang.String, java.lang.String, java.lang.String) */ @@ -966,7 +966,7 @@ public Translations requestAllPageTranslations(String wikiName, String spaceName } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageTranslation(java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageTranslation(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -993,7 +993,7 @@ public Page requestPageTranslation(String wikiName, String spaceName, String pag } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageHistoryInLanguage(java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageHistoryInLanguage(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -1020,7 +1020,7 @@ public History requestPageHistoryInLanguage(String wikiName, String spaceName, S } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestPageTranslationInHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestPageTranslationInHistory(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -1048,7 +1048,7 @@ public Page requestPageTranslationInHistory(String wikiName, String spaceName, S } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#addObject(java.lang.String, java.lang.String, java.lang.String, org.xwiki.android.resources.Object) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#addObject(java.lang.String, java.lang.String, java.lang.String, org.xwiki.android.resources.Object) */ @@ -1075,7 +1075,7 @@ public String addObject(String wikiName, String spaceName, String pageName, Obje } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestAllObjects(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestAllObjects(java.lang.String, java.lang.String, java.lang.String) */ @@ -1102,7 +1102,7 @@ public Objects requestAllObjects(String wikiName, String spaceName, String pageN } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestObjectsInClass(java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestObjectsInClass(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -1130,7 +1130,7 @@ public Objects requestObjectsInClass(String wikiName, String spaceName, String p } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestObject(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestObject(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -1160,7 +1160,7 @@ public Object requestObject(String wikiName, String spaceName, String pageName, } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#deleteObject(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#deleteObject(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -1190,7 +1190,7 @@ public String deleteObject(String wikiName, String spaceName, String pageName, S } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestObjectAllProperties(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestObjectAllProperties(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -1220,7 +1220,7 @@ public Properties requestObjectAllProperties(String wikiName, String spaceName, } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#requestObjectProperty(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#requestObjectProperty(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -1251,7 +1251,7 @@ public Property requestObjectProperty(String wikiName, String spaceName, String } /* (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#addProperty(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, org.xwiki.android.resources.Property) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#addProperty(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, org.xwiki.android.resources.Property) */ diff --git a/xwiki-android-rest/src/org/xwiki/android/rest/RestConnection.java b/xwiki-android-rest/src/org/xwiki/android/rest/RestConnection.java new file mode 100644 index 0000000..cc4b133 --- /dev/null +++ b/xwiki-android-rest/src/org/xwiki/android/rest/RestConnection.java @@ -0,0 +1,8 @@ +package org.xwiki.android.rest; + +public interface RestConnection +{ + XWikiAPI getBaseAPI(); + boolean isClosed(); + void close(); +} diff --git a/xwiki-android-rest/src/org/xwiki/android/rest/Upgrade Http Connector, RestConnection b/xwiki-android-rest/src/org/xwiki/android/rest/Upgrade Http Connector, RestConnection new file mode 100644 index 0000000..9850c5e --- /dev/null +++ b/xwiki-android-rest/src/org/xwiki/android/rest/Upgrade Http Connector, RestConnection @@ -0,0 +1,100 @@ +PoC. +We can achieve parellel http connections in this layer. +Use implementations of RestConnection to adjust params. +RestConnection.close() should shutdown the http connection manager of the http client. + +sample code// + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.conn.ClientConnectionManager; +import org.apache.http.conn.scheme.PlainSocketFactory; +import org.apache.http.conn.scheme.Scheme; +import org.apache.http.conn.scheme.SchemeRegistry; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.conn.PoolingClientConnectionManager; +import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.protocol.HttpContext; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; + +/** + * Created with IntelliJ IDEA. + * User: admin + * Date: 8/11/12 + * Time: 1:53 PM + * To change this template use File | Settings | File Templates. + */ +public class org.xwiki.android.test.org.xwiki.test.integration.Main { + + static DefaultHttpClient client; + public static void main(String[] args) throws IOException, InterruptedException { + + SchemeRegistry schemeRegistry = new SchemeRegistry(); + schemeRegistry.register( + new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); + + ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry); + HttpClient httpClient = new DefaultHttpClient(cm); + +// URIs to perform GETs on + String[] urisToGet = { + "http://www.domain1.com/", + "http://www.domain2.com/", + "http://www.domain3.com/", + "http://www.domain4.com/" + }; + +// create a thread for each URI + GetThread[] threads = new GetThread[urisToGet.length]; + for (int i = 0; i < threads.length; i++) { + HttpGet httpget = new HttpGet(urisToGet[i]); + threads[i] = new GetThread(httpClient, httpget); + } + +// start the threads + for (int j = 0; j < threads.length; j++) { + threads[j].start(); + } + +// join the threads + for (int j = 0; j < threads.length; j++) { + threads[j].join(); + } + + } + + static class GetThread extends Thread { + + private final HttpClient httpClient; + private final HttpContext context; + private final HttpGet httpget; + + public GetThread(HttpClient httpClient, HttpGet httpget) { + this.httpClient = httpClient; + this.context = new BasicHttpContext(); + this.httpget = httpget; + } + + @Override + public void run() { + try { + HttpResponse response = this.httpClient.execute(this.httpget, this.context); + HttpEntity entity = response.getEntity(); + if (entity != null) { + // do something useful with the entity + } + // ensure the connection gets released to the manager + EntityUtils.consume(entity); + } catch (Exception ex) { + this.httpget.abort(); + } + } + + } + +} \ No newline at end of file diff --git a/xwiki-android-rest/src/org/xwiki/android/rest/XWikiRestfulAPI.java b/xwiki-android-rest/src/org/xwiki/android/rest/XWikiAPI.java similarity index 92% rename from xwiki-android-rest/src/org/xwiki/android/rest/XWikiRestfulAPI.java rename to xwiki-android-rest/src/org/xwiki/android/rest/XWikiAPI.java index 95c3b2d..38dc5a7 100644 --- a/xwiki-android-rest/src/org/xwiki/android/rest/XWikiRestfulAPI.java +++ b/xwiki-android-rest/src/org/xwiki/android/rest/XWikiAPI.java @@ -26,7 +26,7 @@ * @author xwiki gsoc 2012 * */ -public interface XWikiRestfulAPI +public interface XWikiAPI { /** @@ -242,6 +242,7 @@ String addPageTag(String wikiName, String spaceName, String pageName, Tag tag) t public String setTags(String wikiName, String spaceName, String pageName,Tags tags) throws RestConnectionException, RestException; /** + * get all comments in page * @param wikiName * name of the Wiki * @param spaceName @@ -253,6 +254,27 @@ String addPageTag(String wikiName, String spaceName, String pageName, Tag tag) t Comments getPageComments(String wikiName, String spaceName, String pageName) throws RestConnectionException, RestException; + /** + * check whether a comment exists for the given page. + * @param wikiName + * @param spaceName + * @param pageName + * @param commentId + * @return + * @throws RestConnectionException + * @throws RestException + */ + public boolean existsPageComment(String wikiName, String spaceName, String pageName,int commentId) throws RestConnectionException, RestException; + + /** + * get a comment. + * @param commentId ID of the comment in the page + * @return comment of a page selected using comment ID + * @throws RestConnectionException + * @throws RestException + */ + public Comment getPageComment(String wikiName, String spaceName, String pageName, int commentId) throws RestConnectionException, RestException; + /** * Add comment to the page * @@ -265,24 +287,12 @@ Comments getPageComments(String wikiName, String spaceName, String pageName) thr * @param comment * Comment object to be added * @return status of the HTTP response - */ + */ + String addPageComment(String wikiName, String spaceName, String pageName, Comment comment) throws RestConnectionException, RestException; - /** - * @param wikiName - * name of the Wiki - * @param spaceName - * name of the space - * @param pageName - * name of the page - * @param commentId - * ID of the comment - * @return page comment according to the comment ID - */ - Comment getPageComments(String wikiName, String spaceName, String pageName, String commentId) - throws RestConnectionException, RestException; - + /** * @param wikiName * name of the Wiki @@ -495,6 +505,9 @@ History getPageHistoryInLanguage(String wikiName, String spaceName, String pageN Page getPageTranslationInHistory(String wikiName, String spaceName, String pageName, String language, String version) throws RestConnectionException, RestException; + + boolean existsObject(String wikiName, String spaceName, String pageName, String objectClassname, + int objectNumber)throws RestConnectionException,RestException; /** * Adds provided object to the given page * @@ -525,7 +538,7 @@ String addObject(String wikiName, String spaceName, String pageName, Object obje * @throws RestException */ public String updateObject(String wikiName, String spaceName, String pageName, String objectClassname, - String objectNumber, Object object) throws RestConnectionException, RestException; + int objectNumber, Object object) throws RestConnectionException, RestException; /** * @param wikiName @@ -622,6 +635,7 @@ Properties getObjectAllProperties(String wikiName, String spaceName, String page */ Property getObjectProperty(String wikiName, String spaceName, String pageName, String objectClassname, String objectNumber, String propertyName) throws RestConnectionException, RestException; + //TODO: refactor number to int /** * Adds the property to the object of a page diff --git a/xwiki-android-rest/src/org/xwiki/android/rest/XWikiRestConnector.java b/xwiki-android-rest/src/org/xwiki/android/rest/XWikiRestConnecion.java similarity index 80% rename from xwiki-android-rest/src/org/xwiki/android/rest/XWikiRestConnector.java rename to xwiki-android-rest/src/org/xwiki/android/rest/XWikiRestConnecion.java index 54a8870..ffea118 100644 --- a/xwiki-android-rest/src/org/xwiki/android/rest/XWikiRestConnector.java +++ b/xwiki-android-rest/src/org/xwiki/android/rest/XWikiRestConnecion.java @@ -25,9 +25,11 @@ import android.util.Log; -public class XWikiRestConnector implements XWikiRestfulAPI +public class XWikiRestConnecion implements RestConnection, XWikiAPI { + private static final String TAG ="XWikiRestConnecion"; + /** * URL of the XWiki domain */ @@ -51,7 +53,7 @@ public class XWikiRestConnector implements XWikiRestfulAPI /** * @param URLprefix URL of the XWiki domain */ - public XWikiRestConnector(String URLprefix) + public XWikiRestConnecion(String URLprefix) { this.URLprefix = URLprefix; isAuthenticated = false; @@ -64,7 +66,7 @@ public XWikiRestConnector(String URLprefix) * @param username username of the user account * @param password password of the user account */ - public XWikiRestConnector(String URLprefix, String username, String password) + public XWikiRestConnecion(String URLprefix, String username, String password) { this.URLprefix = URLprefix; this.username = username; @@ -74,7 +76,7 @@ public XWikiRestConnector(String URLprefix, String username, String password) /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#setAuthentication(java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#setAuthentication(java.lang.String, java.lang.String) */ @Override public void setAuthentication(String username, String password) @@ -109,7 +111,7 @@ private String urlEncode(String word) /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getSearch(java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getSearch(java.lang.String, java.lang.String) */ @Override public SearchResults searchInWiki(String wikiName, String keyword) throws RestConnectionException, RestException, RestException @@ -129,7 +131,7 @@ public SearchResults searchInWiki(String wikiName, String keyword) throws RestCo /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getSearch(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getSearch(java.lang.String, java.lang.String, java.lang.String) */ @Override public SearchResults searchInSpace(String wikiName, String spaceName, String keyword) throws RestConnectionException, RestException @@ -148,7 +150,7 @@ public SearchResults searchInSpace(String wikiName, String spaceName, String key /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getWikis() + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getWikis() */ @Override public Wikis getWikis() throws RestConnectionException, RestException @@ -164,7 +166,7 @@ public Wikis getWikis() throws RestConnectionException, RestException /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getSpaces(java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getSpaces(java.lang.String) */ @Override public Spaces getSpaces(String wikiName) throws RestConnectionException, RestException @@ -179,7 +181,7 @@ public Spaces getSpaces(String wikiName) throws RestConnectionException, RestExc /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getAllPages(java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getAllPages(java.lang.String, java.lang.String) */ @Override public Pages getAllPages(String wikiName, String spaceName) throws RestConnectionException, RestException @@ -225,7 +227,7 @@ public Page getPage(String wikiName, String spaceName, String pageName) throws R /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#addPage(java.lang.String, java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#addPage(java.lang.String, java.lang.String, java.lang.String, * org.xwiki.android.resources.Page) */ @Override @@ -244,7 +246,7 @@ public String addPage(String wikiName, String spaceName, String pageName, Page p /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#deletePage(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#deletePage(java.lang.String, java.lang.String, java.lang.String) */ @Override public String deletePage(String wikiName, String spaceName, String pageName) throws RestConnectionException, RestException @@ -262,7 +264,7 @@ public String deletePage(String wikiName, String spaceName, String pageName) thr /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#deletePage(java.lang.String, java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#deletePage(java.lang.String, java.lang.String, java.lang.String, * java.lang.String) */ @Override @@ -283,7 +285,7 @@ public String deletePage(String wikiName, String spaceName, String pageName, Str /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageHistory(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageHistory(java.lang.String, java.lang.String, java.lang.String) */ @Override @@ -302,7 +304,7 @@ public History getPageHistory(String wikiName, String spaceName, String pageName /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageInVersionHistory(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageInVersionHistory(java.lang.String, java.lang.String, * java.lang.String, java.lang.String) */ @@ -323,7 +325,7 @@ public Page getPageInVersionHistory(String wikiName, String spaceName, String pa /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageChildren(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageChildren(java.lang.String, java.lang.String, java.lang.String) */ @Override @@ -343,7 +345,7 @@ public Pages getPageChildren(String wikiName, String spaceName, String pageName) /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageTags(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageTags(java.lang.String, java.lang.String, java.lang.String) */ @Override @@ -362,7 +364,7 @@ public Tags getPageTags(String wikiName, String spaceName, String pageName) thro /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#addPageTag(java.lang.String, java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#addPageTag(java.lang.String, java.lang.String, java.lang.String, * org.xwiki.android.resources.Tag) */ @@ -395,7 +397,7 @@ public String setTags(String wikiName, String spaceName, String pageName,Tags ta /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getWikiTags(java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getWikiTags(java.lang.String) */ @Override @@ -412,7 +414,7 @@ public Tags getWikiTags(String wikiName) throws RestConnectionException, RestExc /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageComments(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageComments(java.lang.String, java.lang.String, java.lang.String) */ @Override @@ -428,16 +430,24 @@ public Comments getPageComments(String wikiName, String spaceName, String pageNa } return commentresources.getPageComments(); } + + @Override + public boolean existsPageComment(String wikiName, String spaceName, String pageName, int commentId) + throws RestConnectionException, RestException + { + wikiName = urlEncode(wikiName); + spaceName = urlEncode(spaceName); + pageName = urlEncode(pageName); - /* - * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#addPageComment(java.lang.String, java.lang.String, java.lang.String, - * org.xwiki.android.resources.Comment) - */ + CommentResources commentresources = new CommentResources(URLprefix, wikiName, spaceName, pageName); + if (isAuthenticated) { + commentresources.setAuthenticaion(username, password); + } + return commentresources.exists(commentId); + } @Override - public String addPageComment(String wikiName, String spaceName, String pageName, Comment comment) - throws RestConnectionException, RestException + public Comment getPageComment(String wikiName, String spaceName, String pageName, int commentId) throws RestConnectionException, RestException { wikiName = urlEncode(wikiName); spaceName = urlEncode(spaceName); @@ -446,20 +456,18 @@ public String addPageComment(String wikiName, String spaceName, String pageName, CommentResources commentresources = new CommentResources(URLprefix, wikiName, spaceName, pageName); if (isAuthenticated) { commentresources.setAuthenticaion(username, password); - return commentresources.addPageComment(comment); - } else { - return "Autherization details are not provided"; } + return commentresources.getPageComment(commentId); } /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageComments(java.lang.String, java.lang.String, java.lang.String, - * java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#addPageComment(java.lang.String, java.lang.String, java.lang.String, + * org.xwiki.android.resources.Comment) */ @Override - public Comment getPageComments(String wikiName, String spaceName, String pageName, String commentId) + public String addPageComment(String wikiName, String spaceName, String pageName, Comment comment) throws RestConnectionException, RestException { wikiName = urlEncode(wikiName); @@ -469,13 +477,17 @@ public Comment getPageComments(String wikiName, String spaceName, String pageNam CommentResources commentresources = new CommentResources(URLprefix, wikiName, spaceName, pageName); if (isAuthenticated) { commentresources.setAuthenticaion(username, password); + return commentresources.addPageComment(comment); + } else { + return "Autherization details are not provided"; } - return commentresources.getPageComment(commentId); } + + /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageCommentsInHistory(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageCommentsInHistory(java.lang.String, java.lang.String, * java.lang.String, java.lang.String) */ @@ -496,7 +508,7 @@ public Comments getPageCommentsInHistory(String wikiName, String spaceName, Stri /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageCommentsInHistory(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageCommentsInHistory(java.lang.String, java.lang.String, * java.lang.String, java.lang.String, java.lang.String) */ @@ -517,7 +529,7 @@ public Comments getPageCommentsInHistory(String wikiName, String spaceName, Stri /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getAllPageAttachments(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getAllPageAttachments(java.lang.String, java.lang.String, * java.lang.String) */ @@ -539,7 +551,7 @@ public Attachments getAllPageAttachments(String wikiName, String spaceName, Stri /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageAttachment(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageAttachment(java.lang.String, java.lang.String, * java.lang.String, java.lang.String) */ @@ -561,7 +573,7 @@ public InputStream getPageAttachment(String wikiName, String spaceName, String p /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#addPageAttachment(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#addPageAttachment(java.lang.String, java.lang.String, * java.lang.String, java.lang.String, java.lang.String) */ @@ -583,7 +595,7 @@ public String putPageAttachment(String wikiName, String spaceName, String pageNa /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#deletePageAttachment(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#deletePageAttachment(java.lang.String, java.lang.String, * java.lang.String, java.lang.String) */ @@ -605,7 +617,7 @@ public String deletePageAttachment(String wikiName, String spaceName, String pag /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageAttachmentsInHistory(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageAttachmentsInHistory(java.lang.String, java.lang.String, * java.lang.String, java.lang.String) */ @@ -626,7 +638,7 @@ public Attachments getPageAttachmentsInHistory(String wikiName, String spaceName /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageAttachmentsInHistory(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageAttachmentsInHistory(java.lang.String, java.lang.String, * java.lang.String, java.lang.String, java.lang.String) */ @@ -648,7 +660,7 @@ public InputStream getPageAttachmentsInHistory(String wikiName, String spaceName /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageAttachmentsInAttachmentHistory(java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageAttachmentsInAttachmentHistory(java.lang.String, * java.lang.String, java.lang.String, java.lang.String) */ @@ -670,7 +682,7 @@ public Attachments getPageAttachmentsInAttachmentHistory(String wikiName, String /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageAttachmentsInAttachmentHistory(java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageAttachmentsInAttachmentHistory(java.lang.String, * java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -692,7 +704,7 @@ public InputStream getPageAttachmentsInAttachmentHistory(String wikiName, String /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getWikiClasses(java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getWikiClasses(java.lang.String) */ @Override @@ -709,7 +721,7 @@ public Classes getWikiClasses(String wikiName) throws RestConnectionException, R /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getWikiClasses(java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getWikiClasses(java.lang.String, java.lang.String) */ @Override @@ -727,7 +739,7 @@ public Class getWikiClasses(String wikiName, String classname) throws RestConnec /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getAllPageTranslations(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getAllPageTranslations(java.lang.String, java.lang.String, * java.lang.String) */ @@ -748,7 +760,7 @@ public Translations getAllPageTranslations(String wikiName, String spaceName, St /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageTranslation(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageTranslation(java.lang.String, java.lang.String, * java.lang.String, java.lang.String) */ @@ -769,7 +781,7 @@ public Page getPageTranslation(String wikiName, String spaceName, String pageNam /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageHistoryInLanguage(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageHistoryInLanguage(java.lang.String, java.lang.String, * java.lang.String, java.lang.String) */ @@ -790,7 +802,7 @@ public History getPageHistoryInLanguage(String wikiName, String spaceName, Strin /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getPageTranslationInHistory(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getPageTranslationInHistory(java.lang.String, java.lang.String, * java.lang.String, java.lang.String, java.lang.String) */ @@ -808,10 +820,29 @@ public Page getPageTranslationInHistory(String wikiName, String spaceName, Strin } return pageresources.getPageTranslation(pageName, language, version); } + + @Override + public boolean existsObject(String wikiName, String spaceName, String pageName, String objectClassname, + int objectNumber) throws RestConnectionException, RestException + { + wikiName = urlEncode(wikiName); + spaceName = urlEncode(spaceName); + pageName = urlEncode(pageName); + objectClassname = urlEncode(objectClassname); + String objNumber=urlEncode(objectNumber+""); + + ObjectResources objectresources = new ObjectResources(URLprefix, wikiName, spaceName, pageName); + if (isAuthenticated) { + objectresources.setAuthenticaion(username, password); + } + boolean val= objectresources.exists(objectClassname,objNumber); + return val; + + } /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#addObject(java.lang.String, java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#addObject(java.lang.String, java.lang.String, java.lang.String, * org.xwiki.android.resources.Object) */ @@ -841,7 +872,7 @@ public String addObject(String wikiName, String spaceName, String pageName, Obje * @throws RestException */ @Override - public String updateObject(String wikiName, String spaceName, String pageName,String objectClassname, String objectNumber, Object object) throws RestConnectionException, RestException + public String updateObject(String wikiName, String spaceName, String pageName,String objectClassname, int objectNumber, Object object) throws RestConnectionException, RestException { wikiName = urlEncode(wikiName); spaceName = urlEncode(spaceName); @@ -851,13 +882,13 @@ public String updateObject(String wikiName, String spaceName, String pageName,St if (isAuthenticated) { objectresources.setAuthenticaion(username, password); } - return objectresources.updateObject(objectClassname, objectNumber, object); + return objectresources.updateObject(objectClassname, ""+objectNumber, object); } /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getAllObjects(java.lang.String, java.lang.String, java.lang.String) + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getAllObjects(java.lang.String, java.lang.String, java.lang.String) */ @Override @@ -876,7 +907,7 @@ public Objects getAllObjects(String wikiName, String spaceName, String pageName) /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getObjectsInClass(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getObjectsInClass(java.lang.String, java.lang.String, * java.lang.String, java.lang.String) */ @@ -898,7 +929,7 @@ public Objects getObjectsInClass(String wikiName, String spaceName, String pageN /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getObject(java.lang.String, java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getObject(java.lang.String, java.lang.String, java.lang.String, * java.lang.String, java.lang.String) */ @@ -921,7 +952,7 @@ public Object getObject(String wikiName, String spaceName, String pageName, Stri /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#deleteObject(java.lang.String, java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#deleteObject(java.lang.String, java.lang.String, java.lang.String, * java.lang.String, java.lang.String) */ @@ -944,7 +975,7 @@ public String deleteObject(String wikiName, String spaceName, String pageName, S /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getObjectAllProperties(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getObjectAllProperties(java.lang.String, java.lang.String, * java.lang.String, java.lang.String, java.lang.String) */ @@ -967,7 +998,7 @@ public Properties getObjectAllProperties(String wikiName, String spaceName, Stri /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#getObjectProperty(java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#getObjectProperty(java.lang.String, java.lang.String, * java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ @@ -991,7 +1022,7 @@ public Property getObjectProperty(String wikiName, String spaceName, String page /* * (non-Javadoc) - * @see org.xwiki.android.rest.XWikiRestfulAPI#addProperty(java.lang.String, java.lang.String, java.lang.String, + * @see org.xwiki.android.xmlrpc.XWikiRestfulAPI#addProperty(java.lang.String, java.lang.String, java.lang.String, * java.lang.String, java.lang.String, org.xwiki.android.resources.Property) */ @@ -1012,5 +1043,30 @@ public String addObjectProperty(String wikiName, String spaceName, String pageNa return objectresources.addObjectProperty(objectClassname, objectNumber, property); } + //connection methods + + + @Override + public XWikiAPI getBaseAPI() + { + return this; + } + + @Override + public boolean isClosed() + { + return false; + } + + @Override + public void close() + { + Log.e(TAG, "not implemented: should release any resources: Also see that HttpConnector does not release resources"); + //httpclient.getConnectionManager().shutdown(); + //refer: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html + //throw new UnsupportedOperationException("should be releasing resources."); + } + + } diff --git a/xwiki-android-test-core/AndroidManifest.xml b/xwiki-android-test-core/AndroidManifest.xml new file mode 100644 index 0000000..ed58db2 --- /dev/null +++ b/xwiki-android-test-core/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/xwiki-android-test-core/pom.xml b/xwiki-android-test-core/pom.xml new file mode 100644 index 0000000..cd4c949 --- /dev/null +++ b/xwiki-android-test-core/pom.xml @@ -0,0 +1,70 @@ + + + + + + 4.0.0 + + org.xwiki.android + xwiki-android + 1.0-SNAPSHOT + + + xwiki-android-test-core + apk + XWiki Android Core tests + Tests the platform core of the XWiki Android Platform libs. + + + + org.xwiki.android + xwiki-android-rest + apklib + ${project.version} + + + + org.xwiki.android + xwiki-android-core + apklib + ${project.version} + + + + com.google.android + android-test + 2.1.2 + provided + + + + junit + junit + 3.8.2 + provided + + + + + diff --git a/xwiki-android-test-core/proguard-project.txt b/xwiki-android-test-core/proguard-project.txt new file mode 100644 index 0000000..f2fe155 --- /dev/null +++ b/xwiki-android-test-core/proguard-project.txt @@ -0,0 +1,20 @@ +# To enable ProGuard in your project, edit project.properties +# to define the proguard.config property as described in that file. +# +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in ${sdk.dir}/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the ProGuard +# include property in project.properties. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/xwiki-android-test-core/project.properties b/xwiki-android-test-core/project.properties new file mode 100644 index 0000000..0d7f4ae --- /dev/null +++ b/xwiki-android-test-core/project.properties @@ -0,0 +1,16 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system edit +# "ant.properties", and override values to adapt the script to your +# project structure. +# +# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): +#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt + +# Project target. +target=android-7 +android.library.reference.1=..\\xwiki-android-rest +android.library.reference.2=../xwiki-android-core diff --git a/xwiki-android-test-core/res/drawable-hdpi/ic_launcher.png b/xwiki-android-test-core/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 0000000..96a442e Binary files /dev/null and b/xwiki-android-test-core/res/drawable-hdpi/ic_launcher.png differ diff --git a/xwiki-android-test-core/res/drawable-ldpi/ic_launcher.png b/xwiki-android-test-core/res/drawable-ldpi/ic_launcher.png new file mode 100644 index 0000000..9923872 Binary files /dev/null and b/xwiki-android-test-core/res/drawable-ldpi/ic_launcher.png differ diff --git a/xwiki-android-test-core/res/drawable-mdpi/ic_launcher.png b/xwiki-android-test-core/res/drawable-mdpi/ic_launcher.png new file mode 100644 index 0000000..359047d Binary files /dev/null and b/xwiki-android-test-core/res/drawable-mdpi/ic_launcher.png differ diff --git a/xwiki-android-test-core/res/drawable-xhdpi/ic_launcher.png b/xwiki-android-test-core/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 0000000..71c6d76 Binary files /dev/null and b/xwiki-android-test-core/res/drawable-xhdpi/ic_launcher.png differ diff --git a/xwiki-android-test-core/res/values/strings.xml b/xwiki-android-test-core/res/values/strings.xml new file mode 100644 index 0000000..2c78198 --- /dev/null +++ b/xwiki-android-test-core/res/values/strings.xml @@ -0,0 +1,6 @@ + + + + Xwiki-android-test-coreTest + + \ No newline at end of file diff --git a/xwiki-android-test-core/src/org/xwiki/android/core/test/AllTests.java b/xwiki-android-test-core/src/org/xwiki/android/core/test/AllTests.java new file mode 100644 index 0000000..9ca4983 --- /dev/null +++ b/xwiki-android-test-core/src/org/xwiki/android/core/test/AllTests.java @@ -0,0 +1,36 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.xwiki.android.core.test; + +import junit.framework.Test; +import junit.framework.TestSuite; +import android.test.suitebuilder.TestSuiteBuilder; + +/** + * A test suite containing all tests for test application. + */ +public class AllTests extends TestSuite +{ + public static Test suite() + { + return new TestSuiteBuilder(AllTests.class).includePackages("org.xwiki.android.rest.ral.test").includeAllPackagesUnderHere().build(); + } +} diff --git a/xwiki-android-test-core/src/org/xwiki/android/core/test/TestTemplate.java b/xwiki-android-test-core/src/org/xwiki/android/core/test/TestTemplate.java new file mode 100644 index 0000000..2d3a066 --- /dev/null +++ b/xwiki-android-test-core/src/org/xwiki/android/core/test/TestTemplate.java @@ -0,0 +1,86 @@ +package org.xwiki.android.core.test; + +import org.xwiki.android.core.test.properties.Env; +import org.xwiki.android.rest.RestConnectionException; +import org.xwiki.android.rest.XWikiAPI; +import org.xwiki.android.rest.ral.DocumentRao; +import org.xwiki.android.rest.ral.RESTfulManager; +import org.xwiki.android.rest.ral.RaoException; +import org.xwiki.android.rest.ral.XmlRESTFulManager; +import org.xwiki.android.svc.cmn.LoginFacade; +import org.xwiki.android.xmodel.entity.Document; + +import android.test.AndroidTestCase; +import android.util.Log; + +public class TestTemplate extends AndroidTestCase +{ + + private static final String TAG = TestTemplate.class.getSimpleName(); + int count; + + // test org.xwiki.android.test.fixture.teardown.env parameters. + String serverUrl, username, password, wikiName, spaceName, pageName; + + // tested apis + RESTfulManager rm; + DocumentRao rao; + Document doc; + + // api used for output verification + XWikiAPI api; + + + public TestTemplate() + { + // things that need to run only one time. workaround for @BeforeClass + // obtain vars from Var class + username = Env.USERNAME; + password = Env.PASSWORD; + serverUrl = Env.URL; + + wikiName = Env.WIKI_NAME; + spaceName = Env.SPACE_NAME; + pageName = Env.PAGE_NAME; + count=0; + } + + + + @Override + protected void setUp() throws Exception + { + super.setUp(); + Log.d(TAG, "SET UP" + countTestCases()); + + //run before each testxxxx; + // + + rm = new XmlRESTFulManager(serverUrl, username, password); + api = rm.getConnection().getBaseAPI(); + rao = rm.newDocumentRao(); + doc = new Document(wikiName, spaceName, pageName); + doc.setTitle(pageName); + } + + @Override + protected void tearDown() throws Exception + { + super.tearDown(); + count++; + Log.d(TAG, "TEAR DOWN"); + //run after each testxxxx + // add cleanup code here + } + + public void testYourThing1() throws Throwable + { + assertTrue(true); + } + + public void testYourThing2() throws Throwable + { + assertTrue(true); + } + +} diff --git a/xwiki-android-test-core/src/org/xwiki/android/core/test/properties/Env.java b/xwiki-android-test-core/src/org/xwiki/android/core/test/properties/Env.java new file mode 100644 index 0000000..83f66cc --- /dev/null +++ b/xwiki-android-test-core/src/org/xwiki/android/core/test/properties/Env.java @@ -0,0 +1,40 @@ +package org.xwiki.android.core.test.properties; + +public interface Env +{ + String WIKI_NAME = "xwiki"; + + String SPACE_NAME = "Blog"; + + String PAGE_NAME = "test2"; + + String TEMP_PAGE_NAME ="tempTestPage"; //used for create method tests. because page needss to be deleted for creation + + String URL = "10.0.2.2:8080"; + + String USERNAME = "superadmin"; + + String PASSWORD = "admin"; + + // String PAGE_VERSION = "1.0"; + + String COMMENT_ID = "0"; + + // String LANGUAGE = "null"; + + String CLASS_NAME = "Blog.BlogPostClass"; + + int OBJECT_NUMBER = 0; + + String OBJECT_PROPERTY_NAME = "content"; + + // String SEARCH_KEWORD = "test"; + + String TAG_WORD = "testTag"; + + String ATTACHMENT_NAME = "note.txt"; + + + + // String ATTACHMENT_VERSION = "1.0"; +} 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 new file mode 100644 index 0000000..93a3b01 --- /dev/null +++ b/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoCreate.java @@ -0,0 +1,270 @@ +package org.xwiki.android.rest.ral.test; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.PrintWriter; +import java.util.List; + +import junit.framework.AssertionFailedError; + +import org.xwiki.android.context.XWikiApplicationContext; +import org.xwiki.android.core.test.properties.Env; +import org.xwiki.android.resources.Comments; +import org.xwiki.android.resources.Object; +import org.xwiki.android.resources.Property; +import org.xwiki.android.rest.PageResources; +import org.xwiki.android.rest.RestConnectionException; +import org.xwiki.android.rest.RestException; +import org.xwiki.android.rest.XWikiAPI; +import org.xwiki.android.rest.ral.DocumentRao; +import org.xwiki.android.rest.ral.RESTfulManager; +import org.xwiki.android.rest.ral.RaoException; +import org.xwiki.android.rest.ral.XmlRESTFulManager; +import org.xwiki.android.svc.cmn.LoginFacade; +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 org.xwiki.android.xmodel.entity.Tag; +import org.xwiki.android.xmodel.xobjects.XSimpleObject; + +import android.app.Application; +import android.content.Context; +import android.test.AndroidTestCase; +import android.util.Log; + +/** + * Tests XMLDocumentRao.create(); + */ +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; + static int count=1; + + // tested apis + RESTfulManager rm; + DocumentRao rao; + + // api used for output verification + XWikiAPI api; + + // test inputs + Document doc; + // special inputs set in tests + XBlogPost xb1, xb2, xb3; + XSimpleObject so1, so2; // a general so. + Comment c1, c2, c3; + Tag t1, t2; + Attachment a1; + File af1; + + public TestDocumentRaoCreate() + { + username = Env.USERNAME; + password = Env.PASSWORD; + serverUrl = Env.URL; + + wikiName = Env.WIKI_NAME; + spaceName = Env.SPACE_NAME; + pageName = Env.TEMP_PAGE_NAME; + attachmentName=Env.ATTACHMENT_NAME; + } + + @Override + protected void setUp() throws Exception + { + super.setUp(); + rm = new XmlRESTFulManager(serverUrl, username, password); + api = rm.getConnection().getBaseAPI(); + rao = rm.newDocumentRao(); + doc = new Document(wikiName, spaceName, pageName); + doc.setTitle(pageName); + + // setup preconditions on serverside + if(count<6)api.deletePage(wikiName, spaceName, pageName); + if(count==1){ + Application sys=XWikiApplicationContext.getInstance(); + FileOutputStream fos = sys.openFileOutput(attachmentName, Context.MODE_WORLD_READABLE); + PrintWriter writer=new PrintWriter(fos); + writer.println("this is a text attachment."); + writer.flush(); + writer.close(); + af1=sys.getFileStreamPath(attachmentName); + } + Log.d(TAG,"setup test method:"+ count ); + + } + + @Override + protected void tearDown() throws Exception + { + super.tearDown(); + count++; + // rm.close(); + } + + /** + * a minimal test + */ + public void testCreate01() throws Throwable + { + boolean success = true; + + try { + rao.create(doc); + success = api.existsPage(wikiName, spaceName, pageName); + } catch (RaoException e) { + success = false; + } catch (RestException e) { + success = false; + } + assertTrue(success); + } + + /** + * test with xobjects. Use add method. + */ + public void testCreate02() throws Throwable + { + boolean success = false; + xb1 = new XBlogPost(); + xb1.setContent("test post"); + doc.addObject(xb1); + try { + rao.create(doc); + } catch (RaoException e) { + success = false; + } + try { + success = api.existsPage(wikiName, spaceName, pageName); + } catch (RestException e) { + success = false; + } + assertTrue(success); + } + + /** + * test add/set general XSimpleObject. + * + * @throws Throwable + */ + public void testCreate03() throws Throwable + { + boolean success; + so1 = new XSimpleObject("Blog.BlogClass") + { + }; + so2 = new XSimpleObject("Blog.BlogCategoryClass") + { + }; + so2.setNumber(1); + doc.addObject(so1); + doc.setObject(so2); + + try { + rao.create(doc); + } catch (RaoException e) { + success = false; + } + + // verify + try { + 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); + success = crite1 & crite2; + } + } catch (RestException e) { + success = false; + } + } + + /** + * checks wether the objects get added in the correct sequence + * + * @throws Throwable + */ + public void testCreate04() throws Throwable + { + boolean success = false; + xb1 = new XBlogPost(); + xb2 = new XBlogPost(); + xb3 = new XBlogPost(); + + xb1.setContent("0"); + xb2.setContent("1"); + xb3.setContent("2"); + + xb1.setNumber(0); + // dont set num on xb2 + xb3.setNumber(2); + + doc.setObject(xb1); + doc.addObject(xb2); + doc.setObject(xb3); + + rao.create(doc); + + try { + success = api.existsPage(wikiName, spaceName, pageName); + if (success) { + boolean crite1 = api.existsObject(wikiName, spaceName, pageName, xb1.getClassName(), 0); + boolean crite2 = api.existsObject(wikiName, spaceName, pageName, xb1.getClassName(), 1); + boolean crite3 = api.existsObject(wikiName, spaceName, pageName, xb1.getClassName(), 2); + success = crite1 & crite2 & crite3; + } + if (success) { + Property xb2PropRet = + api.getObjectProperty(wikiName, spaceName, pageName, xb2.getClassName(), "1", "content"); + success = xb2PropRet.getValue().equals("1"); + } + + } catch (RestException e) { + success = false; + } + assertTrue(success); + } + + public void testCreate05WithCmnts() throws Throwable + { + + c1 = new Comment("hi"); + c2 = new Comment("reply to hi"); + c1.addReplyComment(c2); + doc.addComment(c1, true); + + try { + rao.create(doc); + } catch (RaoException e) { + throw new AssertionFailedError("xmlrpc exception code=" + e.getCode()); + } + try { + boolean success = api.existsPage(wikiName, spaceName, pageName); + assertTrue(success); + if (success) { + Comments cmnts = api.getPageComments(wikiName, spaceName, pageName); + List cl = cmnts.comments; + assertEquals(2, cl.size()); + int replyto = cl.get(1).replyTo; + assertEquals(0, replyto); + } + } catch (RestException e) { + throw new AssertionFailedError("xmlrpc exception code=" + e.getCode()); + } + } + +// public void testCreate06WithAttachment() throws Throwable +// { +// +// Attachment a=new Attachment(attachmentName, af1); +// doc.addAttachment(a); +// rao.create(doc); +// assertNotNull(api.getPageAttachment(wikiName, spaceName, pageName, attachmentName)); +// +// +// +// } +} diff --git a/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoDelete.java b/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoDelete.java new file mode 100644 index 0000000..a4b26f7 --- /dev/null +++ b/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoDelete.java @@ -0,0 +1,78 @@ +//package org.xwiki.android.rest.ral.test; +// +//import org.xwiki.android.core.test.properties.Env; +//import org.xwiki.android.rest.RestConnectionException; +//import org.xwiki.android.rest.XWikiAPI; +//import org.xwiki.android.rest.ral.DocumentRao; +//import org.xwiki.android.rest.ral.RESTfulManager; +//import org.xwiki.android.rest.ral.RaoException; +//import org.xwiki.android.rest.ral.XmlRESTFulManager; +//import org.xwiki.android.svc.cmn.LoginFacade; +//import org.xwiki.android.xmodel.entity.Document; +// +//import android.test.AndroidTestCase; +//import android.util.Log; +// +//public class TestDocumentRaoDelete extends AndroidTestCase +//{ +// +// private static final String TAG = TestDocumentRaoDelete.class.getSimpleName(); +// int count; +// +// // test org.xwiki.android.test.fixture.teardown.env parameters. +// String serverUrl, username, password, wikiName, spaceName, pageName; +// +// // tested apis +// RESTfulManager rm; +// DocumentRao rao; +// Document doc; +// +// // api used for output verification +// XWikiAPI api; +// +// +// public TestDocumentRaoDelete() +// { +// // things that need to run only one time. workaround for @BeforeClass +// // obtain vars from Var class +// username = Env.USERNAME; +// password = Env.PASSWORD; +// serverUrl = Env.URL; +// +// wikiName = Env.WIKI_NAME; +// spaceName = Env.SPACE_NAME; +// pageName = Env.PAGE_NAME; +// count=0; +// } +// +// +// +// @Override +// protected void setUp() throws Exception +// { +// super.setUp(); +// rm = new XmlRESTFulManager(serverUrl, username, password); +// api = rm.getConnection().getBaseAPI(); +// rao = rm.newDocumentRao(); +// doc = new Document(wikiName, spaceName, pageName); +// doc.setTitle(pageName); +// } +// +// @Override +// protected void tearDown() throws Exception +// { +// super.tearDown(); +// count++; +// } +// +// public void testYourThing1() throws Throwable +// { +// assertTrue(true); +// } +// +// public void testYourThing2() throws Throwable +// { +// assertTrue(true); +// } +// +//} 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 new file mode 100644 index 0000000..f8e9fcb --- /dev/null +++ b/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoRetreive.java @@ -0,0 +1,102 @@ +//package org.xwiki.android.rest.ral.test; +// +//import org.xwiki.android.core.test.properties.Env; +//import org.xwiki.android.resources.Object; +//import org.xwiki.android.resources.Property; +//import org.xwiki.android.rest.RestConnectionException; +//import org.xwiki.android.rest.XWikiAPI; +//import org.xwiki.android.rest.ral.DocumentRao; +//import org.xwiki.android.rest.ral.RESTfulManager; +//import org.xwiki.android.rest.ral.RaoException; +//import org.xwiki.android.rest.ral.XmlRESTFulManager; +//import org.xwiki.android.svc.cmn.LoginFacade; +//import org.xwiki.android.xmodel.blog.XBlogPost; +//import org.xwiki.android.xmodel.entity.Document; +// +//import android.test.AndroidTestCase; +//import android.util.Log; +// +//public class TestDocumentRaoRetreive extends AndroidTestCase +//{ +// +// private static final String TAG = TestDocumentRaoRetreive.class.getSimpleName(); +// // test org.xwiki.android.test.fixture.teardown.env parameters. +// String serverUrl, username, password, wikiName, spaceName, pageName,objectClassname,propertyName; +// int objectNumber; +// +// // tested apis +// RESTfulManager rm; +// DocumentRao rao; +// Document doc; +// +// // api used for output verification +// XWikiAPI api; +// +// static int count=0; +// +// public TestDocumentRaoRetreive() +// { +// super(); +// username = Env.USERNAME; +// password = Env.PASSWORD; +// serverUrl = Env.URL; +// +// wikiName = Env.WIKI_NAME; +// spaceName = Env.SPACE_NAME; +// pageName = Env.PAGE_NAME; +// objectClassname=Env.CLASS_NAME; +// objectNumber=Env.OBJECT_NUMBER; +// propertyName=Env.OBJECT_PROPERTY_NAME; +// count=0; +// } +// +// +// +// @Override +// protected void setUp() throws Exception +// { +// super.setUp(); +// rm = new XmlRESTFulManager(serverUrl, username, password); +// api = rm.getConnection().getBaseAPI(); +// rao = rm.newDocumentRao(); +// doc = new Document(wikiName, spaceName, pageName); +// doc.setTitle(pageName); +// //check precondition +// if(count==0){ +// boolean pre=api.existsPage(wikiName, spaceName, pageName); +// if(!pre){ +// Log.e(TAG, "preconditions failed. Please create a page to retreive. refer Env class"); +// } +// pre=api.existsObject(wikiName, spaceName, pageName, objectClassname, objectNumber); +// if(!pre){ +// Log.e(TAG, "preconditions failed. Please create a Object (blogPost) to retreive. refer Env class"); +// } +// +// Property p=api.getObjectProperty(wikiName, spaceName, pageName, objectClassname, objectNumber+"", propertyName); +// assertNotNull(p); +// assertTrue(pre); +// } +// } +// +// @Override +// protected void tearDown() throws Exception +// { +// super.tearDown(); +// } +// +// public void testRetreive() throws Throwable +// { +// boolean success; +// try { +// Document docRet=rao.retreive(doc); +// assertNotNull(docRet); +// XBlogPost blgPost=(XBlogPost) docRet.getObject(objectClassname+"/"+objectNumber); +// assertNotNull(blgPost); +// assertNotNull(blgPost.getContent()); +// }catch (RaoException e) { +// success=false; +// } +// +// } +// +//} 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 new file mode 100644 index 0000000..a0cfb7d --- /dev/null +++ b/xwiki-android-test-core/src/org/xwiki/android/rest/ral/test/TestDocumentRaoUpdate.java @@ -0,0 +1,99 @@ +//package org.xwiki.android.rest.ral.test; +// +//import org.xwiki.android.core.test.properties.Env; +//import org.xwiki.android.rest.RestConnectionException; +//import org.xwiki.android.rest.XWikiAPI; +//import org.xwiki.android.rest.ral.DocumentRao; +//import org.xwiki.android.rest.ral.RESTfulManager; +//import org.xwiki.android.rest.ral.RaoException; +//import org.xwiki.android.rest.ral.XmlRESTFulManager; +//import org.xwiki.android.svc.cmn.LoginFacade; +//import org.xwiki.android.xmodel.blog.XBlogPost; +//import org.xwiki.android.xmodel.entity.Comment; +//import org.xwiki.android.xmodel.entity.Document; +// +//import android.test.AndroidTestCase; +//import android.util.Log; +// +//public class TestDocumentRaoUpdate extends AndroidTestCase +//{ +// +// private static final String TAG = TestDocumentRaoUpdate.class.getSimpleName(); +// // test org.xwiki.android.test.fixture.teardown.env parameters. +// String serverUrl, username, password, wikiName, spaceName, pageName; +// +// int count; +// +// // tested apis +// RESTfulManager rm; +// DocumentRao rao; +// Document doc; +// +// // api used for output verification +// XWikiAPI api; +// +// public TestDocumentRaoUpdate() +// { +// // things that need to run only one time. workaround for @BeforeClass +// // obtain vars from Var class +// username = Env.USERNAME; +// password = Env.PASSWORD; +// serverUrl = Env.URL; +// +// wikiName = Env.WIKI_NAME; +// spaceName = Env.SPACE_NAME; +// pageName = Env.PAGE_NAME; +// count = 0; +// } +// +// @Override +// protected void setUp() throws Exception +// { +// super.setUp(); +// rm = new XmlRESTFulManager(serverUrl, username, password); +// api = rm.getConnection().getBaseAPI(); +// rao = rm.newDocumentRao(); +// doc = new Document(wikiName, spaceName, pageName); +// doc.setTitle(pageName); +// } +// +// @Override +// protected void tearDown() throws Exception +// { +// super.tearDown(); +// count++; +// } +// +// public void testUpdate1() throws Throwable +// { +// boolean success = false; +// +// XBlogPost xb = new XBlogPost(); +// xb.setContent("edited"); +// xb.setNumber(0); +// doc.setObject(xb); +// +// try { +// rao.update(doc); +// } catch (RaoException e) { +// success = false; +// } +// assertTrue(success); +// +// } +// +// public void testUpdate2() throws Throwable +// { +// boolean success = false; +// +// Comment cmnt = new Comment("hi comment"); +// doc.addComment(cmnt); +// try { +// rao.update(doc); +// } catch (RaoException e) { +// success = false; +// } +// assertTrue(success); +// } +// +//} diff --git a/xwiki-android-test-fixture-setup/pom.xml b/xwiki-android-test-fixture-setup/pom.xml new file mode 100644 index 0000000..878b1f9 --- /dev/null +++ b/xwiki-android-test-fixture-setup/pom.xml @@ -0,0 +1,127 @@ + + + + + + 4.0.0 + + org.xwiki.android + xwiki-android + 1.0-SNAPSHOT + + + org.xwiki.android + xwiki-android-test-fixture-setup + ${parent.version} + XWiki Server Startup and external fixture setup + + + + + org.slf4j + slf4j-log4j12 + 1.6.6 + + + + org.apache.ant + ant + 1.7.0 + + + + commons-httpclient + commons-httpclient + 3.1 + + + + org.apache.httpcomponents + httpclient + 4.1.2 + + + + org.apache.httpcomponents + httpcore + 4.1.2 + + + + + commons-configuration + commons-configuration + 1.8 + + + + org.apache.commons + commons-lang3 + 3.0 + + + + junit + junit + 4.10 + + + + org.xwiki.android + xwiki-rest-model-simplexml + 1.0-SNAPSHOT + + + + + + test + + + resources + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*AllTests.java + + + **/*Test.java + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + + + + diff --git a/xwiki-android-test-fixture-setup/resources/a.png b/xwiki-android-test-fixture-setup/resources/a.png new file mode 100644 index 0000000..fad651f Binary files /dev/null and b/xwiki-android-test-fixture-setup/resources/a.png differ diff --git a/xwiki-android-test-fixture-setup/resources/del.png b/xwiki-android-test-fixture-setup/resources/del.png new file mode 100644 index 0000000..a63fb68 Binary files /dev/null and b/xwiki-android-test-fixture-setup/resources/del.png differ diff --git a/xwiki-android-test-fixture-setup/resources/log4j.properties b/xwiki-android-test-fixture-setup/resources/log4j.properties new file mode 100644 index 0000000..b5851ee --- /dev/null +++ b/xwiki-android-test-fixture-setup/resources/log4j.properties @@ -0,0 +1,7 @@ +#log4j.rootLogger=DEBUG, CA +#log4j.logger.org.apache.http= ERROR + +#log4j.appender.CA=org.apache.log4j.ConsoleAppender + +#log4j.appender.CA.layout=org.apache.log4j.PatternLayout +#log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n \ No newline at end of file diff --git a/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/fixture/Main.java b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/fixture/Main.java new file mode 100644 index 0000000..7cb59c2 --- /dev/null +++ b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/fixture/Main.java @@ -0,0 +1,23 @@ +package org.xwiki.android.test.fixture; + +import org.xwiki.test.integration.utils.XWikiExecutor; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.net.URL; + +/** + * Created by IntelliJ IDEA. + * User: sasindap + * Date: 8/9/12 + * Time: 4:40 PM + * To change this template use File | Settings | File Templates. + */ +public class Main { + public static void main(String[] args) throws Exception { + System.setProperty("xwikiExecutionDirectory","C:\\Users\\admin\\Desktop\\xwiki-enterprise-jetty-10"); + XWikiExecutor executor=new XWikiExecutor(0); + executor.start(); + } +} diff --git a/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/AttachmentOperations.java b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/AttachmentOperations.java new file mode 100644 index 0000000..9809298 --- /dev/null +++ b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/AttachmentOperations.java @@ -0,0 +1,251 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.xwiki.android.test.utils.xmlrpc; + +import org.apache.http.util.EntityUtils; +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; +import org.xwiki.android.resources.Attachments; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; + +/** + * XWiki Android REST Attachment related source. Can get the attachment details/list as a Attachments objects or the + * Stream of raw data of a attachment file. + */ +public class AttachmentOperations { + + RestClient rpc; + + /** + * Path provided from XWiki RESTful API + */ + private final String PAGE_URL_PREFIX = "/xwiki/rest/wikis/"; + + /** + * URL of the XWiki domain + */ + private String URLprefix; + + /** + * Name of Wiki for acquiring attachment + */ + private String wikiName; + + /** + * Name of Space for acquiring attachment + */ + private String spaceName; + + /** + * Name of Page for acquiring attachment + */ + private String pageName; + + /** + * @param URLprefix XWiki URl ex:"www.xwiki.org" + * @param wikiName name of the wiki in UTF-8 format + * @param spaceName name of the space in UTF-8 format + * @param pageName name of the page in UTF-8 format + */ + AttachmentOperations(String URLprefix, String wikiName, String spaceName, String pageName, RestClient rpc) { + this.URLprefix = URLprefix; + this.wikiName = wikiName; + this.spaceName = spaceName; + this.pageName = pageName; + this.rpc=rpc; + } + + /** + * Gets Attachments object according to the provided field values in constructor + * + * @return Attachments object acquired according to the wiki, space & page names + * @throws IOException + * @throws RestException + */ + public Attachments getAllPageAttachments() throws IOException, RestException { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/attachments"; + + return buildAttachments(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + /** + * Gets a stream data of the requested attachment + * + * @param attachmentName filename of the attachment in the page + * @return InputStream of the attachment data + * @throws IOException + * @throws RestException + */ + public InputStream getPageAttachment(String attachmentName) throws IOException, RestException { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/attachments/" + attachmentName; + + return rpc.getRequest(Uri).getEntity().getContent(); + } + + /** + * Add file to the XWiki stored in the internal/external memory + * + * @param file file representing the attachement. + * @param attachmentName name for the attachment + * @return status of the HTTP response + * @throws IOException + * @throws RestException + */ + public String putPageAttachment(String attachmentName, File file) throws IOException, RestException { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/attachments/" + attachmentName; + + return rpc.putRequest(Uri, file).getStatusLine().toString(); + } + + /** + * Gets Attachments object in the provided version of page + * + * @param version version of the page + * @return Attachments object acquired according to the wiki, space & page names + * @throws IOException + * @throws RestException + */ + public Attachments getPageAttachmentsInHistory(String version) throws IOException, RestException { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/history/" + version + "/attachments"; + + return buildAttachments(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + /** + * Gets a stream data of the requested attachment in the provided page version + * + * @param version version of the page + * @param attachmentName file name of the attachement + * @return InputStream of the attachment data in provided page version + * @throws IOException + * @throws RestException + */ + public InputStream getPageAttachmentsInHistory(String version, String attachmentName) throws IOException, RestException { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/history/" + version + "/attachments/" + attachmentName; + + return rpc.getRequest(Uri).getEntity().getContent(); + } + + /** + * Gets the history of the attachment + * + * @param attachmentName filename of the attachment + * @return Attachments object with the version details of the attachment + * @throws IOException + * @throws RestException + */ + public Attachments getPageAttachmentsInAttachmentHistory(String attachmentName) throws IOException, RestException { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/attachments/" + attachmentName + "/history"; + + return buildAttachments(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + /** + * Gets the attachment of a specific attachment version + * + * @param attachmentName filename of the attachment + * @param attachmentVersion version of the attachment + * @return InputStream of the provided attachment version + * @throws IOException + * @throws RestException + */ + public InputStream getPageAttachmentsInAttachmentHistory(String attachmentName, String attachmentVersion) throws IOException, RestException { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/attachments/" + attachmentName + "/history/" + attachmentVersion; + + return rpc.getRequest(Uri).getEntity().getContent(); + } + + /** + * Deletes attachment file from the XWiki server + * + * @param attachmentName filename of the attachment + * @return Status line of Http response + * @throws IOException + * @throws RestException + */ + public String deletePageAttachment(String attachmentName) throws IOException, RestException { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/attachments/" + attachmentName; + + return rpc.deleteRequest(Uri).getStatusLine().toString(); + } + + /** + * Parse xml into a Attachments object + * + * @param content XML content + * @return Attachments object deserialized from the xml content + */ + private Attachments buildAttachments(String content) { + Attachments attachments = new Attachments(); + Serializer serializer = new Persister(); + + try { + attachments = serializer.read(Attachments.class, content); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return attachments; + } + + /** + * Generate XML content from the Attachments object + * + * @param attachments Attachments Object to be serialized into XML + * @return XML content of the provided Attachments object + */ + private String buildXmlAttachments(Attachments attachments) { + Serializer serializer = new Persister(); + + StringWriter result = new StringWriter(); + + try { + serializer.write(attachments, result); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return result.toString(); + } + +} diff --git a/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/CommentOperations.java b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/CommentOperations.java new file mode 100644 index 0000000..1eea20e --- /dev/null +++ b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/CommentOperations.java @@ -0,0 +1,260 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.xwiki.android.test.utils.xmlrpc; + +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.Comments; + +import java.io.IOException; +import java.io.StringWriter; + +/** + * XWiki Android REST Comments related source. Can get the comments details/list as a Comment(s) objects of Simple-xml + * object model + */ +public class CommentOperations +{ + /** + * Path provided from XWiki RESTful API + */ + private final String PAGE_URL_PREFIX = "/xwiki/rest/wikis/"; + + /** + * URL of the XWiki domain + */ + private String URLprefix; + + /** + * Name of Wiki for acquiring comments + */ + private String wikiName; + + /** + * Name of space for acquiring comments + */ + private String spaceName; + + /** + * Name of page for acquiring comments + */ + private String pageName; + private RestClient rpc; + + /** + * @param URLprefix XWiki URl ex:"www.xwiki.org" + * @param wikiName name of the wiki in UTF-8 format + * @param spaceName name of the space in UTF-8 format + * @param pageName name of the page in UTF-8 format + * @param rpc + */ + CommentOperations(String URLprefix, String wikiName, String spaceName, String pageName, RestClient rpc) + { + this.URLprefix = URLprefix; + this.wikiName = wikiName; + this.spaceName = spaceName; + this.pageName = pageName; + this.rpc=rpc; + } + + /** + * @return Comments object of the page + * @throws IOException + * @throws RestException + */ + public Comments getPageComments() throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/comments"; + return buildComments(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + public boolean exists(int commentId) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/comments/" + commentId; + int code = rpc.headRequest(Uri); + if (code == 200) { + return true; + } + if (code == 404) { + return false; + } else { + throw new RestException(code); + } + } + + /** + * @param commentId ID of the comment in the page + * @return comment of a page selected using comment ID + * @throws IOException + * @throws RestException + */ + public Comment getPageComment(int commentId) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/comments/" + commentId; + + return buildComment(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + /** + * @param version version of the page + * @return list of comments as a Comments object in a specific page history version + * @throws IOException + * @throws RestException + */ + public Comments getPageCommentsInHistory(String version) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/history/" + version + "/comments"; + + return buildComments(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + /** + * @param version version of the page + * @param commentId ID of the comment in the page + * @return Comment in a specific page history version + * @throws IOException + * @throws RestException + */ + public Comments getPageCommentsInHistory(String version, String commentId) throws IOException, + RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/history/" + version + "/comments/" + commentId; + + return buildComments(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + /** + * Adds comment to the page + * + * @param comment Comment object to be added + * @return status of the HTTP post request + * @throws IOException + * @throws RestException + */ + public String addPageComment(Comment comment) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/comments"; + + return rpc.postRequest(Uri, buildXmlComment(comment)).getStatusLine().toString(); + } + + /** + * Parse xml into a Comments object + * + * @param content XML content + * @return Comments object deserialized from the xml content + */ + private Comments buildComments(String content) + { + Comments comments = new Comments(); + + Serializer serializer = new Persister(); + + try { + comments = serializer.read(Comments.class, content); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return comments; + } + + /** + * Generate XML content from the Comments object + * + * @param comments Comments object to be serialized into XML + * @return XML content of the provided Comments object + */ + private String buildXmlComments(Comments comments) + { + Serializer serializer = new Persister(); + + StringWriter result = new StringWriter(); + + try { + serializer.write(comments, result); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return result.toString(); + } + + /** + * Parse xml into a Comment object + * + * @param content XML content + * @return Comment object deserialized from the xml content + */ + private Comment buildComment(String content) + { + Comment comment = new Comment(); + + Serializer serializer = new Persister(); + + try { + comment = serializer.read(Comment.class, content); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return comment; + } + + /** + * Generate XML content from the Comment object + * + * @param comment Comment object to be serialized into XML + * @return XML content of the provided Comment object + */ + private String buildXmlComment(Comment comment) + { + Serializer serializer = new Persister(); + + StringWriter result = new StringWriter(); + + try { + serializer.write(comment, result); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return result.toString(); + } +} diff --git a/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/ObjectOperations.java b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/ObjectOperations.java new file mode 100644 index 0000000..956d4ab --- /dev/null +++ b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/ObjectOperations.java @@ -0,0 +1,414 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.xwiki.android.test.utils.xmlrpc; + +import org.apache.http.util.EntityUtils; +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; +import org.xwiki.android.resources.Object; +import org.xwiki.android.resources.Objects; +import org.xwiki.android.resources.Properties; +import org.xwiki.android.resources.Property; + +import java.io.IOException; +import java.io.StringWriter; + +/** + * XWiki Android REST Object related source. Can get the objects as a Object(s) objects of Simple-xml object model + */ +public class ObjectOperations +{ + /** + * Path provided from XWiki RESTful API + */ + private final String PAGE_URL_PREFIX = "/xwiki/rest/wikis/"; + + /** + * URL of the XWiki domain + */ + private String URLprefix; + + /** + * Name of Wiki for acquiring objects + */ + private String wikiName; + + /** + * Name of space for acquiring objects + */ + private String spaceName; + + /** + * Name of page for acquiring objects + */ + private String pageName; + private RestClient rpc; + + /** + * @param URLprefix XWiki URl ex:"www.xwiki.org" + * @param wikiName name of the wiki in UTF-8 format + * @param spaceName name of the space in UTF-8 format + * @param pageName name of the page in UTF-8 format + * @param rpc + */ + ObjectOperations(String URLprefix, String wikiName, String spaceName, String pageName, RestClient rpc) + { + this.URLprefix = URLprefix; + this.wikiName = wikiName; + this.spaceName = spaceName; + this.pageName = pageName; + this.rpc=rpc; + } + + public boolean exists(String objectClassname, String objectNumber) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/objects" + objectClassname + "/" + objectNumber; + + int code = rpc.headRequest(Uri); + if (code == 200) { + return true; + } + if (code == 404) { + return false; + } else { + throw new RestException(code); + } + } + + /** + * @return list of all the Objects in a page + * @throws IOException + * @throws RestException + */ + public Objects getAllObjects() throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/objects"; + + return buildObjects(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + + } + + /** + * Adds object to the page + * + * @param object Object object to be added to the page + * @return status of th HTTP post request + * @throws IOException + * @throws RestException + */ + public String addObject(Object object) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/objects"; + + return rpc.postRequest(Uri, buildXmlObject(object)).getStatusLine().toString(); + + } + + /** + * @param objectClassname name of the class + * @return list of objects in a specific class of a page + * @throws IOException + * @throws RestException + */ + public Objects getObjectsInClassname(String objectClassname) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/objects/" + objectClassname; + + return buildObjects(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + + } + + /** + * @param objectClassname name of the class in the page + * @param objectNumber number of the object in the specified class + * @return selected Object object in the page + * @throws IOException + * @throws RestException + */ + public Object getObject(String objectClassname, String objectNumber) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/objects/" + objectClassname + "/" + objectNumber; + + return buildObject(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + + } + + /** + * Update object in a page + * + * @param objectClassname name of the class of the object + * @param objectNumber number of the object in the class + * @param object Object object to be updated in the page + * @return status of the HTTP put request + * @throws IOException + * @throws RestException + */ + public String updateObject(String objectClassname, int objectNumber, Object object) + throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/objects/" + objectClassname + "/" + objectNumber; + + return rpc.putRequest(Uri, buildXmlObject(object)).getStatusLine().toString(); + + } + + /** + * Deletes the specified object in the page + * + * @param objectClassname name of the class in the page + * @param objectNumber number of the object in the class + * @return status of HTTP resonce in delete request + * @throws IOException + * @throws RestException + */ + public String deleteObject(String objectClassname, String objectNumber) throws IOException, + RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/objects/" + objectClassname + "/" + objectNumber; + + return rpc.deleteRequest(Uri).getStatusLine().toString(); + + } + + /** + * @param objectClassname class name of the object in a page + * @param objectNumber number of the object in the class + * @return list of properties as a Properties object in a object + * @throws IOException + * @throws RestException + */ + public Properties getObjectProperties(String objectClassname, String objectNumber) throws IOException, + RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/objects/" + objectClassname + "/" + objectNumber + "/properties"; + + return buildProperties(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + + } + + /** + * Adds a property to a object in a page + * + * @param objectClassname class name of the object + * @param objectNumber number of the object in the class + * @param property Property object to be added to the Object + * @return status of the HTTP put request + * @throws IOException + * @throws RestException + */ + public String addObjectProperty(String objectClassname, String objectNumber, Property property) + throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/objects/" + objectClassname + "/" + objectNumber + "/properties/" + property.getName(); + + return rpc.putRequest(Uri, buildXmlProperty(property)).getStatusLine().toString(); + } + + /** + * @param objectClassname class name of the objects + * @param objectNumber number of the object in the class + * @param propertyName name of the required property + * @return Specific property of a object in a page + * @throws IOException + * @throws RestException + */ + public Property getObjectProperty(String objectClassname, String objectNumber, String propertyName) + throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/objects/" + objectClassname + "/" + objectNumber + "/properties/" + propertyName; + + return buildProperty(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + + } + + /** + * Parse xml into a Objects object + * + * @param content XML content + * @return Objects object deserialized from the xml content + */ + private Objects buildObjects(String content) + { + Objects objects = new Objects(); + + Serializer serializer = new Persister(); + + try { + objects = serializer.read(Objects.class, content); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return objects; + } + + /** + * Generate XML content from the Objects object + * + * @param objects Objects object to be serialized into XML + * @return XML content of the provided Objects object + */ + private String buildXmlObjects(Objects objects) + { + Serializer serializer = new Persister(); + + StringWriter result = new StringWriter(); + + try { + serializer.write(objects, result); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return result.toString(); + } + + /** + * Generate XML content from the Object object + * + * @param object Object object to be serialized into XML + * @return XML content of the provided Object object + */ + private String buildXmlObject(Object object) + { + Serializer serializer = new Persister(); + + StringWriter result = new StringWriter(); + + try { + serializer.write(object, result); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return result.toString(); + } + + /** + * Parse xml into a Object object + * + * @param content XML content + * @return Object object deserialized from the xml content + */ + private Object buildObject(String content) + { + Object object = new Object(); + + Serializer serializer = new Persister(); + + try { + object = serializer.read(Object.class, content); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return object; + } + + /** + * Parse xml into a Properties object + * + * @param content XML content + * @return Properties object deserialized from the xml content + */ + private Properties buildProperties(String content) + { + Properties properties = new Properties(); + + Serializer serializer = new Persister(); + + try { + properties = serializer.read(Properties.class, content); + } catch (Exception e) { + e.printStackTrace(); + } + + return properties; + } + + /** + * Generate XML content from the Property object + * + * @param property Property object to be serialized into XML + * @return XML content of the provided Property object + */ + private String buildXmlProperty(Property property) + { + Serializer serializer = new Persister(); + + StringWriter result = new StringWriter(); + + try { + serializer.write(property, result); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return result.toString(); + } + + /** + * Parse xml into a Property object + * + * @param content XML content + * @return Property object deserialized from the xml content + */ + private Property buildProperty(String content) + { + Property property = new Property(); + + Serializer serializer = new Persister(); + + try { + property = serializer.read(Property.class, content); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return property; + } +} diff --git a/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/PageOperations.java b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/PageOperations.java new file mode 100644 index 0000000..8087625 --- /dev/null +++ b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/PageOperations.java @@ -0,0 +1,346 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.xwiki.android.test.utils.xmlrpc; + +import org.apache.http.util.EntityUtils; +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; +import org.xwiki.android.resources.Page; +import org.xwiki.android.resources.Pages; + +import java.io.IOException; +import java.io.StringWriter; + +/** + * XWiki Android REST Page(s) related source. Can get the Page details/list as a Page(s) objects of Simple-xml object + * model + */ +public class PageOperations { + /** + * Path provided from XWiki RESTful API + */ + private final String PAGE_URL_PREFIX = "/xwiki/rest/wikis/"; + + /** + * Path provided from XWiki RESTful API for pages + */ + private final String PAGE_URL_SUFFIX = "/pages"; + + /** + * URL of the XWiki domain + */ + private String URLprefix; + + /** + * Name of Wiki for acquiring pages + */ + private String wikiName; + + /** + * Name of space for acquiring pages + */ + private String spaceName; + + private RestClient rpc; + + /** + * @param URLprefix XWiki URl ex:"www.xwiki.org" + * @param wikiName name of the wiki in UTF-8 format + * @param spaceName name of the space in UTF-8 format + * @param rpc + */ + PageOperations(String URLprefix, String wikiName, String spaceName, RestClient rpc) + { + + this.URLprefix = URLprefix; + this.wikiName = wikiName; + this.spaceName = spaceName; + this.rpc=rpc; + } + + + /** + * @return all the pages as a Pages object in the specified space + * @throws IOException + * @throws RestException + */ + public Pages getAllPages() throws IOException, RestException { + String Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + PAGE_URL_SUFFIX; + ; + return buildPages(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + + public boolean exists(String pageName) throws IOException, RestException { + String Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName; + int code = rpc.headRequest(Uri); + + if (code == 200) { + return true; + } + if (code == 404) { + return false; + } else { + throw new RestException(code); + } + + } + + /** + * @param pageName name of the page + * @return requested page as a Page object + * @throws IOException + * @throws RestException + */ + public Page getPage(String pageName) throws IOException, RestException { + String Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName; + + return buildPage(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + /** + * Adds or Modify the page + * + * @param page Page object to be added if not existing in the space or modified if it existing in the space + * @return status of the HTTP put request + * @throws IOException + * @throws RestException + */ + public String addPage(Page page) throws IOException, RestException { + if (page.getName() == null || page.getName().equals("")) { + throw new IllegalArgumentException("page.name should be initialized"); + } + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + page.getName(); + return (rpc.putRequest(Uri, buildXmlPage(page)).getStatusLine().toString()); + } + + /** + * Deletes the page by providing name of the page + * + * @param pageName name of the page + * @return status of the HTTP delete request + * @throws IOException + * @throws RestException + */ + public String deletePage(String pageName) throws IOException, RestException { + String Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName; + + if (rpc.isSecured()) { + return rpc.deleteRequest(Uri).getStatusLine().toString(); + } else { + return "No authenticaiton details found"; + } + + } + + + /** + * @param pageName name of the page + * @param version history version of the page + * @return requested version of the page + * @throws IOException + * @throws RestException + */ + public Page getPageHistoryOnVersion(String pageName, String version) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/history/" + version; + + return buildPage(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + /** + * @param pageName name of the page + * @return children of the page as a Pages object + * @throws IOException + * @throws RestException + */ + public Pages getPageChildren(String pageName) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/children"; + + return buildPages(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + /** + * @param pageName name of the page + * @param language language name of the page + * @return Translated page of the provided language + * @throws IOException + * @throws RestException + */ + public Page getPageTranslation(String pageName, String language) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/translations/" + language; + + return buildPage(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + /** + * Adds page translation to XWiki + * + * @param page Page object of the new translation + * @param language translated language name of the page + * @return status of the HTTP put request + * @throws IOException + * @throws RestException + */ + public String addPageTranslation(Page page, String language) throws IOException, RestException + { + if(page.getName()==null || page.getName().equals("")){ + throw new IllegalArgumentException("page.name must be initialized"); + } + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + page.getName() + + "/translations/" + language; + + String content = buildXmlPage(page); + + return rpc.putRequest(Uri, content).getStatusLine().toString(); + } + + /** + * Deletes specific translation page + * + * @param pageName name of the page + * @param language translated language name of the page + * @return status of the HTTP delete request + * @throws IOException + * @throws RestException + */ + public String deletePageTranslation(String pageName, String language) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/translations/" + language; + + return rpc.deleteRequest(Uri).getStatusLine().toString(); + } + + /** + * @param pageName name of the page + * @param language translated language name of the page + * @param version version of the page + * @return translated page of the spcific page history version + * @throws IOException + * @throws RestException + */ + public Page getPageTranslation(String pageName, String language, String version) throws IOException, RestException + { + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/translations/" + language + "/history/" + version; + + return buildPage(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + + + + /** + * Parse xml into a Pages object + * + * @param content XML content + * @return Pages object deserialized from the xml content + */ + private Pages buildPages(String content) { + Pages pages = new Pages(); + + Serializer serializer = new Persister(); + + try { + pages = serializer.read(Pages.class, content); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return pages; + } + + /** + * Generate XML content from the Pages object + * + * @param pages Pages object to be serialized into XML + * @return XML content of the provided Pages object + */ + private String buildXmlPages(Pages pages) { + Serializer serializer = new Persister(); + + StringWriter result = new StringWriter(); + + try { + serializer.write(pages, result); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return result.toString(); + } + + /** + * Parse xml into a Page object + * + * @param content XML content + * @return Page object deserialized from the xml content + */ + private Page buildPage(String content) { + Page page = new Page(); + + Serializer serializer = new Persister(); + + try { + page = serializer.read(Page.class, content); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return page; + } + + /** + * Generate XML content from the Page object + * + * @param page Page object to be serialized into XML + * @return XML content of the provided Page object + */ + private String buildXmlPage(Page page) { + Serializer serializer = new Persister(); + + StringWriter result = new StringWriter(); + + try { + serializer.write(page, result); + } catch (Exception e) { + e.printStackTrace(); + } + + return result.toString(); + } +} diff --git a/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/RestClient.java b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/RestClient.java new file mode 100644 index 0000000..de5cd49 --- /dev/null +++ b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/RestClient.java @@ -0,0 +1,436 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.android.test.utils.xmlrpc; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URI; +import java.net.URISyntaxException; + +import org.apache.http.HttpException; +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; +import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.AuthState; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.protocol.ClientContext; +import org.apache.http.entity.FileEntity; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.auth.BasicScheme; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.params.BasicHttpParams; +import org.apache.http.params.HttpConnectionParams; +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.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility class for test setup setup. Same as org.xwiki.android.xmlrpc.HttpConnector. But not Dependant on android platform. * + * org.apache.http does not support preemtiveAuth, it is provided with HttpRequestInterceptor. + */ + +public class RestClient { + Logger log = LoggerFactory.getLogger(RestClient.class); + + + private boolean secured; + private String serverUrl; + + /** + * User credential details + */ + private String username, password; + + /** + * For executing http connection + */ + private DefaultHttpClient client; + + public RestClient(String serverUrl) { + this.serverUrl = serverUrl; + initializeHttpClient(); + } + + /** + * @param serverUrl = server name = host:port + * @param username + * @param password + */ + public RestClient(String serverUrl, String username, String password) { + this.username = username; + this.password = password; + this.serverUrl = serverUrl; + initializeHttpClient(); + secured = true; + setCredentials(); + } + + public void setAuthentication(String username, String password) { + this.username = username; + this.password = password; + secured = true; + setCredentials(); + } + + public boolean isSecured() { + return secured; + } + + public int headRequest(String Uri) throws IOException { + + HttpHead request = new HttpHead(); + URI requestUri; + try { + requestUri = new URI(Uri); + request.setURI(requestUri); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + log.debug("Request URL :" + Uri); + + try { + HttpResponse response = client.execute(request); + log.debug("Response status", response.getStatusLine().toString()); + return response.getStatusLine().getStatusCode(); + } catch (ClientProtocolException e) { + e.printStackTrace(); + return -1; + } + } + + /** + * Perform HTTP Get method execution and return the response as a String + * + * @param Uri URL of XWiki RESTful API + * @return Response data of the HTTP connection as a String + */ + public HttpResponse getRequest(String Uri) throws IOException, RestException { + + + BufferedReader in = null; + HttpGet request = new HttpGet(); + String responseText = new String(); + + try { + URI requestUri = new URI(Uri); + request.setURI(requestUri); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + log.debug("Request URL :" + Uri); + try { + + HttpResponse response = client.execute(request); + log.debug("Response status", response.getStatusLine().toString()); + in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); + StringBuffer sb = new StringBuffer(""); + String line = ""; + String NL = System.getProperty("line.separator"); + while ((line = in.readLine()) != null) { + sb.append(line + NL); + } + responseText = sb.toString(); + log.debug("Response", "response: " + responseText); + validate(response.getStatusLine().getStatusCode()); + return response; + } catch (ClientProtocolException e) { + e.printStackTrace(); + return null; + } finally { + if (in != null) { + in.close(); + } + } + } + + /** + * Perform HTTP Delete method execution and return its status + * + * @param Uri URL of XWiki RESTful API + * @return status of the HTTP method execution + * @throws IOException + * @throws RestException + */ + public HttpResponse deleteRequest(String Uri) throws IOException, RestException { + + HttpDelete request = null; + + try { + URI requestUri = new URI(Uri); + request = new HttpDelete(); + request.setURI(requestUri); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + log.debug("Request URL :" + Uri); + try { + HttpResponse response = client.execute(request); + + log.debug("Response status", response.getStatusLine().toString()); + EntityUtils.consume(response.getEntity()); + validate(response.getStatusLine().getStatusCode()); + return response; + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + + /** + * Perform HTTP Post method execution and return its response status + * + * @param Uri URL of XWiki RESTful API + * @param content content to be posted to the server + * @return status code of the Post method execution + * @throws IOException + * @throws RestException + */ + public HttpResponse postRequest(String Uri, String content) throws IOException, RestException { + + + HttpPost request = new HttpPost(); + + try { + URI requestUri = new URI(Uri); + request.setURI(requestUri); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + log.debug("Request URL :" + Uri); + + try { + log.debug("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.debug("Response status", response.getStatusLine().toString()); + EntityUtils.consume(response.getEntity()); + validate(response.getStatusLine().getStatusCode()); + return response; + + } catch (ClientProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + /** + * Perform HTTP Put method execution and return its response status + * + * @param Uri URL of XWiki RESTful API + * @param content content to be posted to the server + * @return status code of the Put method execution + * @throws IOException + * @throws RestException + */ + public HttpResponse putRequest(String Uri, String content) throws IOException, RestException { + + + HttpPut request = new HttpPut(); + + try { + log.debug("Request URL :" + Uri); + System.out.println(Uri); + URI requestUri = new URI(Uri); + request.setURI(requestUri); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + + try { + log.debug("Put 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.debug("Response status", response.getStatusLine().toString()); + EntityUtils.consume(response.getEntity());//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 IOException(e); + } + + return null; + } + + + /** + * Perform HTTP Put method with the raw data file + * + * @param Uri URL of XWiki RESTful API + * @param file file to upload + * @return status of the HTTP Put method execution + * @throws IOException + * @throws RestException + */ + public HttpResponse putRequest(String Uri, File file) throws IOException, RestException { + + HttpPut request = new HttpPut(); + try { + URI requestUri = new URI(Uri); + request.setURI(requestUri); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + log.debug("Request URL :" + Uri); + try { + FileEntity fe = new FileEntity(file, "/"); + request.setEntity(fe); + // request.setHeader("Content-Type","application/xml;charset=UTF-8"); + HttpResponse response = client.execute(request); + log.debug("Response status", response.getStatusLine().toString()); + EntityUtils.consume(response.getEntity()); + validate(response.getStatusLine().getStatusCode()); + return response; + } catch (ClientProtocolException e) { + throw new RuntimeException(e); + } + } + + + public SpaceOperations spaceOperations(String wikiName) { + return new SpaceOperations(serverUrl, wikiName, this); + } + + public PageOperations pageOperations(String wikiName, String spaceName) { + return new PageOperations(serverUrl, wikiName, spaceName, this); + } + + public ObjectOperations objectOperations(String wikiName, String spaceName, String pageName) { + return new ObjectOperations(serverUrl, wikiName, spaceName, pageName, this); + } + + public CommentOperations commentOperations(String wikiName, String spaceName, String pageName) { + return new CommentOperations(serverUrl, wikiName, spaceName, pageName,this); + } + + public AttachmentOperations attachmentOperations(String wikiName, String spaceName, String pageName) { + return new AttachmentOperations(serverUrl, wikiName, spaceName, pageName,this); + } + + public TagOperations tagOperations(String wikiName, String spaceName, String pageName){ + return new TagOperations(serverUrl,wikiName,spaceName,pageName,this); + } + + + + public void shutDown() { + client.getConnectionManager().shutdown(); + } + + + /** + * initializeHttpClient the general property fields + */ + private void initializeHttpClient() { + client = new DefaultHttpClient(); + //set http params + HttpParams params = new BasicHttpParams(); + HttpConnectionParams.setConnectionTimeout(params, 10000);// try 10 seconds to get a socket connection + HttpConnectionParams.setSoTimeout(params, 20000);// wait 30 seconds waiting for data. then break connection. + client.setParams(params); + } + + /** + * set user credentials with manually developed preemtive Auth + */ + private void setCredentials() { + // Setting preemtiveAuth manually since org.apache.http does not support it + HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { + + public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { + AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); + CredentialsProvider credsProvider = + (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER); + HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); + + if (authState.getAuthScheme() == null) { + AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); + Credentials creds = credsProvider.getCredentials(authScope); + + if (creds != null) { + authState.setAuthScheme(new BasicScheme()); + authState.setCredentials(creds); + } + } + } + + }; + client.addRequestInterceptor(preemptiveAuth, 0); + Credentials defaultcreds = new UsernamePasswordCredentials(username, password); + client.getCredentialsProvider().setCredentials(new AuthScope(null, -1, AuthScope.ANY_REALM), defaultcreds); + + } + + private void validate(int code) throws RestException { + if (400 <= code & code < 500) { + throw new RestException(code); + } else if (500 <= code & code < 600) { + throw new RuntimeException("server error : bad request. code:" + code); + } + } + + +} + + + + + + + + + diff --git a/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/RestException.java b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/RestException.java new file mode 100644 index 0000000..222cd61 --- /dev/null +++ b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/RestException.java @@ -0,0 +1,69 @@ +package org.xwiki.android.test.utils.xmlrpc; +/** + * + * codes are equivelent to http response codes. + * + */ +public class RestException extends Exception +{ + + int code; +// public RestException() +// { +// super(); +// // TODO Auto-generated constructor stub +// } +// +// public RestException(String detailMessage, Throwable throwable) +// { +// super(detailMessage, throwable); +// // TODO Auto-generated constructor stub +// } +// +// public RestException(String detailMessage) +// { +// super(detailMessage); +// // TODO Auto-generated constructor stub +// } +// +// public RestException(Throwable throwable) +// { +// super(throwable); +// // TODO Auto-generated constructor stub +// } + + public RestException(int code) + { + super(); + this.code=code; + } + + public RestException(int code, String detailMessage, Throwable throwable) + { + super(detailMessage, throwable); + this.code=code; + } + + public RestException(int code, String detailMessage) + { + super(detailMessage); + this.code=code; + } + + public RestException(int code, Throwable throwable) + { + super(throwable); + this.code=code; + } + + + public int getCode() + { + return code; + } + + @Override + public String getMessage() { + return "code: "+code; //To change body of overridden methods use File | Settings | File Templates. + } +} diff --git a/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/SpaceOperations.java b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/SpaceOperations.java new file mode 100644 index 0000000..dc2e699 --- /dev/null +++ b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/SpaceOperations.java @@ -0,0 +1,129 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.xwiki.android.test.utils.xmlrpc; + +import org.apache.http.util.EntityUtils; +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; +import org.xwiki.android.resources.Spaces; + +import java.io.IOException; +import java.io.StringWriter; + +/** + * XWiki Android REST Space related source. Can get the spaces details/list as a Space(s) objects of Simple-xml object + * model + */ +public class SpaceOperations +{ + /** + * Path provided from XWiki RESTful API + */ + private final String SPACE_URL_PREFIX = "/xwiki/rest/wikis/"; + + /** + * Path provided from XWiki RESTful API for spaces + */ + private final String SPACE_URL_SUFFIX = "/spaces"; + + /** + * URL of the XWiki domain + */ + private String uRLprefix; + + /** + * Name of Wiki for acquiring pages + */ + private String wikiName; + private RestClient rpc; + + + /** + * @param URLprefix XWiki URl ex:"www.xwiki.org" + * @param wikiName name of the wiki in UTF-8 format + * @param rpc + */ + public SpaceOperations(String URLprefix, String wikiName, RestClient rpc) + { + this.uRLprefix = URLprefix; + this.wikiName = wikiName; + this.rpc=rpc; + } + + /** + * @return list of spaces in the specified Wiki + * @throws IOException + * @throws RestException + */ + Spaces getSpaces() throws IOException, RestException + { + + String Uri = "http://" + uRLprefix + SPACE_URL_PREFIX + wikiName + SPACE_URL_SUFFIX; + + return buildSpaces(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + + } + + /** + * Parse xml into a Spaces object + * + * @param content XML content + * @return Spaces object deserialized from the xml content + */ + private Spaces buildSpaces(String content) + { + Spaces spaces = new Spaces(); + + Serializer serializer = new Persister(); + + try { + spaces = serializer.read(Spaces.class, content); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return spaces; + } + + /** + * Generate XML content from the Spaces object + * + * @param spaces Spaces object to be serialized into XML + * @return XML content of the provided Spaces object + */ + private String buildXmlSpaces(Spaces spaces) + { + Serializer serializer = new Persister(); + + StringWriter result = new StringWriter(); + + try { + serializer.write(spaces, result); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return result.toString(); + } + +} diff --git a/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/TagOperations.java b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/TagOperations.java new file mode 100644 index 0000000..a71e538 --- /dev/null +++ b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/TagOperations.java @@ -0,0 +1,221 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.xwiki.android.test.utils.xmlrpc; + +import org.apache.http.util.EntityUtils; +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; +import org.xwiki.android.resources.Tag; +import org.xwiki.android.resources.Tags; + +import java.io.IOException; +import java.io.StringWriter; + +public class TagOperations +{ + /** + * Path provided from XWiki RESTful API + */ + private final String PAGE_URL_PREFIX = "/xwiki/rest/wikis/"; + + /** + * URL of the XWiki domain + */ + private String URLprefix; + + /** + * Name of Wiki for acquiring tags + */ + private String wikiName; + + /** + * Name of space for acquiring tags + */ + private String spaceName; + + /** + * Name of page for acquiring tags + */ + private String pageName; + + /** + * Type of the tag. + *

+ * 0= Wiki tag 1=Page tag + */ + private int tagType; + private RestClient rpc; + + /** + * @param URLprefix XWiki URl ex:"www.xwiki.org" + * @param wikiName name of the wiki in UTF-8 format + */ + TagOperations(String URLprefix, String wikiName, RestClient rpc) + { + this.URLprefix = URLprefix; + this.wikiName = wikiName; + tagType = 0; + this.rpc=rpc; + } + + /** + * @param URLprefix XWiki URl ex:"www.xwiki.org" + * @param wikiName name of the wiki in UTF-8 format + * @param spaceName name of the space in UTF-8 format + * @param pageName name of the page in UTF-8 format + */ + TagOperations(String URLprefix, String wikiName, String spaceName, String pageName, RestClient rpc) + { + this.rpc=rpc; + this.URLprefix = URLprefix; + this.wikiName = wikiName; + this.spaceName = spaceName; + this.pageName = pageName; + tagType = 1; + } + + /** + * Gets all the tags either from wiki or from the page. If only the URL & Wiki name fields are provided then method + * will return wiki tags and if space name and page name is provided it will return page tags + * + * @return list of all tags as a Tags object + * @throws IOException + * @throws RestException + */ + public Tags getTags() throws IOException, RestException + { + + String Uri; + + if (tagType == 0) { + Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/tags/"; + } else if (tagType == 1) { + Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/tags"; + } else { + Uri = ""; + } + + return buildTags(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + /** + * Adds tag to the wiki or the page depending on the fields provided in the constructor + * + * @param tag Tag object to be added + * @return statsu of the HTTP put request + * @throws IOException + * @throws RestException + */ + public String addTag(Tag tag) throws IOException, RestException + { + + String Uri = null; + if (tagType == 0) { + + Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/tags"; + String response = EntityUtils.toString(rpc.getRequest(Uri).getEntity()); + Tags temp_tags; + + temp_tags = buildTags(response); + + temp_tags.getTags().add(tag); + + return rpc.putRequest(Uri, buildXml(temp_tags)).getStatusLine().toString(); + + } else if (tagType == 1) { + Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/tags"; + String response = EntityUtils.toString(rpc.getRequest(Uri).getEntity()); + Tags temp_tags; + + temp_tags = buildTags(response); + + temp_tags.getTags().add(tag); + + return rpc.putRequest(Uri, buildXml(temp_tags)).getStatusLine().toString(); + } + throw new IllegalStateException("tagType should be =0 or 1"); + } + + public String setTags(Tags tags)throws IOException, RestException + { + String Uri = null; + + if (tagType == 0) { + + Uri = "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/tags"; + + return rpc.putRequest(Uri, buildXml(tags)).getStatusLine().toString(); + + } else { + Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/tags"; + return rpc.putRequest(Uri, buildXml(tags)).getStatusLine().toString(); + } + + } + + /** + * Parse xml into a Tags object + * + * @param content XML content + * @return Tags object deserialized from the xml content + */ + private Tags buildTags(String content) + { + Tags tags = new Tags(); + + Serializer serializer = new Persister(); + + try { + tags = serializer.read(Tags.class, content); + } catch (Exception e) { + e.printStackTrace(); + } + + return tags; + } + + /** + * Generate XML content from the Tags object + * + * @param tags Tags object to be serialized into XML + * @return XML content of the provided Tags object + */ + private String buildXml(Tags tags) + { + Serializer serializer = new Persister(); + + StringWriter result = new StringWriter(); + + try { + serializer.write(tags, result); + } catch (Exception e) { + e.printStackTrace(); + } + + return result.toString(); + } +} diff --git a/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/TranslationOperations.java b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/TranslationOperations.java new file mode 100644 index 0000000..5547d68 --- /dev/null +++ b/xwiki-android-test-fixture-setup/src/org/xwiki/android/test/utils/xmlrpc/TranslationOperations.java @@ -0,0 +1,137 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.xwiki.android.test.utils.xmlrpc; + +import org.apache.http.util.EntityUtils; +import org.simpleframework.xml.Serializer; +import org.simpleframework.xml.core.Persister; +import org.xwiki.android.resources.Translations; + +import java.io.IOException; +import java.io.StringWriter; + +/** + * XWiki Android REST Translation related source. Can get the translation details/list as a Translations objects of + * Simple-xml object model. Translated pages will not requested through this class and it will provide from + * PageResources class + */ +public class TranslationOperations +{ + /** + * Path provided from XWiki RESTful API + */ + private final String PAGE_URL_PREFIX = "/xwiki/rest/wikis/"; + + /** + * URL of the XWiki domain + */ + private String URLprefix; + + /** + * Name of Wiki for acquiring translations + */ + private String wikiName; + + /** + * Name of space for acquiring translations + */ + private String spaceName; + + /** + * Name of page for acquiring translations + */ + private String pageName; + private RestClient rpc; + + /** + * @param URLprefix XWiki URl ex:"www.xwiki.org" + * @param wikiName name of the Wiki in UTF-8 format + * @param spaceName name of the space in UTF-8 format + * @param pageName name of the page in UTF-8 format + */ + TranslationOperations(String URLprefix, String wikiName, String spaceName, String pageName , RestClient rpc) + { + this.URLprefix = URLprefix; + this.wikiName = wikiName; + this.spaceName = spaceName; + this.pageName = pageName; + this.rpc=rpc; + } + + /** + * @return list of all the translations are included in Translations object + * @throws IOException + * @throws RestException + */ + public Translations getAllTranslations() throws IOException, RestException + { + + String Uri = + "http://" + URLprefix + PAGE_URL_PREFIX + wikiName + "/spaces/" + spaceName + "/pages/" + pageName + + "/translations"; + + return buildTranslations(EntityUtils.toString(rpc.getRequest(Uri).getEntity())); + } + + /** + * Parse xml into a Translations object + * + * @param content XML content + * @return Translations object deserialized from the xml content + */ + private Translations buildTranslations(String content) + { + Translations translations = new Translations(); + + Serializer serializer = new Persister(); + + try { + translations = serializer.read(Translations.class, content); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return translations; + } + + /** + * Generate XML content from the Translations object + * + * @param translations Translations object to be serialized into XML + * @return XML content of the provided Translations object + */ + private String buildXmlTranslations(Translations translations) + { + Serializer serializer = new Persister(); + + StringWriter result = new StringWriter(); + + try { + serializer.write(translations, result); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return result.toString(); + } +} diff --git a/xwiki-android-test-fixture-setup/src/org/xwiki/test/integration/utils/AntBuildListener.java b/xwiki-android-test-fixture-setup/src/org/xwiki/test/integration/utils/AntBuildListener.java new file mode 100644 index 0000000..1e88512 --- /dev/null +++ b/xwiki-android-test-fixture-setup/src/org/xwiki/test/integration/utils/AntBuildListener.java @@ -0,0 +1,85 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.test.integration.utils; + +import org.apache.tools.ant.BuildEvent; +import org.apache.tools.ant.BuildListener; +import org.apache.tools.ant.Project; + +/** + * Allow logging Ant messages to the console. This is used by the {@link org.xwiki.test.integration.XWikiTestSetup} + * class which uses Ant tasks to start/stop XWiki. + */ +public class AntBuildListener implements BuildListener +{ + private boolean isDebugModeOn; + + /** + * @param isDebugModeOn if true then display debug messages too on the console + */ + public AntBuildListener(boolean isDebugModeOn) + { + this.isDebugModeOn = isDebugModeOn; + } + + @Override + public void buildStarted(BuildEvent event) + { + // Voluntarily do nothing + } + + @Override + public void buildFinished(BuildEvent event) + { + // Voluntarily do nothing + } + + @Override + public void targetStarted(BuildEvent event) + { + // Voluntarily do nothing + } + + @Override + public void targetFinished(BuildEvent event) + { + // Voluntarily do nothing + } + + @Override + public void taskStarted(BuildEvent event) + { + // Voluntarily do nothing + } + + @Override + public void taskFinished(BuildEvent event) + { + // Voluntarily do nothing + } + + @Override + public void messageLogged(BuildEvent event) + { + if ((event.getPriority() != Project.MSG_DEBUG) || isDebugModeOn) { + System.out.println(event.getMessage()); + } + } +} \ No newline at end of file diff --git a/xwiki-android-test-fixture-setup/src/org/xwiki/test/integration/utils/XWikiExecutor.java b/xwiki-android-test-fixture-setup/src/org/xwiki/test/integration/utils/XWikiExecutor.java new file mode 100644 index 0000000..119176d --- /dev/null +++ b/xwiki-android-test-fixture-setup/src/org/xwiki/test/integration/utils/XWikiExecutor.java @@ -0,0 +1,498 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.test.integration.utils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.params.HttpMethodParams; +import org.apache.commons.lang3.SystemUtils; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; +import org.apache.tools.ant.taskdefs.ExecTask; +import org.apache.tools.ant.types.Commandline; +import org.apache.tools.ant.types.Environment; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Start and stop a xwiki instance. + * + * @version $Id$ + * @since 2.0RC1 + */ +public class XWikiExecutor +{ + protected static final Logger LOGGER = LoggerFactory.getLogger(XWikiExecutor.class); + + public static final String SKIP_STARTING_XWIKI_INSTANCE = System.getProperty("xwiki.test.skipStart", "false"); + + public static final String BASEDIR = System.getProperty("basedir"); + + public static final String URL = System.getProperty("xwiki.test.baseURL", "http://localhost"); + + public static final String DEFAULT_PORT = System.getProperty("xwikiPort", "8080"); + + public static final String DEFAULT_STOPPORT = System.getProperty("xwikiStopPort", "8079"); + + public static final String DEFAULT_RMIPORT = System.getProperty("rmiPort", "9010"); + + private static final String DEFAULT_EXECUTION_DIRECTORY = System.getProperty("xwikiExecutionDirectory"); + + private static final String START_COMMAND = System.getProperty("xwikiExecutionStartCommand"); + + private static final String STOP_COMMAND = System.getProperty("xwikiExecutionStopCommand"); + + private static final boolean DEBUG = System.getProperty("debug", "false").equalsIgnoreCase("true"); + + private static final String WEBINF_PATH = "/webapps/xwiki/WEB-INF"; + + private static final String XWIKICFG_PATH = WEBINF_PATH + "/xwiki.cfg"; + + private static final String XWIKIPROPERTIES_PATH = WEBINF_PATH + "/xwiki.properties"; + + private static final int TIMEOUT_SECONDS = 120; + + private Project project; + + private int port; + + private int stopPort; + + private int rmiPort; + + private String executionDirectory; + + private List env = new ArrayList(); + + private String opts; + + /** + * Was XWiki server already started. We don't try to stop it if it was already started. + */ + private boolean wasStarted; + + private class Response + { + public boolean timedOut; + + public byte[] responseBody; + + public int responseCode; + } + + public XWikiExecutor(int index) + { + this.project = new Project(); + this.project.init(); + this.project.addBuildListener(new AntBuildListener(DEBUG)); + + // resolve ports + String portString = System.getProperty("xwikiPort" + index); + this.port = portString != null ? Integer.valueOf(portString) : (Integer.valueOf(DEFAULT_PORT) + index); + String stopPortString = System.getProperty("xwikiStopPort" + index); + this.stopPort = + stopPortString != null ? Integer.valueOf(stopPortString) : (Integer.valueOf(DEFAULT_STOPPORT) - index); + String rmiPortString = System.getProperty("rmiPort" + index); + this.rmiPort = + rmiPortString != null ? Integer.valueOf(rmiPortString) : (Integer.valueOf(DEFAULT_RMIPORT) + index); + + // resolve execution directory + this.executionDirectory = System.getProperty("xwikiExecutionDirectory"); + if (this.executionDirectory == null) { + this.executionDirectory = DEFAULT_EXECUTION_DIRECTORY; + if (this.executionDirectory == null) { + this.executionDirectory = BASEDIR + "/target/xwiki"; + } + if (index > 0) { + this.executionDirectory += "-" + index; + } + } + } + + public int getPort() + { + return this.port; + } + + public int getStopPort() + { + return this.stopPort; + } + + public int getRMIPort() + { + return this.rmiPort; + } + + public String getExecutionDirectory() + { + if (this.executionDirectory == null) { + throw new RuntimeException("Invalid configuration for the execution directory. The " + + "[xwikiExecutionDirectory] system property must be specified."); + } + return this.executionDirectory; + } + + public void addEnvironmentVariable(String key, String value) + { + Environment.Variable variable = new Environment.Variable(); + + variable.setKey(key); + variable.setValue(value); + + this.env.add(variable); + } + + public void setOpts(String opts) + { + this.opts = opts; + } + + public void start() throws Exception + { + if (SKIP_STARTING_XWIKI_INSTANCE.equals("true")){ + System.out.println(String.format("Using running instance at [%s:%s]", URL, getPort())); + } + else { + System.out.println(String.format("Starting XWiki server at [%s:%s]", URL, getPort())); + // First, verify if XWiki is started. If it is then don't start it again. + this.wasStarted = !isXWikiStarted(getURL(), 15).timedOut; + if (!this.wasStarted) { + startXWikiInSeparateThread(); + waitForXWikiToLoad(); + } else { + System.out.println("XWiki server is already started!"); + } + } + } + + private void startXWikiInSeparateThread() + { + Thread startThread = new Thread(new Runnable() + { + @Override + public void run() + { + try { + startXWiki(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + }); + startThread.start(); + } + + private void startXWiki() throws Exception + { + File dir = new File(getExecutionDirectory()); + if (dir.exists()) { + ExecTask execTask = (ExecTask) this.project.createTask("exec"); + execTask.setDir(new File(getExecutionDirectory())); + for (Environment.Variable variable : this.env) { + execTask.addEnv(variable); + } + + if (this.opts != null) { + Environment.Variable optsVariable = new Environment.Variable(); + optsVariable.setKey("XWIKI_OPTS"); + optsVariable.setValue(this.opts); + execTask.addEnv(optsVariable); + } + + String startCommand = getDefaultStartCommand(getPort(), getStopPort(), getRMIPort()); + System.out.println("start command: "+startCommand); + Commandline commandLine = new Commandline(startCommand); + execTask.setCommand(commandLine); + + execTask.execute(); + } else { + throw new Exception("Invalid directory from where to start XWiki [" + this.executionDirectory + "]"); + } + } + + private Task createStopTask() throws Exception + { + ExecTask execTask; + File dir = new File(getExecutionDirectory()); + if (dir.exists()) { + execTask = (ExecTask) this.project.createTask("exec"); + execTask.setDir(new File(getExecutionDirectory())); + + String stopCommand = getDefaultStopCommand(getStopPort()); + Commandline commandLine = new Commandline(stopCommand); + execTask.setCommand(commandLine); + } else { + throw new Exception("Invalid directory from where to stop XWiki [" + this.executionDirectory + "]"); + } + + return execTask; + } + + private void waitForXWikiToLoad() throws Exception + { + + System.out.println("sleeping 3 sec allowing xwiki to start"); + try{ + Thread.sleep(3000); + }catch (InterruptedException e){ + } + // Wait till the main page becomes available which means the server is started fine + System.out.println("Checking that XWiki is up and running..."); + + Response response = isXWikiStarted(getURL(), TIMEOUT_SECONDS); + if (response.timedOut) { + String message = + "Failed to start XWiki in [" + TIMEOUT_SECONDS + "] seconds, last error code [" + response.responseCode + + ", message [" + new String(response.responseBody) + "]"+" url tried was: "+ getURL(); + System.out.println(message); + stop(); + throw new RuntimeException(message); + } else { + System.out.println("Server is answering to [" + getURL() + "]... cool"); + } + } + + public Response isXWikiStarted(String url, int timeout) throws Exception + { + HttpClient client = new HttpClient(); + + boolean connected = false; + long startTime = System.currentTimeMillis(); + Response response = new Response(); + response.timedOut = false; + response.responseCode = -1; + response.responseBody = new byte[0]; + while (!connected && !response.timedOut) { + GetMethod method = new GetMethod(url); + + // Don't retry automatically since we're doing that in the algorithm below + method.getParams() + .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); + // Set a socket timeout to ensure the server has no chance of not answering to our request... + method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(10000)); + + try { + // Execute the method. + response.responseCode = client.executeMethod(method); + + // We must always read the response body. + response.responseBody = method.getResponseBody(); + + if (DEBUG) { + System.out.println(String.format("Result of pinging [%s] = [%s], Message = [%s]", url, + response.responseCode, new String(response.responseBody))); + } + + // check the http response code is either not an error, either "unauthorized" + // (which is the case for products that deny view for guest, for example). + connected = (response.responseCode < 400 || response.responseCode == 401); + } catch (Exception e) { + // Do nothing as it simply means the server is not ready yet... + } finally { + // Release the connection. + method.releaseConnection(); + } + Thread.sleep(500L); + response.timedOut = (System.currentTimeMillis() - startTime > timeout * 1000L); + } + return response; + } + + public void stop() throws Exception + { + // Stop XWiki if it was started by start() + if (!this.wasStarted) { + createStopTask().execute(); + } + System.out.println("XWiki server stopped"); + } + + public String getWebInfDirectory() + { + return getExecutionDirectory() + WEBINF_PATH; + } + + public String getXWikiCfgPath() + { + return getExecutionDirectory() + XWIKICFG_PATH; + } + + public String getXWikiPropertiesPath() + { + return getExecutionDirectory() + XWIKIPROPERTIES_PATH; + } + + public Properties loadXWikiCfg() throws Exception + { + return getProperties(getXWikiCfgPath()); + } + + public Properties loadXWikiProperties() throws Exception + { + return getProperties(getXWikiPropertiesPath()); + } + + public PropertiesConfiguration loadXWikiPropertiesConfiguration() throws Exception + { + return getPropertiesConfiguration(getXWikiPropertiesPath()); + } + + /** + * @deprecated since 4.2M1 use {@link #getPropertiesConfiguration(String)} instead + */ + @Deprecated + private Properties getProperties(String path) throws Exception + { + Properties properties = new Properties(); + + FileInputStream fis; + try { + fis = new FileInputStream(path); + + try { + properties.load(fis); + } finally { + fis.close(); + } + } catch (FileNotFoundException e) { + LOGGER.debug("Failed to load properties [" + path + "]", e); + } + + return properties; + } + + /** + * @since 4.2M1 + */ + private PropertiesConfiguration getPropertiesConfiguration(String path) throws Exception + { + PropertiesConfiguration properties = new PropertiesConfiguration(); + + FileInputStream fis; + try { + fis = new FileInputStream(path); + + try { + properties.load(fis); + } finally { + fis.close(); + } + } catch (FileNotFoundException e) { + LOGGER.debug("Failed to load properties [" + path + "]", e); + } + + return properties; + } + + public void saveXWikiCfg(Properties properties) throws Exception + { + saveProperties(getXWikiCfgPath(), properties); + } + + /** + * @deprecated since 4.2M1 use {@link #saveXWikiProperties(PropertiesConfiguration)} instead + */ + @Deprecated + public void saveXWikiProperties(Properties properties) throws Exception + { + saveProperties(getXWikiPropertiesPath(), properties); + } + + /** + * @since 4.2M1 + */ + public void saveXWikiProperties(PropertiesConfiguration properties) throws Exception + { + savePropertiesConfiguration(getXWikiPropertiesPath(), properties); + } + + private void saveProperties(String path, Properties properties) throws Exception + { + FileOutputStream fos = new FileOutputStream(path); + try { + properties.store(fos, null); + } finally { + fos.close(); + } + } + + private void savePropertiesConfiguration(String path, PropertiesConfiguration properties) throws Exception + { + FileOutputStream fos = new FileOutputStream(path); + try { + properties.save(fos); + } finally { + fos.close(); + } + } + + private String getURL() + { + // We use "xpage=plain" for 2 reasons: + // 1) the page loads faster since it doesn't need to display the skin + // 2) if the page doesn't exist it won't return a 404 HTTP Response code + return URL + ":" + getPort() + "/"; + } + + private String getDefaultStartCommand(int port, int stopPort, int rmiPort) + { + String startCommand = START_COMMAND; + if (startCommand == null) { + if (SystemUtils.IS_OS_WINDOWS) { + startCommand = String.format("cmd /c start_xwiki.bat %s %s", port, stopPort); + } else { + startCommand = String.format("sh -f start_xwiki.sh %s %s", port, stopPort); + } + } else { + startCommand = startCommand.replaceFirst(DEFAULT_PORT, String.valueOf(port)); + startCommand = startCommand.replaceFirst(DEFAULT_STOPPORT, String.valueOf(stopPort)); + startCommand = startCommand.replaceFirst(DEFAULT_RMIPORT, String.valueOf(rmiPort)); + } + + return startCommand; + } + + private String getDefaultStopCommand(int stopPort) + { + String stopCommand = STOP_COMMAND; + if (stopCommand == null) { + if (SystemUtils.IS_OS_WINDOWS) { + stopCommand = String.format("cmd /c stop_xwiki.bat %s", stopPort); + } else { + stopCommand = String.format("sh -f stop_xwiki.sh %s", stopPort); + } + } else { + stopCommand = stopCommand.replaceFirst(DEFAULT_STOPPORT, String.valueOf(stopPort)); + } + + return stopCommand; + } +} \ No newline at end of file diff --git a/xwiki-android-test-fixture-setup/test/org/xwiki/android/test/fixture/setup/AllTests.java b/xwiki-android-test-fixture-setup/test/org/xwiki/android/test/fixture/setup/AllTests.java new file mode 100644 index 0000000..3da2f40 --- /dev/null +++ b/xwiki-android-test-fixture-setup/test/org/xwiki/android/test/fixture/setup/AllTests.java @@ -0,0 +1,20 @@ +package org.xwiki.android.test.fixture.setup; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * Created with IntelliJ IDEA. + * User: admin + * Date: 8/11/12 + * Time: 9:59 PM + * To change this template use File | Settings | File Templates. + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + ServerStartTest.class, + SetupTestPageTest.class + }) +public class AllTests { + +} diff --git a/xwiki-android-test-fixture-setup/test/org/xwiki/android/test/fixture/setup/ServerStartTest.java b/xwiki-android-test-fixture-setup/test/org/xwiki/android/test/fixture/setup/ServerStartTest.java new file mode 100644 index 0000000..7e7b375 --- /dev/null +++ b/xwiki-android-test-fixture-setup/test/org/xwiki/android/test/fixture/setup/ServerStartTest.java @@ -0,0 +1,44 @@ +package org.xwiki.android.test.fixture.setup; + + +import junit.framework.AssertionFailedError; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.xwiki.android.test.fixture.setup.env.TestEnv; +import org.xwiki.test.integration.utils.XWikiExecutor; + +public class ServerStartTest extends Assert { + XWikiExecutor exec; + + boolean failed; + @Before + public void setup(){ + + exec=new XWikiExecutor(TestEnv.SERVER_INDEX); + try { + exec.start(); + } catch (Exception e) { + throw new AssertionFailedError(e.getMessage()); + } + } + + @Test + public void setupProperly(){ + try { + exec.isXWikiStarted(TestEnv.SCHEME + TestEnv.SERVER_URL, 10000); + } catch (Exception e) { + // failed=true; + throw new AssertionFailedError(e.getMessage()); + } + } + + @After + public void cleanup() throws Throwable{ + if(failed){ + exec.stop();//error if cant stop + } + } + +} diff --git a/xwiki-android-test-fixture-setup/test/org/xwiki/android/test/fixture/setup/SetupTestPageTest.java b/xwiki-android-test-fixture-setup/test/org/xwiki/android/test/fixture/setup/SetupTestPageTest.java new file mode 100644 index 0000000..9fee65c --- /dev/null +++ b/xwiki-android-test-fixture-setup/test/org/xwiki/android/test/fixture/setup/SetupTestPageTest.java @@ -0,0 +1,178 @@ +package org.xwiki.android.test.fixture.setup; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xwiki.android.resources.*; +import org.xwiki.android.resources.Object; +import org.xwiki.android.test.fixture.setup.env.TestEnv; +import org.xwiki.android.test.utils.xmlrpc.*; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.ArrayList; + +/** + * Created with IntelliJ IDEA. + * User: admin + * Date: 8/11/12 + * Time: 7:38 PM + * To change this template use File | Settings | File Templates. + */ +public class SetupTestPageTest extends Assert { + Logger logger = LoggerFactory.getLogger(SetupTestPageTest.class); + boolean failed; + + String serverUrl, username, password, wikiName, spaceName, pageName, objecteClass, objectPropertyName, objectPropertyValue, + commentText, commentReplyText, tagWord, attachementName, attachmentDelName; + int commentReplyReplyTo; + RestClient rc; + + @Before + public void setup() { + serverUrl = TestEnv.SERVER_URL; + username = TestEnv.USERNAME; + password = TestEnv.PASSWORD; + + rc = new RestClient(serverUrl, username, password); + + wikiName = TestEnv.WIKI_NAME; + spaceName = TestEnv.SPACE_NAME; + pageName = TestEnv.PAGE_NAME; + objecteClass = TestEnv.CLASS_NAME; + objectPropertyName = TestEnv.OBJECT_PROPERTY_NAME; + objectPropertyValue = TestEnv.OBJECT_PROPERTY_VALUE; + commentText = TestEnv.COMMENT_TEXT; + commentReplyText = TestEnv.COMMENT_REPLY_TEXT; + commentReplyReplyTo = TestEnv.COMMENT_REPLY_REPLY_TO; + tagWord = TestEnv.TAG_WORD; + attachementName=TestEnv.ATTACHMENT_NAME; + attachmentDelName=TestEnv.ATTACHMENT_DEL_NAME; + + logger.debug("setting up the external fixture at server: " + serverUrl + " wiki:" + wikiName + " spaces: [ " + spaceName + " ]"); + } + + @Test + public void setupPage() throws RestException, IOException { + //delete page if exists. We delete it here because after the tests are over u can manually see the state on the server by starting it again. + PageOperations pageOps = rc.pageOperations(wikiName, spaceName); + if (pageOps.exists(pageName)) { + pageOps.deletePage(pageName); + } + ; + //add page + + Page p = new Page(); + p.setTitle(pageName); + p.setName(pageName); + p.setContent("Hi All! Happy Testing!"); + pageOps.addPage(p); + + //add obj + org.xwiki.android.resources.Object obj = new Object(); + Property prop = new Property(); + prop.setName(objectPropertyName); + prop.setValue(objectPropertyValue); + prop.attributes = new ArrayList(); //without attributes list added simplexml will throw exception. + obj.withClassName(objecteClass).withProperties(prop); + + ObjectOperations objOps = rc.objectOperations(wikiName, spaceName, pageName); + objOps.addObject(obj); + + //add comment + Comment c0 = new Comment(); + c0.setText(commentText); + + Comment c1 = new Comment(); + c1.setReplyTo(commentReplyReplyTo); + c1.setText(commentReplyText); + + CommentOperations cmntOps = rc.commentOperations(wikiName, spaceName, pageName); + cmntOps.addPageComment(c0); + cmntOps.addPageComment(c1); + + //edit comment c0 + //we have to use object with class XWiki.Comment + Object cobj=new Object(); + cobj.withClassName("XWiki.XWikiComments"); + Property cp=new Property(); + cp.setName("comment"); + cp.setValue("hi edited"); + cp.attributes=new ArrayList();//simple xml does not check for null + cobj.withProperties(cp); + objOps.updateObject("XWiki.XWikiComments",0,cobj); + //add tag + + Tag t = new Tag(); + t.setName(tagWord); + Tags ts = new Tags(); + ts.withTags(t); + TagOperations tagOps = rc.tagOperations(wikiName, spaceName, pageName); + tagOps.setTags(ts); + + //add attachment + AttachmentOperations attachmentOperations = rc.attachmentOperations(wikiName, spaceName, pageName); + URL url = ClassLoader.getSystemResource(attachementName); + URL urldel = ClassLoader.getSystemResource(attachementName); + logger.debug("Attachment path: " + url); + File f = null; + File fdel=null; + try { + f = new File(url.toURI()); + fdel=new File(urldel.toURI()); + } catch (URISyntaxException e) { + logger.error("cannot create file for attachment:", e); + } + attachmentOperations.putPageAttachment(attachementName, f); + attachmentOperations.putPageAttachment(attachementName, f);//add it again + attachmentOperations.putPageAttachment(attachmentDelName,fdel); + + + + //page translations. + Page pfr=new Page(); + pfr.setName(pageName); + pfr.setTitle(pageName); + pfr.setContent("Bonzua!"); + pageOps.addPageTranslation(pfr,"fr"); + pfr.setContent("Bonzua! Happy Testing in French"); + pageOps.addPageTranslation(pfr, "fr"); + + + + } + + @After + public void cleanup() throws Throwable { + rc.shutDown(); + } + + /// + /* + public static final String COMMENT_ID = "0"; + + public static final String LANGUAGE = "null"; + + public static final String CLASS_NAME = "Blog.BlogPostClass"; + + public static final String OBJECT_NUMBER = "0"; + + public static final String OBJECT_PROPERTY_NAME = "content"; + + public static final String SEARCH_KEWORD = "test"; + + public static final String TAG_WORD = "testTag"; + + public static final String ATTACHMENT_NAME = "a.png"; + + public static final String ATTACHMENT_PATH = "./"; + + public static final String ATTACHMENT_VERSION = "1.0"; + */ + +} diff --git a/xwiki-android-test-fixture-setup/test/org/xwiki/android/test/fixture/setup/env/TestEnv.java b/xwiki-android-test-fixture-setup/test/org/xwiki/android/test/fixture/setup/env/TestEnv.java new file mode 100644 index 0000000..1868bf0 --- /dev/null +++ b/xwiki-android-test-fixture-setup/test/org/xwiki/android/test/fixture/setup/env/TestEnv.java @@ -0,0 +1,72 @@ +package org.xwiki.android.test.fixture.setup.env; + +/** + * Environment variables for tests. + * How to setup external text setup. + */ + +public class TestEnv { + + public static final String DEFAULT_EXECUTION_DIRECTORY="D:\\Xwiki-server"; //machine dependant. If prop not set in pom specify here. ex: for IDE builds + public static final int SERVER_INDEX; + public static final String HOST = "localhost"; + 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. + + 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 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_REPLY_TEXT = "huy"; + + + public static final String LANGUAGE = "fr"; + public static final String TRANSLATION_VERSION="2.1"; + + public static final String CLASS_NAME = "Blog.BlogPostClass"; + public static final String OBJECT_NUMBER = "0"; + public static final String OBJECT_PROPERTY_NAME = "content"; + public static final String OBJECT_PROPERTY_VALUE = "test blog content"; + + public static final String SEARCH_KEWORD = "test"; + + public static final String TAG_WORD = "testTag"; + + public static final String ATTACHMENT_NAME = "a.png"; + public static final String ATTACHMENT_DEL_NAME = "del.png"; + 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(System.getProperty("xwikiExecutionDirectory")==null){ + System.out.println("ERROR : xwikiExecutionDirectory not set in pom. "); + if(DEFAULT_EXECUTION_DIRECTORY==null){ + throw new RuntimeException("If IDE build, set a defalut xwiki execution directory for you local machine. see TestConstants.DEFAULT_EXECUTION_DIRECTORY") ; + } + if(SERVER_INDEX>0){ + System.setProperty("xwikiExecutionDirectory", DEFAULT_EXECUTION_DIRECTORY+"-"+SERVER_INDEX);//set it up to default val above, if not set by pom.else rewrite. + }else{ + System.setProperty("xwikiExecutionDirectory", DEFAULT_EXECUTION_DIRECTORY);//set it up to default val above, if not set by pom.else rewrite. + } + } + System.out.println("xwiki execution dir:"+ System.getProperty("xwikiExecutionDirectory") +" index: "+SERVER_INDEX + " server Url: "+SERVER_URL); + } + +} diff --git a/xwiki-android-test-fixture-teardown/pom.xml b/xwiki-android-test-fixture-teardown/pom.xml new file mode 100644 index 0000000..158fb5d --- /dev/null +++ b/xwiki-android-test-fixture-teardown/pom.xml @@ -0,0 +1,76 @@ + + + + xwiki-android + org.xwiki.android + 1.0-SNAPSHOT + + 4.0.0 + + xwiki-android-test-fixture-teardown + + + + + org.slf4j + slf4j-log4j12 + 1.6.6 + + + + org.apache.ant + ant + 1.7.0 + + + + commons-httpclient + commons-httpclient + 3.1 + + + + org.apache.httpcomponents + httpclient + 4.1.2 + + + + org.apache.httpcomponents + httpcore + 4.1.2 + + + + + commons-configuration + commons-configuration + 1.8 + + + + org.apache.commons + commons-lang3 + 3.0 + + + + junit + junit + 4.10 + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + + + \ No newline at end of file diff --git a/xwiki-android-test-fixture-teardown/resources/log4j.properties b/xwiki-android-test-fixture-teardown/resources/log4j.properties new file mode 100644 index 0000000..762bbbb --- /dev/null +++ b/xwiki-android-test-fixture-teardown/resources/log4j.properties @@ -0,0 +1,7 @@ +log4j.rootLogger=DEBUG, CA +log4j.logger.org.apache.http=ERROR , CA + +log4j.appender.CA=org.apache.log4j.ConsoleAppender + +log4j.appender.CA.layout=org.apache.log4j.PatternLayout +log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n diff --git a/xwiki-android-test-fixture-teardown/src/org/xwiki/test/integration/AntBuildListener.java b/xwiki-android-test-fixture-teardown/src/org/xwiki/test/integration/AntBuildListener.java new file mode 100644 index 0000000..dd1bfa8 --- /dev/null +++ b/xwiki-android-test-fixture-teardown/src/org/xwiki/test/integration/AntBuildListener.java @@ -0,0 +1,85 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.test.integration; + +import org.apache.tools.ant.BuildEvent; +import org.apache.tools.ant.BuildListener; +import org.apache.tools.ant.Project; + +/** + * Allow logging Ant messages to the console. This is used by the {@link org.xwiki.test.integration.XWikiTestSetup} + * class which uses Ant tasks to start/stop XWiki. + */ +public class AntBuildListener implements BuildListener +{ + private boolean isDebugModeOn; + + /** + * @param isDebugModeOn if true then display debug messages too on the console + */ + public AntBuildListener(boolean isDebugModeOn) + { + this.isDebugModeOn = isDebugModeOn; + } + + @Override + public void buildStarted(BuildEvent event) + { + // Voluntarily do nothing + } + + @Override + public void buildFinished(BuildEvent event) + { + // Voluntarily do nothing + } + + @Override + public void targetStarted(BuildEvent event) + { + // Voluntarily do nothing + } + + @Override + public void targetFinished(BuildEvent event) + { + // Voluntarily do nothing + } + + @Override + public void taskStarted(BuildEvent event) + { + // Voluntarily do nothing + } + + @Override + public void taskFinished(BuildEvent event) + { + // Voluntarily do nothing + } + + @Override + public void messageLogged(BuildEvent event) + { + if ((event.getPriority() != Project.MSG_DEBUG) || isDebugModeOn) { + System.out.println(event.getMessage()); + } + } +} \ No newline at end of file diff --git a/xwiki-android-test-fixture-teardown/src/org/xwiki/test/integration/XWikiExecutor.java b/xwiki-android-test-fixture-teardown/src/org/xwiki/test/integration/XWikiExecutor.java new file mode 100644 index 0000000..cff53bd --- /dev/null +++ b/xwiki-android-test-fixture-teardown/src/org/xwiki/test/integration/XWikiExecutor.java @@ -0,0 +1,491 @@ +/* + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.xwiki.test.integration; + +import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.params.HttpMethodParams; +import org.apache.commons.lang3.SystemUtils; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; +import org.apache.tools.ant.taskdefs.ExecTask; +import org.apache.tools.ant.types.Commandline; +import org.apache.tools.ant.types.Environment; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +/** + * Start and stop a xwiki instance. + * + * @version $Id$ + * @since 2.0RC1 + */ +public class XWikiExecutor +{ + protected static final Logger LOGGER = LoggerFactory.getLogger(XWikiExecutor.class); + + public static final String SKIP_STARTING_XWIKI_INSTANCE = System.getProperty("xwiki.test.skipStart", "false"); + + public static final String BASEDIR = System.getProperty("basedir"); + + public static final String URL = System.getProperty("xwiki.test.baseURL", "http://localhost"); + + public static final String DEFAULT_PORT = System.getProperty("xwikiPort", "8080"); + + public static final String DEFAULT_STOPPORT = System.getProperty("xwikiStopPort", "8079"); + + public static final String DEFAULT_RMIPORT = System.getProperty("rmiPort", "9010"); + + private static final String DEFAULT_EXECUTION_DIRECTORY = System.getProperty("xwikiExecutionDirectory"); + + private static final String START_COMMAND = System.getProperty("xwikiExecutionStartCommand"); + + private static final String STOP_COMMAND = System.getProperty("xwikiExecutionStopCommand"); + + private static final boolean DEBUG = System.getProperty("debug", "false").equalsIgnoreCase("true"); + + private static final String WEBINF_PATH = "/webapps/xwiki/WEB-INF"; + + private static final String XWIKICFG_PATH = WEBINF_PATH + "/xwiki.cfg"; + + private static final String XWIKIPROPERTIES_PATH = WEBINF_PATH + "/xwiki.properties"; + + private static final int TIMEOUT_SECONDS = 120; + + private Project project; + + private int port; + + private int stopPort; + + private int rmiPort; + + private String executionDirectory; + + private List env = new ArrayList(); + + private String opts; + + /** + * Was XWiki server already started. We don't try to stop it if it was already started. + */ + private boolean wasStarted; + + private class Response + { + public boolean timedOut; + + public byte[] responseBody; + + public int responseCode; + } + + public XWikiExecutor(int index) + { + this.project = new Project(); + this.project.init(); + this.project.addBuildListener(new AntBuildListener(DEBUG)); + + // resolve ports + String portString = System.getProperty("xwikiPort" + index); + this.port = portString != null ? Integer.valueOf(portString) : (Integer.valueOf(DEFAULT_PORT) + index); + String stopPortString = System.getProperty("xwikiStopPort" + index); + this.stopPort = + stopPortString != null ? Integer.valueOf(stopPortString) : (Integer.valueOf(DEFAULT_STOPPORT) - index); + String rmiPortString = System.getProperty("rmiPort" + index); + this.rmiPort = + rmiPortString != null ? Integer.valueOf(rmiPortString) : (Integer.valueOf(DEFAULT_RMIPORT) + index); + + // resolve execution directory + this.executionDirectory = System.getProperty("xwikiExecutionDirectory" + index); + if (this.executionDirectory == null) { + this.executionDirectory = DEFAULT_EXECUTION_DIRECTORY; + if (this.executionDirectory == null) { + this.executionDirectory = BASEDIR + "/target/xwiki"; + } + if (index > 0) { + this.executionDirectory += "-" + index; + } + } + } + + public int getPort() + { + return this.port; + } + + public int getStopPort() + { + return this.stopPort; + } + + public int getRMIPort() + { + return this.rmiPort; + } + + public String getExecutionDirectory() + { + if (this.executionDirectory == null) { + throw new RuntimeException("Invalid configuration for the execution directory. The " + + "[xwikiExecutionDirectory] system property must be specified."); + } + return this.executionDirectory; + } + + public void addEnvironmentVariable(String key, String value) + { + Environment.Variable variable = new Environment.Variable(); + + variable.setKey(key); + variable.setValue(value); + + this.env.add(variable); + } + + public void setOpts(String opts) + { + this.opts = opts; + } + + public void start() throws Exception + { + if (SKIP_STARTING_XWIKI_INSTANCE.equals("true")){ + System.out.println(String.format("Using running instance at [%s:%s]", URL, getPort())); + } + else { + System.out.println(String.format("Starting XWiki server at [%s:%s]", URL, getPort())); + // First, verify if XWiki is started. If it is then don't start it again. + this.wasStarted = !isXWikiStarted(getURL(), 15).timedOut; + if (!this.wasStarted) { + startXWikiInSeparateThread(); + waitForXWikiToLoad(); + } else { + System.out.println("XWiki server is already started!"); + } + } + } + + private void startXWikiInSeparateThread() + { + Thread startThread = new Thread(new Runnable() + { + @Override + public void run() + { + try { + startXWiki(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + }); + startThread.start(); + } + + private void startXWiki() throws Exception + { + File dir = new File(getExecutionDirectory()); + if (dir.exists()) { + ExecTask execTask = (ExecTask) this.project.createTask("exec"); + execTask.setDir(new File(getExecutionDirectory())); + for (Environment.Variable variable : this.env) { + execTask.addEnv(variable); + } + + if (this.opts != null) { + Environment.Variable optsVariable = new Environment.Variable(); + optsVariable.setKey("XWIKI_OPTS"); + optsVariable.setValue(this.opts); + execTask.addEnv(optsVariable); + } + + String startCommand = getDefaultStartCommand(getPort(), getStopPort(), getRMIPort()); + Commandline commandLine = new Commandline(startCommand); + execTask.setCommand(commandLine); + + execTask.execute(); + } else { + throw new Exception("Invalid directory from where to start XWiki [" + this.executionDirectory + "]"); + } + } + + private Task createStopTask() throws Exception + { + ExecTask execTask; + File dir = new File(getExecutionDirectory()); + if (dir.exists()) { + execTask = (ExecTask) this.project.createTask("exec"); + execTask.setDir(new File(getExecutionDirectory())); + + String stopCommand = getDefaultStopCommand(getStopPort()); + Commandline commandLine = new Commandline(stopCommand); + execTask.setCommand(commandLine); + } else { + throw new Exception("Invalid directory from where to stop XWiki [" + this.executionDirectory + "]"); + } + + return execTask; + } + + private void waitForXWikiToLoad() throws Exception + { + // Wait till the main page becomes available which means the server is started fine + System.out.println("Checking that XWiki is up and running..."); + + Response response = isXWikiStarted(getURL(), TIMEOUT_SECONDS); + if (response.timedOut) { + String message = + "Failed to start XWiki in [" + TIMEOUT_SECONDS + "] seconds, last error code [" + response.responseCode + + ", message [" + new String(response.responseBody) + "]"; + System.out.println(message); + stop(); + throw new RuntimeException(message); + } else { + System.out.println("Server is answering to [" + getURL() + "]... cool"); + } + } + + public Response isXWikiStarted(String url, int timeout) throws Exception + { + HttpClient client = new HttpClient(); + + boolean connected = false; + long startTime = System.currentTimeMillis(); + Response response = new Response(); + response.timedOut = false; + response.responseCode = -1; + response.responseBody = new byte[0]; + while (!connected && !response.timedOut) { + GetMethod method = new GetMethod(url); + + // Don't retry automatically since we're doing that in the algorithm below + method.getParams() + .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); + // Set a socket timeout to ensure the server has no chance of not answering to our request... + method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(10000)); + + try { + // Execute the method. + response.responseCode = client.executeMethod(method); + + // We must always read the response body. + response.responseBody = method.getResponseBody(); + + if (DEBUG) { + System.out.println(String.format("Result of pinging [%s] = [%s], Message = [%s]", url, + response.responseCode, new String(response.responseBody))); + } + + // check the http response code is either not an error, either "unauthorized" + // (which is the case for products that deny view for guest, for example). + connected = (response.responseCode < 400 || response.responseCode == 401); + } catch (Exception e) { + // Do nothing as it simply means the server is not ready yet... + } finally { + // Release the connection. + method.releaseConnection(); + } + Thread.sleep(500L); + response.timedOut = (System.currentTimeMillis() - startTime > timeout * 1000L); + } + return response; + } + + public void stop() throws Exception + { + // Stop XWiki if it was started by start() + if (!this.wasStarted) { + createStopTask().execute(); + } + System.out.println("XWiki server stopped"); + } + + public String getWebInfDirectory() + { + return getExecutionDirectory() + WEBINF_PATH; + } + + public String getXWikiCfgPath() + { + return getExecutionDirectory() + XWIKICFG_PATH; + } + + public String getXWikiPropertiesPath() + { + return getExecutionDirectory() + XWIKIPROPERTIES_PATH; + } + + public Properties loadXWikiCfg() throws Exception + { + return getProperties(getXWikiCfgPath()); + } + + public Properties loadXWikiProperties() throws Exception + { + return getProperties(getXWikiPropertiesPath()); + } + + public PropertiesConfiguration loadXWikiPropertiesConfiguration() throws Exception + { + return getPropertiesConfiguration(getXWikiPropertiesPath()); + } + + /** + * @deprecated since 4.2M1 use {@link #getPropertiesConfiguration(String)} instead + */ + @Deprecated + private Properties getProperties(String path) throws Exception + { + Properties properties = new Properties(); + + FileInputStream fis; + try { + fis = new FileInputStream(path); + + try { + properties.load(fis); + } finally { + fis.close(); + } + } catch (FileNotFoundException e) { + LOGGER.debug("Failed to load properties [" + path + "]", e); + } + + return properties; + } + + /** + * @since 4.2M1 + */ + private PropertiesConfiguration getPropertiesConfiguration(String path) throws Exception + { + PropertiesConfiguration properties = new PropertiesConfiguration(); + + FileInputStream fis; + try { + fis = new FileInputStream(path); + + try { + properties.load(fis); + } finally { + fis.close(); + } + } catch (FileNotFoundException e) { + LOGGER.debug("Failed to load properties [" + path + "]", e); + } + + return properties; + } + + public void saveXWikiCfg(Properties properties) throws Exception + { + saveProperties(getXWikiCfgPath(), properties); + } + + /** + * @deprecated since 4.2M1 use {@link #saveXWikiProperties(PropertiesConfiguration)} instead + */ + @Deprecated + public void saveXWikiProperties(Properties properties) throws Exception + { + saveProperties(getXWikiPropertiesPath(), properties); + } + + /** + * @since 4.2M1 + */ + public void saveXWikiProperties(PropertiesConfiguration properties) throws Exception + { + savePropertiesConfiguration(getXWikiPropertiesPath(), properties); + } + + private void saveProperties(String path, Properties properties) throws Exception + { + FileOutputStream fos = new FileOutputStream(path); + try { + properties.store(fos, null); + } finally { + fos.close(); + } + } + + private void savePropertiesConfiguration(String path, PropertiesConfiguration properties) throws Exception + { + FileOutputStream fos = new FileOutputStream(path); + try { + properties.save(fos); + } finally { + fos.close(); + } + } + + private String getURL() + { + // We use "xpage=plain" for 2 reasons: + // 1) the page loads faster since it doesn't need to display the skin + // 2) if the page doesn't exist it won't return a 404 HTTP Response code + return URL + ":" + getPort() + "/org/xwiki/bin/view/org.xwiki.android.test.org.xwiki.test.setup.Main?xpage=plain"; + } + + private String getDefaultStartCommand(int port, int stopPort, int rmiPort) + { + String startCommand = START_COMMAND; + if (startCommand == null) { + if (SystemUtils.IS_OS_WINDOWS) { + startCommand = String.format("cmd /c start_xwiki.bat %s %s", port, stopPort); + } else { + startCommand = String.format("sh -f start_xwiki.sh %s %s", port, stopPort); + } + } else { + startCommand = startCommand.replaceFirst(DEFAULT_PORT, String.valueOf(port)); + startCommand = startCommand.replaceFirst(DEFAULT_STOPPORT, String.valueOf(stopPort)); + startCommand = startCommand.replaceFirst(DEFAULT_RMIPORT, String.valueOf(rmiPort)); + } + + return startCommand; + } + + private String getDefaultStopCommand(int stopPort) + { + String stopCommand = STOP_COMMAND; + if (stopCommand == null) { + if (SystemUtils.IS_OS_WINDOWS) { + stopCommand = String.format("cmd /c stop_xwiki.bat %s", stopPort); + } else { + stopCommand = String.format("sh -f stop_xwiki.sh %s", stopPort); + } + } else { + stopCommand = stopCommand.replaceFirst(DEFAULT_STOPPORT, String.valueOf(stopPort)); + } + + return stopCommand; + } +} \ No newline at end of file diff --git a/xwiki-android-test-fixture-teardown/test/org/xwiki/android/test/fixture/ServerShutdownTest.java b/xwiki-android-test-fixture-teardown/test/org/xwiki/android/test/fixture/ServerShutdownTest.java new file mode 100644 index 0000000..a3e34d6 --- /dev/null +++ b/xwiki-android-test-fixture-teardown/test/org/xwiki/android/test/fixture/ServerShutdownTest.java @@ -0,0 +1,53 @@ +package org.xwiki.android.test.fixture; + + +import junit.framework.AssertionFailedError; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.xwiki.android.test.fixture.teardown.env.TestEnv; +import org.xwiki.test.integration.XWikiExecutor; + +/** + * Created by IntelliJ IDEA. + * User: sasindap + * Date: 8/10/12 + * Time: 10:13 AM + * To change this template use File | Settings | File Templates. + */ + +public class ServerShutdownTest extends Assert { + Logger logger= LoggerFactory.getLogger(ServerShutdownTest.class.getSimpleName()); + + XWikiExecutor exec; + boolean failed; + + @Before + public void shutdownServer() { + + + exec = new XWikiExecutor(new Integer(System.getProperty("xwikiExecutionIndex"))); + + try { + exec.stop(); + } catch (Exception e) { + throw new AssertionFailedError(e.getMessage()); + } + } + + @Test(expected = Exception.class) + public void shutdownProperly() throws Exception { + exec.isXWikiStarted(TestEnv.SERVER_URL, 1000); + } + + @After + public void cleanup() throws Throwable { + logger.info("server shutdown. You can check out the pages , objects , etc created by the test cases by manually starting the server again." + + "\n please note that running tests on xwiki-androidt-test-fixtures-setup will clean up all above."); + } + +} diff --git a/xwiki-android-test-fixture-teardown/test/org/xwiki/android/test/fixture/teardown/env/TestEnv.java b/xwiki-android-test-fixture-teardown/test/org/xwiki/android/test/fixture/teardown/env/TestEnv.java new file mode 100644 index 0000000..c5b65ba --- /dev/null +++ b/xwiki-android-test-fixture-teardown/test/org/xwiki/android/test/fixture/teardown/env/TestEnv.java @@ -0,0 +1,61 @@ +package org.xwiki.android.test.fixture.teardown.env; + +/** + * Environment variables for tests. + * How to setup external text setup. + */ + +public class TestEnv { + + public static final String DEFAULT_EXECUTION_DIRECTORY; //see static block to change default. + public static final int SERVER_INDEX; + public static final String HOST = "localhost"; + public static final int PORT = 8080; //actual port is port+server_index + 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. + + 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.0"; + + 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_REPLY_TEXT = "huy"; + + + public static final String LANGUAGE = "null"; + + public static final String CLASS_NAME = "Blog.BlogPostClass"; + + public static final String OBJECT_NUMBER = "0"; + + public static final String OBJECT_PROPERTY_NAME = "content"; + public static final String OBJECT_PROPERTY_VALUE = "test blog content"; + + public static final String SEARCH_KEWORD = "test"; + + public static final String TAG_WORD = "testTag"; + + public static final String ATTACHMENT_NAME = "a.png"; + + public static final String ATTACHMENT_PATH = "./"; + + public static final String ATTACHMENT_VERSION = "1.0"; + + static { + SERVER_INDEX = new Integer(System.getProperty("xwikiExecutionIndex", "0")); + DEFAULT_EXECUTION_DIRECTORY = System.getProperty("xwikiExecutionDirectory", "C:\\Users\\admin\\Desktop\\xwiki-enterprise-jetty"); + SERVER_URL = HOST + ":" + (PORT + SERVER_INDEX); + + } + +} diff --git a/xwiki-android-test-rest/AndroidManifest.xml b/xwiki-android-test-rest/AndroidManifest.xml index c8b78b0..77b55f5 100644 --- a/xwiki-android-test-rest/AndroidManifest.xml +++ b/xwiki-android-test-rest/AndroidManifest.xml @@ -8,13 +8,6 @@ - - - - - - diff --git a/xwiki-android-test-rest/project.properties b/xwiki-android-test-rest/project.properties index 323b6c5..53844ab 100644 --- a/xwiki-android-test-rest/project.properties +++ b/xwiki-android-test-rest/project.properties @@ -10,6 +10,7 @@ # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt -android.library.reference.1=../xwiki-android-rest # Project target. -target=Google Inc.:Google APIs:7 +target=android-7 +android.library.reference.1=../xwiki-android-rest + diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/AttachmentTest.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/AttachmentTest.java index adefc43..f82d391 100644 --- a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/AttachmentTest.java +++ b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/AttachmentTest.java @@ -20,15 +20,18 @@ package org.xwiki.android.test.rest; +import java.io.InputStream; + import org.xwiki.android.resources.Attachments; import org.xwiki.android.rest.Requests; +import org.xwiki.android.test.rest.env.TestConstants; import android.test.AndroidTestCase; public class AttachmentTest extends AndroidTestCase { - private String wikiName, spaceName, pageName, url, username, password, version, filePath, attachmentName, - attachmentVersion; + private String wikiName, spaceName, pageName, url, username, password, pageVersion, filePath, attachmentName, + attachmentVersion,attachmentDelName; private boolean isSecured = true; @@ -38,18 +41,19 @@ public class AttachmentTest extends AndroidTestCase protected void setUp() throws Exception { super.setUp(); - wikiName = TestResources.WIKI_NAME; - spaceName = TestResources.SPACE_NAME; - pageName = TestResources.PAGE_NAME; - url = TestResources.URL; - version = TestResources.PAGE_VERSION; - attachmentName = TestResources.ATTACHMENT_NAME; - filePath = TestResources.ATTACHMENT_PATH; - attachmentVersion = TestResources.ATTACHMENT_VERSION; + wikiName = TestConstants.WIKI_NAME; + spaceName = TestConstants.SPACE_NAME; + pageName = TestConstants.PAGE_NAME; + url = TestConstants.SERVER_URL; + pageVersion = TestConstants.ATTACHMENT_PAGE_HISTORY_VERSION; + attachmentName = TestConstants.ATTACHMENT_NAME; + filePath = TestConstants.ATTACHMENT_PATH; + attachmentVersion = TestConstants.ATTACHMENT_VERSION; + attachmentDelName= TestConstants.ATTACHMENT_DEL_NAME; if (isSecured) { - username = TestResources.USERNAME; - password = TestResources.PASSWORD; + username = TestConstants.USERNAME; + password = TestConstants.PASSWORD; } request = new Requests(url); @@ -79,22 +83,22 @@ public void testAddPageAttachment() throws Throwable public void testDeletePageAttachment() throws Throwable { - String s = request.deletePageAttachment(wikiName, spaceName, pageName, attachmentName); + String s = request.deletePageAttachment(wikiName, spaceName, pageName, attachmentDelName); assertNotNull(s); } public void testRequestPageAttchmentsInHistory() throws Throwable { - Attachments attachments = request.requestPageAttachmentsInHistory(wikiName, spaceName, pageName, version); + Attachments attachments = request.requestPageAttachmentsInHistory(wikiName, spaceName, pageName, pageVersion); assertNotNull(attachments); } - // public void testRequestPageAttachmentInHistory() throws Throwable - // { - // InputStream inputStream = - // request.requestPageAttachmentsInHistory(wikiName, spaceName, pageName, version, attachmentName); - // assertNotNull(inputStream); - // } +// public void testRequestPageAttachmentInHistory() throws Throwable +// { +// InputStream inputStream = +// request.requestPageAttachmentsInHistory(wikiName, spaceName, pageName, pageVersion, attachmentName); +// assertNotNull(inputStream); +// } // this test is not working in xwiki 3.5.1. Server Error. public void testRequestPageAttchmentsInAttachmentHistory() throws Throwable { @@ -103,12 +107,12 @@ public void testRequestPageAttchmentsInAttachmentHistory() throws Throwable assertNotNull(attachments); } - // public void testRequestPageAttachmentInAttachmentHistory() throws Throwable - // { - // InputStream inputStream = - // request.requestPageAttachmentsInAttachmentHistory(wikiName, spaceName, pageName, attachmentName, - // attachmentVersion); - // assertNotNull(inputStream); - // } + public void testRequestPageAttachmentInAttachmentHistory() throws Throwable + { + InputStream inputStream = + request.requestPageAttachmentsInAttachmentHistory(wikiName, spaceName, pageName, attachmentName, + attachmentVersion); + assertNotNull(inputStream); + } } diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/ClassTest.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/ClassTest.java index 161aaf4..6f916b6 100644 --- a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/ClassTest.java +++ b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/ClassTest.java @@ -23,6 +23,7 @@ import org.xwiki.android.resources.Class; import org.xwiki.android.resources.Classes; import org.xwiki.android.rest.Requests; +import org.xwiki.android.test.rest.env.TestConstants; import android.test.AndroidTestCase; @@ -38,13 +39,13 @@ public class ClassTest extends AndroidTestCase protected void setUp() throws Exception { super.setUp(); - wikiName = TestResources.WIKI_NAME; - url = TestResources.URL; - classname = TestResources.CLASS_NAME; + wikiName = TestConstants.WIKI_NAME; + url = TestConstants.SERVER_URL; + classname = TestConstants.CLASS_NAME; if (isSecured) { - username = TestResources.USERNAME; - password = TestResources.PASSWORD; + username = TestConstants.USERNAME; + password = TestConstants.PASSWORD; } request = new Requests(url); diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/CommentsTest.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/CommentsTest.java index a9c96d6..57b027d 100644 --- a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/CommentsTest.java +++ b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/CommentsTest.java @@ -23,6 +23,7 @@ import org.xwiki.android.resources.Comment; import org.xwiki.android.resources.Comments; import org.xwiki.android.rest.Requests; +import org.xwiki.android.test.rest.env.TestConstants; import android.test.AndroidTestCase; @@ -38,16 +39,16 @@ public class CommentsTest extends AndroidTestCase protected void setUp() throws Exception { super.setUp(); - wikiName = TestResources.WIKI_NAME; - spaceName = TestResources.SPACE_NAME; - pageName = TestResources.PAGE_NAME; - url = TestResources.URL; - commentId = TestResources.COMMENT_ID; - version = TestResources.PAGE_VERSION; + wikiName = TestConstants.WIKI_NAME; + spaceName = TestConstants.SPACE_NAME; + pageName = TestConstants.PAGE_NAME; + url = TestConstants.SERVER_URL; + commentId = TestConstants.COMMENT_ID; + version = TestConstants.COMMENT_PAGE_HISTORY_VERSION; if (isSecured) { - username = TestResources.USERNAME; - password = TestResources.PASSWORD; + username = TestConstants.USERNAME; + password = TestConstants.PASSWORD; } request = new Requests(url); @@ -86,7 +87,7 @@ public void testRequestCommentsInHistoryWithID() throws Throwable public void testAddComment() throws Throwable { Comment comment = new Comment(); - comment.setText("This is tesing comment in android rest"); + comment.setText("This is tesing comment in android xmlrpc"); String s = request.addPageComment(wikiName, spaceName, pageName, comment); assertNotNull(s); } diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/HistoryTest.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/HistoryTest.java index f3fc012..da8968d 100644 --- a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/HistoryTest.java +++ b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/HistoryTest.java @@ -22,6 +22,7 @@ import org.xwiki.android.resources.History; import org.xwiki.android.rest.Requests; +import org.xwiki.android.test.rest.env.TestConstants; import android.test.AndroidTestCase; @@ -37,15 +38,15 @@ public class HistoryTest extends AndroidTestCase protected void setUp() throws Exception { super.setUp(); - wikiName = TestResources.WIKI_NAME; - spaceName = TestResources.SPACE_NAME; - pageName = TestResources.PAGE_NAME; - url = TestResources.URL; - language = TestResources.LANGUAGE; + wikiName = TestConstants.WIKI_NAME; + spaceName = TestConstants.SPACE_NAME; + pageName = TestConstants.PAGE_NAME; + url = TestConstants.SERVER_URL; + language = TestConstants.LANGUAGE; if (isSecured) { - username = TestResources.USERNAME; - password = TestResources.PASSWORD; + username = TestConstants.USERNAME; + password = TestConstants.PASSWORD; } request = new Requests(url); diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/ObjectTest.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/ObjectTest.java index cebfc3a..a38589e 100644 --- a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/ObjectTest.java +++ b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/ObjectTest.java @@ -26,6 +26,7 @@ import org.xwiki.android.resources.Properties; import org.xwiki.android.resources.Property; import org.xwiki.android.rest.Requests; +import org.xwiki.android.test.rest.env.TestConstants; import android.test.AndroidTestCase; @@ -41,17 +42,17 @@ public class ObjectTest extends AndroidTestCase protected void setUp() throws Exception { super.setUp(); - wikiName = TestResources.WIKI_NAME; - spaceName = TestResources.SPACE_NAME; - pageName = TestResources.PAGE_NAME; - url = TestResources.URL; - classname = TestResources.CLASS_NAME; - objectNo = TestResources.OBJECT_NUMBER; - propertyName = TestResources.OBJECT_PROPERTY_NAME; + wikiName = TestConstants.WIKI_NAME; + spaceName = TestConstants.SPACE_NAME; + pageName = TestConstants.PAGE_NAME; + url = TestConstants.SERVER_URL; + classname = TestConstants.CLASS_NAME; + objectNo = TestConstants.OBJECT_NUMBER; + propertyName = TestConstants.OBJECT_PROPERTY_NAME; if (isSecured) { - username = TestResources.USERNAME; - password = TestResources.PASSWORD; + username = TestConstants.USERNAME; + password = TestConstants.PASSWORD; } request = new Requests(url); diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/PageTest.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/PageTest.java index f604ff9..7e28965 100644 --- a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/PageTest.java +++ b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/PageTest.java @@ -23,12 +23,13 @@ import org.xwiki.android.resources.Page; import org.xwiki.android.resources.Pages; import org.xwiki.android.rest.Requests; +import org.xwiki.android.test.rest.env.TestConstants; import android.test.AndroidTestCase; public class PageTest extends AndroidTestCase { - private String wikiName, spaceName, pageName, url, username, password, language, version; + private String wikiName, spaceName, pageName, url, username, password, language, pageVersion,translationVersion; private boolean isSecured = true; @@ -38,16 +39,17 @@ public class PageTest extends AndroidTestCase protected void setUp() throws Exception { super.setUp(); - wikiName = TestResources.WIKI_NAME; - spaceName = TestResources.SPACE_NAME; - pageName = TestResources.PAGE_NAME; - url = TestResources.URL; - language = TestResources.LANGUAGE; - version = TestResources.PAGE_VERSION; + wikiName = TestConstants.WIKI_NAME; + spaceName = TestConstants.SPACE_NAME; + pageName = TestConstants.PAGE_NAME; + url = TestConstants.SERVER_URL; + language = TestConstants.LANGUAGE; + pageVersion = TestConstants.PAGE_VERSION; + translationVersion= TestConstants.TRANSLATION_VERSION; if (isSecured) { - username = TestResources.USERNAME; - password = TestResources.PASSWORD; + username = TestConstants.USERNAME; + password = TestConstants.PASSWORD; } request = new Requests(url); @@ -86,7 +88,7 @@ public void testRequestPageChildren() throws Throwable public void testRequestPageInVersionHistory() throws Throwable { - Page page = request.requestPageInVersionHistory(wikiName, spaceName, pageName, version); + Page page = request.requestPageInVersionHistory(wikiName, spaceName, pageName, pageVersion); assertNotNull(page); } @@ -98,7 +100,7 @@ public void testRequestPageTranslation() throws Throwable public void testRequestPageTranslationInHistory() throws Throwable { - Page page = request.requestPageTranslationInHistory(wikiName, spaceName, pageName, language, version); + Page page = request.requestPageTranslationInHistory(wikiName, spaceName, pageName, language, translationVersion); assertNotNull(page); } diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/SearchTest.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/SearchTest.java index ff8435f..62e7a3e 100644 --- a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/SearchTest.java +++ b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/SearchTest.java @@ -22,6 +22,7 @@ import org.xwiki.android.resources.SearchResults; import org.xwiki.android.rest.Requests; +import org.xwiki.android.test.rest.env.TestConstants; import android.test.AndroidTestCase; @@ -37,14 +38,14 @@ public class SearchTest extends AndroidTestCase protected void setUp() throws Exception { super.setUp(); - wikiName = TestResources.WIKI_NAME; - spaceName = TestResources.SPACE_NAME; - url = TestResources.URL; - keyword = TestResources.SEARCH_KEWORD; + wikiName = TestConstants.WIKI_NAME; + spaceName = TestConstants.SPACE_NAME; + url = TestConstants.SERVER_URL; + keyword = TestConstants.SEARCH_KEWORD; if (isSecured) { - username = TestResources.USERNAME; - password = TestResources.PASSWORD; + username = TestConstants.USERNAME; + password = TestConstants.PASSWORD; } request = new Requests(url); diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/SpaceTest.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/SpaceTest.java index d71b2fb..df37d36 100644 --- a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/SpaceTest.java +++ b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/SpaceTest.java @@ -22,6 +22,7 @@ import org.xwiki.android.resources.Spaces; import org.xwiki.android.rest.Requests; +import org.xwiki.android.test.rest.env.TestConstants; import android.test.AndroidTestCase; @@ -37,12 +38,12 @@ public class SpaceTest extends AndroidTestCase protected void setUp() throws Exception { super.setUp(); - wikiName = TestResources.WIKI_NAME; - url = TestResources.URL; + wikiName = TestConstants.WIKI_NAME; + url = TestConstants.SERVER_URL; if (isSecured) { - username = TestResources.USERNAME; - password = TestResources.PASSWORD; + username = TestConstants.USERNAME; + password = TestConstants.PASSWORD; } request = new Requests(url); diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/TagTest.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/TagTest.java index 66f6a33..fee8ac5 100644 --- a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/TagTest.java +++ b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/TagTest.java @@ -23,6 +23,7 @@ import org.xwiki.android.resources.Tag; import org.xwiki.android.resources.Tags; import org.xwiki.android.rest.Requests; +import org.xwiki.android.test.rest.env.TestConstants; import android.test.AndroidTestCase; @@ -38,15 +39,15 @@ public class TagTest extends AndroidTestCase protected void setUp() throws Exception { super.setUp(); - wikiName = TestResources.WIKI_NAME; - spaceName = TestResources.SPACE_NAME; - pageName = TestResources.PAGE_NAME; - url = TestResources.URL; - tagText = TestResources.TAG_WORD; + wikiName = TestConstants.WIKI_NAME; + spaceName = TestConstants.SPACE_NAME; + pageName = TestConstants.PAGE_NAME; + url = TestConstants.SERVER_URL; + tagText = TestConstants.TAG_WORD; if (isSecured) { - username = TestResources.USERNAME; - password = TestResources.PASSWORD; + username = TestConstants.USERNAME; + password = TestConstants.PASSWORD; } request = new Requests(url); diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/TestResources.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/TestResources.java deleted file mode 100644 index 5533d6c..0000000 --- a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/TestResources.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * See the NOTICE file distributed with this work for additional - * information regarding copyright ownership. - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This software is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ - -package org.xwiki.android.test.rest; - -public class TestResources -{ - 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 URL = "10.0.2.2:8080"; - - public static final String USERNAME = "superadmin"; - - public static final String PASSWORD = "admin"; - - public static final String PAGE_VERSION = "1.0"; - - public static final String COMMENT_ID = "0"; - - public static final String LANGUAGE = "null"; - - public static final String CLASS_NAME = "Blog.BlogPostClass"; - - public static final String OBJECT_NUMBER = "0"; - - public static final String OBJECT_PROPERTY_NAME = "content"; - - public static final String SEARCH_KEWORD = "test"; - - public static final String TAG_WORD = "testTag"; - - public static final String ATTACHMENT_NAME = "a.png"; - - public static final String ATTACHMENT_PATH = "/sdcard/"; - - public static final String ATTACHMENT_VERSION = "1.0"; -} diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/TranslationTest.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/TranslationTest.java index 0f35151..f9ec118 100644 --- a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/TranslationTest.java +++ b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/TranslationTest.java @@ -22,6 +22,7 @@ import org.xwiki.android.resources.Translations; import org.xwiki.android.rest.Requests; +import org.xwiki.android.test.rest.env.TestConstants; import android.test.AndroidTestCase; @@ -37,14 +38,14 @@ public class TranslationTest extends AndroidTestCase protected void setUp() throws Exception { super.setUp(); - wikiName = TestResources.WIKI_NAME; - spaceName = TestResources.SPACE_NAME; - pageName = TestResources.PAGE_NAME; - url = TestResources.URL; + wikiName = TestConstants.WIKI_NAME; + spaceName = TestConstants.SPACE_NAME; + pageName = TestConstants.PAGE_NAME; + url = TestConstants.SERVER_URL; if (isSecured) { - username = TestResources.USERNAME; - password = TestResources.PASSWORD; + username = TestConstants.USERNAME; + password = TestConstants.PASSWORD; } request = new Requests(url); diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/WikiTest.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/WikiTest.java index 96e905e..dcfb8fd 100644 --- a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/WikiTest.java +++ b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/WikiTest.java @@ -22,6 +22,7 @@ import org.xwiki.android.resources.Wikis; import org.xwiki.android.rest.Requests; +import org.xwiki.android.test.rest.env.TestConstants; import android.test.AndroidTestCase; @@ -37,11 +38,11 @@ public class WikiTest extends AndroidTestCase protected void setUp() throws Exception { super.setUp(); - url = TestResources.URL; + url = TestConstants.SERVER_URL; if (isSecured) { - username = TestResources.USERNAME; - password = TestResources.PASSWORD; + username = TestConstants.USERNAME; + password = TestConstants.PASSWORD; } request = new Requests(url); diff --git a/xwiki-android-test-rest/src/org/xwiki/android/test/rest/env/TestConstants.java b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/env/TestConstants.java new file mode 100644 index 0000000..4143ef9 --- /dev/null +++ b/xwiki-android-test-rest/src/org/xwiki/android/test/rest/env/TestConstants.java @@ -0,0 +1,62 @@ +package org.xwiki.android.test.rest.env; + +/** + * Environment variables for tests. + * How to setup external text setup. + */ + +public class TestConstants { + + public static final int SERVER_INDEX; //not used. + 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 String USERNAME = "Admin"; + public static final String PASSWORD = "admin"; + + //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 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_REPLY_TEXT = "huy"; + + + public static final String LANGUAGE = "fr"; + public static final String TRANSLATION_VERSION="2.1"; + + public static final String CLASS_NAME = "Blog.BlogPostClass"; + public static final String OBJECT_NUMBER = "0"; + public static final String OBJECT_PROPERTY_NAME = "content"; + public static final String OBJECT_PROPERTY_VALUE = "test blog content"; + + public static final String SEARCH_KEWORD = "test"; + + public static final String TAG_WORD = "testTag"; + + public static final String ATTACHMENT_NAME = "a.png"; + public static final String ATTACHMENT_DEL_NAME = "del.png"; + 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")){ + System.err.println("localhost on android means devices interface card!!!"); + } + } + +} diff --git a/xwiki-android-tests-instrumentation/AndroidManifest.xml b/xwiki-android-tests-instrumentation/AndroidManifest.xml index 6d3f5ab..94e3e1b 100644 --- a/xwiki-android-tests-instrumentation/AndroidManifest.xml +++ b/xwiki-android-tests-instrumentation/AndroidManifest.xml @@ -7,7 +7,7 @@ - diff --git a/xwiki-android-tests-instrumentation/project.properties b/xwiki-android-tests-instrumentation/project.properties index 192ff3d..6d257c7 100644 --- a/xwiki-android-tests-instrumentation/project.properties +++ b/xwiki-android-tests-instrumentation/project.properties @@ -10,8 +10,10 @@ # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt -android.library.reference.1=..\\xwiki-android-rest -android.library.reference.2=..\\xwiki-android-components # Project target. -target=Google Inc.:Google APIs:7 +target=android-7 +android.library.reference.1=../xwiki-android-rest +android.library.reference.2=../xwiki-android-components android.library.reference.3=../xwiki-android-core + + diff --git a/xwiki-android-tests-instrumentation/src/org/xwiki/android/tests/TestConstants.java b/xwiki-android-tests-instrumentation/src/org/xwiki/android/tests/TestConstants.java new file mode 100644 index 0000000..0e99e07 --- /dev/null +++ b/xwiki-android-tests-instrumentation/src/org/xwiki/android/tests/TestConstants.java @@ -0,0 +1,66 @@ +package org.xwiki.android.tests; + +/** + * Environment variables for tests. + * How to setup external text setup. + */ + +public class TestConstants { + + public static final String DEFAULT_EXECUTION_DIRECTORY; //see static block to change default. + public static final int SERVER_INDEX; + public static final String HOST = "10.0.2.2"; // do not put local host as this is run on AVD. It'll call AVDs nic. + public static final int PORT = 8080; //actual port is port+server_index + 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. + + 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 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_REPLY_TEXT = "huy"; + + + public static final String LANGUAGE = "fr"; + public static final String TRANSLATION_VERSION="2.1"; + + public static final String CLASS_NAME = "Blog.BlogPostClass"; + public static final String OBJECT_NUMBER = "0"; + public static final String OBJECT_PROPERTY_NAME = "content"; + public static final String OBJECT_PROPERTY_VALUE = "test blog content"; + + public static final String SEARCH_KEWORD = "test"; + + public static final String TAG_WORD = "testTag"; + + public static final String ATTACHMENT_NAME = "a.png"; + public static final String ATTACHMENT_DEL_NAME = "del.png"; + 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")); + DEFAULT_EXECUTION_DIRECTORY = System.getProperty("xwikiExecutionDirectory", "C:\\Users\\admin\\Desktop\\xwiki-enterprise-jetty"); + SERVER_URL = HOST + ":" + (PORT + SERVER_INDEX); + if(System.getProperty("xwikiExecutionDirectory"+SERVER_INDEX)==null){ + System.out.println("xwiki execution dir:"+ DEFAULT_EXECUTION_DIRECTORY +" index: "+SERVER_INDEX + " server Url: "+SERVER_URL); + }else{ + System.out.println("xwiki execution dir:"+ System.getProperty("xwikiExecutionDirectory"+SERVER_INDEX) +" index: "+SERVER_INDEX + " server Url: "+SERVER_URL); + } + System.setProperty("xwikiExecutionDirectory", DEFAULT_EXECUTION_DIRECTORY);//set it up to default val above, if not set by pom.else rewrite. + } + +} diff --git a/xwiki-android-tests-instrumentation/src/org/xwiki/android/tests/TestResources.java b/xwiki-android-tests-instrumentation/src/org/xwiki/android/tests/TestResources.java deleted file mode 100644 index 2801234..0000000 --- a/xwiki-android-tests-instrumentation/src/org/xwiki/android/tests/TestResources.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * See the NOTICE file distributed with this work for additional - * information regarding copyright ownership. - * - * This is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of - * the License, or (at your option) any later version. - * - * This software is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this software; if not, write to the Free - * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA, or see the FSF site: http://www.fsf.org. - */ - -package org.xwiki.android.tests; - -public class TestResources -{ - 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 URL = "10.0.2.2:8080"; - - public static final String USERNAME = "Admin"; - - public static final String PASSWORD = "admin"; - - public static final String PAGE_VERSION = "1.0"; - - public static final String COMMENT_ID = "0"; - - public static final String LANGUAGE = "null"; - - public static final String CLASS_NAME = "Blog.BlogPostClass"; - - public static final String OBJECT_NUMBER = "0"; - - public static final String OBJECT_PROPERTY_NAME = "content"; - - public static final String SEARCH_KEWORD = "test"; - - public static final String TAG_WORD = "testTag"; - - public static final String ATTACHMENT_NAME = "a.png"; - - public static final String ATTACHMENT_PATH = "/sdcard/"; - - public static final String ATTACHMENT_VERSION = "1.0"; -} diff --git a/xwiki-rest-model/xwiki-rest-model-simplexml/src/org/xwiki/android/resources/Comments.java b/xwiki-rest-model/xwiki-rest-model-simplexml/src/org/xwiki/android/resources/Comments.java index a178d5d..0d3a94c 100644 --- a/xwiki-rest-model/xwiki-rest-model-simplexml/src/org/xwiki/android/resources/Comments.java +++ b/xwiki-rest-model/xwiki-rest-model-simplexml/src/org/xwiki/android/resources/Comments.java @@ -51,7 +51,7 @@ @Namespace(reference = "http://www.xwiki.org") public class Comments // extends LinkCollection { - @ElementList(name = "comment", inline = true) + @ElementList(name = "comment", inline = true , required=false) public List comments; /** diff --git a/xwiki-rest-model/xwiki-rest-model-simplexml/src/org/xwiki/android/resources/Properties.java b/xwiki-rest-model/xwiki-rest-model-simplexml/src/org/xwiki/android/resources/Properties.java index 1eecaab..710b211 100644 --- a/xwiki-rest-model/xwiki-rest-model-simplexml/src/org/xwiki/android/resources/Properties.java +++ b/xwiki-rest-model/xwiki-rest-model-simplexml/src/org/xwiki/android/resources/Properties.java @@ -49,7 +49,7 @@ @Root @Namespace(reference = "http://www.xwiki.org") -public class Properties +public class Properties extends LinkCollection { @ElementList(name = "property", inline = true)