1 package org.netbeans.modules.xml.schema.model.impl.xdm; 2 3 import java.util.ArrayList ; 4 import junit.framework.*; 5 import org.netbeans.modules.xml.schema.model.*; 6 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 7 import org.netbeans.modules.xml.xam.Component; 8 9 13 public class SyncUpdateVisitorTest extends TestCase { 14 15 public static final String TEST_XSD = "resources/PurchaseOrder.xsd"; 16 private Schema schema; 17 private SchemaModel model; 18 19 public SyncUpdateVisitorTest(String testName) { 20 super(testName); 21 } 22 23 protected void setUp() throws Exception { 24 } 25 26 protected void setUp1() throws Exception { 27 model = (SchemaModel)Util.loadSchemaModel(TEST_XSD); 28 schema = model.getSchema(); 29 } 30 31 public static Test suite() { 32 TestSuite suite = new TestSuite(SyncUpdateVisitorTest.class); 33 34 return suite; 35 } 36 37 public void testVisitDelete() throws Exception { 38 setUp1(); 39 SyncUpdateVisitor instance = new SyncUpdateVisitor(); 40 41 int size = schema.getComplexTypes().size(); 42 GlobalComplexType gct = schema.getComplexTypes().iterator().next(); 43 model.startTransaction(); 44 instance.update(schema, gct ,SyncUpdateVisitor.Operation.REMOVE); 45 model.endTransaction(); 46 assertEquals(size-1,schema.getComplexTypes().size()); 47 48 size = schema.getSimpleTypes().size(); 49 GlobalSimpleType gst = schema.getSimpleTypes().iterator().next(); 50 model.startTransaction(); 51 instance.update(schema, gst ,SyncUpdateVisitor.Operation.REMOVE); 52 model.endTransaction(); 53 assertEquals(size-1,schema.getSimpleTypes().size()); 54 55 size = schema.getElements().size(); 56 GlobalElement ge = schema.getElements().iterator().next(); 57 model.startTransaction(); 58 instance.update(schema, ge ,SyncUpdateVisitor.Operation.REMOVE); 59 model.endTransaction(); 60 assertEquals(size-1,schema.getElements().size()); 61 } 62 63 public void testVisitAdd() throws Exception { 64 setUp1(); 65 SyncUpdateVisitor instance = new SyncUpdateVisitor(); 66 67 int size = schema.getComplexTypes().size(); 68 schema.getModel().getFactory().createGlobalComplexType(); 69 GlobalComplexType gct = schema.getModel().getFactory().createGlobalComplexType(); 70 model.startTransaction(); 71 instance.update(schema, gct ,SyncUpdateVisitor.Operation.ADD); 72 model.endTransaction(); 73 assertEquals(size+1,schema.getComplexTypes().size()); 74 75 size = schema.getSchemaReferences().size(); 76 SchemaComponent sc = schema.getModel().getFactory().createImport(); 77 model.startTransaction(); 78 instance.update(schema, sc ,SyncUpdateVisitor.Operation.ADD); 79 model.endTransaction(); 80 assertEquals(size+1,schema.getSchemaReferences().size()); 81 } 82 83 public void testRemoveAllPurchaseOrder() throws Exception { 84 setUp1(); 85 model.startTransaction(); 86 recursiveRemoveChildren(schema); 87 assertEquals("children removed", 0, schema.getChildren().size()); 88 model.endTransaction(); 89 } 90 91 100 101 public void testRemoveAllLoanApp() throws Exception { 102 model = TestCatalogModel.getDefault().getSchemaModel(NamespaceLocation.LOANAPP); 103 schema = model.getSchema(); 104 model.startTransaction(); 105 recursiveRemoveChildren(schema); 106 assertEquals("children removed", 0, schema.getChildren().size()); 107 model.endTransaction(); 108 } 109 110 public static void recursiveRemoveChildren(SchemaComponent target) { 111 SchemaModel model = target.getModel(); 112 ArrayList <SchemaComponent> children = new ArrayList <SchemaComponent>(target.getChildren()); 113 for (SchemaComponent child : children) { 114 recursiveRemoveChildren(child); 115 } 116 if (target.getParent() != null) { 117 model.removeChildComponent(target); 118 } 119 } 120 121 public void testCanPasteAllLoanApp() throws Exception { 122 model = TestCatalogModel.getDefault().getSchemaModel(NamespaceLocation.LOANAPP); 123 schema = model.getSchema(); 124 recursiveCanPasteChildren(schema); 125 recursiveCannotPasteChildren(schema); 126 } 127 128 public static void recursiveCanPasteChildren(SchemaComponent target) { 129 SchemaModel model = target.getModel(); 130 ArrayList <SchemaComponent> children = new ArrayList <SchemaComponent>(target.getChildren()); 131 for (SchemaComponent child : children) { 132 recursiveCanPasteChildren(child); 133 } 134 if (target.getParent() != null) { 135 target.getParent().canPaste(target); } 137 } 138 139 public static void recursiveCannotPasteChildren(SchemaComponent target) { 140 SchemaModel model = target.getModel(); 141 ArrayList <SchemaComponent> children = new ArrayList <SchemaComponent>(target.getChildren()); 142 for (SchemaComponent child : children) { 143 recursiveCannotPasteChildren(child); 144 } 145 if (target.getParent() != null) { 146 if (! (target instanceof SimpleTypeRestriction && target.getParent() instanceof LocalSimpleType)) { 147 String msg = target.getClass().getName() + " canPaste " + target.getParent().getClass().getName(); 148 assertFalse(msg, target.canPaste(target.getParent())); 149 } 150 } 151 } 152 } 153 154 | Popular Tags |