Skip to content

Commit

Permalink
typetools: tests, serialization improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Ketoth Xupack <ketoth.xupack@gmail.com>
  • Loading branch information
KetothXupack committed Oct 11, 2013
1 parent 6d11d86 commit b9668a4
Show file tree
Hide file tree
Showing 17 changed files with 265 additions and 60 deletions.
23 changes: 12 additions & 11 deletions projects/typetools/src/main/java/org/nohope/Interval.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.nohope;

import net.jcip.annotations.Immutable;
import org.apache.commons.lang3.ArrayUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalTime;
Expand All @@ -14,12 +15,12 @@
* Date: 10/4/12
* Time: 5:53 PM
*/
@Immutable
public final class Interval implements Serializable {
private static final long serialVersionUID = 1L;

private final Set<Integer> daysOfWeek = new HashSet<>();
private LocalTime begin;
private LocalTime end;
private final LocalTime begin;
private final LocalTime end;

/**
* @deprecated do not use this constructor directly.
Expand All @@ -28,6 +29,8 @@ public final class Interval implements Serializable {
@SuppressWarnings("unused")
@Deprecated
private Interval() {
begin = null;
end = null;
}

public Interval(@Nonnull final LocalTime begin,
Expand Down Expand Up @@ -77,19 +80,17 @@ public LocalTime getBegin() {
return begin;
}

public Interval setBegin(@Nonnull final LocalTime begin) {
this.begin = begin;
return this;
public Interval withBegin(@Nonnull final LocalTime begin) {
return new Interval(begin, end);
}

@Nonnull
public LocalTime getEnd() {
return end;
}

public Interval setEnd(@Nonnull final LocalTime end) {
this.end = end;
return this;
public Interval withEnd(@Nonnull final LocalTime end) {
return new Interval(begin, end);
}

public boolean contains(@Nonnull final DateTime timestamp) {
Expand Down Expand Up @@ -123,8 +124,8 @@ public boolean equals(final Object o) {
}

final Interval interval = (Interval) o;
return begin.equals(interval.begin)
return begin.isEqual(interval.begin)
&& daysOfWeek.equals(interval.daysOfWeek)
&& end.equals(interval.end);
&& end.isEqual(interval.end);
}
}
31 changes: 31 additions & 0 deletions projects/typetools/src/main/java/org/nohope/SeriesElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ public final class SeriesElement<T extends Serializable> implements Serializable
private final DateTime timestamp;
private final T value;

/**
* @deprecated do not use this constructor directly.
* It's used for jackson serialization only
*/
@Deprecated
@SuppressWarnings("unused")
private SeriesElement() {
this.timestamp = null;
this.value = null;
}

public SeriesElement(@Nonnull final DateTime timestamp,
@Nonnull final T value) {
this.timestamp = timestamp;
Expand All @@ -37,4 +48,24 @@ public T getValue() {
public String toString() {
return JSON.jsonify(this).toString();
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

final SeriesElement that = (SeriesElement) o;
return timestamp.isEqual(timestamp) && value.equals(that.value);
}

@Override
public int hashCode() {
int result = ((Long) timestamp.getMillis()).hashCode();
result = 31 * result + value.hashCode();
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.nohope.typetools;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
Expand All @@ -14,7 +15,7 @@ public static boolean isDoubleCorrect(final Double val) {
return null != val && !(val.isNaN() || val.isInfinite());
}

public static double asDouble(final Double val, final Double defValue) {
public static double asDouble(final Double val, @Nonnull final Double defValue) {
return isDoubleCorrect(val) ? val : defValue;
}

Expand Down
32 changes: 0 additions & 32 deletions projects/typetools/src/main/java/org/nohope/typetools/TMap.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void setUtcTimezone() {
}

/** Sets both system and joda time default timezones to given timezone */
private static void setDefaultTimezone(final String id) {
static void setDefaultTimezone(final String id) {
final DateTimeZone defaultZone = DateTimeZone.forID(id);
DateTimeZone.setDefault(defaultZone);
TimeZone.setDefault(TimeZone.getTimeZone(id));
Expand Down
20 changes: 18 additions & 2 deletions projects/typetools/src/test/java/org/nohope/IntervalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;
import org.junit.Test;
import org.nohope.test.SerializationUtils;

import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -149,9 +150,24 @@ public void equality() {
assertFalse(interval.equals(interval3));
}


@Test
public void serialization() {
//FIXME : test-utils causes cyclic dependency here...
final LocalTime now = new LocalTime(16, 0);
final LocalTime later = new LocalTime(16, 1);
final Interval interval = new Interval(now, later);

final Interval mongo = SerializationUtils.assertJavaClonedEquals(interval);
final Interval java = SerializationUtils.assertMongoClonedEquals(interval);

assertEquals(mongo.getBegin(), java.getBegin());
assertEquals(mongo.getEnd(), java.getEnd());
assertEquals(mongo.getDaysOfWeek(), java.getDaysOfWeek());

final Interval same1 = interval.withBegin(interval.getBegin());
final Interval same2 = interval.withEnd(interval.getEnd());
assertNotSame(interval, same1);
assertNotSame(interval, same2);
assertEquals(interval, same1);
assertEquals(interval, same2);
}
}
38 changes: 38 additions & 0 deletions projects/typetools/src/test/java/org/nohope/SeriesElementTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.nohope;

import org.joda.time.DateTime;
import org.junit.Test;
import org.nohope.test.SerializationUtils;

import static com.mongodb.util.MyAsserts.assertFalse;
import static com.mongodb.util.MyAsserts.assertTrue;
import static org.junit.Assert.assertEquals;

/**
* @author <a href="mailto:ketoth.xupack@gmail.com">Ketoth Xupack</a>
* @since 2013-10-11 13:56
*/
public class SeriesElementTest {

@Test
@SuppressWarnings({"ObjectEqualsNull", "EqualsBetweenInconvertibleTypes"})
public void serialization() {
final DateTime date = DateTime.parse("2000-01-01T01:01:01.001");
final SeriesElement<Integer> e = new SeriesElement<>(date, 1);
assertEquals("{\"timestamp\":946666861001,\"value\":1}", e.toString());
final SeriesElement<Integer> mongoCloned = SerializationUtils.assertMongoClonedEquals(e);
final SeriesElement<Integer> javaCloned = SerializationUtils.assertJavaClonedEquals(e);

assertTrue(date.isEqual(mongoCloned.getTimestamp()));
assertTrue(date.isEqual(javaCloned.getTimestamp()));
assertEquals(1, (int) mongoCloned.getValue());
assertEquals(1, (int) javaCloned.getValue());

assertFalse(e.equals(null));
assertFalse(e.equals(1));
assertTrue(e.equals(e));

assertEquals(e.hashCode(), javaCloned.hashCode());
assertEquals(e.hashCode(), mongoCloned.hashCode());
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package org.nohope.concurrent;

import org.junit.Ignore;
import org.junit.Test;

import java.util.HashMap;
import java.util.concurrent.TimeoutException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

/**
* @author <a href="mailto:ketoth.xupack@gmail.com">Ketoth Xupack</a>
Expand All @@ -26,7 +23,6 @@ public void emptiness() throws InterruptedException {
assertTrue(m.isEmpty());
}

@Ignore("depends on cpu")
@Test(timeout = 10000)
public void clean() throws InterruptedException {
final BlockingMap<Integer, Integer> m = new BlockingMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.regex.Pattern;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.nohope.typetools.node.Node.*;

/**
Expand All @@ -29,6 +30,8 @@ private static Node from(final BSONObject obj) {

@Test
public void booleanOp() throws IOException {
assertNotNull(Node.empty());

final BasicBSONObject obj0 = new BasicBSONObject();
obj0.put("a", 1);
obj0.put("b", 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,11 @@ void packageDefaultMethod() {
}

private static class Child extends Parent {
public static void staticMethod() {
}

public final void finalMethod() {
}
}

private static class ChildOverride extends Parent {
Expand All @@ -678,6 +683,9 @@ public void inheritedMethod() {
searchMethod(Child.class, PROTECTED, "protectedMethod");
searchMethod(Child.class, PRIVATE, "privateMethod");
searchMethod(Child.class, PACKAGE_DEFAULT, "packageDefaultMethod");
searchMethod(Child.class, and(STATIC, PUBLIC), "staticMethod");
searchMethod(Child.class, and(FINAL, PUBLIC), "finalMethod");
searchMethod(Child.class, or(FINAL, PUBLIC), "finalMethod");

searchMethod(Child.class, ALL, "publicMethod");
searchMethod(Child.class, ALL, "protectedMethod");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.nohope.reflection;

import org.junit.Test;
import org.nohope.test.EnumTestSupport;

import static org.junit.Assert.assertEquals;
import static org.nohope.reflection.ModifierMatcher.*;

/**
* @author <a href="mailto:ketoth.xupack@gmail.com">Ketoth Xupack</a>
* @since 2013-10-11 13:39
*/
public class ModifierMatcherTest extends EnumTestSupport<ModifierMatcher> {

@Override
protected Class<ModifierMatcher> getEnumClass() {
return ModifierMatcher.class;
}

@Test
public void reprTest() {
assertEquals("!((PUBLIC && ABSTRACT) || FINAL)", not(or(and(PUBLIC, ABSTRACT), FINAL)).toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.nohope.typetools;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
* @author <a href="mailto:ketoth.xupack@gmail.com">Ketoth Xupack</a>
* @since 2013-10-11 12:59
*/
public class SortedListTest {

@Test
public void sortingTest() {
final SortedList<Integer> list = new SortedList<>(new SortedList.SerializableComparator<Integer>() {
private static final long serialVersionUID = 1L;

@Override
public int compare(final Integer o1, final Integer o2) {
return o1.compareTo(o2);
}
});

list.add(2);
list.add(3);
list.add(1);

try {
list.add(1, 0);
fail();
} catch (final UnsupportedOperationException ignored) {
}

assertEquals(1, (int) list.get(0));
assertEquals(1, (int) list.get(0));
assertEquals(2, (int) list.get(1));
assertEquals(3, (int) list.get(2));
}
}
Loading

0 comments on commit b9668a4

Please sign in to comment.