]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2501 add unit tests
authorSimon Brandhof <simon.brandhof@gmail.com>
Wed, 30 Jan 2013 12:10:53 +0000 (13:10 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Wed, 30 Jan 2013 12:11:10 +0000 (13:11 +0100)
sonar-core/src/main/java/org/sonar/core/graph/GraphUtil.java
sonar-core/src/test/java/org/sonar/core/graph/BeanGraphTest.java [new file with mode: 0644]

index feea6d2ac80e1adde39a4190ba866fa953006fa5..d0a701064d2b6f496a7c213b40ad9b7afeb936ad 100644 (file)
@@ -63,13 +63,4 @@ public class GraphUtil {
     }
     return result;
   }
-
-  public static void setNullableProperty(Element elt, String key, @Nullable Object value) {
-    if (value == null) {
-      elt.removeProperty(key);
-    } else {
-      elt.setProperty(key, value);
-    }
-  }
-
 }
diff --git a/sonar-core/src/test/java/org/sonar/core/graph/BeanGraphTest.java b/sonar-core/src/test/java/org/sonar/core/graph/BeanGraphTest.java
new file mode 100644 (file)
index 0000000..23bf7d6
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.graph;
+
+import com.tinkerpop.blueprints.Direction;
+import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class BeanGraphTest {
+
+  @Test
+  public void should_get_underlying_graph() {
+    TinkerGraph graph = new TinkerGraph();
+    BeanGraph beanGraph = new BeanGraph(graph);
+
+    assertThat(beanGraph.getUnderlyingGraph()).isSameAs(graph);
+  }
+
+  @Test
+  public void should_create_bean_vertex() {
+    TinkerGraph graph = new TinkerGraph();
+    BeanGraph beanGraph = new BeanGraph(graph);
+    Person person = beanGraph.createVertex(Person.class);
+
+    assertThat(person).isNotNull();
+    assertThat(person.element()).isNotNull();
+    assertThat(person.element().getId()).isNotNull();
+    assertThat(person.beanGraph()).isSameAs(beanGraph);
+    assertThat(person.age()).isNull();
+  }
+
+  @Test
+  public void should_wrap_existing_element() {
+    TinkerGraph graph = new TinkerGraph();
+    BeanGraph beanGraph = new BeanGraph(graph);
+    Vertex vertex = graph.addVertex(null);
+    vertex.setProperty("age", 42);
+
+    Person person = beanGraph.wrap(vertex, Person.class);
+    assertThat(person).isNotNull();
+    assertThat(person.element()).isSameAs(vertex);
+    assertThat(person.age()).isEqualTo(42);
+  }
+
+  @Test
+  public void should_create_adjacent_bean_vertex() {
+    TinkerGraph graph = new TinkerGraph();
+    BeanGraph beanGraph = new BeanGraph(graph);
+    Person person = beanGraph.createVertex(Person.class);
+
+    Person adjacent = beanGraph.createAdjacentVertex(person, Person.class, "knows", "type", "family");
+    assertThat(adjacent).isNotNull();
+    assertThat(person.knows()).hasSize(1);
+    assertThat(person.knows().iterator().next()).isSameAs(adjacent);
+    assertThat(adjacent.knows()).isEmpty();
+  }
+
+  static class Person extends BeanVertex {
+    Integer age() {
+      return (Integer) getProperty("age");
+    }
+
+    Iterable<Person> knows() {
+      return getVertices(Person.class, Direction.OUT, "knows");
+    }
+  }
+}