Fix ClassCastException caused by sorting intents in gui when using both string and long keys.
Change-Id: Ide180556761cfe3ca92d14e197d2e3dfc175563e
diff --git a/core/api/src/main/java/org/onosproject/net/intent/Key.java b/core/api/src/main/java/org/onosproject/net/intent/Key.java
index fadf8b9..ef109f2 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/Key.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/Key.java
@@ -130,8 +130,12 @@
@Override
public int compareTo(Key o) {
- StringKey sk = (StringKey) o;
- return this.key.compareTo(sk.key);
+ if (o instanceof StringKey) {
+ StringKey sk = (StringKey) o;
+ return this.key.compareTo(sk.key);
+ } else {
+ return this.key.compareTo(o.toString());
+ }
}
}
@@ -176,9 +180,13 @@
@Override
public int compareTo(Key o) {
- Long myKey = key;
- Long otherKey = ((LongKey) o).key;
- return myKey.compareTo(otherKey);
+ if (o instanceof LongKey) {
+ Long myKey = key;
+ Long otherKey = ((LongKey) o).key;
+ return myKey.compareTo(otherKey);
+ } else {
+ return this.toString().compareTo(o.toString());
+ }
}
}
}
diff --git a/core/api/src/test/java/org/onosproject/net/intent/KeyTest.java b/core/api/src/test/java/org/onosproject/net/intent/KeyTest.java
index b383afe..1de9fbb 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/KeyTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/KeyTest.java
@@ -21,7 +21,10 @@
import com.google.common.testing.EqualsTester;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.comparesEqualTo;
+import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.notNullValue;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
@@ -118,4 +121,74 @@
.addEqualityGroup(longKey3)
.testEquals();
}
+
+ /**
+ * Tests compareTo for string based keys.
+ */
+ @Test
+ public void stringKeyCompare() {
+ Key stringKey1 = Key.of(KEY_1, NetTestTools.APP_ID);
+ Key copyOfStringKey1 = Key.of(KEY_1, NetTestTools.APP_ID);
+ Key stringKey2 = Key.of(KEY_2, NetTestTools.APP_ID);
+ Key copyOfStringKey2 = Key.of(KEY_2, NetTestTools.APP_ID);
+ Key stringKey3 = Key.of(KEY_3, NetTestTools.APP_ID);
+ Key copyOfStringKey3 = Key.of(KEY_3, NetTestTools.APP_ID);
+
+ assertThat(stringKey1, comparesEqualTo(copyOfStringKey1));
+ assertThat(stringKey1, lessThan(stringKey2));
+ assertThat(stringKey1, lessThan(stringKey3));
+
+ assertThat(stringKey2, greaterThan(stringKey1));
+ assertThat(stringKey2, comparesEqualTo(copyOfStringKey2));
+ assertThat(stringKey2, lessThan(stringKey3));
+
+ assertThat(stringKey3, greaterThan(stringKey1));
+ assertThat(stringKey3, greaterThan(stringKey2));
+ assertThat(stringKey3, comparesEqualTo(copyOfStringKey3));
+ }
+
+ /**
+ * Tests compareTo for long based keys.
+ */
+ @Test
+ public void longKeyCompare() {
+ Key longKey1 = Key.of(LONG_KEY_1, NetTestTools.APP_ID);
+ Key copyOfLongKey1 = Key.of(LONG_KEY_1, NetTestTools.APP_ID);
+ Key longKey2 = Key.of(LONG_KEY_2, NetTestTools.APP_ID);
+ Key copyOfLongKey2 = Key.of(LONG_KEY_2, NetTestTools.APP_ID);
+ Key longKey3 = Key.of(LONG_KEY_3, NetTestTools.APP_ID);
+ Key copyOfLongKey3 = Key.of(LONG_KEY_3, NetTestTools.APP_ID);
+
+ assertThat(longKey1, comparesEqualTo(copyOfLongKey1));
+ assertThat(longKey1, lessThan(longKey2));
+ assertThat(longKey1, lessThan(longKey3));
+
+ assertThat(longKey2, greaterThan(longKey1));
+ assertThat(longKey2, comparesEqualTo(copyOfLongKey2));
+ assertThat(longKey2, lessThan(longKey3));
+
+ assertThat(longKey3, greaterThan(longKey1));
+ assertThat(longKey3, greaterThan(longKey2));
+ assertThat(longKey3, comparesEqualTo(copyOfLongKey3));
+ }
+
+ /**
+ * Tests compareTo for string and long based keys.
+ */
+ @Test
+ public void stringAndLongKeyCompare() {
+ Key stringKey0 = Key.of("0" + KEY_1, NetTestTools.APP_ID);
+ Key longKey1 = Key.of(LONG_KEY_1, NetTestTools.APP_ID);
+ Key stringKey2 = Key.of(KEY_2, NetTestTools.APP_ID);
+
+
+ assertThat(stringKey0, lessThan(longKey1));
+ assertThat(stringKey0, lessThan(stringKey2));
+
+ assertThat(longKey1, greaterThan(stringKey0));
+ assertThat(longKey1, lessThan(stringKey2));
+
+ assertThat(stringKey2, greaterThan(stringKey0));
+ assertThat(stringKey2, greaterThan(longKey1));
+ }
}