/*
Copyright 2009 Janne Hietamäki
Licensed 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 mungbean;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import jdave.Specification;
import jdave.contract.EqualsHashCodeContract;
import jdave.junit4.JDaveRunner;
import org.junit.runner.RunWith;
@RunWith(JDaveRunner.class)
public class ObjectIdSpec extends Specification<ObjectId> {
public class WithGivenBytes {
public ObjectId create() {
return new ObjectId(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
}
public void toStringReturnsHumanReadableString() {
specify(context.toString(), does.equal("ObjectId(0102030405060708090a0b0c)"));
}
public void returnsIdEncodedToHex() {
specify(new ObjectId("0102030405060708090a0b0c").toHex(), does.equal("0102030405060708090a0b0c"));
}
public void meetsEqualsAndHashCodeContracts() {
specify(context, new EqualsHashCodeContract<ObjectId>() {
@Override
protected ObjectId equal() {
return new ObjectId(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
}
@Override
protected ObjectId nonEqual() {
return new ObjectId(new byte[] { 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
}
@Override
protected ObjectId subType() {
return new ObjectId(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }) {
};
}
});
}
}
public class WithAutoGenerated {
public ObjectId create() {
return new ObjectId();
}
public void isNotEqualToOtherOne() {
specify(context.equals(new ObjectId()), does.equal(false));
}
}
public class IdSequence {
public void isUnique() {
Set<ObjectId> ids = new HashSet<ObjectId>();
for (int a = 0; a < 10000; a++) {
ObjectId i = new ObjectId();
if (ids.contains(i)) {
throw new RuntimeException("ERROR! Duplicate id " + i);
}
ids.add(i);
}
}
}
public class ConcurrentThreads {
public void doNotGeneratedConflictingIds() throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newFixedThreadPool(10);
List<Future<ObjectId>> futures = new ArrayList<Future<ObjectId>>(10000);
for (int a = 0; a < 10000; a++) {
Future<ObjectId> future = executor.submit(new Callable<ObjectId>() {
@Override
public ObjectId call() throws Exception {
return new ObjectId();
}
});
futures.add(future);
}
Set<ObjectId> ids = new HashSet<ObjectId>();
for (Future<ObjectId> id : futures) {
ObjectId i = id.get();
if (ids.contains(i)) {
throw new RuntimeException("ERROR! Duplicate id " + i);
}
ids.add(i);
}
}
}
}