[#5514] Fix DiskFileUpload and MemoryFileUpload equals(...) method.

Motivation:

DiskFileUpload and MemoryFileUpload.equals(...) are broken.

Modifications:

Fix implementation and add unit test.

Result:

Equals method are correct now.
This commit is contained in:
Norman Maurer 2016-07-11 08:52:33 +02:00
parent afafadd3d7
commit c735b3e147
5 changed files with 97 additions and 26 deletions

View File

@ -70,16 +70,12 @@ public class DiskFileUpload extends AbstractDiskHttpData implements FileUpload {
@Override
public int hashCode() {
return getName().hashCode();
return FileUploadUtil.hashCode(this);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Attribute)) {
return false;
}
Attribute attribute = (Attribute) o;
return getName().equalsIgnoreCase(attribute.getName());
return o instanceof FileUpload && FileUploadUtil.equals(this, (FileUpload) o);
}
@Override
@ -92,13 +88,7 @@ public class DiskFileUpload extends AbstractDiskHttpData implements FileUpload {
}
public int compareTo(FileUpload o) {
int v;
v = getName().compareToIgnoreCase(o.getName());
if (v != 0) {
return v;
}
// TODO should we compare size ?
return v;
return FileUploadUtil.compareTo(this, o);
}
@Override

View File

@ -0,0 +1,33 @@
/*
* Copyright 2016 The Netty Project
*
* The Netty Project 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.
*/
package io.netty.handler.codec.http.multipart;
final class FileUploadUtil {
private FileUploadUtil() { }
static int hashCode(FileUpload upload) {
return upload.getName().hashCode();
}
static boolean equals(FileUpload upload1, FileUpload upload2) {
return upload1.getName().equalsIgnoreCase(upload2.getName());
}
static int compareTo(FileUpload upload1, FileUpload upload2) {
return upload1.getName().compareToIgnoreCase(upload2.getName());
}
}

View File

@ -64,16 +64,12 @@ public class MemoryFileUpload extends AbstractMemoryHttpData implements FileUplo
@Override
public int hashCode() {
return getName().hashCode();
return FileUploadUtil.hashCode(this);
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Attribute)) {
return false;
}
Attribute attribute = (Attribute) o;
return getName().equalsIgnoreCase(attribute.getName());
return o instanceof FileUpload && FileUploadUtil.equals(this, (FileUpload) o);
}
@Override
@ -86,13 +82,7 @@ public class MemoryFileUpload extends AbstractMemoryHttpData implements FileUplo
}
public int compareTo(FileUpload o) {
int v;
v = getName().compareToIgnoreCase(o.getName());
if (v != 0) {
return v;
}
// TODO should we compare size for instance ?
return v;
return FileUploadUtil.compareTo(this, o);
}
@Override

View File

@ -0,0 +1,29 @@
/*
* Copyright 2016 The Netty Project
*
* The Netty Project 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.
*/
package io.netty.handler.codec.http.multipart;
import org.junit.Assert;
import org.junit.Test;
public class DiskFileUploadTest {
@Test
public final void testDiskFileUploadEquals() {
DiskFileUpload f2 =
new DiskFileUpload("d1", "d1", "application/json", null, null, 100);
Assert.assertEquals(f2, f2);
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright 2016 The Netty Project
*
* The Netty Project 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.
*/
package io.netty.handler.codec.http.multipart;
import org.junit.Assert;
import org.junit.Test;
public class MemoryFileUploadTest {
@Test
public final void testMemoryFileUploadEquals() {
MemoryFileUpload f1 =
new MemoryFileUpload("m1", "m1", "application/json", null, null, 100);
Assert.assertEquals(f1, f1);
}
}