From 017e56a9a2704fb33c027830f80ee96d998302d6 Mon Sep 17 00:00:00 2001
From: Jeffrey Phillips Freeman <jeffrey.freeman@syncleus.com>
Date: Mon, 10 Nov 2014 14:32:28 -0500
Subject: [PATCH] Added some more unit tests.

Change-Id: Icedcbd5e96043453f467f2bf578ad06aa4a1e75a
---
 .../graph/TypedIncidenceMethodHandler.java    |  3 +-
 .../com/syncleus/grail/graph/GodExtended.java |  2 +-
 .../syncleus/grail/graph/GodIntermediate.java | 25 ++++++++
 .../grail/graph/GrailGraphFactoryTest.java    | 35 +++++++++++
 .../TypedAdjacencyMethodHandlerTest.java      | 14 +++++
 .../TypedIncidenceMethodHandlerTest.java      | 29 ++++++++++
 .../neural/AbstractActivationNeuronTest.java  | 58 +++++++++++++++++++
 .../BadAccessActivationFunction.java          | 49 ++++++++++++++++
 ...oDefaultConstructorActivationFunction.java | 49 ++++++++++++++++
 9 files changed, 261 insertions(+), 3 deletions(-)
 create mode 100644 src/test/java/com/syncleus/grail/graph/GodIntermediate.java
 create mode 100644 src/test/java/com/syncleus/grail/graph/GrailGraphFactoryTest.java
 create mode 100644 src/test/java/com/syncleus/grail/neural/AbstractActivationNeuronTest.java
 create mode 100644 src/test/java/com/syncleus/grail/neural/activation/BadAccessActivationFunction.java
 create mode 100644 src/test/java/com/syncleus/grail/neural/activation/NoDefaultConstructorActivationFunction.java

diff --git a/src/main/java/com/syncleus/grail/graph/TypedIncidenceMethodHandler.java b/src/main/java/com/syncleus/grail/graph/TypedIncidenceMethodHandler.java
index 4f65b04..c2a36f1 100644
--- a/src/main/java/com/syncleus/grail/graph/TypedIncidenceMethodHandler.java
+++ b/src/main/java/com/syncleus/grail/graph/TypedIncidenceMethodHandler.java
@@ -46,8 +46,7 @@ public class TypedIncidenceMethodHandler implements MethodHandler<TypedIncidence
         if( ! (element instanceof Vertex) )
             throw new IllegalStateException("element is not a type of Vertex " + element.getClass().getName());
 
-        if(annotation.label() == null)
-            throw new IllegalStateException("method " + method.getName() + " label must be specified on @TypedIncidence annotation");
+        assert annotation.label() != null;
 
         if( ClassUtilities.isGetMethod(method) ) {
             if( arguments == null )
diff --git a/src/test/java/com/syncleus/grail/graph/GodExtended.java b/src/test/java/com/syncleus/grail/graph/GodExtended.java
index 196ebbc..38b560a 100644
--- a/src/test/java/com/syncleus/grail/graph/GodExtended.java
+++ b/src/test/java/com/syncleus/grail/graph/GodExtended.java
@@ -23,7 +23,7 @@ import com.tinkerpop.frames.annotations.gremlin.GremlinGroovy;
 import com.tinkerpop.frames.modules.typedgraph.TypeValue;
 
 @TypeValue("GodExtended")
-public interface GodExtended extends God {
+public interface GodExtended extends GodIntermediate {
     @GremlinGroovy("it.in('father').in('father')")
     God getGrandson();
 }
diff --git a/src/test/java/com/syncleus/grail/graph/GodIntermediate.java b/src/test/java/com/syncleus/grail/graph/GodIntermediate.java
new file mode 100644
index 0000000..a244704
--- /dev/null
+++ b/src/test/java/com/syncleus/grail/graph/GodIntermediate.java
@@ -0,0 +1,25 @@
+/******************************************************************************
+ *                                                                             *
+ *  Copyright: (c) Syncleus, Inc.                                              *
+ *                                                                             *
+ *  You may redistribute and modify this source code under the terms and       *
+ *  conditions of the Open Source Community License - Type C version 1.0       *
+ *  or any later version as published by Syncleus, Inc. at www.syncleus.com.   *
+ *  There should be a copy of the license included with this file. If a copy   *
+ *  of the license is not included you are granted no right to distribute or   *
+ *  otherwise use this file except through a legal and valid license. You      *
+ *  should also contact Syncleus, Inc. at the information below if you cannot  *
+ *  find a license:                                                            *
+ *                                                                             *
+ *  Syncleus, Inc.                                                             *
+ *  2604 South 12th Street                                                     *
+ *  Philadelphia, PA 19148                                                     *
+ *                                                                             *
+ ******************************************************************************/
+package com.syncleus.grail.graph;
+
+import com.tinkerpop.frames.modules.typedgraph.TypeValue;
+
+@TypeValue("GodIntermediate")
+public interface GodIntermediate extends God {
+}
diff --git a/src/test/java/com/syncleus/grail/graph/GrailGraphFactoryTest.java b/src/test/java/com/syncleus/grail/graph/GrailGraphFactoryTest.java
new file mode 100644
index 0000000..e0bcd89
--- /dev/null
+++ b/src/test/java/com/syncleus/grail/graph/GrailGraphFactoryTest.java
@@ -0,0 +1,35 @@
+/******************************************************************************
+ *                                                                             *
+ *  Copyright: (c) Syncleus, Inc.                                              *
+ *                                                                             *
+ *  You may redistribute and modify this source code under the terms and       *
+ *  conditions of the Open Source Community License - Type C version 1.0       *
+ *  or any later version as published by Syncleus, Inc. at www.syncleus.com.   *
+ *  There should be a copy of the license included with this file. If a copy   *
+ *  of the license is not included you are granted no right to distribute or   *
+ *  otherwise use this file except through a legal and valid license. You      *
+ *  should also contact Syncleus, Inc. at the information below if you cannot  *
+ *  find a license:                                                            *
+ *                                                                             *
+ *  Syncleus, Inc.                                                             *
+ *  2604 South 12th Street                                                     *
+ *  Philadelphia, PA 19148                                                     *
+ *                                                                             *
+ ******************************************************************************/
+package com.syncleus.grail.graph;
+
+import com.tinkerpop.frames.FramedTransactionalGraph;
+import com.tinkerpop.frames.modules.javahandler.JavaHandlerModule;
+import junit.framework.Assert;
+import org.junit.Test;
+
+import java.util.Collections;
+
+public class GrailGraphFactoryTest {
+    @Test
+    public void testCustomModules() {
+        final GrailGraphFactory factory = new GrailGraphFactory(Collections.singleton(new JavaHandlerModule()));
+        final FramedTransactionalGraph<?> graph = factory.create(new MockTransactionalTinkerGraph());
+        Assert.assertTrue( !graph.getVertices().iterator().hasNext() );
+    }
+}
diff --git a/src/test/java/com/syncleus/grail/graph/TypedAdjacencyMethodHandlerTest.java b/src/test/java/com/syncleus/grail/graph/TypedAdjacencyMethodHandlerTest.java
index d32a737..68513b6 100644
--- a/src/test/java/com/syncleus/grail/graph/TypedAdjacencyMethodHandlerTest.java
+++ b/src/test/java/com/syncleus/grail/graph/TypedAdjacencyMethodHandlerTest.java
@@ -49,6 +49,20 @@ public class TypedAdjacencyMethodHandlerTest {
         HANDLER.processElement(MOCK_FRAME, GET_SON_METHOD, new Object[]{God.class}, TYPED_ANNOTATION, framedGraph, MOCK_ELEMENT);
     }
 
+    @Test(expected = IllegalStateException.class)
+    public void testWrongArgument() {
+        final FramedGraph framedGraph = BlankGraphFactory.makeTinkerGraph();
+
+        HANDLER.processElement(MOCK_FRAME, GET_SON_METHOD, new Object[]{new Object()}, TYPED_ANNOTATION, framedGraph, MOCK_VERTEX);
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testToManyArgument() {
+        final FramedGraph framedGraph = BlankGraphFactory.makeTinkerGraph();
+
+        HANDLER.processElement(MOCK_FRAME, GET_SON_METHOD, new Object[]{new Object(), new Object()}, TYPED_ANNOTATION, framedGraph, MOCK_VERTEX);
+    }
+
     @Test(expected = IllegalStateException.class)
     public void testNullArguments() {
         final FramedGraph framedGraph = BlankGraphFactory.makeTinkerGraph();
diff --git a/src/test/java/com/syncleus/grail/graph/TypedIncidenceMethodHandlerTest.java b/src/test/java/com/syncleus/grail/graph/TypedIncidenceMethodHandlerTest.java
index a9ec114..a77ea1b 100644
--- a/src/test/java/com/syncleus/grail/graph/TypedIncidenceMethodHandlerTest.java
+++ b/src/test/java/com/syncleus/grail/graph/TypedIncidenceMethodHandlerTest.java
@@ -27,6 +27,7 @@ import java.lang.reflect.Method;
 public class TypedIncidenceMethodHandlerTest {
     private static final TypedIncidenceMethodHandler HANDLER = new TypedIncidenceMethodHandler(null);
     private static final Method GET_SON_EDGE_METHOD;
+    private static final Method ADD_SON_EDGE_METHOD;
     private static final TypedIncidence TYPED_ANNOTATION;
     private static final Object MOCK_FRAME = new Object();
     private static final Element MOCK_ELEMENT = new MockElement();
@@ -40,6 +41,13 @@ public class TypedIncidenceMethodHandlerTest {
             throw new IllegalStateException(caught);
         }
 
+        try {
+            ADD_SON_EDGE_METHOD = God.class.getMethod("addSonEdge", Class.class);
+        }
+        catch( final NoSuchMethodException caught ) {
+            throw new IllegalStateException(caught);
+        }
+
         TYPED_ANNOTATION = GET_SON_EDGE_METHOD.getAnnotation(TypedIncidence.class);
     }
 
@@ -50,6 +58,27 @@ public class TypedIncidenceMethodHandlerTest {
         HANDLER.processElement(MOCK_FRAME, GET_SON_EDGE_METHOD, new Object[]{God.class}, TYPED_ANNOTATION, framedGraph, MOCK_ELEMENT);
     }
 
+    @Test(expected = IllegalStateException.class)
+    public void testWrongArgument() {
+        final FramedGraph framedGraph = BlankGraphFactory.makeTinkerGraph();
+
+        HANDLER.processElement(MOCK_FRAME, GET_SON_EDGE_METHOD, new Object[]{new Object()}, TYPED_ANNOTATION, framedGraph, MOCK_VERTEX);
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testTooManyArgument() {
+        final FramedGraph framedGraph = BlankGraphFactory.makeTinkerGraph();
+
+        HANDLER.processElement(MOCK_FRAME, GET_SON_EDGE_METHOD, new Object[]{new Object(), new Object()}, TYPED_ANNOTATION, framedGraph, MOCK_VERTEX);
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void testTooBadPrefix() {
+        final FramedGraph framedGraph = BlankGraphFactory.makeTinkerGraph();
+
+        HANDLER.processElement(MOCK_FRAME, ADD_SON_EDGE_METHOD, new Object[]{God.class}, TYPED_ANNOTATION, framedGraph, MOCK_VERTEX);
+    }
+
     @Test(expected = IllegalStateException.class)
     public void testNullArguments() {
         final FramedGraph framedGraph = BlankGraphFactory.makeTinkerGraph();
diff --git a/src/test/java/com/syncleus/grail/neural/AbstractActivationNeuronTest.java b/src/test/java/com/syncleus/grail/neural/AbstractActivationNeuronTest.java
new file mode 100644
index 0000000..53dcd12
--- /dev/null
+++ b/src/test/java/com/syncleus/grail/neural/AbstractActivationNeuronTest.java
@@ -0,0 +1,58 @@
+/******************************************************************************
+ *                                                                             *
+ *  Copyright: (c) Syncleus, Inc.                                              *
+ *                                                                             *
+ *  You may redistribute and modify this source code under the terms and       *
+ *  conditions of the Open Source Community License - Type C version 1.0       *
+ *  or any later version as published by Syncleus, Inc. at www.syncleus.com.   *
+ *  There should be a copy of the license included with this file. If a copy   *
+ *  of the license is not included you are granted no right to distribute or   *
+ *  otherwise use this file except through a legal and valid license. You      *
+ *  should also contact Syncleus, Inc. at the information below if you cannot  *
+ *  find a license:                                                            *
+ *                                                                             *
+ *  Syncleus, Inc.                                                             *
+ *  2604 South 12th Street                                                     *
+ *  Philadelphia, PA 19148                                                     *
+ *                                                                             *
+ ******************************************************************************/
+package com.syncleus.grail.neural;
+
+import com.syncleus.grail.graph.BlankGraphFactory;
+import com.syncleus.grail.neural.activation.*;
+import com.tinkerpop.frames.FramedTransactionalGraph;
+import junit.framework.Assert;
+import org.junit.Test;
+
+import java.lang.reflect.*;
+
+public class AbstractActivationNeuronTest {
+
+    @Test( expected = UndeclaredThrowableException.class )
+    public void testBadAccessActivation() {
+        final FramedTransactionalGraph<?> graph = BlankGraphFactory.makeTinkerGraph();
+        final ActivationNeuron neuron = graph.addVertex(null, ActivationNeuron.class);
+        neuron.setActivationFunctionClass(BadAccessActivationFunction.class);
+        try {
+            neuron.propagate();
+        }
+        catch( final UndeclaredThrowableException caught ) {
+            Assert.assertTrue(InvocationTargetException.class.equals(caught.getUndeclaredThrowable().getClass()));
+            throw caught;
+        }
+    }
+
+    @Test( expected = UndeclaredThrowableException.class )
+    public void testNoDefaultConstructorActivation() {
+        final FramedTransactionalGraph<?> graph = BlankGraphFactory.makeTinkerGraph();
+        final ActivationNeuron neuron = graph.addVertex(null, ActivationNeuron.class);
+        neuron.setActivationFunctionClass(NoDefaultConstructorActivationFunction.class);
+        try {
+            neuron.propagate();
+        }
+        catch( final UndeclaredThrowableException caught ) {
+            Assert.assertTrue(InvocationTargetException.class.equals(caught.getUndeclaredThrowable().getClass()));
+            throw caught;
+        }
+    }
+}
diff --git a/src/test/java/com/syncleus/grail/neural/activation/BadAccessActivationFunction.java b/src/test/java/com/syncleus/grail/neural/activation/BadAccessActivationFunction.java
new file mode 100644
index 0000000..d30e1d0
--- /dev/null
+++ b/src/test/java/com/syncleus/grail/neural/activation/BadAccessActivationFunction.java
@@ -0,0 +1,49 @@
+/******************************************************************************
+ *                                                                             *
+ *  Copyright: (c) Syncleus, Inc.                                              *
+ *                                                                             *
+ *  You may redistribute and modify this source code under the terms and       *
+ *  conditions of the Open Source Community License - Type C version 1.0       *
+ *  or any later version as published by Syncleus, Inc. at www.syncleus.com.   *
+ *  There should be a copy of the license included with this file. If a copy   *
+ *  of the license is not included you are granted no right to distribute or   *
+ *  otherwise use this file except through a legal and valid license. You      *
+ *  should also contact Syncleus, Inc. at the information below if you cannot  *
+ *  find a license:                                                            *
+ *                                                                             *
+ *  Syncleus, Inc.                                                             *
+ *  2604 South 12th Street                                                     *
+ *  Philadelphia, PA 19148                                                     *
+ *                                                                             *
+ ******************************************************************************/
+package com.syncleus.grail.neural.activation;
+
+public class BadAccessActivationFunction implements ActivationFunction {
+    private BadAccessActivationFunction() {
+    }
+
+    @Override
+    public double activate(final double activity) {
+        return 0;
+    }
+
+    @Override
+    public double activateDerivative(final double activity) {
+        return 0;
+    }
+
+    @Override
+    public boolean isBound() {
+        return false;
+    }
+
+    @Override
+    public double getUpperLimit() {
+        return 0;
+    }
+
+    @Override
+    public double getLowerLimit() {
+        return 0;
+    }
+}
diff --git a/src/test/java/com/syncleus/grail/neural/activation/NoDefaultConstructorActivationFunction.java b/src/test/java/com/syncleus/grail/neural/activation/NoDefaultConstructorActivationFunction.java
new file mode 100644
index 0000000..8dbf04a
--- /dev/null
+++ b/src/test/java/com/syncleus/grail/neural/activation/NoDefaultConstructorActivationFunction.java
@@ -0,0 +1,49 @@
+/******************************************************************************
+ *                                                                             *
+ *  Copyright: (c) Syncleus, Inc.                                              *
+ *                                                                             *
+ *  You may redistribute and modify this source code under the terms and       *
+ *  conditions of the Open Source Community License - Type C version 1.0       *
+ *  or any later version as published by Syncleus, Inc. at www.syncleus.com.   *
+ *  There should be a copy of the license included with this file. If a copy   *
+ *  of the license is not included you are granted no right to distribute or   *
+ *  otherwise use this file except through a legal and valid license. You      *
+ *  should also contact Syncleus, Inc. at the information below if you cannot  *
+ *  find a license:                                                            *
+ *                                                                             *
+ *  Syncleus, Inc.                                                             *
+ *  2604 South 12th Street                                                     *
+ *  Philadelphia, PA 19148                                                     *
+ *                                                                             *
+ ******************************************************************************/
+package com.syncleus.grail.neural.activation;
+
+public class NoDefaultConstructorActivationFunction implements ActivationFunction {
+    public NoDefaultConstructorActivationFunction(String something) {
+    }
+
+    @Override
+    public double activate(final double activity) {
+        return 0;
+    }
+
+    @Override
+    public double activateDerivative(final double activity) {
+        return 0;
+    }
+
+    @Override
+    public boolean isBound() {
+        return false;
+    }
+
+    @Override
+    public double getUpperLimit() {
+        return 0;
+    }
+
+    @Override
+    public double getLowerLimit() {
+        return 0;
+    }
+}
-- 
GitLab