Example usage for org.json JSONObject getJSONArray

List of usage examples for org.json JSONObject getJSONArray

Introduction

In this page you can find the example usage for org.json JSONObject getJSONArray.

Prototype

public JSONArray getJSONArray(String key) throws JSONException 

Source Link

Document

Get the JSONArray value associated with a key.

Usage

From source file:com.hichinaschool.flashcards.libanki.sync.Syncer.java

public void mergeChanges(JSONObject lchg, JSONObject rchg) {
    try {/*w  w w .j  av  a  2s . c  o m*/
        // then the other objects
        mergeModels(rchg.getJSONArray("models"));
        mergeDecks(rchg.getJSONArray("decks"));
        mergeTags(rchg.getJSONArray("tags"));
        if (rchg.has("conf")) {
            mergeConf(rchg.getJSONObject("conf"));
        }
        // this was left out of earlier betas
        if (rchg.has("crt")) {
            mCol.setCrt(rchg.getLong("crt"));
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    prepareToChunk();
}

From source file:com.hichinaschool.flashcards.libanki.sync.Syncer.java

public void applyChunk(JSONObject chunk) {
    try {//from  w w  w  . j  a  v  a  2s  . co m
        if (chunk.has("revlog")) {
            mergeRevlog(chunk.getJSONArray("revlog"));
        }
        if (chunk.has("cards")) {
            mergeCards(chunk.getJSONArray("cards"));
        }
        if (chunk.has("notes")) {
            mergeNotes(chunk.getJSONArray("notes"));
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hichinaschool.flashcards.libanki.sync.Syncer.java

private void remove(JSONObject graves) {
    // pretend to be the server so we don't set usn = -1
    boolean wasServer = mCol.getServer();
    mCol.setServer(true);/*from ww  w.ja  v a2 s .  c o  m*/
    try {
        // notes first, so we don't end up with duplicate graves
        mCol._remNotes(Utils.jsonArrayToLongArray(graves.getJSONArray("notes")));
        // then cards
        mCol.remCards(Utils.jsonArrayToLongArray(graves.getJSONArray("cards")), false);
        // and decks
        JSONArray decks = graves.getJSONArray("decks");
        for (int i = 0; i < decks.length(); i++) {
            mCol.getDecks().rem(decks.getLong(i), false, false);
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    mCol.setServer(wasServer);
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitLogTest.java

@Test
public void testLogWithTag() 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 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);

        JSONArray commitsArray = log(gitHeadUri);
        assertEquals(1, commitsArray.length());

        String commitUri = commitsArray.getJSONObject(0).getString(ProtocolConstants.KEY_LOCATION);
        JSONObject updatedCommit = tag(commitUri, "tag");
        JSONArray tagsArray = updatedCommit.getJSONArray(GitConstants.KEY_TAGS);
        assertEquals(1, tagsArray.length());
        assertEquals(Constants.R_TAGS + "tag", tagsArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME));

        commitsArray = log(gitHeadUri);//from w  ww  . j a  va 2  s.c o m
        assertEquals(1, commitsArray.length());

        tagsArray = commitsArray.getJSONObject(0).getJSONArray(GitConstants.KEY_TAGS);
        assertEquals(1, tagsArray.length());
        assertEquals(Constants.R_TAGS + "tag", tagsArray.getJSONObject(0).get(ProtocolConstants.KEY_FULL_NAME));
    }
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitLogTest.java

@Test
public void testLogWithParents() 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 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);

        // modify
        JSONObject testTxt = getChild(project, "test.txt");
        modifyFile(testTxt, "first change");
        addFile(testTxt);//from   w w  w. j  a v a 2  s  .  c  o  m

        // commit1
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit1", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // modify again
        modifyFile(testTxt, "second change");
        addFile(testTxt);

        // commit2
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, "commit2", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // get the full log
        JSONArray commitsArray = log(gitHeadUri);
        assertEquals(3, commitsArray.length());

        JSONObject commit = commitsArray.getJSONObject(0);
        assertEquals("commit2", commit.get(GitConstants.KEY_COMMIT_MESSAGE));
        assertEquals(1, commit.getJSONArray(ProtocolConstants.KEY_PARENTS).length());
        String parent = commit.getJSONArray(ProtocolConstants.KEY_PARENTS).getJSONObject(0)
                .getString(ProtocolConstants.KEY_NAME);

        commit = commitsArray.getJSONObject(1);
        assertEquals("commit1", commit.get(GitConstants.KEY_COMMIT_MESSAGE));
        assertEquals(parent, commit.get(ProtocolConstants.KEY_NAME));
        assertEquals(1, commit.getJSONArray(ProtocolConstants.KEY_PARENTS).length());
        parent = commit.getJSONArray(ProtocolConstants.KEY_PARENTS).getJSONObject(0)
                .getString(ProtocolConstants.KEY_NAME);

        commit = commitsArray.getJSONObject(2);
        assertEquals("Initial commit", commit.get(GitConstants.KEY_COMMIT_MESSAGE));
        assertEquals(parent, commit.get(ProtocolConstants.KEY_NAME));
        assertEquals(0, commit.getJSONArray(ProtocolConstants.KEY_PARENTS).length());
    }
}

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  ww  w . j  av  a  2s. c  om

        // 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/*from w  w  w  . ja va 2  s .c om*/
    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//from  w ww  . j av a 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(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 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  w ww  .j  ava  2  s .c o m*/

        // 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 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());
}