Add unit test sample frame. BLD200611021900
authornnguyen@netbeans.org
Thu, 02 Nov 2006 18:18:06 +0000
changeset 7644d709ddfa16d7
parent 7643 4afffbe74902
child 7645 adabc71c0b50
Add unit test sample frame.
Improved implementation that pass added unit tests.
o.sample.registry.model/nbproject/build-impl.xml
o.sample.registry.model/nbproject/project.properties
o.sample.registry.model/src/org/sample/registry/model/RegistryModelFactory.java
o.sample.registry.model/src/org/sample/registry/model/RegistryVisitor.java
o.sample.registry.model/src/org/sample/registry/model/impl/RegistryComponentFactoryImpl.java
o.sample.registry.model/src/org/sample/registry/model/impl/RegistryComponentImpl.java
o.sample.registry.model/src/org/sample/registry/model/impl/RegistryModelImpl.java
o.sample.registry.model/src/org/sample/registry/model/impl/RegistryQNames.java
o.sample.registry.model/src/org/sample/registry/model/impl/ServiceProviderImpl.java
o.sample.registry.model/src/org/sample/registry/model/impl/ServiceTypeImpl.java
o.sample.registry.model/src/org/sample/registry/model/registry.xsd
o.sample.registry.model/test/unit/src/org/sample/registry/model/RegistryModelTest.java
o.sample.registry.model/test/unit/src/org/sample/registry/model/SyncUpdateTest.java
o.sample.registry.model/test/unit/src/org/sample/registry/model/TestCatalogModel.java
o.sample.registry.model/test/unit/src/org/sample/registry/model/Util.java
o.sample.registry.model/test/unit/src/org/sample/registry/model/test1.xml
o.sample.registry.model/test/unit/src/org/sample/registry/model/test1_simpleTypeElement.xml
     1.1 --- a/o.sample.registry.model/nbproject/build-impl.xml	Thu Nov 02 10:18:32 2006 +0000
     1.2 +++ b/o.sample.registry.model/nbproject/build-impl.xml	Thu Nov 02 18:18:06 2006 +0000
     1.3 @@ -3,7 +3,7 @@
     1.4  *** GENERATED FROM project.xml - DO NOT EDIT  ***
     1.5  ***         EDIT ../build.xml INSTEAD         ***
     1.6  -->
     1.7 -<project name="org.sample.registry.model-impl" basedir=".." xmlns:nbmproject="http://www.netbeans.org/ns/nb-module-project/2">
     1.8 +<project name="org.sample.registry.model-impl" basedir="..">
     1.9      <property file="nbproject/private/platform-private.properties"/>
    1.10      <property file="nbproject/platform.properties"/>
    1.11      <macrodef name="property" uri="http://www.netbeans.org/ns/nb-module-project/2">
    1.12 @@ -14,8 +14,8 @@
    1.13          </sequential>
    1.14      </macrodef>
    1.15      <property file="${user.properties.file}"/>
    1.16 -    <nbmproject:property name="harness.dir" value="nbplatform.${nbplatform.active}.harness.dir"/>
    1.17 -    <nbmproject:property name="netbeans.dest.dir" value="nbplatform.${nbplatform.active}.netbeans.dest.dir"/>
    1.18 +    <nbmproject2:property name="harness.dir" value="nbplatform.${nbplatform.active}.harness.dir" xmlns:nbmproject2="http://www.netbeans.org/ns/nb-module-project/2"/>
    1.19 +    <nbmproject2:property name="netbeans.dest.dir" value="nbplatform.${nbplatform.active}.netbeans.dest.dir" xmlns:nbmproject2="http://www.netbeans.org/ns/nb-module-project/2"/>
    1.20      <fail message="You must define 'nbplatform.${nbplatform.active}.harness.dir'">
    1.21          <condition>
    1.22              <not>
     2.1 --- a/o.sample.registry.model/nbproject/project.properties	Thu Nov 02 10:18:32 2006 +0000
     2.2 +++ b/o.sample.registry.model/nbproject/project.properties	Thu Nov 02 18:18:06 2006 +0000
     2.3 @@ -1,2 +1,27 @@
     2.4  javac.compilerargs=-Xlint:unchecked
     2.5  javac.source=1.5
     2.6 +test.unit.cp.extra=\
     2.7 +    ${netbeans.dest.dir}/ide8/modules/org-netbeans-modules-xml-xdm.jar:\
     2.8 +    ${netbeans.dest.dir}/ide8/modules/org-netbeans-modules-xml-xam.jar:\
     2.9 +    ${netbeans.dest.dir}/ide8/modules/org-netbeans-modules-xml-retriever.jar:\
    2.10 +    ${netbeans.dest.dir}/ide8/modules/org-sample-registry-model.jar:\
    2.11 +    ${netbeans.dest.dir}/ide8/modules/org-netbeans-modules-editor.jar:\
    2.12 +    ${netbeans.dest.dir}/ide8/modules/org-netbeans-modules-lexer-editorbridge.jar:\
    2.13 +    ${netbeans.dest.dir}/platform7/lib/org-openide-modules.jar:\
    2.14 +    ${netbeans.dest.dir}/platform7/lib/org-openide-util.jar:\
    2.15 +    ${netbeans.dest.dir}/platform7/modules/org-openide-options.jar:\
    2.16 +    ${netbeans.dest.dir}/platform7/modules/org-openide-text.jar:\
    2.17 +    ${netbeans.dest.dir}/platform7/modules/org-openide-nodes.jar:\
    2.18 +    ${netbeans.dest.dir}/platform7/modules/org-openide-loaders.jar:\
    2.19 +    ${netbeans.dest.dir}/platform7/core/org-openide-filesystems.jar:\
    2.20 +    ${netbeans.dest.dir}/ide8/modules/org-netbeans-modules-editor-util.jar:\
    2.21 +    ${netbeans.dest.dir}/ide8/modules/org-netbeans-modules-xml-text.jar:\
    2.22 +    ${netbeans.dest.dir}/ide8/modules/org-netbeans-modules-xml-core.jar:\
    2.23 +    ${netbeans.dest.dir}/ide8/modules/org-netbeans-modules-editor-lib.jar:\
    2.24 +    ${netbeans.dest.dir}/ide8/modules/org-netbeans-modules-projectapi.jar:\
    2.25 +    ${netbeans.dest.dir}/platform7/modules/org-netbeans-modules-masterfs.jar:\
    2.26 +    ${netbeans.dest.dir}/platform7/modules/org-netbeans-modules-editor-mimelookup.jar:\
    2.27 +    ${netbeans.dest.dir}/platform7/modules/org-openide-windows.jar:\
    2.28 +    ${netbeans.dest.dir}/platform7/modules/org-openide-dialogs.jar:\
    2.29 +    ${netbeans.dest.dir}/platform7/modules/org-openide-awt.jar:\
    2.30 +    ${netbeans.dest.dir}/ide8/modules/org-apache-xml-resolver.jar
     3.1 --- a/o.sample.registry.model/src/org/sample/registry/model/RegistryModelFactory.java	Thu Nov 02 10:18:32 2006 +0000
     3.2 +++ b/o.sample.registry.model/src/org/sample/registry/model/RegistryModelFactory.java	Thu Nov 02 18:18:06 2006 +0000
     3.3 @@ -20,15 +20,26 @@
     3.4  
     3.5  import org.netbeans.modules.xml.xam.AbstractModelFactory;
     3.6  import org.netbeans.modules.xml.xam.ModelSource;
     3.7 +import org.sample.registry.model.impl.RegistryModelImpl;
     3.8  
     3.9 -public class RegistryModelFactory extends AbstractModelFactory {    
    3.10 +public class RegistryModelFactory extends AbstractModelFactory<RegistryModel> {    
    3.11      /** Creates a new instance of RegistryModelFactory */
    3.12 -    public RegistryModelFactory() {
    3.13 +    private RegistryModelFactory() {
    3.14      }
    3.15  
    3.16 -    protected RegistryModel createModel(ModelSource source) {
    3.17 -        //TODO
    3.18 -        return null;
    3.19 +    private static RegistryModelFactory instance;
    3.20 +    public static RegistryModelFactory getInstance() {
    3.21 +        if (instance == null) {
    3.22 +            instance = new RegistryModelFactory();
    3.23 +        }
    3.24 +        return instance;
    3.25      }
    3.26      
    3.27 +    protected RegistryModel createModel(ModelSource source) {
    3.28 +        return new RegistryModelImpl(source);
    3.29 +    }
    3.30 +
    3.31 +    public RegistryModel getModel(ModelSource source) {
    3.32 +        return (RegistryModel) super.getModel(source);
    3.33 +    }
    3.34  }
     4.1 --- a/o.sample.registry.model/src/org/sample/registry/model/RegistryVisitor.java	Thu Nov 02 10:18:32 2006 +0000
     4.2 +++ b/o.sample.registry.model/src/org/sample/registry/model/RegistryVisitor.java	Thu Nov 02 18:18:06 2006 +0000
     4.3 @@ -50,7 +50,6 @@
     4.4              visitChild();
     4.5          }
     4.6          protected void visitChild() {
     4.7 -            visitChild();
     4.8          }
     4.9      }
    4.10      
     5.1 --- a/o.sample.registry.model/src/org/sample/registry/model/impl/RegistryComponentFactoryImpl.java	Thu Nov 02 10:18:32 2006 +0000
     5.2 +++ b/o.sample.registry.model/src/org/sample/registry/model/impl/RegistryComponentFactoryImpl.java	Thu Nov 02 18:18:06 2006 +0000
     5.3 @@ -37,27 +37,39 @@
     5.4      public RegistryComponentFactoryImpl(RegistryModelImpl model) {
     5.5          this.model = model;
     5.6      }
     5.7 -
     5.8 +    
     5.9      public RegistryComponent create(Element element, RegistryComponent context) {
    5.10 -        return new CreateVisitor().create(element, context);
    5.11 +        if (context == null) {
    5.12 +            if (areSameQName(RegistryQNames.REGISTRY, element)) {
    5.13 +                return new RegistryImpl(model, element);
    5.14 +            } else {
    5.15 +                return null;
    5.16 +            }
    5.17 +        } else {
    5.18 +            return new CreateVisitor().create(element, context);
    5.19 +        }
    5.20      }
    5.21      
    5.22      public ServiceType createServiceType() {
    5.23          return new ServiceTypeImpl(model);
    5.24      }
    5.25 -
    5.26 +    
    5.27      public ServiceProvider createServiceProvider() {
    5.28          return new ServiceProviderImpl(model);
    5.29      }
    5.30 -
    5.31 +    
    5.32      public Service createService() {
    5.33          return new ServiceImpl(model);
    5.34      }
    5.35 -
    5.36 +    
    5.37      public Registry createRegistry() {
    5.38          return new RegistryImpl(model);
    5.39      }
    5.40 -
    5.41 +    
    5.42 +    public static boolean areSameQName(RegistryQNames q, Element e) {
    5.43 +        return q.getQName().equals(AbstractDocumentComponent.getQName(e));
    5.44 +    }
    5.45 +    
    5.46      public static class CreateVisitor extends RegistryVisitor.Default {
    5.47          Element element;
    5.48          RegistryComponent created;
    5.49 @@ -72,10 +84,6 @@
    5.50              return areSameQName(q, element);
    5.51          }
    5.52          
    5.53 -        private boolean areSameQName(RegistryQNames q, Element e) {
    5.54 -            return q.getQName().equals(AbstractDocumentComponent.getQName(e));
    5.55 -        }
    5.56 -
    5.57          public void visit(Registry context) {
    5.58              if (isElementQName(RegistryQNames.ENTRIES)) {
    5.59                  created = new EntriesImpl((RegistryModelImpl) context.getModel(), element);
    5.60 @@ -86,19 +94,19 @@
    5.61          
    5.62          public void visit(Entries context) {
    5.63              if (isElementQName(RegistryQNames.SERVICE)) {
    5.64 -                new ServiceImpl((RegistryModelImpl)context.getModel(), element);
    5.65 +                created = new ServiceImpl((RegistryModelImpl)context.getModel(), element);
    5.66              }
    5.67          }
    5.68          
    5.69          public void visit(KnownTypes context) {
    5.70 -            if (isElementQName(RegistryQNames.KNOWN_TYPES)) {
    5.71 -                new ServiceImpl((RegistryModelImpl)context.getModel(), element);
    5.72 +            if (isElementQName(RegistryQNames.TYPE)) {
    5.73 +                created = new ServiceTypeImpl((RegistryModelImpl)context.getModel(), element);
    5.74              }
    5.75          }
    5.76          
    5.77          public void visit(Service context) {
    5.78              if (isElementQName(RegistryQNames.SERVICE_PROVIDER)) {
    5.79 -                new ServiceProviderImpl((RegistryModelImpl)context.getModel(), element);
    5.80 +                created = new ServiceProviderImpl((RegistryModelImpl)context.getModel(), element);
    5.81              }
    5.82          }
    5.83          
     6.1 --- a/o.sample.registry.model/src/org/sample/registry/model/impl/RegistryComponentImpl.java	Thu Nov 02 10:18:32 2006 +0000
     6.2 +++ b/o.sample.registry.model/src/org/sample/registry/model/impl/RegistryComponentImpl.java	Thu Nov 02 18:18:06 2006 +0000
     6.3 @@ -19,6 +19,7 @@
     6.4  package org.sample.registry.model.impl;
     6.5  
     6.6  import java.util.List;
     6.7 +import javax.xml.namespace.QName;
     6.8  import org.netbeans.modules.xml.xam.Nameable;
     6.9  import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
    6.10  import org.netbeans.modules.xml.xam.dom.Attribute;
    6.11 @@ -74,4 +75,5 @@
    6.12              super.setAttribute(Nameable.NAME_PROPERTY, RegistryAttributes.NAME, name);
    6.13          }
    6.14      }
    6.15 +    
    6.16  }
     7.1 --- a/o.sample.registry.model/src/org/sample/registry/model/impl/RegistryModelImpl.java	Thu Nov 02 10:18:32 2006 +0000
     7.2 +++ b/o.sample.registry.model/src/org/sample/registry/model/impl/RegistryModelImpl.java	Thu Nov 02 18:18:06 2006 +0000
     7.3 @@ -18,6 +18,8 @@
     7.4   */
     7.5  package org.sample.registry.model.impl;
     7.6  
     7.7 +import java.util.Set;
     7.8 +import javax.xml.namespace.QName;
     7.9  import org.netbeans.modules.xml.xam.ComponentUpdater;
    7.10  import org.netbeans.modules.xml.xam.ModelSource;
    7.11  import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
    7.12 @@ -45,14 +47,23 @@
    7.13      }
    7.14  
    7.15      public RegistryComponent createComponent(RegistryComponent parent, Element element) {
    7.16 -        return factory.create(element, parent);
    7.17 +        return getFactory().create(element, parent);
    7.18      }
    7.19  
    7.20      public Registry createRootComponent(Element root) {
    7.21 -        return new RegistryImpl(this, root);
    7.22 +        Registry newRegistry = (Registry) getFactory().create(root, null);
    7.23 +        if (newRegistry != null) {
    7.24 +            registry = newRegistry;
    7.25 +        }
    7.26 +        return newRegistry;
    7.27      }
    7.28  
    7.29      public RegistryComponentFactory getFactory() {
    7.30          return factory;
    7.31      }
    7.32 +    
    7.33 +    public Set<QName> getQNames() {
    7.34 +        return RegistryQNames.getMappedQNames();
    7.35 +    }
    7.36 +        
    7.37  }
     8.1 --- a/o.sample.registry.model/src/org/sample/registry/model/impl/RegistryQNames.java	Thu Nov 02 10:18:32 2006 +0000
     8.2 +++ b/o.sample.registry.model/src/org/sample/registry/model/impl/RegistryQNames.java	Thu Nov 02 18:18:06 2006 +0000
     8.3 @@ -18,13 +18,15 @@
     8.4   */
     8.5  package org.sample.registry.model.impl;
     8.6  
     8.7 +import java.util.Collections;
     8.8 +import java.util.HashSet;
     8.9 +import java.util.Set;
    8.10  import javax.xml.namespace.QName;
    8.11  
    8.12  public enum RegistryQNames {
    8.13      REGISTRY("registry"),
    8.14      ENTRIES("entries"),
    8.15      SERVICE("service"),
    8.16 -    LOCATION("location"),
    8.17      SERVICE_PROVIDER("provider"),
    8.18      URL("url"),
    8.19      KNOWN_TYPES("known-types"),
    8.20 @@ -32,9 +34,22 @@
    8.21      DEFINITION("definition"),
    8.22      DOCUMENTATION("documentation");
    8.23      
    8.24 -    public static final String REGISTRY_NS = "http://org.samples.org/registry";
    8.25 +    public static final String REGISTRY_NS = "http://www.samples.org/registry";
    8.26      public static final String REGISTRY_PREFIX = "reg";
    8.27      
    8.28 +    private static Set<QName> mappedQNames = new HashSet<QName>();
    8.29 +    static {
    8.30 +        mappedQNames.add(REGISTRY.getQName());
    8.31 +        mappedQNames.add(ENTRIES.getQName());
    8.32 +        mappedQNames.add(SERVICE.getQName());
    8.33 +        mappedQNames.add(SERVICE_PROVIDER.getQName());
    8.34 +        //mappedQNames.add(URL.getQName());
    8.35 +        mappedQNames.add(KNOWN_TYPES.getQName());
    8.36 +        mappedQNames.add(TYPE.getQName());
    8.37 +        //mappedQNames.add(DEFINITION.getQName());
    8.38 +        //mappedQNames.add(DOCUMENTATION.getQName());
    8.39 +    }
    8.40 +
    8.41      private QName qname;
    8.42      
    8.43      RegistryQNames(String localName) {
    8.44 @@ -52,4 +67,8 @@
    8.45      public String getQualifiedName() {
    8.46          return qname.getPrefix() + ":" + qname.getLocalPart();
    8.47      }
    8.48 +    
    8.49 +    public static Set<QName> getMappedQNames() {
    8.50 +        return Collections.unmodifiableSet(mappedQNames);
    8.51 +    }
    8.52  }
     9.1 --- a/o.sample.registry.model/src/org/sample/registry/model/impl/ServiceProviderImpl.java	Thu Nov 02 10:18:32 2006 +0000
     9.2 +++ b/o.sample.registry.model/src/org/sample/registry/model/impl/ServiceProviderImpl.java	Thu Nov 02 18:18:06 2006 +0000
     9.3 @@ -33,11 +33,11 @@
     9.4      }
     9.5  
     9.6      public String getURL() {
     9.7 -        return getText();
     9.8 +        return getChildElementText(RegistryQNames.URL.getQName());
     9.9      }
    9.10      
    9.11      public void setURL(String url) {
    9.12 -        setText(URL_PROPERTY, url);
    9.13 +        setChildElementText(URL_PROPERTY, url, RegistryQNames.URL.getQName());
    9.14      }
    9.15  
    9.16      public void accept(RegistryVisitor visitor) {
    10.1 --- a/o.sample.registry.model/src/org/sample/registry/model/impl/ServiceTypeImpl.java	Thu Nov 02 10:18:32 2006 +0000
    10.2 +++ b/o.sample.registry.model/src/org/sample/registry/model/impl/ServiceTypeImpl.java	Thu Nov 02 18:18:06 2006 +0000
    10.3 @@ -23,6 +23,7 @@
    10.4  import org.sample.registry.model.RegistryModel;
    10.5  import org.sample.registry.model.RegistryVisitor;
    10.6  import org.sample.registry.model.ServiceType;
    10.7 +import org.sample.registry.model.impl.RegistryQNames;
    10.8  import org.w3c.dom.Element;
    10.9  
   10.10  public class ServiceTypeImpl extends RegistryComponentImpl.Named
   10.11 @@ -37,15 +38,15 @@
   10.12      }
   10.13      
   10.14      public String getDefinition() {
   10.15 -        return getText();
   10.16 +        return getChildElementText(RegistryQNames.DEFINITION.getQName());
   10.17      }
   10.18      
   10.19      public void setDefinition(String definition) {
   10.20 -        setText(DEFINITION_PROPERTY, definition);
   10.21 +        setChildElementText(DEFINITION_PROPERTY, definition, RegistryQNames.DEFINITION.getQName());
   10.22      }
   10.23      
   10.24      public String getDocumentation() {
   10.25 -        return getText();
   10.26 +        return getChildElementText(RegistryQNames.DOCUMENTATION.getQName());
   10.27      }
   10.28      
   10.29      public void setDocumentation(String documentation) {
    11.1 --- a/o.sample.registry.model/src/org/sample/registry/model/registry.xsd	Thu Nov 02 10:18:32 2006 +0000
    11.2 +++ b/o.sample.registry.model/src/org/sample/registry/model/registry.xsd	Thu Nov 02 18:18:06 2006 +0000
    11.3 @@ -35,7 +35,6 @@
    11.4        <xsd:element name="service" minOccurs="0" maxOccurs="unbounded">
    11.5          <xsd:complexType>
    11.6            <xsd:sequence>
    11.7 -            <xsd:element name="location" type="xsd:anyURI"/>
    11.8                <xsd:element name="provider" type="tns:ProviderType"/>
    11.9            </xsd:sequence>
   11.10            <xsd:attribute name="type" type="xsd:QName" use="required"/>
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/o.sample.registry.model/test/unit/src/org/sample/registry/model/RegistryModelTest.java	Thu Nov 02 18:18:06 2006 +0000
    12.3 @@ -0,0 +1,44 @@
    12.4 +package org.sample.registry.model;
    12.5 +
    12.6 +import junit.framework.*;
    12.7 +import org.netbeans.modules.xml.xam.dom.DocumentModel;
    12.8 +
    12.9 +public class RegistryModelTest extends TestCase {
   12.10 +    
   12.11 +    public RegistryModelTest(String testName) {
   12.12 +        super(testName);
   12.13 +    }
   12.14 +
   12.15 +    protected void setUp() throws Exception {
   12.16 +    }
   12.17 +
   12.18 +    protected void tearDown() throws Exception {
   12.19 +    }
   12.20 +
   12.21 +    public static Test suite() {
   12.22 +        TestSuite suite = new TestSuite(RegistryModelTest.class);
   12.23 +        
   12.24 +        return suite;
   12.25 +    }
   12.26 +
   12.27 +    public void testReadWrite() throws Exception {
   12.28 +        RegistryModel model = Util.loadRegistryModel("test1.xml");
   12.29 +        Registry root = model.getRootComponent();
   12.30 +        Entries entries = root.getEntries();
   12.31 +        Service service = entries.getServices().iterator().next();
   12.32 +        assertEquals("service1", service.getName());
   12.33 +        ServiceProvider sp = service.getProvider();
   12.34 +        assertEquals("acme.com", sp.getName());
   12.35 +        assertEquals("http://www.acme.com/finance", sp.getURL());
   12.36 +        
   12.37 +        String newValue = "http://www.acme.org/finance";
   12.38 +        model.startTransaction();
   12.39 +        sp.setURL(newValue);
   12.40 +        model.endTransaction();
   12.41 +        
   12.42 +        model = Util.dumpAndReloadModel(model);
   12.43 +        service = model.getRootComponent().getEntries().getServices().iterator().next();
   12.44 +        assertEquals(newValue, service.getProvider().getURL());
   12.45 +    }
   12.46 +    
   12.47 +}
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/o.sample.registry.model/test/unit/src/org/sample/registry/model/SyncUpdateTest.java	Thu Nov 02 18:18:06 2006 +0000
    13.3 @@ -0,0 +1,26 @@
    13.4 +package org.sample.registry.model;
    13.5 +
    13.6 +import junit.framework.*;
    13.7 +
    13.8 +public class SyncUpdateTest extends TestCase {
    13.9 +    
   13.10 +    public SyncUpdateTest(String testName) {
   13.11 +        super(testName);
   13.12 +    }
   13.13 +
   13.14 +    protected void setUp() throws Exception {
   13.15 +    }
   13.16 +
   13.17 +    protected void tearDown() throws Exception {
   13.18 +    }
   13.19 +    
   13.20 +    public void testSyncSimpleTypeElement() throws Exception {
   13.21 +        RegistryModel model = Util.loadRegistryModel("test1.xml");
   13.22 +        ServiceType type = model.getRootComponent().getKnownTypes().getKnownTypes().get(0);
   13.23 +        assertEquals("http://www.finance.org/processor", type.getDefinition());
   13.24 +        
   13.25 +        Util.setDocumentContentTo(model, "test1_simpleTypeElement.xml");
   13.26 +        
   13.27 +        assertEquals("http://www.acme.org/processor", type.getDefinition());
   13.28 +    }
   13.29 +}
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/o.sample.registry.model/test/unit/src/org/sample/registry/model/TestCatalogModel.java	Thu Nov 02 18:18:06 2006 +0000
    14.3 @@ -0,0 +1,167 @@
    14.4 +/*
    14.5 + * TestCatalogModel.java
    14.6 + *
    14.7 + * Created on April 2, 2006, 10:41 AM
    14.8 + *
    14.9 + * To change this template, choose Tools | Template Manager
   14.10 + * and open the template in the editor.
   14.11 + */
   14.12 +
   14.13 +package org.sample.registry.model;
   14.14 +
   14.15 +import java.io.File;
   14.16 +import java.io.FileInputStream;
   14.17 +import java.io.IOException;
   14.18 +import java.util.HashMap;
   14.19 +import java.util.Map;
   14.20 +import javax.swing.text.Document;
   14.21 +import org.netbeans.modules.xml.retriever.catalog.impl.CatalogFileWrapperDOMImpl;
   14.22 +import org.netbeans.modules.xml.retriever.catalog.impl.CatalogWriteModelImpl;
   14.23 +import org.netbeans.modules.xml.xam.locator.CatalogModel;
   14.24 +import org.netbeans.modules.xml.xam.locator.CatalogModelException;
   14.25 +import org.netbeans.modules.xml.xam.ModelSource;
   14.26 +import org.openide.filesystems.FileObject;
   14.27 +import org.openide.filesystems.FileUtil;
   14.28 +import org.openide.loaders.DataObject;
   14.29 +import org.openide.loaders.DataObjectNotFoundException;
   14.30 +import org.openide.util.Lookup;
   14.31 +import org.openide.util.lookup.Lookups;
   14.32 +
   14.33 +/**
   14.34 + *
   14.35 + * @author girix
   14.36 + */
   14.37 +
   14.38 +public class TestCatalogModel extends CatalogWriteModelImpl{
   14.39 +    private TestCatalogModel(File file) throws IOException{
   14.40 +        super(file);
   14.41 +    }
   14.42 +    
   14.43 +    static TestCatalogModel singletonCatMod = null;
   14.44 +    public static TestCatalogModel getDefault(){
   14.45 +        if (singletonCatMod == null){
   14.46 +            CatalogFileWrapperDOMImpl.TEST_ENVIRONMENT = true;
   14.47 +            try {
   14.48 +                singletonCatMod = new TestCatalogModel(Util.getTempDir("schematest/catalog"));
   14.49 +                FileObject catalogFO = singletonCatMod.getCatalogFileObject();
   14.50 +                File catFile = FileUtil.toFile(catalogFO);
   14.51 +                catFile.deleteOnExit();
   14.52 +                initCatalogFile();
   14.53 +            } catch (Exception ex) {
   14.54 +                ex.printStackTrace();
   14.55 +                return null;
   14.56 +            }
   14.57 +        }
   14.58 +        return singletonCatMod;
   14.59 +    }
   14.60 +    
   14.61 +    
   14.62 +    /**
   14.63 +     * This method could be overridden by the Unit testcase to return a special
   14.64 +     * ModelSource object for a FileObject with custom impl of classes added to the lookup.
   14.65 +     * This is optional if both getDocument(FO) and createCatalogModel(FO) are overridden.
   14.66 +     */
   14.67 +    protected ModelSource createModelSource(final FileObject thisFileObj, boolean editable) throws CatalogModelException{
   14.68 +        assert thisFileObj != null : "Null file object.";
   14.69 +        final CatalogModel catalogModel = createCatalogModel(thisFileObj);
   14.70 +        final DataObject dobj;
   14.71 +        try {
   14.72 +            dobj = DataObject.find(thisFileObj);
   14.73 +        } catch (DataObjectNotFoundException ex) {
   14.74 +            throw new CatalogModelException(ex);
   14.75 +        }
   14.76 +        Lookup proxyLookup = Lookups.proxy(
   14.77 +                new Lookup.Provider() {
   14.78 +            public Lookup getLookup() {
   14.79 +                Document document = null;
   14.80 +                document = getDocument(thisFileObj);
   14.81 +                return Lookups.fixed(new Object[] {
   14.82 +                    FileUtil.toFile(thisFileObj),
   14.83 +                    thisFileObj,
   14.84 +                    document,
   14.85 +                    dobj,
   14.86 +                    catalogModel
   14.87 +                });
   14.88 +            }
   14.89 +        }
   14.90 +        );
   14.91 +        return new ModelSource(proxyLookup, editable);
   14.92 +    }
   14.93 +    
   14.94 +    private Document getDocument(FileObject fo){
   14.95 +        Document result = null;
   14.96 +        if (documentPooling) {
   14.97 +            result = documentPool().get(fo);
   14.98 +        }
   14.99 +        if (result != null) return result;
  14.100 +        try {
  14.101 +            
  14.102 +            File file = FileUtil.toFile(fo);
  14.103 +            FileInputStream fis = new FileInputStream(file);
  14.104 +            byte buffer[] = new byte[fis.available()];
  14.105 +            result = new org.netbeans.editor.BaseDocument(
  14.106 +                    org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
  14.107 +            result.remove(0, result.getLength());
  14.108 +            fis.read(buffer);
  14.109 +            fis.close();
  14.110 +            String str = new String(buffer);
  14.111 +            result.insertString(0,str,null);
  14.112 +            
  14.113 +        } catch (Exception dObjEx) {
  14.114 +            return null;
  14.115 +        }
  14.116 +        if (documentPooling) {
  14.117 +            documentPool().put(fo, result);
  14.118 +        }
  14.119 +        return result;
  14.120 +    }
  14.121 +    
  14.122 +    protected CatalogModel createCatalogModel(FileObject fo) throws CatalogModelException{
  14.123 +        return getDefault();
  14.124 +    }
  14.125 +    
  14.126 +    public ModelSource createTestModelSource(FileObject fo, boolean editable) throws CatalogModelException{
  14.127 +        final DataObject dobj;
  14.128 +        final CatalogModel catalogModel = createCatalogModel(fo);
  14.129 +        try {
  14.130 +            dobj = DataObject.find(fo);
  14.131 +        } catch (DataObjectNotFoundException ex) {
  14.132 +            throw new CatalogModelException(ex);
  14.133 +        }
  14.134 +        Lookup lookup = Lookups.proxy(new Lookup.Provider() {
  14.135 +            public Lookup getLookup() {
  14.136 +                        return Lookups.fixed(new Object[] {
  14.137 +                            dobj.getPrimaryFile(),
  14.138 +                            getDocument(dobj.getPrimaryFile()),
  14.139 +                            dobj,
  14.140 +                            catalogModel
  14.141 +                        });
  14.142 +            }
  14.143 +        } );
  14.144 +        return new ModelSource(lookup, editable);
  14.145 +    }
  14.146 +    
  14.147 +    private static void initCatalogFile() throws Exception {
  14.148 +    }
  14.149 +    
  14.150 +    private Map<FileObject,Document> fileToDocumentMap;
  14.151 +    private Map<FileObject,Document> documentPool() {
  14.152 +        if (fileToDocumentMap == null) {
  14.153 +            fileToDocumentMap = new HashMap<FileObject,Document>();
  14.154 +        }
  14.155 +        return fileToDocumentMap;
  14.156 +    }
  14.157 +    private boolean documentPooling = true;
  14.158 +    
  14.159 +    public void setDocumentPooling(boolean v) {
  14.160 +        documentPooling = v;
  14.161 +        if (! documentPooling) {
  14.162 +            clearDocumentPool();
  14.163 +        }
  14.164 +    }
  14.165 +
  14.166 +    public void clearDocumentPool() {
  14.167 +        fileToDocumentMap = null;
  14.168 +    }
  14.169 +}
  14.170 +
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/o.sample.registry.model/test/unit/src/org/sample/registry/model/Util.java	Thu Nov 02 18:18:06 2006 +0000
    15.3 @@ -0,0 +1,137 @@
    15.4 +/*
    15.5 + * Util.java
    15.6 + *
    15.7 + * Created on October 4, 2005, 7:48 PM
    15.8 + *
    15.9 + * To change this template, choose Tools | Template Manager
   15.10 + * and open the template in the editor.
   15.11 + */
   15.12 +
   15.13 +package org.sample.registry.model;
   15.14 +
   15.15 +import java.io.BufferedInputStream;
   15.16 +import java.io.BufferedOutputStream;
   15.17 +import java.io.BufferedReader;
   15.18 +import java.io.File;
   15.19 +import java.io.FileInputStream;
   15.20 +import java.io.FileOutputStream;
   15.21 +import java.io.InputStream;
   15.22 +import java.io.InputStreamReader;
   15.23 +import java.io.OutputStream;
   15.24 +import java.io.PrintWriter;
   15.25 +import java.net.URI;
   15.26 +import javax.swing.text.Document;
   15.27 +import org.netbeans.modules.xml.xam.ModelSource;
   15.28 +import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
   15.29 +import org.netbeans.modules.xml.xam.dom.DocumentModel;
   15.30 +
   15.31 +/**
   15.32 + *
   15.33 + * @author nn136682
   15.34 + */
   15.35 +public class Util {
   15.36 +    public static Document getResourceAsDocument(String path) throws Exception {
   15.37 +        InputStream in = Util.class.getResourceAsStream(path);
   15.38 +        return loadDocument(in);
   15.39 +    }
   15.40 +
   15.41 +    public static Document loadDocument(InputStream in) throws Exception {
   15.42 +//	Document sd = new PlainDocument();
   15.43 +        Document sd = new org.netbeans.editor.BaseDocument(
   15.44 +                org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
   15.45 +        return setDocumentContentTo(sd, in);
   15.46 +    }
   15.47 +    
   15.48 +    public static Document setDocumentContentTo(Document doc, InputStream in) throws Exception {
   15.49 +        BufferedReader br = new BufferedReader(new InputStreamReader(in));
   15.50 +        StringBuffer sbuf = new StringBuffer();
   15.51 +        try {
   15.52 +            String line = null;
   15.53 +            while ((line = br.readLine()) != null) {
   15.54 +                sbuf.append(line);
   15.55 +                sbuf.append(System.getProperty("line.separator"));
   15.56 +            }
   15.57 +        } finally {
   15.58 +            br.close();
   15.59 +        }
   15.60 +        doc.remove(0, doc.getLength());
   15.61 +        doc.insertString(0,sbuf.toString(),null);
   15.62 +        return doc;
   15.63 +    }
   15.64 +    
   15.65 +    public static Document setDocumentContentTo(Document doc, String resourcePath) throws Exception {
   15.66 +        return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
   15.67 +    }
   15.68 +
   15.69 +    public static void setDocumentContentTo(DocumentModel model, String resourcePath) throws Exception {
   15.70 +        setDocumentContentTo(((AbstractDocumentModel)model).getBaseDocument(), resourcePath);
   15.71 +        model.sync();
   15.72 +    }
   15.73 +    
   15.74 +    public static int count = 0;
   15.75 +    public static RegistryModel loadRegistryModel(String resourcePath) throws Exception {
   15.76 +        URI locationURI = new URI(resourcePath);
   15.77 +        TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath));
   15.78 +        ModelSource ms = TestCatalogModel.getDefault().getModelSource(locationURI);
   15.79 +        return RegistryModelFactory.getInstance().getModel(ms);
   15.80 +    }
   15.81 +    
   15.82 +    public static RegistryModel loadRegistryModel(File schemaFile) throws Exception {
   15.83 +        URI locationURI = new URI(schemaFile.getName());
   15.84 +        TestCatalogModel.getDefault().addURI(locationURI, schemaFile.toURI());
   15.85 +        ModelSource ms = TestCatalogModel.getDefault().getModelSource(locationURI);
   15.86 +        return RegistryModelFactory.getInstance().getModel(ms);
   15.87 +    }
   15.88 +    
   15.89 +    public static void dumpToStream(Document doc, OutputStream out) throws Exception{
   15.90 +        PrintWriter w = new PrintWriter(out);
   15.91 +        w.print(doc.getText(0, doc.getLength()));
   15.92 +        w.close();
   15.93 +        out.close();
   15.94 +    }
   15.95 +    
   15.96 +    public static void dumpToFile(Document doc, File f) throws Exception {
   15.97 +        OutputStream out = new BufferedOutputStream(new FileOutputStream(f));
   15.98 +        PrintWriter w = new PrintWriter(out);
   15.99 +        w.print(doc.getText(0, doc.getLength()));
  15.100 +        w.close();
  15.101 +        out.close();
  15.102 +    }
  15.103 +    
  15.104 +    public static RegistryModel dumpAndReloadModel(RegistryModel sm) throws Exception {
  15.105 +        return dumpAndReloadModel((Document) sm.getModelSource().getLookup().lookup(Document.class));
  15.106 +    }
  15.107 +    
  15.108 +    public static File dumpToTempFile(Document doc) throws Exception {
  15.109 +        File f = File.createTempFile("reg", "xml");
  15.110 +        dumpToFile(doc, f);
  15.111 +        return f;
  15.112 +    }
  15.113 +    
  15.114 +    public static RegistryModel dumpAndReloadModel(Document doc) throws Exception {
  15.115 +        File f = dumpToTempFile(doc);
  15.116 +        URI dumpURI = new URI("dummyDump" + count++);
  15.117 +        TestCatalogModel.getDefault().addURI(dumpURI, f.toURI());
  15.118 +        ModelSource ms = TestCatalogModel.getDefault().getModelSource(dumpURI);
  15.119 +        return RegistryModelFactory.getInstance().getModel(ms);
  15.120 +    }
  15.121 +    
  15.122 +    public static Document loadDocument(File f) throws Exception {
  15.123 +        InputStream in = new BufferedInputStream(new FileInputStream(f));
  15.124 +        return loadDocument(in);
  15.125 +    }
  15.126 +        
  15.127 +    public static URI getResourceURI(String path) throws RuntimeException {
  15.128 +        try {
  15.129 +            return Util.class.getResource(path).toURI();
  15.130 +        } catch (Exception ex) {
  15.131 +            throw new RuntimeException(ex);
  15.132 +        }
  15.133 +    }
  15.134 +    
  15.135 +    public static File getTempDir(String path) throws Exception {
  15.136 +        File tempdir = new File(System.getProperty("java.io.tmpdir"), path);
  15.137 +        tempdir.mkdirs();
  15.138 +        return tempdir;
  15.139 +    }
  15.140 +}
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/o.sample.registry.model/test/unit/src/org/sample/registry/model/test1.xml	Thu Nov 02 18:18:06 2006 +0000
    16.3 @@ -0,0 +1,16 @@
    16.4 +<?xml version="1.0" encoding="UTF-8"?>
    16.5 +<registry xmlns="http://www.samples.org/registry">
    16.6 +    <entries>
    16.7 +        <service name='service1' type='financial-processor'>
    16.8 +            <provider name='acme.com'>
    16.9 +                <url>http://www.acme.com/finance</url>
   16.10 +            </provider>
   16.11 +        </service>
   16.12 +    </entries>
   16.13 +    <known-types>
   16.14 +        <type name='financial-processor' >
   16.15 +            <definition>http://www.finance.org/processor</definition>
   16.16 +            <documentation>http://acme.finance/processor/fp.wsdl</documentation>
   16.17 +        </type>
   16.18 +    </known-types>
   16.19 +</registry>
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/o.sample.registry.model/test/unit/src/org/sample/registry/model/test1_simpleTypeElement.xml	Thu Nov 02 18:18:06 2006 +0000
    17.3 @@ -0,0 +1,16 @@
    17.4 +<?xml version="1.0" encoding="UTF-8"?>
    17.5 +<registry xmlns="http://www.samples.org/registry">
    17.6 +    <entries>
    17.7 +        <service name='service1' type='financial-processor'>
    17.8 +            <provider name='acme.com'>
    17.9 +                <url>http://www.acme.com/finance</url>
   17.10 +            </provider>
   17.11 +        </service>
   17.12 +    </entries>
   17.13 +    <known-types>
   17.14 +        <type name='financial-processor' >
   17.15 +            <definition>http://www.acme.org/processor</definition>
   17.16 +            <documentation>http://acme.finance/processor/fp.wsdl</documentation>
   17.17 +        </type>
   17.18 +    </known-types>
   17.19 +</registry>