Skip to content

Commit

Permalink
fix for equals/hashCode in AtmosphereResourceImpl
Browse files Browse the repository at this point in the history
- hashCode implemented based on "uuid" field
- both equals and hashCode null-resistant for "uuid"
- test
  • Loading branch information
elusive-code committed Oct 20, 2014
1 parent 65423a5 commit 3372263
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,13 @@ public boolean equals(Object o) {

AtmosphereResourceImpl that = (AtmosphereResourceImpl) o;

if (!uuid.equals(that.uuid)) return false;
if (uuid != null ? !uuid.equals(that.uuid) : that.uuid != null) return false;

return true;
}

@Override
public int hashCode() {
return uuid != null ? uuid.hashCode() : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

import static org.atmosphere.cpr.ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID;
import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class AtmosphereResourceTest {
private AtmosphereFramework framework;
Expand Down Expand Up @@ -175,4 +174,35 @@ public void run() {

}

@Test
public void testHashCode(){
String uuid = UUID.randomUUID().toString();

AtmosphereRequest request = AtmosphereRequest.newInstance();
request.setAttribute(ApplicationConfig.SUSPENDED_ATMOSPHERE_RESOURCE_UUID,uuid);
AtmosphereResponse response = AtmosphereResponse.newInstance(request);

AtmosphereResourceImpl res0 = new AtmosphereResourceImpl();
res0.initialize(framework.getAtmosphereConfig(),
framework.getBroadcasterFactory().get(),
request, response, null, null);

AtmosphereResourceImpl res1 = new AtmosphereResourceImpl();
res1.initialize(framework.getAtmosphereConfig(),
framework.getBroadcasterFactory().get(),
request, response, null, null);

assertEquals(res0,res1);

HashSet set = new HashSet();
set.add(res0);
set.add(res1);

assertEquals(set.size(),1);
assertTrue(set.contains(res0));
assertTrue(set.contains(res1));
assertEquals(res0,set.iterator().next());
assertEquals(res1,set.iterator().next());
}

}

0 comments on commit 3372263

Please sign in to comment.