View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.rdf.api;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotEquals;
24  
25  /**
26   * Abstract test class for the BlankNode interface.
27   */
28  public abstract class AbstractBlankNodeTest {
29  
30      /**
31       * This method must be overridden by the implementing test to create a
32       * {@link BlankNode} to be tested.
33       * <p>
34       * Each call to this method must provide a new, unique BlankNode.
35       *
36       * @return {@link RDF} instance to be tested.
37       */
38      protected abstract BlankNode getBlankNode();
39  
40      /**
41       * Gets a new blank node object based on the given identifier.
42       * <p>
43       * Subsequent calls to this method during a single test with the same
44       * identifier must return BlankNode objects that are equals and have the
45       * same hashCode. The objects returned from successive calls during a single
46       * test may be the same object, or they may be different objects.
47       * </p>
48       *
49       * @param identifier
50       *            The identifier to use as the reference for creating the blank
51       *            node that is returned.
52       * @return A new blank node based on the
53       */
54      protected abstract BlankNode getBlankNode(String identifier);
55  
56      /**
57       * Test method for {@link BlankNode#uniqueReference()}.
58       */
59      @Test
60      public final void testInternalIdentifier() {
61          final BlankNode testNull = new BlankNode() {
62              @Override
63              public String ntriplesString() {
64                  return null;
65              }
66  
67              @Override
68              public String uniqueReference() {
69                  return null;
70              }
71          };
72          final BlankNode testAutomatic1 = getBlankNode();
73          final BlankNode testAutomatic2 = getBlankNode();
74          final BlankNode testManual3a = getBlankNode("3");
75          final BlankNode testManual3b = getBlankNode("3");
76          final BlankNode testManual4 = getBlankNode("4");
77  
78          // Test against our fake stub
79          assertNotEquals(testNull.uniqueReference(), testAutomatic1.uniqueReference());
80          assertNotEquals(testAutomatic1.uniqueReference(), testNull.uniqueReference());
81          assertNotEquals(testNull.uniqueReference(), testManual3a.uniqueReference());
82          assertNotEquals(testManual3a.uniqueReference(), testNull.uniqueReference());
83  
84          // Test the two imported instances against each other
85          assertEquals(testAutomatic1.uniqueReference(), testAutomatic1.uniqueReference());
86          assertEquals(testAutomatic2.uniqueReference(), testAutomatic2.uniqueReference());
87          assertNotEquals(testAutomatic1.uniqueReference(), testAutomatic2.uniqueReference());
88          assertNotEquals(testAutomatic2.uniqueReference(), testAutomatic1.uniqueReference());
89          assertNotEquals(testAutomatic1.uniqueReference(), testManual3a.uniqueReference());
90          assertEquals(testManual3b.uniqueReference(), testManual3a.uniqueReference());
91          assertNotEquals(testManual3a.uniqueReference(), testManual4.uniqueReference());
92      }
93  
94      /**
95       * Test method for {@link BlankNode#equals(java.lang.Object)}.
96       */
97      @Test
98      public final void testEquals() {
99          final BlankNode testNull = new BlankNode() {
100             @Override
101             public String ntriplesString() {
102                 return null;
103             }
104 
105             @Override
106             public String uniqueReference() {
107                 return null;
108             }
109         };
110         final BlankNode testAutomatic1 = getBlankNode();
111         final BlankNode testAutomatic2 = getBlankNode();
112         final BlankNode testManual3a = getBlankNode("3");
113         final BlankNode testManual3b = getBlankNode("3");
114         final BlankNode testManual4 = getBlankNode("4");
115 
116         // Test against our fake stub
117         assertNotEquals(testNull, testAutomatic1);
118         assertNotEquals(testAutomatic1, testNull);
119         assertNotEquals(testNull, testManual3a);
120         assertNotEquals(testManual3a, testNull);
121 
122         // Test the two imported instances against each other
123         assertEquals(testAutomatic1, testAutomatic1);
124         assertEquals(testAutomatic2, testAutomatic2);
125         assertNotEquals(testAutomatic1, testAutomatic2);
126         assertNotEquals(testAutomatic2, testAutomatic1);
127         assertNotEquals(testAutomatic1, testManual3a);
128         assertEquals(testManual3b, testManual3a);
129         assertNotEquals(testManual3a, testManual4);
130     }
131 
132     /**
133      * Test method for {@link BlankNode#hashCode()}.
134      */
135     @Test
136     public final void testHashCode() {
137         final BlankNode testNull = new BlankNode() {
138             @Override
139             public String ntriplesString() {
140                 return null;
141             }
142 
143             @Override
144             public String uniqueReference() {
145                 return null;
146             }
147         };
148         final BlankNode testAutomatic1 = getBlankNode();
149         final BlankNode testAutomatic2 = getBlankNode();
150         final BlankNode testManual3a = getBlankNode("3");
151         final BlankNode testManual3b = getBlankNode("3");
152         final BlankNode testManual4 = getBlankNode("4");
153 
154         // Test against our fake stub
155         assertNotEquals(testNull.hashCode(), testAutomatic1.hashCode());
156         assertNotEquals(testAutomatic1.hashCode(), testNull.hashCode());
157         assertNotEquals(testNull.hashCode(), testManual3a.hashCode());
158         assertNotEquals(testManual3a.hashCode(), testNull.hashCode());
159 
160         // Test the two imported instances against each other
161         assertEquals(testAutomatic1.hashCode(), testAutomatic1.hashCode());
162         assertEquals(testAutomatic2.hashCode(), testAutomatic2.hashCode());
163         assertNotEquals(testAutomatic1.hashCode(), testAutomatic2.hashCode());
164         assertNotEquals(testAutomatic2.hashCode(), testAutomatic1.hashCode());
165         assertNotEquals(testAutomatic1.hashCode(), testManual3a.hashCode());
166         assertEquals(testManual3b.hashCode(), testManual3a.hashCode());
167         assertNotEquals(testManual3a.hashCode(), testManual4.hashCode());
168     }
169 
170     /**
171      * Test method for {@link RDFTerm#ntriplesString()}.
172      */
173     @Test
174     public final void testNtriplesString() {
175         final BlankNode testNull = new BlankNode() {
176             @Override
177             public String ntriplesString() {
178                 return null;
179             }
180 
181             @Override
182             public String uniqueReference() {
183                 return null;
184             }
185         };
186         final BlankNode testAutomatic1 = getBlankNode();
187         final BlankNode testAutomatic2 = getBlankNode();
188         final BlankNode testManual3a = getBlankNode("3");
189         final BlankNode testManual3b = getBlankNode("3");
190         final BlankNode testManual4 = getBlankNode("4");
191 
192         // Test against our fake stub
193         assertNotEquals(testNull.ntriplesString(), testAutomatic1.ntriplesString());
194         assertNotEquals(testAutomatic1.ntriplesString(), testNull.ntriplesString());
195         assertNotEquals(testNull.ntriplesString(), testManual3a.ntriplesString());
196         assertNotEquals(testManual3a.ntriplesString(), testNull.ntriplesString());
197 
198         // Test the two imported instances against each other
199         assertEquals(testAutomatic1.ntriplesString(), testAutomatic1.ntriplesString());
200         assertEquals(testAutomatic2.ntriplesString(), testAutomatic2.ntriplesString());
201         assertNotEquals(testAutomatic1.ntriplesString(), testAutomatic2.ntriplesString());
202         assertNotEquals(testAutomatic2.ntriplesString(), testAutomatic1.ntriplesString());
203         assertNotEquals(testAutomatic1.ntriplesString(), testManual3a.ntriplesString());
204         assertEquals(testManual3b.ntriplesString(), testManual3a.ntriplesString());
205         assertNotEquals(testManual3a.ntriplesString(), testManual4.ntriplesString());
206     }
207 
208 }