merge with branch 1.x

This commit is contained in:
Sebastian Sdorra
2017-01-12 19:50:39 +01:00
250 changed files with 16399 additions and 1573 deletions

View File

@@ -0,0 +1,108 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.net.ahc;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class AdvancedHttpClientTest {
@Mock(answer = Answers.CALLS_REAL_METHODS)
private AdvancedHttpClient client;
private static final String URL = "https://www.scm-manager.org";
@Test
public void testGet()
{
AdvancedHttpRequest request = client.get(URL);
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.GET, request.getMethod());
}
@Test
public void testDelete()
{
AdvancedHttpRequestWithBody request = client.delete(URL);
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.DELETE, request.getMethod());
}
@Test
public void testPut()
{
AdvancedHttpRequestWithBody request = client.put(URL);
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.PUT, request.getMethod());
}
@Test
public void testPost()
{
AdvancedHttpRequestWithBody request = client.post(URL);
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.POST, request.getMethod());
}
@Test
public void testOptions()
{
AdvancedHttpRequestWithBody request = client.options(URL);
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.OPTIONS, request.getMethod());
}
@Test
public void testHead()
{
AdvancedHttpRequest request = client.head(URL);
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.HEAD, request.getMethod());
}
@Test
public void testMethod()
{
AdvancedHttpRequestWithBody request = client.method("PROPFIND", URL);
assertEquals(URL, request.getUrl());
assertEquals("PROPFIND", request.getMethod());
}
}

View File

@@ -0,0 +1,57 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.net.ahc;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class AdvancedHttpRequestTest {
@Mock
private AdvancedHttpClient ahc;
@Test
public void testSelf()
{
AdvancedHttpRequest ahr = new AdvancedHttpRequest(ahc, HttpMethod.GET, "https://www.scm-manager.org");
assertEquals(AdvancedHttpRequest.class, ahr.self().getClass());
}
}

View File

@@ -0,0 +1,169 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.net.ahc;
import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class AdvancedHttpRequestWithBodyTest {
@Mock
private AdvancedHttpClient ahc;
@Mock
private ContentTransformer transformer;
private AdvancedHttpRequestWithBody request;
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@Before
public void before(){
request = new AdvancedHttpRequestWithBody(ahc, HttpMethod.PUT, "https://www.scm-manager.org");
}
@Test
public void testContentLength()
{
request.contentLength(12l);
assertEquals("12", request.getHeaders().get("Content-Length").iterator().next());
}
@Test
public void testContentType(){
request.contentType("text/plain");
assertEquals("text/plain", request.getHeaders().get("Content-Type").iterator().next());
}
@Test
public void testFileContent() throws IOException{
File file = tempFolder.newFile();
request.fileContent(file);
assertThat(request.getContent(), instanceOf(FileContent.class));
}
@Test
public void testRawContent() throws IOException {
request.rawContent("test".getBytes(Charsets.UTF_8));
assertThat(request.getContent(), instanceOf(RawContent.class));
}
@Test
public void testRawContentWithByteSource() throws IOException {
ByteSource bs = ByteSource.wrap("test".getBytes(Charsets.UTF_8));
request.rawContent(bs);
assertThat(request.getContent(), instanceOf(ByteSourceContent.class));
}
@Test
public void testFormContent(){
FormContentBuilder builder = request.formContent();
assertNotNull(builder);
builder.build();
assertThat(request.getContent(), instanceOf(StringContent.class));
}
@Test
public void testStringContent(){
request.stringContent("test");
assertThat(request.getContent(), instanceOf(StringContent.class));
}
@Test
public void testStringContentWithCharset(){
request.stringContent("test", Charsets.UTF_8);
assertThat(request.getContent(), instanceOf(StringContent.class));
}
@Test
public void testXmlContent() throws IOException{
when(ahc.createTransformer(String.class, ContentType.XML)).thenReturn(transformer);
when(transformer.marshall("<root />")).thenReturn(ByteSource.wrap("<root></root>".getBytes(Charsets.UTF_8)));
Content content = request.xmlContent("<root />").getContent();
assertThat(content, instanceOf(ByteSourceContent.class));
ByteSourceContent bsc = (ByteSourceContent) content;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bsc.process(baos);
assertEquals("<root></root>", baos.toString("UTF-8"));
}
@Test
public void testJsonContent() throws IOException{
when(ahc.createTransformer(String.class, ContentType.JSON)).thenReturn(transformer);
when(transformer.marshall("{}")).thenReturn(ByteSource.wrap("{'root': {}}".getBytes(Charsets.UTF_8)));
Content content = request.jsonContent("{}").getContent();
assertThat(content, instanceOf(ByteSourceContent.class));
ByteSourceContent bsc = (ByteSourceContent) content;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bsc.process(baos);
assertEquals("{'root': {}}", baos.toString("UTF-8"));
}
@Test
public void testTransformedContent() throws IOException{
when(ahc.createTransformer(String.class, "text/plain")).thenReturn(transformer);
when(transformer.marshall("hello")).thenReturn(ByteSource.wrap("hello world".getBytes(Charsets.UTF_8)));
Content content = request.transformedContent("text/plain", "hello").getContent();
assertThat(content, instanceOf(ByteSourceContent.class));
ByteSourceContent bsc = (ByteSourceContent) content;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bsc.process(baos);
assertEquals("hello world", baos.toString("UTF-8"));
}
@Test
public void testSelf()
{
assertEquals(AdvancedHttpRequestWithBody.class, request.self().getClass());
}
}

View File

@@ -0,0 +1,214 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.net.ahc;
import com.google.common.base.Charsets;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class AdvancedHttpResponseTest {
@Mock(answer = Answers.CALLS_REAL_METHODS)
private AdvancedHttpResponse response;
@Mock
private ContentTransformer transformer;
@Test
public void testContent() throws IOException
{
ByteSource bs = ByteSource.wrap("test123".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
byte[] data = response.content();
assertEquals("test123", new String(data, Charsets.UTF_8));
}
@Test
public void testContentWithoutByteSource() throws IOException
{
assertNull(response.content());
}
@Test
public void testContentAsString() throws IOException
{
ByteSource bs = ByteSource.wrap("123test".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
assertEquals("123test", response.contentAsString());
}
@Test
public void testContentAsStingWithoutByteSource() throws IOException
{
assertNull(response.contentAsString());
}
@Test
public void testContentAsReader() throws IOException
{
ByteSource bs = ByteSource.wrap("abc123".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
assertEquals("abc123", CharStreams.toString(response.contentAsReader()));
}
@Test
public void testContentAsReaderWithoutByteSource() throws IOException
{
assertNull(response.contentAsReader());
}
@Test
public void testContentAsStream() throws IOException
{
ByteSource bs = ByteSource.wrap("cde456".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
byte[] data = ByteStreams.toByteArray(response.contentAsStream());
assertEquals("cde456", new String(data, Charsets.UTF_8));
}
@Test
public void testContentAsStreamWithoutByteSource() throws IOException
{
assertNull(response.contentAsStream());
}
@Test
public void testContentFromJson() throws IOException{
ByteSource bs = ByteSource.wrap("{}".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
when(response.createTransformer(String.class, ContentType.JSON)).thenReturn(transformer);
when(transformer.unmarshall(String.class, bs)).thenReturn("{root: null}");
String c = response.contentFromJson(String.class);
assertEquals("{root: null}", c);
}
@Test
public void testContentFromXml() throws IOException{
ByteSource bs = ByteSource.wrap("<root />".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
when(response.createTransformer(String.class, ContentType.XML)).thenReturn(transformer);
when(transformer.unmarshall(String.class, bs)).thenReturn("<root></root>");
String c = response.contentFromXml(String.class);
assertEquals("<root></root>", c);
}
@Test(expected = ContentTransformerException.class)
public void testContentTransformedWithoutHeader() throws IOException{
Multimap<String,String> map = LinkedHashMultimap.create();
when(response.getHeaders()).thenReturn(map);
response.contentTransformed(String.class);
}
@Test
public void testContentTransformedFromHeader() throws IOException{
Multimap<String,String> map = LinkedHashMultimap.create();
map.put("Content-Type", "text/plain");
when(response.getHeaders()).thenReturn(map);
when(response.createTransformer(String.class, "text/plain")).thenReturn(
transformer);
ByteSource bs = ByteSource.wrap("hello".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
when(transformer.unmarshall(String.class, bs)).thenReturn("hello world");
String v = response.contentTransformed(String.class);
assertEquals("hello world", v);
}
@Test
public void testContentTransformed() throws IOException{
when(response.createTransformer(String.class, "text/plain")).thenReturn(
transformer);
ByteSource bs = ByteSource.wrap("hello".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
when(transformer.unmarshall(String.class, bs)).thenReturn("hello world");
String v = response.contentTransformed(String.class, "text/plain");
assertEquals("hello world", v);
}
@Test
public void testContentTransformedWithoutByteSource() throws IOException{
assertNull(response.contentTransformed(String.class, "text/plain"));
}
@Test
public void testGetFirstHeader() throws IOException
{
Multimap<String,String> mm = LinkedHashMultimap.create();
mm.put("Test", "One");
mm.put("Test-2", "One");
mm.put("Test-2", "Two");
when(response.getHeaders()).thenReturn(mm);
assertEquals("One", response.getFirstHeader("Test"));
assertEquals("One", response.getFirstHeader("Test-2"));
assertNull(response.getFirstHeader("Test-3"));
}
@Test
public void testIsSuccessful() throws IOException
{
// successful
when(response.getStatus()).thenReturn(200);
assertTrue(response.isSuccessful());
when(response.getStatus()).thenReturn(201);
assertTrue(response.isSuccessful());
when(response.getStatus()).thenReturn(204);
assertTrue(response.isSuccessful());
when(response.getStatus()).thenReturn(301);
assertTrue(response.isSuccessful());
// not successful
when(response.getStatus()).thenReturn(400);
assertFalse(response.isSuccessful());
when(response.getStatus()).thenReturn(404);
assertFalse(response.isSuccessful());
when(response.getStatus()).thenReturn(500);
assertFalse(response.isSuccessful());
when(response.getStatus()).thenReturn(199);
assertFalse(response.isSuccessful());
}
}

View File

@@ -0,0 +1,142 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.net.ahc;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import java.io.IOException;
import java.util.Collection;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class BaseHttpRequestTest {
@Mock
private AdvancedHttpClient ahc;
private BaseHttpRequest<AdvancedHttpRequest> request;
@Before
public void before(){
request = new AdvancedHttpRequest(ahc, HttpMethod.GET, "https://www.scm-manager.org");
}
@Test
public void testBasicAuth()
{
request.basicAuth("tricia", "mcmillian123");
Multimap<String,String> headers = request.getHeaders();
assertEquals("Basic dHJpY2lhOm1jbWlsbGlhbjEyMw==", headers.get("Authorization").iterator().next());
}
@Test
public void testQueryString(){
request.queryString("a", "b");
assertEquals("https://www.scm-manager.org?a=b", request.getUrl());
}
@Test
public void testQueryStringMultiple(){
request.queryString("a", "b");
request.queryString("c", "d", "e");
assertEquals("https://www.scm-manager.org?a=b&c=d&c=e", request.getUrl());
}
@Test
public void testQueryStringEncoded(){
request.queryString("a", "äüö");
assertEquals("https://www.scm-manager.org?a=%C3%A4%C3%BC%C3%B6", request.getUrl());
}
@Test
public void testQueryStrings(){
Iterable<? extends Object> i1 = Lists.newArrayList("b");
Iterable<? extends Object> i2 = Lists.newArrayList("d", "e");
request.queryStrings("a", i1);
request.queryStrings("c", i2);
assertEquals("https://www.scm-manager.org?a=b&c=d&c=e", request.getUrl());
}
@Test
public void testQuerqStringNullValue(){
request.queryString("a", null, "b");
assertEquals("https://www.scm-manager.org?a=&a=b", request.getUrl());
}
@Test
public void testHeader(){
request.header("a", "b");
assertEquals("b", request.getHeaders().get("a").iterator().next());
}
@Test
public void testHeaderMultiple(){
request.header("a", "b", "c", "d");
Collection<String> values = request.getHeaders().get("a");
assertThat(values, contains("b", "c", "d"));
}
@Test
public void testRequest() throws IOException{
request.request();
verify(ahc).request(request);
}
@Test
public void testBuilderMethods(){
Iterable<? extends Object> i1 = Lists.newArrayList("b");
assertThat(request.decodeGZip(true), instanceOf(AdvancedHttpRequest.class));
assertTrue(request.isDecodeGZip());
assertThat(request.disableCertificateValidation(true), instanceOf(AdvancedHttpRequest.class));
assertTrue(request.isDisableCertificateValidation());
assertThat(request.disableHostnameValidation(true), instanceOf(AdvancedHttpRequest.class));
assertTrue(request.isDisableHostnameValidation());
assertThat(request.ignoreProxySettings(true), instanceOf(AdvancedHttpRequest.class));
assertTrue(request.isIgnoreProxySettings());
assertThat(request.header("a", "b"), instanceOf(AdvancedHttpRequest.class));
assertThat(request.headers("a", i1), instanceOf(AdvancedHttpRequest.class));
assertThat(request.queryString("a", "b"), instanceOf(AdvancedHttpRequest.class));
assertThat(request.queryStrings("a", i1), instanceOf(AdvancedHttpRequest.class));
}
}

View File

@@ -0,0 +1,74 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.net.ahc;
import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class ByteSourceContentTest {
@Mock
private AdvancedHttpRequestWithBody request;
@Test
public void testPrepareRequest() throws IOException
{
ByteSource source = ByteSource.wrap("abc".getBytes(Charsets.UTF_8));
ByteSourceContent content = new ByteSourceContent(source);
content.prepare(request);
verify(request).contentLength(3l);
}
@Test
public void testProcess() throws IOException{
ByteSource source = ByteSource.wrap("abc".getBytes(Charsets.UTF_8));
ByteSourceContent content = new ByteSourceContent(source);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
content.process(baos);
assertEquals("abc", baos.toString("UTF-8"));
}
}

View File

@@ -0,0 +1,116 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.net.ahc;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
//~--- JDK imports ------------------------------------------------------------
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class FileContentTest
{
/**
* Method description
*
*
* @throws IOException
*/
@Test
public void testPrepareRequest() throws IOException
{
FileContent content = create("abc");
content.prepare(request);
verify(request).contentLength(3l);
}
/**
* Method description
*
*
* @throws IOException
*/
@Test
public void testProcess() throws IOException
{
FileContent content = create("abc");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
content.process(baos);
assertEquals("abc", baos.toString("UTF-8"));
}
private FileContent create(String value) throws IOException
{
File file = temp.newFile();
Files.write("abc", file, Charsets.UTF_8);
return new FileContent(file);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@Rule
public TemporaryFolder temp = new TemporaryFolder();
/** Field description */
@Mock
private AdvancedHttpRequestWithBody request;
}

View File

@@ -0,0 +1,95 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.net.ahc;
import com.google.common.collect.Lists;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class FormContentBuilderTest {
@Mock
private AdvancedHttpRequestWithBody request;
@InjectMocks
private FormContentBuilder builder;
@Test
public void testFieldEncoding()
{
builder.field("a", "ü", "ä", "ö").build();
assertContent("a=%C3%BC&a=%C3%A4&a=%C3%B6");
}
@Test
public void testBuild()
{
builder.field("a", "b").build();
assertContent("a=b");
verify(request).contentType("application/x-www-form-urlencoded");
}
@Test
public void testFieldWithArray()
{
builder.field("a", "b").field("c", "d", "e").build();
assertContent("a=b&c=d&c=e");
}
@Test
public void testFieldWithIterable()
{
Iterable<? extends Object> i1 = Lists.newArrayList("b");
builder.fields("a", i1)
.fields("c", Lists.newArrayList("d", "e"))
.build();
assertContent("a=b&c=d&c=e");
}
private void assertContent(String content){
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(request).stringContent(captor.capture());
assertEquals(content, captor.getValue());
}
}

View File

@@ -0,0 +1,72 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.net.ahc;
import com.google.common.base.Charsets;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class RawContentTest {
@Mock
private AdvancedHttpRequestWithBody request;
@Test
public void testPrepareRequest()
{
RawContent raw = new RawContent("abc".getBytes(Charsets.UTF_8));
raw.prepare(request);
verify(request).contentLength(3);
}
@Test
public void testProcess() throws IOException
{
RawContent raw = new RawContent("abc".getBytes(Charsets.UTF_8));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
raw.process(baos);
assertEquals("abc", baos.toString("UTF-8"));
}
}

View File

@@ -0,0 +1,59 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.net.ahc;
import com.google.common.base.Charsets;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Sebastian Sdorra
*/
public class StringContentTest {
@Test
public void testStringContent()
{
StringContent sc = new StringContent("abc", Charsets.UTF_8);
assertEquals("abc", new String(sc.getData()));
}
@Test
public void testStringContentWithCharset()
{
StringContent sc = new StringContent("üäö", Charsets.ISO_8859_1);
assertEquals("üäö", new String(sc.getData(), Charsets.ISO_8859_1));
}
}

View File

@@ -0,0 +1,141 @@
/***
* Copyright (c) 2015, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* https://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.api;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.Person;
import sonia.scm.repository.PreProcessorUtil;
import sonia.scm.repository.Repository;
import sonia.scm.repository.spi.HookChangesetProvider;
import sonia.scm.repository.spi.HookChangesetRequest;
import sonia.scm.repository.spi.HookChangesetResponse;
import sonia.scm.repository.spi.HookContextProvider;
/**
* Unit tests for {@link HookContext}.
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class HookContextTest {
@Mock
private HookContextProvider provider;
@Mock
private Repository repository;
@Mock
private PreProcessorUtil preProcessorUtil;
@Mock
private HookChangesetProvider changesetProvider;
@InjectMocks
private HookContext context;
/**
* Set up mocks for upcoming test.
*/
@Before
public void setUpMocks(){
when(repository.getName()).thenReturn("test");
when(provider.getChangesetProvider()).thenReturn(changesetProvider);
when(provider.getSupportedFeatures()).thenReturn(Sets.newHashSet(HookFeature.CHANGESET_PROVIDER));
List<Changeset> changesets = Lists.newArrayList(new Changeset("1", Long.MIN_VALUE, new Person("Trillian")));
HookChangesetResponse response = new HookChangesetResponse(changesets);
when(changesetProvider.handleRequest(any(HookChangesetRequest.class))).thenReturn(response);
}
/**
* Tests {@link HookContext#getBranchProvider()}.
*/
@Test
public void testGetBranchProvider() {
context.getBranchProvider();
verify(provider).getBranchProvider();
}
/**
* Tests {@link HookContext#getTagProvider()}.
*/
@Test
public void testGetTagProvider() {
context.getTagProvider();
verify(provider).getTagProvider();
}
/**
* Tests {@link HookContext#getMessageProvider()}.
*/
@Test
public void testGetMessageProvider() {
context.getMessageProvider();
verify(provider).getMessageProvider();
}
/**
* Tests {@link HookContext#getChangesetProvider()}.
*/
@Test
public void testGetChangesetProvider() {
HookChangesetBuilder builder = context.getChangesetProvider();
List<Changeset> changesets = builder.getChangesetList();
assertNotNull(changesets);
assertEquals("1", changesets.get(0).getId());
}
/**
* Tests {@link HookContext#isFeatureSupported(sonia.scm.repository.api.HookFeature)}.
*/
@Test
public void testIsFeatureSupported(){
assertTrue(context.isFeatureSupported(HookFeature.CHANGESET_PROVIDER));
assertFalse(context.isFeatureSupported(HookFeature.BRANCH_PROVIDER));
}
}

View File

@@ -0,0 +1,136 @@
/***
* Copyright (c) 2015, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* https://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository.spi;
import java.util.Collections;
import java.util.Set;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import sonia.scm.repository.api.HookException;
import sonia.scm.repository.api.HookFeature;
import sonia.scm.repository.api.HookFeatureIsNotSupportedException;
/**
* Unit tests for {@link HookContextProvider}.
*
* @author Sebastian Sdorra
*/
public class HookContextProviderTest {
/**
* Expected exception rule.
*/
@Rule
public ExpectedException expectedException = ExpectedException.none();
private final HookContextProvider simpleHookContextProvider = new HookContextProvider() {
@Override
public Set<HookFeature> getSupportedFeatures() {
return Collections.emptySet();
}
};
/**
* Tests {@link HookContextProvider#getSupportedFeatures()}.
*/
@Test
public void testGetSupportedFeatures() {
assertThat(simpleHookContextProvider.getSupportedFeatures(), empty());
}
/**
* Tests {@link HookContextProvider#getBranchProvider()}.
*/
@Test
public void testGetBranchProvider(){
expectNotSupported(HookFeature.BRANCH_PROVIDER);
simpleHookContextProvider.getBranchProvider();
}
/**
* Tests {@link HookContextProvider#getTagProvider()}.
*/
@Test
public void testGetTagProvider(){
expectNotSupported(HookFeature.TAG_PROVIDER);
simpleHookContextProvider.getTagProvider();
}
/**
* Tests {@link HookContextProvider#getChangesetProvider()}.
*/
@Test
public void testGetChangesetProvider(){
expectNotSupported(HookFeature.CHANGESET_PROVIDER);
simpleHookContextProvider.getChangesetProvider();
}
/**
* Tests {@link HookContextProvider#createMessageProvider()}.
*/
@Test
public void testCreateMessageProvider(){
expectNotSupported(HookFeature.MESSAGE_PROVIDER);
simpleHookContextProvider.createMessageProvider();
}
/**
* Tests {@link HookContextProvider#getMessageProvider()}.
*/
@Test
public void testGetMessageProvider(){
expectNotSupported(HookFeature.MESSAGE_PROVIDER);
simpleHookContextProvider.getMessageProvider();
}
/**
* Tests {@link HookContextProvider#getMessageProvider()} with disconnected client.
*/
@Test
public void testGetMessageProviderDisconnected(){
expectedException.expect(HookException.class);
expectedException.expectMessage(containsString("message provider"));
simpleHookContextProvider.handleClientDisconnect();
simpleHookContextProvider.getMessageProvider();
}
private void expectNotSupported(HookFeature feature){
expectedException.expect(HookFeatureIsNotSupportedException.class);
expectedException.expectMessage(containsString(feature.toString()));
}
}

View File

@@ -0,0 +1,110 @@
/**
* Copyright (c) 2014, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.security;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
import sonia.scm.SCMContextProvider;
/**
* Unit tests for {@link DefaultCipherHandler}.
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class DefaultCipherHandlerTest {
@Mock
private SCMContextProvider context;
@Mock
private KeyGenerator keyGenerator;
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
/**
* Tests loading and storing default key.
*
* @throws IOException
*/
@Test
public void testLoadingAndStoringDefaultKey() throws IOException {
File baseDirectory = tempFolder.newFolder();
when(context.getBaseDirectory()).thenReturn(baseDirectory);
when(keyGenerator.createKey()).thenReturn("secret");
DefaultCipherHandler cipher = new DefaultCipherHandler(context, keyGenerator);
File configDirectory = new File(baseDirectory, "config");
assertTrue(new File(configDirectory, DefaultCipherHandler.CIPHERKEY_FILENAME).exists());
// plain text for assertion
String plain = "hallo123";
// encrypt value with new generated key
String encrypted = cipher.encode(plain);
// load key from disk
cipher = new DefaultCipherHandler(context, keyGenerator);
// decrypt with loaded key
assertEquals(plain, cipher.decode(encrypted));
}
/**
* Test encode and decode method with a separate key.
*/
@Test
public void testEncodeDecodeWithSeparateKey(){
char[] key = "testkey".toCharArray();
DefaultCipherHandler cipher = new DefaultCipherHandler("somekey");
assertEquals("hallo123", cipher.decode(key, cipher.encode(key, "hallo123")));
}
/**
* Test encode and decode method with the default key.
*/
@Test
public void testEncodeDecodeWithDefaultKey() {
DefaultCipherHandler cipher = new DefaultCipherHandler("testkey");
assertEquals("hallo123", cipher.decode(cipher.encode("hallo123")));
}
}

View File

@@ -168,6 +168,136 @@ public class HttpUtilTest
HttpUtil.checkForCRLFInjection("abcka");
}
/**
* Method description
*
*/
@Test
public void testCreateBaseUrl()
{
HttpServletRequest request = mock(HttpServletRequest.class);
String url = "https://www.scm-manager.org/test/as/db";
when(request.getRequestURL()).thenReturn(new StringBuffer(url));
when(request.getRequestURI()).thenReturn("/test/as/db");
when(request.getContextPath()).thenReturn("/scm");
assertEquals("https://www.scm-manager.org/scm",
HttpUtil.createBaseUrl(request));
}
/**
* Method description
*
*/
@Test
public void testCreateForwardedUrl()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader(HttpUtil.HEADER_X_FORWARDED_HOST)).thenReturn(
"www.scm-manager.org");
when(request.getHeader(HttpUtil.HEADER_X_FORWARDED_PROTO)).thenReturn(
"https");
when(request.getHeader(HttpUtil.HEADER_X_FORWARDED_PORT)).thenReturn("443");
when(request.getContextPath()).thenReturn("/scm");
assertEquals("https://www.scm-manager.org:443/scm",
HttpUtil.createForwardedBaseUrl(request));
}
/**
* Method description
*
*/
@Test
public void testCreateForwardedUrlWithPortAndProtoFromRequest()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader(HttpUtil.HEADER_X_FORWARDED_HOST)).thenReturn(
"www.scm-manager.org");
when(request.getScheme()).thenReturn("https");
when(request.getServerPort()).thenReturn(443);
when(request.getContextPath()).thenReturn("/scm");
assertEquals("https://www.scm-manager.org:443/scm",
HttpUtil.createForwardedBaseUrl(request));
}
/**
* Method description
*
*/
@Test
public void testCreateForwardedUrlWithPortInHost()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader(HttpUtil.HEADER_X_FORWARDED_HOST)).thenReturn(
"www.scm-manager.org:443");
when(request.getHeader(HttpUtil.HEADER_X_FORWARDED_PROTO)).thenReturn(
"https");
when(request.getContextPath()).thenReturn("/scm");
assertEquals("https://www.scm-manager.org:443/scm",
HttpUtil.createForwardedBaseUrl(request));
}
/**
* Method description
*
*/
@Test
public void testCreateForwardedUrlWithPortInHostAndPortHeader()
{
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getHeader(HttpUtil.HEADER_X_FORWARDED_HOST)).thenReturn(
"www.scm-manager.org:80");
when(request.getHeader(HttpUtil.HEADER_X_FORWARDED_PROTO)).thenReturn(
"https");
when(request.getHeader(HttpUtil.HEADER_X_FORWARDED_PORT)).thenReturn("443");
when(request.getContextPath()).thenReturn("/scm");
assertEquals("https://www.scm-manager.org:443/scm",
HttpUtil.createForwardedBaseUrl(request));
}
/**
* Method description
*
*/
@Test
public void testGetCompleteUrl()
{
HttpServletRequest request = mock(HttpServletRequest.class);
String url = "https://www.scm-manager.org/test/as/db";
when(request.getRequestURL()).thenReturn(new StringBuffer(url));
when(request.getRequestURI()).thenReturn("/test/as/db");
when(request.getScheme()).thenReturn("https");
when(request.getServerPort()).thenReturn(443);
when(request.getContextPath()).thenReturn("/scm");
assertEquals("https://www.scm-manager.org/scm",
HttpUtil.getCompleteUrl(request));
when(request.getHeader(HttpUtil.HEADER_X_FORWARDED_HOST)).thenReturn(
"scm.scm-manager.org");
assertEquals("https://scm.scm-manager.org:443/scm",
HttpUtil.getCompleteUrl(request));
}
/**
* Method description
*
*/
@Test
public void testIsForwarded()
{
HttpServletRequest request = mock(HttpServletRequest.class);
assertFalse(HttpUtil.isForwarded(request));
when(request.getHeader(HttpUtil.HEADER_X_FORWARDED_HOST)).thenReturn("");
assertFalse(HttpUtil.isForwarded(request));
when(request.getHeader(HttpUtil.HEADER_X_FORWARDED_HOST)).thenReturn("ser");
assertTrue(HttpUtil.isForwarded(request));
}
/**
* Method description
*

View File

@@ -57,8 +57,9 @@ public class UrlBuilderTest
builder.appendParameter("i", 123).appendParameter("s", "abc");
builder.appendParameter("b", true).appendParameter("l", 321l);
assertEquals("http://www.short.de?i=123&s=abc&b=true&l=321",
builder.toString());
assertEquals("http://www.short.de?i=123&s=abc&b=true&l=321", builder.toString());
builder.appendParameter("c", "a b");
assertEquals("http://www.short.de?i=123&s=abc&b=true&l=321&c=a%20b", builder.toString());
}
/**

View File

@@ -0,0 +1,187 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.web;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Charsets;
import com.google.common.collect.Sets;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import sonia.scm.cache.Cache;
import sonia.scm.cache.CacheManager;
import sonia.scm.util.HttpUtil;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
//~--- JDK imports ------------------------------------------------------------
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
/**
*
* @author Sebastian Sdorra <sebastian.sdorra@triology.de>
*/
@SuppressWarnings("unchecked")
@RunWith(MockitoJUnitRunner.class)
public class UserAgentParserTest
{
/** Field description */
private static final String UA_1 = "mozilla/5.0";
/** Field description */
private static final String UA_2 = "wget/1.5.3";
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*/
@Before
public void prepare()
{
Set<UserAgentProvider> providers = Sets.newHashSet(provider1, provider2);
when(cacheManager.getCache(UserAgentParser.CACHE_NAME)).thenReturn(cache);
parser = new UserAgentParser(providers, cacheManager);
}
/**
* Method description
*
*/
@Test
public void testDefaultValues()
{
UserAgent ua = parser.parse(UA_1);
assertEquals(Charsets.ISO_8859_1, ua.getBasicAuthenticationCharset());
assertTrue(ua.isBrowser());
}
/**
* Method description
*
*/
@Test
public void testParse()
{
UserAgent ua = UserAgent.builder("UA1").build();
when(provider1.parseUserAgent(UA_1)).thenReturn(ua);
UserAgent ua2 = UserAgent.builder("UA2").build();
when(provider2.parseUserAgent(UA_2)).thenReturn(ua2);
assertEquals(ua, parser.parse(UA_1));
assertEquals(ua2, parser.parse(UA_2));
}
/**
* Method description
*
*/
@Test
public void testParseHttpServletRequest()
{
when(request.getHeader(HttpUtil.HEADER_USERAGENT)).thenReturn(UA_2);
UserAgent ua = UserAgent.builder("UA2").build();
when(provider1.parseUserAgent(UA_2)).thenReturn(ua);
assertEquals(ua, parser.parse(request));
}
/**
* Method description
*
*/
@Test
public void testParseNotFound()
{
assertEquals(UserAgentParser.UNKNOWN, parser.parse(UA_1));
assertEquals(UserAgentParser.UNKNOWN, parser.parse(UA_2));
}
/**
* Method description
*
*/
@Test
public void testParseWithCache()
{
UserAgent ua = UserAgent.builder("UA").build();
when(cache.get(UA_1)).thenReturn(ua);
assertEquals(ua, parser.parse(UA_1));
assertEquals(UserAgentParser.UNKNOWN, parser.parse(UA_2));
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@Mock
private Cache cache;
/** Field description */
@Mock
private CacheManager cacheManager;
/** Field description */
private UserAgentParser parser;
/** Field description */
@Mock
private UserAgentProvider provider1;
/** Field description */
@Mock
private UserAgentProvider provider2;
/** Field description */
@Mock
private HttpServletRequest request;
}