Java tutorial
/******************************************************************************* * Copyright (c) 2008 ThoughtWorks, Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * ThoughtWorks, Inc. - initial API and implementation *******************************************************************************/ package com.thoughtworks.twist.mingle.core; import java.io.Reader; import java.io.StringReader; import java.util.List; import junit.framework.TestCase; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpStatus; import org.eclipse.mylyn.tasks.core.TaskRepository; import org.eclipse.mylyn.tasks.core.data.TaskAttribute; import org.eclipse.mylyn.tasks.core.data.TaskData; /** * @author Ketan Padegaonkar */ public class MingleClientTest extends TestCase { public void testValidatesWhenHttpStatusIs200() throws Exception { MingleClient mingleClient = new TestMingleClient("http://localhost:30001/projects/foobar", "ketan", "ketan", HttpStatus.SC_OK); assertTrue(mingleClient.validate()); } public void testThrowsMingleAuthenticationExceptionOnHttpStatus401() throws Exception { MingleClient mingleClient = new TestMingleClient("http://localhost:30001/projects/foobar", "ketan", "ketan", HttpStatus.SC_UNAUTHORIZED); try { mingleClient.validate(); fail("expected MingleAuthenticationException, should not have reached here"); } catch (MingleAuthenticationException e) { // pass } } public void testDoesNotValidateConnectionOnHttpErrors() throws Exception { MingleClient mingleClient = new TestMingleClient("http://localhost:30001/projects/foobar", "ketan", "ketan", 0) { @Override protected int executeMethod(HttpMethod method) { throw new RuntimeException(); } }; try { assertFalse(mingleClient.validate()); fail("expected IOException, should not have reached here"); } catch (RuntimeException e) { // pass } } public void testParsesTaskData() throws Exception { MingleClient mingleClient = clientWithCards(); List<TaskData> allTasks = mingleClient.getAllTaskData(); assertEquals(2, allTasks.size()); TaskData task = allTasks.get(0); assertAtttributeValue("Hello World Task", task.getRoot().getAttribute(TaskAttribute.SUMMARY)); assertEquals("1", task.getTaskId()); assertAtttributeValue("This card says hello world", task.getRoot().getAttribute(TaskAttribute.DESCRIPTION)); assertAtttributeValue("attrib1", task.getRoot().getAttribute("someAttribute")); assertAtttributeValue("attrib2", task.getRoot().getAttribute("someAttribute1")); task = allTasks.get(1); assertAtttributeValue("name2", task.getRoot().getAttribute(TaskAttribute.SUMMARY)); assertEquals("id2", task.getTaskId()); assertAtttributeValue("description2", task.getRoot().getAttribute(TaskAttribute.DESCRIPTION)); } private void assertAtttributeValue(String string, TaskAttribute attribute) { assertEquals(string, attribute.getValue()); } private MingleClient clientWithCards() { MingleClient mingleClient = new TestMingleClient("http://localhost:30001/projects/foobar", "ketan", "ketan", HttpStatus.SC_OK) { @Override protected Reader getResponse(HttpMethod method) { return new StringReader("<cards>\n" + " <card>\n" + " <number>1</number>\n" + " <name>Hello World Task</name>\n" + " <description>This card says hello world</description>\n" + " <someAttribute>attrib1</someAttribute>\n" + " <someAttribute1>attrib2</someAttribute1>\n" + " </card>\n" + " <card>\n" + " <number>id2</number>\n" + " <name>name2</name>\n" + " <description>description2</description>\n" + " </card>\n" + "</cards>\n"); } }; return mingleClient; } private class TestMingleClient extends MingleClient { private int httpStatus = HttpStatus.SC_OK; public TestMingleClient(String projectUrl, String userName, String password, int httpStatus) { super(projectUrl, userName, password, new MingleTaskAttributeMapper(new TaskRepository(MingleConstants.CONNECTOR_KIND, projectUrl))); this.httpStatus = httpStatus; } @Override protected int executeMethod(HttpMethod method) { return httpStatus; } } public void testInvalidXmlThrowsException() throws Exception { MingleClient mingleClient = new TestMingleClient("http://localhost:3000/projects/foo", "ketan", "ketan", HttpStatus.SC_OK) { protected Reader getResponse(HttpMethod method) { return new StringReader("<html>Some response for client</html>\n"); } }; try { mingleClient.getAllTaskData(); fail("Expected CouldNotParseTasksException"); } catch (CouldNotParseTasksException e) { // pass } } }