001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.schematic.internal.document; 017 018import static org.junit.Assert.assertTrue; 019import org.junit.Assert; 020import org.junit.Before; 021import org.junit.Test; 022import org.modeshape.schematic.document.Array; 023import org.modeshape.schematic.document.Document; 024import org.modeshape.schematic.document.Document.Field; 025 026public class BasicDocumentTest { 027 private BasicDocument doc; 028 029 @Before 030 public void beforeTest() { 031 doc = new BasicDocument(); 032 doc.put("foo", "value for foo"); 033 doc.put("bar", "value for bar"); 034 doc.put("baz", new BasicDocument("key1", "value1", "key2", "value2")); 035 doc.put("bom", new BasicArray("v1", "v2", "v3", new BasicDocument("v4-Key", "v4-value"))); 036 } 037 038 @Test 039 public void shouldBeCloneable() { 040 assertEquals(doc, doc.clone()); 041 } 042 043 protected void assertEquals( Document doc1, 044 Document doc2 ) { 045 assertTrue(doc1 != doc2); 046 Assert.assertEquals(doc1, doc2); 047 for (Field field : doc1.fields()) { 048 Object value2 = doc2.get(field.getName()); 049 Assert.assertEquals(value2, field.getValue()); 050 if (value2 instanceof Array) { 051 assertTrue(value2 != field.getValue()); 052 assertEquals((Array)field.getValue(), (Array)value2); 053 } else if (value2 instanceof Document) { 054 assertTrue(value2 != field.getValue()); 055 assertEquals(field.getValueAsDocument(), (Document)value2); 056 } else { 057 // The values can actually be the same instances since they're immutable ... 058 } 059 } 060 } 061 062 protected void assertEquals( Array array1, 063 Array array2 ) { 064 assertTrue(array1 != array2); 065 Assert.assertEquals(array1, array2); 066 for (int i = 0; i != array1.size(); ++i) { 067 Object value1 = array1.get(i); 068 Object value2 = array2.get(i); 069 Assert.assertEquals(value1, value2); 070 if (value2 instanceof Array) { 071 assertTrue(value1 != value2); 072 assertEquals((Array) value1, (Array) value2); 073 } else if (value2 instanceof Document) { 074 assertTrue(value1 != value2); 075 assertEquals((Document)value1, (Document)value2); 076 } else { 077 // The values can actually be the same instances since they're immutable ... 078 } 079 } 080 } 081}