List of usage examples for org.json JSONObject getJSONObject
public JSONObject getJSONObject(String key) throws JSONException
From source file:org.eclipse.orion.server.tests.servlets.git.GitLogTest.java
@Test public void testLogAllBranches() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo JSONObject clone = clone(clonePath); String cloneLocation = clone.getString(ProtocolConstants.KEY_LOCATION); String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION); String branchesLocation = clone.getString(GitConstants.KEY_BRANCH); String gitCommitUri = clone.getString(GitConstants.KEY_COMMIT); // get project metadata WebRequest request = getGetRequest(cloneContentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); // create branch final String newBranchName = "branch"; branch(branchesLocation, newBranchName); // modify JSONObject testTxt = getChild(project, "test.txt"); modifyFile(testTxt, "first change"); addFile(testTxt);/*from w w w . java2 s. c o m*/ // commit1 Thread.sleep(1000); // TODO: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=370696#c1 request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit1", false); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // checkout "branch" checkoutBranch(cloneLocation, newBranchName); // modify again modifyFile(testTxt, "second change"); addFile(testTxt); // commit2 Thread.sleep(1000); // TODO: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=370696#c1 request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit2", false); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // get standard log for HEAD - only init and commit2 should be visible JSONArray commitsArray = log(gitHeadUri); assertEquals(2, commitsArray.length()); JSONObject commit = commitsArray.getJSONObject(0); assertEquals("commit2", commit.get(GitConstants.KEY_COMMIT_MESSAGE)); commit = commitsArray.getJSONObject(1); assertEquals("Initial commit", commit.get(GitConstants.KEY_COMMIT_MESSAGE)); // get log for all branches - all 3 commits should be listed commitsArray = log(gitCommitUri); assertEquals(3, commitsArray.length()); commit = commitsArray.getJSONObject(0); assertEquals("commit2", commit.get(GitConstants.KEY_COMMIT_MESSAGE)); JSONArray branchesArray = commit.getJSONArray(GitConstants.KEY_BRANCHES); assertEquals(1, branchesArray.length()); assertEquals(Constants.R_HEADS + newBranchName, branchesArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME)); commit = commitsArray.getJSONObject(1); assertEquals("commit1", commit.get(GitConstants.KEY_COMMIT_MESSAGE)); branchesArray = commit.getJSONArray(GitConstants.KEY_BRANCHES); assertEquals(1, branchesArray.length()); assertEquals(Constants.R_HEADS + Constants.MASTER, branchesArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME)); commit = commitsArray.getJSONObject(2); assertEquals("Initial commit", commit.get(GitConstants.KEY_COMMIT_MESSAGE)); branchesArray = commit.getJSONArray(GitConstants.KEY_BRANCHES); assertEquals(1, branchesArray.length()); assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + Constants.MASTER, branchesArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME)); } }
From source file:org.eclipse.orion.server.tests.servlets.git.GitLogTest.java
@Test public void testLogFolder() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); String projectName = getMethodName(); JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); // modify// w w w . j ava 2 s . c o m JSONObject testTxt = getChild(project, "test.txt"); modifyFile(testTxt, "test.txt change"); addFile(testTxt); // commit1 WebRequest request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit1", false); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // modify again JSONObject folder = getChild(project, "folder"); JSONObject folderTxt = getChild(folder, "folder.txt"); modifyFile(folderTxt, "folder.txt change"); addFile(folderTxt); // commit2 request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit2", false); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // get log for file JSONObject log = logObject(gitHeadUri); String repositoryPath = log.getString(GitConstants.KEY_REPOSITORY_PATH); assertEquals("", repositoryPath); JSONArray commitsArray = log.getJSONArray(ProtocolConstants.KEY_CHILDREN); assertEquals(3, commitsArray.length()); JSONObject commit = commitsArray.getJSONObject(0); assertEquals("commit2", commit.get(GitConstants.KEY_COMMIT_MESSAGE)); commit = commitsArray.getJSONObject(1); assertEquals("commit1", commit.get(GitConstants.KEY_COMMIT_MESSAGE)); commit = commitsArray.getJSONObject(2); assertEquals("Initial commit", commit.get(GitConstants.KEY_COMMIT_MESSAGE)); log = logObject(folder.getJSONObject(GitConstants.KEY_GIT).getString(GitConstants.KEY_HEAD)); repositoryPath = log.getString(GitConstants.KEY_REPOSITORY_PATH); assertEquals("folder/", repositoryPath); commitsArray = log.getJSONArray(ProtocolConstants.KEY_CHILDREN); assertEquals(2, commitsArray.length()); commit = commitsArray.getJSONObject(0); assertEquals("commit2", commit.get(GitConstants.KEY_COMMIT_MESSAGE)); commit = commitsArray.getJSONObject(1); assertEquals("Initial commit", commit.get(GitConstants.KEY_COMMIT_MESSAGE)); }
From source file:org.eclipse.orion.server.tests.servlets.git.GitLogTest.java
@Test public void testLogFile() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); String projectName = getMethodName(); JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); // modify/*w w w . j a va2s . c o m*/ JSONObject testTxt = getChild(project, "test.txt"); modifyFile(testTxt, "test.txt change"); addFile(testTxt); // commit1 WebRequest request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit1", false); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // modify again JSONObject folder = getChild(project, "folder"); JSONObject folderTxt = getChild(folder, "folder.txt"); modifyFile(folderTxt, "folder.txt change"); addFile(folderTxt); // commit2 request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit2", false); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // get log for file JSONObject log = logObject(testTxt.getJSONObject(GitConstants.KEY_GIT).getString(GitConstants.KEY_HEAD)); String repositoryPath = log.getString(GitConstants.KEY_REPOSITORY_PATH); assertEquals("test.txt", repositoryPath); JSONArray commitsArray = log.getJSONArray(ProtocolConstants.KEY_CHILDREN); assertEquals(2, commitsArray.length()); JSONObject commit = commitsArray.getJSONObject(0); assertEquals("commit1", commit.getString(GitConstants.KEY_COMMIT_MESSAGE)); // check commit content location assertEquals("test.txt change", getCommitContent(commit)); // check commit diff location String[] parts = GitDiffTest.getDiff(commit.getString(GitConstants.KEY_DIFF)); assertEquals("", parts[1]); // no diff between the commit and working tree // check commit location JSONObject jsonObject = logObject(commit.getString(ProtocolConstants.KEY_LOCATION)); assertEquals(log.getString(GitConstants.KEY_CLONE), jsonObject.getString(GitConstants.KEY_CLONE)); assertEquals(log.getString(GitConstants.KEY_REPOSITORY_PATH), jsonObject.getString(GitConstants.KEY_REPOSITORY_PATH)); assertNull(jsonObject.optString(GitConstants.KEY_LOG_TO_REF, null)); assertNull(jsonObject.optString(GitConstants.KEY_LOG_FROM_REF, null)); assertThat(jsonObject.getJSONArray(ProtocolConstants.KEY_CHILDREN), isJSONArrayEqual(log.getJSONArray(ProtocolConstants.KEY_CHILDREN))); // check second commit commit = commitsArray.getJSONObject(1); assertEquals("Initial commit", commit.getString(GitConstants.KEY_COMMIT_MESSAGE)); // check commit content location assertEquals("test", getCommitContent(commit)); // check commit diff location parts = GitDiffTest.getDiff(commit.getString(GitConstants.KEY_DIFF)); StringBuilder sb = new StringBuilder(); sb.append("diff --git a/test.txt b/test.txt").append("\n"); sb.append("index 30d74d2..3146ed5 100644").append("\n"); sb.append("--- a/test.txt").append("\n"); sb.append("+++ b/test.txt").append("\n"); sb.append("@@ -1 +1 @@").append("\n"); sb.append("-test").append("\n"); sb.append("\\ No newline at end of file").append("\n"); sb.append("+test.txt change").append("\n"); sb.append("\\ No newline at end of file").append("\n"); assertEquals(sb.toString(), parts[1]); // check commit location jsonObject = logObject(commit.getString(ProtocolConstants.KEY_LOCATION)); JSONObject log2 = log; JSONArray commitsArray2 = log2.getJSONArray(ProtocolConstants.KEY_CHILDREN); commitsArray2.remove(0); assertEquals(log.getString(GitConstants.KEY_CLONE), jsonObject.getString(GitConstants.KEY_CLONE)); assertEquals(log.getString(GitConstants.KEY_REPOSITORY_PATH), jsonObject.getString(GitConstants.KEY_REPOSITORY_PATH)); assertNull(jsonObject.optString(GitConstants.KEY_LOG_TO_REF, null)); assertNull(jsonObject.optString(GitConstants.KEY_LOG_FROM_REF, null)); assertThat(commitsArray2, isJSONArrayEqual(log.getJSONArray(ProtocolConstants.KEY_CHILDREN))); }
From source file:org.eclipse.orion.server.tests.servlets.git.GitLogTest.java
@Test public void testLogNewBranch() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); String workspaceId = workspaceIdFromLocation(workspaceLocation); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null); JSONObject clone = clone(getClonePath(workspaceId, project)); String cloneLocation = clone.getString(ProtocolConstants.KEY_LOCATION); String branchesLocation = clone.getString(GitConstants.KEY_BRANCH); // get project metadata WebRequest request = getGetRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION)); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); JSONArray commitsArray = log(gitHeadUri); assertEquals(1, commitsArray.length()); branch(branchesLocation, "a"); checkoutBranch(cloneLocation, "a"); // get project metadata again request = getGetRequest(project.getString(ProtocolConstants.KEY_LOCATION)); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); project = new JSONObject(response.getText()); gitSection = project.getJSONObject(GitConstants.KEY_GIT); gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); log(gitHeadUri);// w w w .j a v a 2s .com }
From source file:org.eclipse.orion.server.tests.servlets.git.GitLogTest.java
@Test public void testDiffFromLog() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo JSONObject clone = clone(clonePath); String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project/folder metadata WebRequest request = getGetRequest(cloneContentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject folder = new JSONObject(response.getText()); JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); // modify JSONObject testTxt = getChild(folder, "test.txt"); modifyFile(testTxt, "hello"); addFile(testTxt);/*from ww w . ja va2s . c om*/ // commit request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "2nd commit", false); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // get the full log JSONArray commits = log(gitHeadUri); assertEquals(2, commits.length()); JSONObject commit = commits.getJSONObject(0); assertEquals("2nd commit", commit.get(GitConstants.KEY_COMMIT_MESSAGE)); JSONArray diffs = commit.getJSONArray(GitConstants.KEY_COMMIT_DIFFS); assertEquals(1, diffs.length()); // check diff object JSONObject diff = diffs.getJSONObject(0); assertEquals("test.txt", diff.getString(GitConstants.KEY_COMMIT_DIFF_NEWPATH)); assertEquals("test.txt", diff.getString(GitConstants.KEY_COMMIT_DIFF_OLDPATH)); assertEquals(Diff.TYPE, diff.getString(ProtocolConstants.KEY_TYPE)); assertEquals(ChangeType.MODIFY, ChangeType.valueOf(diff.getString(GitConstants.KEY_COMMIT_DIFF_CHANGETYPE))); String contentLocation = diff.getString(ProtocolConstants.KEY_CONTENT_LOCATION); String fileContent = getFileContent( new JSONObject().put(ProtocolConstants.KEY_LOCATION, contentLocation)); assertEquals("hello", fileContent); String diffLocation = diff.getString(GitConstants.KEY_DIFF); // check diff location String[] parts = GitDiffTest.getDiff(diffLocation); StringBuilder sb = new StringBuilder(); sb.append("diff --git a/test.txt b/test.txt").append("\n"); sb.append("index 30d74d2..b6fc4c6 100644").append("\n"); sb.append("--- a/test.txt").append("\n"); sb.append("+++ b/test.txt").append("\n"); sb.append("@@ -1 +1 @@").append("\n"); sb.append("-test").append("\n"); sb.append("\\ No newline at end of file").append("\n"); sb.append("+hello").append("\n"); sb.append("\\ No newline at end of file").append("\n"); assertEquals(sb.toString(), parts[1]); } }
From source file:org.eclipse.orion.server.tests.servlets.git.GitLogTest.java
@Test public void testToRefKey() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); // git log for HEAD JSONObject log = logObject(gitHeadUri); // project contains only initial commit, so HEAD points to master branch // information about master branch is expected JSONObject toRef = log.getJSONObject(GitConstants.KEY_LOG_TO_REF); assertEquals(gitHeadUri, toRef.getString(GitConstants.KEY_HEAD)); assertEquals(gitSection.getString(GitConstants.KEY_CLONE), toRef.getString(GitConstants.KEY_CLONE)); assertEquals(GitConstants.KEY_BRANCH_NAME, toRef.getString(ProtocolConstants.KEY_TYPE)); assertEquals(Constants.MASTER, toRef.getString(ProtocolConstants.KEY_NAME)); assertEquals(true, toRef.getBoolean(GitConstants.KEY_BRANCH_CURRENT)); }/*from w w w.ja v a2 s . c o m*/ }
From source file:org.eclipse.orion.server.tests.servlets.git.GitLogTest.java
@Test public void testFromRefKey() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); String logUri = gitHeadUri.replace(Constants.HEAD, Constants.HEAD + ".." + Constants.HEAD); // git log for HEAD..HEAD JSONObject log = logObject(logUri); // project contains only initial commit, so HEAD points to master branch // information about master branch is expected JSONObject fromRef = log.getJSONObject(GitConstants.KEY_LOG_FROM_REF); assertEquals(gitHeadUri, fromRef.getString(GitConstants.KEY_HEAD)); assertEquals(gitSection.getString(GitConstants.KEY_CLONE), fromRef.getString(GitConstants.KEY_CLONE)); assertEquals(GitConstants.KEY_BRANCH_NAME, fromRef.getString(ProtocolConstants.KEY_TYPE)); assertEquals(Constants.MASTER, fromRef.getString(ProtocolConstants.KEY_NAME)); assertEquals(true, fromRef.getBoolean(GitConstants.KEY_BRANCH_CURRENT)); }/*w w w . j a v a 2 s. co m*/ }
From source file:org.eclipse.orion.server.tests.servlets.git.GitLogTest.java
@Test public void testRefPropertiesForCommits() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); String gitCommitUri = gitSection.getString(GitConstants.KEY_COMMIT); // save initial commit name JSONArray commitsArray = log(gitCommitUri); JSONObject initCommit = commitsArray.getJSONObject(0); String initCommitName = initCommit.getString(ProtocolConstants.KEY_NAME); String initCommitLocation = initCommit.getString(ProtocolConstants.KEY_LOCATION); // modify JSONObject testTxt = getChild(project, "test.txt"); modifyFile(testTxt, "test.txt change"); addFile(testTxt);// ww w .j a v a 2s . c om // commit request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit", false); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); String logUri = initCommitLocation.replace(initCommitName, initCommitName + ".." + initCommitName); // git log for {initial commit}..{initial commit} // both fromRef and toRef shouldn't exist, as commit ref doesn't point to a branch JSONObject log = logObject(logUri); JSONObject fromRef = log.optJSONObject(GitConstants.KEY_LOG_FROM_REF); JSONObject toRef = log.optJSONObject(GitConstants.KEY_LOG_TO_REF); assertNull(fromRef); assertNull(toRef); } }
From source file:org.eclipse.orion.server.tests.servlets.git.GitLogTest.java
@Test public void testGetNonExistingCommit() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); String projectName = getMethodName(); JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString()); JSONObject testTxt = getChild(project, "test.txt"); // get log for file JSONObject log = logObject(testTxt.getJSONObject(GitConstants.KEY_GIT).getString(GitConstants.KEY_HEAD)); JSONArray commitsArray = log.getJSONArray(ProtocolConstants.KEY_CHILDREN); assertEquals(1, commitsArray.length()); JSONObject commit = commitsArray.getJSONObject(0); assertEquals("Initial commit", commit.getString(GitConstants.KEY_COMMIT_MESSAGE)); String commitName = commit.getString(ProtocolConstants.KEY_NAME); String dummyName = "dummyName"; assertFalse(dummyName.equals(commitName)); String commitLocation = commit.getString(ProtocolConstants.KEY_CONTENT_LOCATION); // prepare the dummy commit location commitLocation = commitLocation.replace(commitName, dummyName).replace("?parts=body", "?page=1&pageSize=1"); WebRequest request = getGetRequest(commitLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); }
From source file:io.selendroid.server.CreateSessionHandlerTest.java
@Test public void assertNewTestSessionCreationAndGetCapabilties() throws Exception { HttpResponse sessionResponse = executeCreateSessionRequest(); JSONObject json = parseJsonResponse(sessionResponse); String sessionId = json.getString("sessionId"); // Get capabilities of session HttpResponse getCapaResp = executeRequest("http://" + host + ":" + port + "/wd/hub/session/" + sessionId, HttpMethod.GET);// w ww . jav a 2 s .c o m SelendroidAssert.assertResponseIsOk(getCapaResp); JSONObject capa = parseJsonResponse(getCapaResp); Assert.assertEquals(sessionId, capa.getString("sessionId")); Assert.assertEquals(0, capa.getInt("status")); Assert.assertEquals("selendroid", capa.getJSONObject("value").getString(Capabilities.NAME)); }