You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/12/18 14:53:43 UTC
[1/2] httpcomponents-core git commit: Added ComplexCancellable that
represents a sequence of Cancellable processes
Repository: httpcomponents-core
Updated Branches:
refs/heads/master 496f5bf39 -> 694db38c1
Added ComplexCancellable that represents a sequence of Cancellable processes
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/694db38c
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/694db38c
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/694db38c
Branch: refs/heads/master
Commit: 694db38c1ec148a0d8e2f37aa07f3f6f2089a5d2
Parents: a564f2a
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Mon Dec 18 12:03:34 2017 +0100
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Mon Dec 18 15:51:00 2017 +0100
----------------------------------------------------------------------
.../hc/core5/concurrent/ComplexCancellable.java | 78 ++++++++++++++++++++
.../concurrent/TestComplexCancellable.java | 53 +++++++++++++
2 files changed, 131 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/694db38c/httpcore5/src/main/java/org/apache/hc/core5/concurrent/ComplexCancellable.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/concurrent/ComplexCancellable.java b/httpcore5/src/main/java/org/apache/hc/core5/concurrent/ComplexCancellable.java
new file mode 100644
index 0000000..83b61d0
--- /dev/null
+++ b/httpcore5/src/main/java/org/apache/hc/core5/concurrent/ComplexCancellable.java
@@ -0,0 +1,78 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.concurrent;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.hc.core5.util.Args;
+
+/**
+ * Cancellable that represents a sequence of other {@link Cancellable} processes.
+ * Dependent process will get cancelled if the future itself is cancelled.
+ *
+ * @since 5.0
+ */
+public final class ComplexCancellable implements Cancellable, CancellableDependency {
+
+ private final AtomicReference<Cancellable> dependencyRef;
+ private final AtomicBoolean cancelled;
+
+ public ComplexCancellable() {
+ this.dependencyRef = new AtomicReference<>(null);
+ this.cancelled = new AtomicBoolean(false);
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled.get();
+ }
+
+ @Override
+ public void setDependency(final Cancellable dependency) {
+ Args.notNull(dependency, "dependency");
+ if (!cancelled.get()) {
+ dependencyRef.set(dependency);
+ } else {
+ dependency.cancel();
+ }
+ }
+
+ @Override
+ public boolean cancel() {
+ if (cancelled.compareAndSet(false, true)) {
+ final Cancellable dependency = dependencyRef.getAndSet(null);
+ if (dependency != null) {
+ dependency.cancel();
+ }
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/694db38c/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestComplexCancellable.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestComplexCancellable.java b/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestComplexCancellable.java
new file mode 100644
index 0000000..8013dd9
--- /dev/null
+++ b/httpcore5/src/test/java/org/apache/hc/core5/concurrent/TestComplexCancellable.java
@@ -0,0 +1,53 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.concurrent;
+
+import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestComplexCancellable {
+
+ @Test
+ public void testCancelled() throws Exception {
+ final ComplexCancellable cancellable = new ComplexCancellable();
+
+ final BasicFuture<Object> dependency1 = new BasicFuture<>(null);
+ cancellable.setDependency(dependency1);
+
+ Assert.assertFalse(cancellable.isCancelled());
+
+ cancellable.cancel();
+ Assert.assertThat(cancellable.isCancelled(), CoreMatchers.is(true));
+ Assert.assertThat(dependency1.isCancelled(), CoreMatchers.is(true));
+
+ final BasicFuture<Object> dependency2 = new BasicFuture<>(null);
+ cancellable.setDependency(dependency2);
+ Assert.assertThat(dependency2.isCancelled(), CoreMatchers.is(true));
+ }
+
+}
[2/2] httpcomponents-core git commit: Removed #equals and #hashCode
methods from BasicHeader added by HTTPCORE-439;
HeaderGroup#removeHeader to remove the first header matching the header name /
value passed as a parameter.
Posted by ol...@apache.org.
Removed #equals and #hashCode methods from BasicHeader added by HTTPCORE-439; HeaderGroup#removeHeader to remove the first header matching the header name / value passed as a parameter.
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/a564f2a9
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/a564f2a9
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/a564f2a9
Branch: refs/heads/master
Commit: a564f2a9c5205547ed184871108aa35be0ace752
Parents: 496f5bf
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Thu Dec 14 23:01:11 2017 +0100
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Mon Dec 18 15:51:00 2017 +0100
----------------------------------------------------------------------
.../hc/core5/http/message/BasicHeader.java | 22 ---
.../hc/core5/http/message/HeaderGroup.java | 10 +-
.../hc/core5/http/message/TestBasicHeader.java | 136 +++----------------
.../core5/http/message/TestNameValuePair.java | 120 ++++++++++++++++
4 files changed, 145 insertions(+), 143 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/a564f2a9/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHeader.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHeader.java b/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHeader.java
index f229ee4..36ca08c 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHeader.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHeader.java
@@ -28,14 +28,12 @@
package org.apache.hc.core5.http.message;
import java.io.Serializable;
-import java.util.Locale;
import java.util.Objects;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.util.Args;
-import org.apache.hc.core5.util.LangUtils;
/**
* Immutable {@link Header}.
@@ -78,18 +76,6 @@ public class BasicHeader implements Header, Cloneable, Serializable {
}
@Override
- public boolean equals(final Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj instanceof BasicHeader) {
- final BasicHeader that = (BasicHeader) obj;
- return this.name.equalsIgnoreCase(that.name) && LangUtils.equals(this.value, that.value);
- }
- return false;
- }
-
- @Override
public String getName() {
return name;
}
@@ -100,14 +86,6 @@ public class BasicHeader implements Header, Cloneable, Serializable {
}
@Override
- public int hashCode() {
- int hash = LangUtils.HASH_SEED;
- hash = LangUtils.hashCode(hash, this.name.toLowerCase(Locale.ROOT));
- hash = LangUtils.hashCode(hash, this.value);
- return hash;
- }
-
- @Override
public boolean isSensitive() {
return this.sensitive;
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/a564f2a9/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java b/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java
index aeffc27..e000649 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java
@@ -38,6 +38,7 @@ import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.MessageHeaders;
import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.util.CharArrayBuffer;
+import org.apache.hc.core5.util.LangUtils;
/**
* A class for combining a set of headers.
@@ -96,7 +97,14 @@ public class HeaderGroup implements MessageHeaders, Serializable {
if (header == null) {
return;
}
- headers.remove(header);
+ for (int i = 0; i < this.headers.size(); i++) {
+ final Header current = this.headers.get(i);
+ if (current == header || current.getName().equalsIgnoreCase(header.getName())
+ && LangUtils.equals(header.getValue(), current.getValue())) {
+ this.headers.remove(current);
+ return;
+ }
+ }
}
/**
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/a564f2a9/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicHeader.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicHeader.java b/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicHeader.java
index db837ff..032ae9e 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicHeader.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestBasicHeader.java
@@ -36,132 +36,28 @@ import org.junit.Test;
public class TestBasicHeader {
@Test
- public void testNullNotEqual() throws Exception {
- final Header header = new BasicHeader("name", "value");
-
- Assert.assertFalse(header.equals(null));
- }
-
- @Test
- public void testObjectNotEqual() throws Exception {
- final Header header = new BasicHeader("name", "value");
-
- Assert.assertFalse(header.equals(new Object()));
- }
-
- @Test
- public void testNameEquals() throws Exception {
- final Header header = new BasicHeader("name", "value");
- final Header header2 = new BasicHeader("NAME", "value");
-
- Assert.assertEquals(header, header2);
- Assert.assertEquals(header.hashCode(), header2.hashCode());
-
- final Header header3 = new BasicHeader("NAME", "value");
- final Header header4 = new BasicHeader("name", "value");
-
- Assert.assertEquals(header3, header4);
- Assert.assertEquals(header3.hashCode(), header4.hashCode());
- }
-
- @Test
- public void testValueEquals() throws Exception {
- final Header header = new BasicHeader("name", "value");
- final Header header2 = new BasicHeader("name", "value");
-
- Assert.assertEquals(header, header2);
- Assert.assertEquals(header.hashCode(), header2.hashCode());
- }
-
- @Test
- public void testNameNotEqual() throws Exception {
- final Header header = new BasicHeader("name", "value");
- final Header header2 = new BasicHeader("name2", "value");
-
- Assert.assertNotEquals(header, header2);
- }
-
- @Test
- public void testValueNotEqual() throws Exception {
- final Header header = new BasicHeader("name", "value");
- final Header header2 = new BasicHeader("name", "value2");
-
- Assert.assertNotEquals(header, header2);
-
- final Header header3 = new BasicHeader("name", "value");
- final Header header4 = new BasicHeader("name", "VALUE");
-
- Assert.assertNotEquals(header3, header4);
-
- final Header header5 = new BasicHeader("name", "VALUE");
- final Header header6 = new BasicHeader("name", "value");
-
- Assert.assertNotEquals(header5, header6);
- }
-
- @Test
- public void testNullValuesEquals() throws Exception {
- final Header header = new BasicHeader("name", null);
- final Header header2 = new BasicHeader("name", null);
-
- Assert.assertEquals(header, header2);
- Assert.assertEquals(header.hashCode(), header2.hashCode());
- }
-
- @Test
- public void testNullValueNotEqual() throws Exception {
- final Header header = new BasicHeader("name", null);
- final Header header2 = new BasicHeader("name", "value");
-
- Assert.assertNotEquals(header, header2);
- }
-
- @Test
- public void testNullValue2NotEqual() throws Exception {
- final Header header = new BasicHeader("name", "value");
- final Header header2 = new BasicHeader("name", null);
-
- Assert.assertNotEquals(header, header2);
+ public void testConstructor() {
+ final Header param = new BasicHeader("name", "value");
+ Assert.assertEquals("name", param.getName());
+ Assert.assertEquals("value", param.getValue());
}
@Test
- public void testEquals() throws Exception {
- final Header header = new BasicHeader("name", "value");
- final Header header2 = new BasicHeader("name", "value");
-
- Assert.assertEquals(header, header);
- Assert.assertEquals(header.hashCode(), header.hashCode());
- Assert.assertEquals(header2, header2);
- Assert.assertEquals(header2.hashCode(), header2.hashCode());
- Assert.assertEquals(header, header2);
- Assert.assertEquals(header.hashCode(), header2.hashCode());
+ public void testInvalidName() {
+ try {
+ new BasicHeader(null, null);
+ Assert.fail("IllegalArgumentException should have been thrown");
+ } catch (final IllegalArgumentException ex) {
+ //expected
+ }
}
@Test
- public void testHashCode() throws Exception {
- final Header header = new BasicHeader("name", null);
- final Header header2 = new BasicHeader("name2", null);
-
- Assert.assertNotEquals(header.hashCode(), header2.hashCode());
-
- final Header header3 = new BasicHeader("name", "value");
- final Header header4 = new BasicHeader("name", "value2");
-
- Assert.assertNotEquals(header3.hashCode(), header4.hashCode());
-
- final Header header5 = new BasicHeader("name", "value");
- final Header header6 = new BasicHeader("name", null);
-
- Assert.assertNotEquals(header5.hashCode(), header6.hashCode());
+ public void testToString() {
+ final Header param1 = new BasicHeader("name1", "value1");
+ Assert.assertEquals("name1: value1", param1.toString());
+ final Header param2 = new BasicHeader("name1", null);
+ Assert.assertEquals("name1: ", param2.toString());
}
- @Test
- public void testSensitive() throws Exception {
- // sensitivity has no affect on equality.
- final Header header = new BasicHeader("name", "value", true);
- final Header header2 = new BasicHeader("name", "value", false);
-
- Assert.assertEquals(header, header2);
- Assert.assertEquals(header.hashCode(), header2.hashCode());
- }
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/a564f2a9/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestNameValuePair.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestNameValuePair.java b/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestNameValuePair.java
index a7eaabf..a3729f4 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestNameValuePair.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/message/TestNameValuePair.java
@@ -62,4 +62,124 @@ public class TestNameValuePair {
Assert.assertEquals("name1", param2.toString());
}
+ @Test
+ public void testNullNotEqual() throws Exception {
+ final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
+
+ Assert.assertFalse(NameValuePair.equals(null));
+ }
+
+ @Test
+ public void testObjectNotEqual() throws Exception {
+ final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
+
+ Assert.assertFalse(NameValuePair.equals(new Object()));
+ }
+
+ @Test
+ public void testNameEquals() throws Exception {
+ final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
+ final NameValuePair NameValuePair2 = new BasicNameValuePair("NAME", "value");
+
+ Assert.assertEquals(NameValuePair, NameValuePair2);
+ Assert.assertEquals(NameValuePair.hashCode(), NameValuePair2.hashCode());
+
+ final NameValuePair NameValuePair3 = new BasicNameValuePair("NAME", "value");
+ final NameValuePair NameValuePair4 = new BasicNameValuePair("name", "value");
+
+ Assert.assertEquals(NameValuePair3, NameValuePair4);
+ Assert.assertEquals(NameValuePair3.hashCode(), NameValuePair4.hashCode());
+ }
+
+ @Test
+ public void testValueEquals() throws Exception {
+ final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
+ final NameValuePair NameValuePair2 = new BasicNameValuePair("name", "value");
+
+ Assert.assertEquals(NameValuePair, NameValuePair2);
+ Assert.assertEquals(NameValuePair.hashCode(), NameValuePair2.hashCode());
+ }
+
+ @Test
+ public void testNameNotEqual() throws Exception {
+ final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
+ final NameValuePair NameValuePair2 = new BasicNameValuePair("name2", "value");
+
+ Assert.assertNotEquals(NameValuePair, NameValuePair2);
+ }
+
+ @Test
+ public void testValueNotEqual() throws Exception {
+ final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
+ final NameValuePair NameValuePair2 = new BasicNameValuePair("name", "value2");
+
+ Assert.assertNotEquals(NameValuePair, NameValuePair2);
+
+ final NameValuePair NameValuePair3 = new BasicNameValuePair("name", "value");
+ final NameValuePair NameValuePair4 = new BasicNameValuePair("name", "VALUE");
+
+ Assert.assertNotEquals(NameValuePair3, NameValuePair4);
+
+ final NameValuePair NameValuePair5 = new BasicNameValuePair("name", "VALUE");
+ final NameValuePair NameValuePair6 = new BasicNameValuePair("name", "value");
+
+ Assert.assertNotEquals(NameValuePair5, NameValuePair6);
+ }
+
+ @Test
+ public void testNullValuesEquals() throws Exception {
+ final NameValuePair NameValuePair = new BasicNameValuePair("name", null);
+ final NameValuePair NameValuePair2 = new BasicNameValuePair("name", null);
+
+ Assert.assertEquals(NameValuePair, NameValuePair2);
+ Assert.assertEquals(NameValuePair.hashCode(), NameValuePair2.hashCode());
+ }
+
+ @Test
+ public void testNullValueNotEqual() throws Exception {
+ final NameValuePair NameValuePair = new BasicNameValuePair("name", null);
+ final NameValuePair NameValuePair2 = new BasicNameValuePair("name", "value");
+
+ Assert.assertNotEquals(NameValuePair, NameValuePair2);
+ }
+
+ @Test
+ public void testNullValue2NotEqual() throws Exception {
+ final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
+ final NameValuePair NameValuePair2 = new BasicNameValuePair("name", null);
+
+ Assert.assertNotEquals(NameValuePair, NameValuePair2);
+ }
+
+ @Test
+ public void testEquals() throws Exception {
+ final NameValuePair NameValuePair = new BasicNameValuePair("name", "value");
+ final NameValuePair NameValuePair2 = new BasicNameValuePair("name", "value");
+
+ Assert.assertEquals(NameValuePair, NameValuePair);
+ Assert.assertEquals(NameValuePair.hashCode(), NameValuePair.hashCode());
+ Assert.assertEquals(NameValuePair2, NameValuePair2);
+ Assert.assertEquals(NameValuePair2.hashCode(), NameValuePair2.hashCode());
+ Assert.assertEquals(NameValuePair, NameValuePair2);
+ Assert.assertEquals(NameValuePair.hashCode(), NameValuePair2.hashCode());
+ }
+
+ @Test
+ public void testHashCode() throws Exception {
+ final NameValuePair NameValuePair = new BasicNameValuePair("name", null);
+ final NameValuePair NameValuePair2 = new BasicNameValuePair("name2", null);
+
+ Assert.assertNotEquals(NameValuePair.hashCode(), NameValuePair2.hashCode());
+
+ final NameValuePair NameValuePair3 = new BasicNameValuePair("name", "value");
+ final NameValuePair NameValuePair4 = new BasicNameValuePair("name", "value2");
+
+ Assert.assertNotEquals(NameValuePair3.hashCode(), NameValuePair4.hashCode());
+
+ final NameValuePair NameValuePair5 = new BasicNameValuePair("name", "value");
+ final NameValuePair NameValuePair6 = new BasicNameValuePair("name", null);
+
+ Assert.assertNotEquals(NameValuePair5.hashCode(), NameValuePair6.hashCode());
+ }
+
}