DTD Producer test added.
1.1 --- a/mdr/test/cfg-unit.xml Mon Sep 16 14:29:16 2002 +0000
1.2 +++ b/mdr/test/cfg-unit.xml Tue Sep 17 10:36:05 2002 +0000
1.3 @@ -37,6 +37,7 @@
1.4 <include name="org/netbeans/mdr/test/InterceptionTest.class"/>
1.5 <include name="org/netbeans/mdr/test/AdditionalIndexTest.class"/>
1.6 <include name="org/netbeans/mdr/test/StaticFeaturesTest.class"/>
1.7 + <include name="org/netbeans/mdr/test/DTDProducerTest.class"/>
1.8 <!-- <include name="org/netbeans/mdr/test/RecoveryTest.class"/> -->
1.9 <exclude name="org/netbeans/mdr/test/MDRExportImportTest.class/testUML"/>
1.10 </patternset>
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/mdr/test/unit/src/org/netbeans/mdr/test/DTDProducerTest.java Tue Sep 17 10:36:05 2002 +0000
2.3 @@ -0,0 +1,178 @@
2.4 +/*
2.5 + * Sun Public License Notice
2.6 + *
2.7 + * The contents of this file are subject to the Sun Public License
2.8 + * Version 1.0 (the "License"). You may not use this file except in
2.9 + * compliance with the License. A copy of the License is available at
2.10 + * http://www.sun.com/
2.11 + *
2.12 + * The Original Code is NetBeans. The Initial Developer of the Original
2.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
2.14 + * Microsystems, Inc. All Rights Reserved.
2.15 + */
2.16 +
2.17 +package org.netbeans.mdr.test;
2.18 +
2.19 +import java.io.*;
2.20 +import java.util.*;
2.21 +import java.net.*;
2.22 +
2.23 +import junit.extensions.*;
2.24 +
2.25 +import org.netbeans.api.mdr.DTDProducer;
2.26 +import org.netbeans.api.xmi.*;
2.27 +import org.netbeans.api.xmi.sax.*;
2.28 +import org.openide.util.Lookup;
2.29 +import org.netbeans.junit.*;
2.30 +
2.31 +import org.netbeans.mdr.util.*;
2.32 +import org.netbeans.mdr.NBMDRepositoryImpl;
2.33 +import org.netbeans.lib.jmi.xmi.*;
2.34 +import org.netbeans.lib.jmi.mapping.*;
2.35 +
2.36 +import org.xml.sax.*;
2.37 +import org.xml.sax.helpers.DefaultHandler;
2.38 +import javax.xml.parsers.*;
2.39 +
2.40 +import javax.jmi.reflect.*;
2.41 +import javax.jmi.model.*;
2.42 +
2.43 +/**
2.44 + * Test case verifying functionality of XmiReader and XmiWriter.
2.45 + */
2.46 +public class DTDProducerTest extends MDRTestCase {
2.47 +
2.48 + public DTDProducerTest(String testName) {
2.49 + super (testName);
2.50 + }
2.51 +
2.52 + public static void main (String[] args) {
2.53 + junit.textui.TestRunner.run (suite ());
2.54 + }
2.55 +
2.56 + public static NbTest suite () {
2.57 + NbTestSuite suite = new NbTestSuite ();
2.58 + suite.addTestSuite (DTDProducerTest.class);
2.59 +
2.60 + NbTestSetup setup = new NbTestSetup (suite) {
2.61 + public void setUp () {
2.62 + }
2.63 + public void tearDown () {
2.64 + }
2.65 + };
2.66 + return setup;
2.67 + }
2.68 +
2.69 + // **************************************************************************
2.70 +
2.71 + public void test () {
2.72 + // doTest ("01-02-15_Diff.xml", "UML", "uml_inst");
2.73 + doTest ("mof.xml", "Model", "mof_inst");
2.74 + doTest ("Java.xml", "Core", "java_inst");
2.75 + }
2.76 +
2.77 + public void doTest (String xmlName, String packageName, String fileName) {
2.78 + RefPackage pkg = instantiateModel (xmlName, packageName);
2.79 + new RandomDataGenerator ().generate (pkg, 0, 12);
2.80 + RefPackage pkg2 = instantiateModel (xmlName, packageName);
2.81 +
2.82 + Lookup lookup = Lookup.getDefault ();
2.83 + DTDProducer producer = DTDProducer.getDefault ();
2.84 + XMIWriter writer = XMIWriterFactory.getDefault ().createXMIWriter ();
2.85 + XMIReader reader = XMIReaderFactory.getDefault ().createXMIReader ();
2.86 +
2.87 + try {
2.88 + OutputStream os = new FileOutputStream (fileName + ".dtd");
2.89 + producer.generate (os, pkg);
2.90 + os.flush ();
2.91 + os.close ();
2.92 + } catch (Exception e) {
2.93 + e.printStackTrace();
2.94 + fail (e.getMessage ());
2.95 + }
2.96 +
2.97 + File file_temp = new File (fileName + ".temp");
2.98 + File file = new File (fileName + ".xml");
2.99 +
2.100 + try {
2.101 + OutputStream os = new FileOutputStream (file_temp);
2.102 + repository.beginTrans (true);
2.103 + writer.write (os, fileName + ".temp", pkg, null);
2.104 + os.flush ();
2.105 + os.close ();
2.106 + } catch (Exception e) {
2.107 + e.printStackTrace();
2.108 + fail (e.getMessage ());
2.109 + } finally {
2.110 + repository.endTrans ();
2.111 + }
2.112 +
2.113 + addDTDTag (file_temp, file, fileName + ".dtd");
2.114 +
2.115 + /*
2.116 + try {
2.117 + repository.beginTrans (true);
2.118 + reader.read (file.toURL ().toExternalForm(), pkg2);
2.119 + } catch (Exception e) {
2.120 + e.printStackTrace ();
2.121 + fail (e.getMessage ());
2.122 + } finally {
2.123 + repository.endTrans ();
2.124 + }
2.125 + */
2.126 +
2.127 + Validator validator = new Validator ();
2.128 + validator.validate (file);
2.129 +
2.130 + }
2.131 +
2.132 + private RefPackage instantiateModel (String docName, String mainPackageName) {
2.133 + ModelPackage pkg = loadMOFModel (docName, "PureMOF_test");
2.134 + RefPackage result = createExtent (
2.135 + findMofPackage (pkg, mainPackageName),
2.136 + mainPackageName
2.137 + );
2.138 + return result;
2.139 + }
2.140 +
2.141 + private void addDTDTag (File src, File dest, String dtdFileName) {
2.142 + try {
2.143 + InputStream in = new FileInputStream (src);
2.144 + BufferedReader reader = new BufferedReader (new InputStreamReader(in));
2.145 + PrintStream writer = new PrintStream (new FileOutputStream (dest));
2.146 + String line = reader.readLine ();
2.147 + writer.println (line);
2.148 + writer.println ("<!DOCTYPE XMI SYSTEM \"" + dtdFileName + "\">");
2.149 + line = reader.readLine ();
2.150 + while (line != null) {
2.151 + writer.println (line);
2.152 + line = reader.readLine();
2.153 + }
2.154 + writer.close ();
2.155 + writer.flush ();
2.156 + } catch (IOException e) {
2.157 + e.printStackTrace ();
2.158 + fail (e.getMessage ());
2.159 + }
2.160 + }
2.161 +
2.162 + // ..........................................................................
2.163 +
2.164 + private class Validator extends DefaultHandler {
2.165 +
2.166 + public void validate (File xmiFile) {
2.167 + try {
2.168 + InputSource input = new InputSource (new FileInputStream (xmiFile));
2.169 + SAXParserFactory factory = SAXParserFactory.newInstance ();
2.170 + factory.setValidating (true);
2.171 + SAXParser saxParser = factory.newSAXParser ();
2.172 + saxParser.parse (input, this);
2.173 + } catch (Exception e) {
2.174 + e.printStackTrace ();
2.175 + fail (e.getMessage ());
2.176 + }
2.177 + }
2.178 +
2.179 + }
2.180 +
2.181 +}
3.1 --- a/mdr/test/unit/src/org/netbeans/mdr/test/RandomDataGenerator.java Mon Sep 16 14:29:16 2002 +0000
3.2 +++ b/mdr/test/unit/src/org/netbeans/mdr/test/RandomDataGenerator.java Tue Sep 17 10:36:05 2002 +0000
3.3 @@ -213,55 +213,65 @@
3.4 return values;
3.5 }
3.6
3.7 - public void generateAssociation (RefAssociation proxy, int count) {
3.8 + public void generateAssociation (RefAssociation proxy, int count) {
3.9 Association assoc = (Association) proxy.refMetaObject ();
3.10 - AssociationEnd endA = null, endB = null;
3.11 - Iterator content = assoc.getContents ().iterator ();
3.12 - while (content.hasNext ()) {
3.13 - Object elem = content.next ();
3.14 - if (elem instanceof AssociationEnd) {
3.15 - if (endA == null)
3.16 - endA = (AssociationEnd) elem;
3.17 - else {
3.18 - endB = (AssociationEnd) elem;
3.19 - break;
3.20 +
3.21 + try {
3.22 +
3.23 + AssociationEnd endA = null, endB = null;
3.24 + Iterator content = assoc.getContents ().iterator ();
3.25 + while (content.hasNext ()) {
3.26 + Object elem = content.next ();
3.27 + if (elem instanceof AssociationEnd) {
3.28 + if (endA == null)
3.29 + endA = (AssociationEnd) elem;
3.30 + else {
3.31 + endB = (AssociationEnd) elem;
3.32 + break;
3.33 + } // if
3.34 } // if
3.35 - } // if
3.36 - } // while
3.37 + } // while
3.38 +
3.39 + MofClass typeA = findSubtype ((MofClass) endA.getType ());
3.40 + MofClass typeB = findSubtype ((MofClass) endB.getType ());
3.41 +
3.42 + if ((typeA == null) || (typeB == null))
3.43 + return;
3.44 +
3.45 + MultiplicityType multA = endA.getMultiplicity ();
3.46 + MultiplicityType multB = endB.getMultiplicity ();
3.47 + int lowerA = Math.max (1, multA.getLower ());
3.48 + int lowerB = Math.max (1, multB.getLower ());
3.49 + int upperA = lowerA + 4;
3.50 + int upperB = lowerB + 4;
3.51 + if (multA.getUpper () != -1)
3.52 + upperA = Math.min (upperA, multA.getUpper ());
3.53 + if (multB.getUpper () != -1)
3.54 + upperB = Math.min (upperB, multB.getUpper ());
3.55 +
3.56 + do {
3.57 + int x, y;
3.58 + int countA = lowerA + ((upperA - lowerA > 0) ? random.nextInt (upperA - lowerA) : 0);
3.59 + int countB = lowerB + ((upperB - lowerB > 0) ? random.nextInt (upperB - lowerB) : 0);
3.60 + RefObject [] objA = new RefObject [countA];
3.61 + RefObject [] objB = new RefObject [countB];
3.62 + for (x = 0; x < countA; x++) {
3.63 + objA [x] = generateInstance (typeA);
3.64 + }
3.65 + for (x = 0; x < countB; x++) {
3.66 + objB [x] = generateInstance (typeB);
3.67 + }
3.68 + for (x = 0; x < countA; x++)
3.69 + for (y = 0; y < countB; y++)
3.70 + proxy.refAddLink (objA[x], objB[y]);
3.71 + } while (--count > 0);
3.72
3.73 - MofClass typeA = findSubtype ((MofClass) endA.getType ());
3.74 - MofClass typeB = findSubtype ((MofClass) endB.getType ());
3.75 + } catch (Exception e) {
3.76 + // [PENDING]
3.77 + System.out.println(assoc.getName ());
3.78 + e.printStackTrace ();
3.79 + }
3.80
3.81 - if ((typeA == null) || (typeB == null))
3.82 - return;
3.83 -
3.84 - MultiplicityType multA = endA.getMultiplicity ();
3.85 - MultiplicityType multB = endB.getMultiplicity ();
3.86 - int lowerA = Math.max (1, multA.getLower ());
3.87 - int lowerB = Math.max (1, multB.getLower ());
3.88 - int upperA = lowerA + 4;
3.89 - int upperB = lowerB + 4;
3.90 - if (multA.getUpper () != -1)
3.91 - upperA = Math.min (upperA, multA.getUpper ());
3.92 - if (multB.getUpper () != -1)
3.93 - upperB = Math.min (upperB, multB.getUpper ());
3.94 -
3.95 - do {
3.96 - int x, y;
3.97 - int countA = lowerA + ((upperA - lowerA > 0) ? random.nextInt (upperA - lowerA) : 0);
3.98 - int countB = lowerB + ((upperB - lowerB > 0) ? random.nextInt (upperB - lowerB) : 0);
3.99 - RefObject [] objA = new RefObject [countA];
3.100 - RefObject [] objB = new RefObject [countB];
3.101 - for (x = 0; x < countA; x++) {
3.102 - objA [x] = generateInstance (typeA);
3.103 - }
3.104 - for (x = 0; x < countB; x++) {
3.105 - objB [x] = generateInstance (typeB);
3.106 - }
3.107 - for (x = 0; x < countA; x++)
3.108 - for (y = 0; y < countB; y++)
3.109 - proxy.refAddLink (objA[x], objB[y]);
3.110 - } while (--count > 0);
3.111 }
3.112
3.113 public MofClass findSubtype (MofClass mofClass) {