[jbosstools-commits] JBoss Tools SVN: r6804 - in trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test: model and 1 other directory.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Mar 7 22:44:21 EST 2008


Author: rob.stryker at jboss.com
Date: 2008-03-07 22:44:21 -0500 (Fri, 07 Mar 2008)
New Revision: 6804

Added:
   trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/XBMarshallTest.java
   trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/XBUnmarshallTest.java
Removed:
   trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/MarshallTest.java
   trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/UnmarshallTest.java
Modified:
   trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/ArchivesTestSuite.java
Log:
Archives Unit Tests

Modified: trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/ArchivesTestSuite.java
===================================================================
--- trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/ArchivesTestSuite.java	2008-03-08 03:43:41 UTC (rev 6803)
+++ trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/ArchivesTestSuite.java	2008-03-08 03:44:21 UTC (rev 6804)
@@ -4,16 +4,16 @@
 import junit.framework.TestSuite;
 
 import org.jboss.ide.eclipse.archives.test.core.ArchivesCoreTest;
-import org.jboss.ide.eclipse.archives.test.model.MarshallTest;
-import org.jboss.ide.eclipse.archives.test.model.UnmarshallTest;
+import org.jboss.ide.eclipse.archives.test.model.XBMarshallTest;
+import org.jboss.ide.eclipse.archives.test.model.XBUnmarshallTest;
 import org.jboss.ide.eclipse.archives.test.util.TruezipUtilTest;
 
 public class ArchivesTestSuite extends TestSuite {
     public static Test suite() { 
         TestSuite suite = new TestSuite("Archives Tests");
         suite.addTestSuite(ArchivesCoreTest.class);
-        suite.addTestSuite(MarshallTest.class);
-        suite.addTestSuite(UnmarshallTest.class);
+        suite.addTestSuite(XBMarshallTest.class);
+        suite.addTestSuite(XBUnmarshallTest.class);
         suite.addTestSuite(TruezipUtilTest.class);
         suite.addTestSuite(ModelUtilTest.class);
 

Deleted: trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/MarshallTest.java
===================================================================
--- trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/MarshallTest.java	2008-03-08 03:43:41 UTC (rev 6803)
+++ trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/MarshallTest.java	2008-03-08 03:44:21 UTC (rev 6804)
@@ -1,254 +0,0 @@
-/**
- * JBoss, a Division of Red Hat
- * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.ide.eclipse.archives.test.model;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.URL;
-
-import junit.framework.TestCase;
-
-import org.eclipse.core.runtime.FileLocator;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.runtime.Path;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XMLBinding;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbAction;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbFileSet;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbFolder;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbPackage;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbPackages;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbProperty;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XMLBinding.XbException;
-import org.jboss.ide.eclipse.archives.test.ArchivesTest;
-import org.jboss.ide.eclipse.archives.test.util.FileIOUtil;
-import org.osgi.framework.Bundle;
-
-/**
- * 
- * This class tests marshalling each type of xb object, 
- * expecting the XMLBinding class (and XB itself) to throw 
- * errors if required fields are missing. 
- * 
- * It also verifies correct models are properly marshalled.
- * 
- * @author rob.stryker <rob.stryker at redhat.com>
- * 
- */
-public class MarshallTest extends TestCase {
-	private Bundle bundle;
-	private IPath bundlePath;
-	private IPath expectedOutputs;
-	private IPath outputs;
-	protected void setUp() {
-		if( bundlePath == null ) {
-			try {
-				bundle = ArchivesTest.getDefault().getBundle();
-				URL bundleURL = FileLocator.toFileURL(bundle.getEntry(""));
-				bundlePath = new Path(bundleURL.getFile());
-				expectedOutputs = bundlePath.append("expectedOutputs");
-				outputs = bundlePath.append("output");
-			} catch( IOException ioe) {
-				fail("Failed to set up " + getClass().getName());
-			}
-		}
-	}
-
-	public void tearDown() {
-		FileIOUtil.clearFolder(outputs.toFile().listFiles());
-	}
-		
-	public void testStringWriter() {
-		XbPackages packs = new XbPackages();
-		try {
-			File expected = expectedOutputs.append("emptyPackages.xml").toFile();
-			String expectedContents = FileIOUtil.getFileContents(expected);
-			String asString = XMLBinding.serializePackages(packs, new NullProgressMonitor());
-			assertEquals(asString, expectedContents);
-		} catch( XbException xbe ) {
-			fail(xbe.getMessage());
-		}
-	}
-	
-	public void testFileWriter() {
-		XbPackages packs = new XbPackages();
-		IPath out = outputs.append("test.xml");
-		try {
-			File expected = expectedOutputs.append("emptyPackages.xml").toFile();
-			String expectedContents = FileIOUtil.getFileContents(expected);
-
-			XMLBinding.marshallToFile(packs, out, new NullProgressMonitor());
-			String actualContents = FileIOUtil.getFileContents(out.toFile());
-			
-			assertEquals(expectedContents, actualContents);
-		} catch( XbException xbe ) {
-			xbe.printStackTrace();
-			fail(xbe.getMessage());
-		} catch( IOException ioe ) {
-			fail(ioe.getMessage());
-		}
-	}
-	
-	protected void write(XbPackages packs, boolean shouldPass) {
-		XbException e = null;
-		try {
-			IPath out = outputs.append("test.xml");
-			XMLBinding.marshallToFile(packs, out, new NullProgressMonitor());
-		} catch( IOException ioe ) {
-			fail("IOException during testWritePackage operation");
-		}catch( XbException xbe ) {
-			e = xbe;
-		} finally {
-			if( e == null && !shouldPass) {
-				fail("Incomplete Model saved when it should not have been.");
-			}
-			if( e != null && shouldPass) {
-				fail("Model failed to save when it should have saved. " + e.getMessage());
-			}
-		}
-	}
-	
-	protected void writePackage(String name, String toDir, boolean shouldPass) {
-		XbException e = null;
-		XbPackages packs = new XbPackages();
-		XbPackage pack = new XbPackage();
-		pack.setName(name);
-		pack.setToDir(toDir);
-		packs.addChild(pack);
-		write(packs, shouldPass);
-	}
-	
-	public void testWritePackageSuccess() {
-		writePackage("someName", "someFile.jar", true);
-	}
-	public void testWritePackageMissingName() {
-		writePackage(null, "someFile.jar", false);
-	}
-
-	public void testWritePackageMissingDir() {
-		writePackage("someName", null, false);
-	}
-
-	protected void writeProperties(String name, String value, boolean shouldPass) {
-		XbPackages packs = new XbPackages();
-		XbPackage pack = new XbPackage();
-		pack.setName("test");
-		pack.setToDir("test2");
-		XbProperty property = new XbProperty();
-
-		try {
-			property.setName(name);
-			property.setValue(value);
-			pack.getProperties().addProperty(property);
-		} catch( NullPointerException npe ) {
-			if( shouldPass ) 
-				fail("Model failed to save when it should have saved. - " + npe.getMessage());
-			return; // success
-		}
-		
-		packs.addChild(pack);
-		write(packs, shouldPass);
-	}
-
-	public void testWritePropertiesSuccess() {
-		writeProperties("name", "val", true); 
-	}
-	
-	public void testWritePropertiesMissingName() {
-		writeProperties(null, "val", false);
-	}
-	
-	public void testWritePropertiesMissingValue() {
-		writeProperties("name", null, false);
-	}
-	
-	public void writeFolder(String name, boolean shouldPass) {
-		XbPackages packs = new XbPackages();
-		XbPackage pack = new XbPackage();
-		pack.setName("name");
-		pack.setToDir("todir");
-		packs.addChild(pack);
-		XbFolder folder = new XbFolder();
-		folder.setName(name);
-		pack.addChild(folder);
-		write(packs, shouldPass);
-	}
-	
-	public void testWriteFolderSuccess() {
-		writeFolder("someFolder", true);
-	}
-		
-	public void testWriteFolderMissingName() {
-		writeFolder(null, false);
-	}
-		
-	public void writeFileset(String dir, String includes, boolean shouldSucceed) {
-		XbPackages packs = new XbPackages();
-		XbPackage pack = new XbPackage();
-		pack.setName("name");
-		pack.setToDir("todir");
-		packs.addChild(pack);
-		XbFileSet fs = new XbFileSet();
-		fs.setDir(dir);
-		fs.setIncludes(includes);
-		pack.addChild(fs);
-		write(packs, shouldSucceed);
-	}
-	
-	public void testWriteFilesetSuccess() {
-		writeFileset("folder", "includes", true);
-	}
-	
-	public void testWriteFilesetMissingFolder() {
-		writeFileset(null, "includes", false);
-	}
-	
-	public void testWriteFilesetMissingIncludes() {
-		writeFileset("path", null, false);
-	}
-	
-	
-	protected void writeAction(String time, String type, boolean shouldSucceed) {
-		XbPackages packs = new XbPackages();
-		XbPackage pack = new XbPackage();
-		pack.setName("name");
-		pack.setToDir("todir");
-		packs.addChild(pack);
-		XbAction act = new XbAction();
-		act.setTime(time);
-		act.setType(type);
-		pack.addChild(act);
-		write(packs, shouldSucceed);
-	}
-	
-	public void testWriteActionSuccess() {
-		writeAction("preBuild", "ant", true);
-	}
-	
-	public void testWriteActionMissingTime() {
-		writeAction(null, "ant", false);
-	}
-	
-	public void testWriteActionMissingType() {
-		writeAction("preBuild", null, false);
-	}
-}

Deleted: trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/UnmarshallTest.java
===================================================================
--- trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/UnmarshallTest.java	2008-03-08 03:43:41 UTC (rev 6803)
+++ trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/UnmarshallTest.java	2008-03-08 03:44:21 UTC (rev 6804)
@@ -1,227 +0,0 @@
-/**
- * JBoss, a Division of Red Hat
- * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
-* This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.ide.eclipse.archives.test.model;
-
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.net.URL;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-import org.eclipse.core.runtime.FileLocator;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.core.runtime.Path;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XMLBinding;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbFileSet;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbPackage;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbPackages;
-import org.jboss.ide.eclipse.archives.core.model.internal.xb.XMLBinding.XbException;
-import org.jboss.ide.eclipse.archives.test.ArchivesTest;
-import org.jboss.ide.eclipse.archives.test.util.FileIOUtil;
-import org.osgi.framework.Bundle;
-
-/**
- * Tests unmarshalling and validating packaging files. 
- * The tests expect XB to throw exceptions when improper 
- * nonconformant files are parsed. 
- * 
- * @author rob.stryker <rob.stryker at redhat.com>
- *
- */
-public class UnmarshallTest extends TestCase {
-	private Bundle bundle;
-	private IPath bundlePath;
-	private IPath archiveDescriptors;
-	protected void setUp() {
-		if( bundlePath == null ) {
-			try {
-				bundle = ArchivesTest.getDefault().getBundle();
-				URL bundleURL = FileLocator.toFileURL(bundle.getEntry(""));
-				bundlePath = new Path(bundleURL.getFile());
-				archiveDescriptors = bundlePath.append("inputs").append("archiveDescriptors").append("validation");
-			} catch( IOException ioe) {
-				fail("Failed to set up " + getClass().getName());
-			}
-		}
-	}
-
-	public void tearDown() {
-		FileIOUtil.clearFolder(bundlePath.append("tmp").toFile().listFiles());
-	}
-	
-	/*
-	 * The tests
-	 */
-	
-	public void testAbsoluteSchemaFailure() {
-		parse("AbsoluteSchemaFailure.xml", false, "Broken schema successfully parsed.");
-	}
-	
-	public void testAttributeSchemaFailure() {
-		parse("AttributeSchemaFailure.xml", false, "Schema with random attribute successfully parsed.");
-	}
-	
-	
-	public void testCorrectPackage() {
-		parse("CorrectPackage.xml", true, shouldParse("CorrectPackage.xml"));
-	}
-
-	public void testPackageMissingPackageName() {
-		parse("MissingPackageName.xml", false, failedMissingRequiredAtt("name"));
-	}
-
-	public void testPackageMissingDirectory() {
-		parse("MissingPackageDir.xml", false, failedMissingRequiredAtt("todir"));
-	}
-	
-	public void testPackageMissingOptionalType() {
-		parse("MissingPackageType.xml", true, failedFalsePositiveRequiredAtt("type"));
-	}
-	
-	public void testPackageMissingOptionalExploded() {
-		XbPackages packs = parse("MissingPackageExploded.xml", true, failedFalsePositiveRequiredAtt("exploded"));
-		XbPackage pack = (XbPackage)packs.getAllChildren().get(0);
-		assertFalse(pack.isExploded());
-	}
-	
-	public void testPackageMissingOptionalInWorkspace() {
-		XbPackages packs = parse("MissingPackageInWorkspace.xml", true, failedFalsePositiveRequiredAtt("inWorkspace"));
-		XbPackage pack = (XbPackage)packs.getAllChildren().get(0);
-		assertTrue(pack.isInWorkspace());
-	}
-	
-	public void testPackageMissingOptionalId() {
-		XbPackages packs = parse("MissingPackageID.xml", true, failedMissingRequiredAtt("id"));
-		XbPackage pack = (XbPackage)packs.getAllChildren().get(0);
-		assertNull(pack.getId());
-	}
-	
-	public void testCorrectFolder() {
-		parse("CorrectFolder.xml", true, shouldParse("CorrectFolder.xml"));
-	}
-	
-	public void testFolderMissingName() {
-		parse("MissingFolderName.xml", false, failedMissingRequiredAtt("name"));
-	}
-	
-	public void testCorrectProperties() {
-		parse("CorrectProperties.xml", true, shouldParse("CorrectProperties.xml"));
-	}
-	
-	public void testPropertiesMissingKey() {
-		parse("MissingPropertiesName.xml", false, failedMissingRequiredAtt("name"));
-	}
-	
-	public void testPropertiesMissingValue() {
-		parse("MissingPropertiesValue.xml", false, failedMissingRequiredAtt("value"));
-	}
-	
-	
-	public void testCorrectFileset() {
-		parse("CorrectFileset.xml", true, shouldParse("CorrectFileset.xml"));
-	}
-	
-	public void testFilesetMissingDir() {
-		parse("MissingFilesetDir.xml", false, failedMissingRequiredAtt("todir"));
-	}
-
-	public void testFilesetMissingIncludes() {
-		parse("MissingFilesetIncludes.xml", false, failedMissingRequiredAtt("includes"));
-	}
-	
-	public void testFilesetMissingOptionalExcludes() {
-		parse("MissingFilesetExcludes.xml", true, failedFalsePositiveRequiredAtt("excludes"));
-		// no default
-	}
-	
-	public void testFilesetMissingOptionalInWorkspace() {
-		XbPackages packs = parse("MissingFilesetInWorkspace.xml", true, failedFalsePositiveRequiredAtt("inWorkspace"));
-		// no default
-		XbPackage pack = (XbPackage)packs.getAllChildren().get(0);
-		List l = pack.getChildren(XbFileSet.class);
-		assertNotNull(l);
-		assertEquals(1, l.size());
-		XbFileSet fs = (XbFileSet)l.get(0);
-		assertTrue(fs.isInWorkspace());
-	}
-
-	public void testFilesetMissingOptionalFlattened() {
-		XbPackages packs = parse("MissingFilesetFlattened.xml", true, failedFalsePositiveRequiredAtt("flattened"));
-		// no default
-		XbPackage pack = (XbPackage)packs.getAllChildren().get(0);
-		List l = pack.getChildren(XbFileSet.class);
-		assertNotNull(l);
-		assertEquals(1, l.size());
-		XbFileSet fs = (XbFileSet)l.get(0);
-		assertFalse(fs.isFlattened());
-	}
-
-	
-	public void testCorrectAction() {
-		parse("CorrectAction.xml", true, shouldParse("CorrectAction.xml"));
-	}
-	
-	public void testActionMissingTime() {
-		parse("MissingActionTime.xml", false, failedMissingRequiredAtt("time"));
-	}
-	
-	public void testActionMissingType() {
-		parse("MissingActionType.xml", false, failedMissingRequiredAtt("type"));
-	}
-	
-	
-	/*
-	 * Utility
-	 */
-	
-	protected XbPackages parse(String file, boolean shouldSucceed, String failMsg) {
-		FileInputStream fis = null;
-		XbPackages packs = null;
-		try {
-			fis = new FileInputStream(archiveDescriptors.append(file).toFile());
-			packs = XMLBinding.unmarshal(fis, new NullProgressMonitor());
-		} catch( IOException ioe ) {
-			fail(ioe.getMessage());
-		} catch( XbException e ) {
-			if( shouldSucceed )
-				fail(failMsg + " - " + e.getMessage());
-			return packs;
-		}
-		if( !shouldSucceed )
-			fail(failMsg);
-		return packs;
-	}
-	
-	protected String failedMissingRequiredAtt(String att) {
-		return "File parsed while missing a required attribute: " + att;
-	}
-	
-	protected String failedFalsePositiveRequiredAtt(String attribute) {
-		return "File failed to parse even though \"" + attribute + "\" is not required";
-	}
-	
-	protected String shouldParse(String file) {
-		return file + " should parse correctly.";
-	}
-}

Copied: trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/XBMarshallTest.java (from rev 6803, trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/MarshallTest.java)
===================================================================
--- trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/XBMarshallTest.java	                        (rev 0)
+++ trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/XBMarshallTest.java	2008-03-08 03:44:21 UTC (rev 6804)
@@ -0,0 +1,254 @@
+/**
+ * JBoss, a Division of Red Hat
+ * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ide.eclipse.archives.test.model;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+import junit.framework.TestCase;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XMLBinding;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbAction;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbFileSet;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbFolder;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbPackage;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbPackages;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbProperty;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XMLBinding.XbException;
+import org.jboss.ide.eclipse.archives.test.ArchivesTest;
+import org.jboss.ide.eclipse.archives.test.util.FileIOUtil;
+import org.osgi.framework.Bundle;
+
+/**
+ * 
+ * This class tests marshalling each type of xb object, 
+ * expecting the XMLBinding class (and XB itself) to throw 
+ * errors if required fields are missing. 
+ * 
+ * It also verifies correct models are properly marshalled.
+ * 
+ * @author rob.stryker <rob.stryker at redhat.com>
+ * 
+ */
+public class XBMarshallTest extends TestCase {
+	private Bundle bundle;
+	private IPath bundlePath;
+	private IPath expectedOutputs;
+	private IPath outputs;
+	protected void setUp() {
+		if( bundlePath == null ) {
+			try {
+				bundle = ArchivesTest.getDefault().getBundle();
+				URL bundleURL = FileLocator.toFileURL(bundle.getEntry(""));
+				bundlePath = new Path(bundleURL.getFile());
+				expectedOutputs = bundlePath.append("expectedOutputs");
+				outputs = bundlePath.append("output");
+			} catch( IOException ioe) {
+				fail("Failed to set up " + getClass().getName());
+			}
+		}
+	}
+
+	public void tearDown() {
+		FileIOUtil.clearFolder(outputs.toFile().listFiles());
+	}
+		
+	public void testStringWriter() {
+		XbPackages packs = new XbPackages();
+		try {
+			File expected = expectedOutputs.append("emptyPackages.xml").toFile();
+			String expectedContents = FileIOUtil.getFileContents(expected);
+			String asString = XMLBinding.serializePackages(packs, new NullProgressMonitor());
+			assertEquals(asString, expectedContents);
+		} catch( XbException xbe ) {
+			fail(xbe.getMessage());
+		}
+	}
+	
+	public void testFileWriter() {
+		XbPackages packs = new XbPackages();
+		IPath out = outputs.append("test.xml");
+		try {
+			File expected = expectedOutputs.append("emptyPackages.xml").toFile();
+			String expectedContents = FileIOUtil.getFileContents(expected);
+
+			XMLBinding.marshallToFile(packs, out, new NullProgressMonitor());
+			String actualContents = FileIOUtil.getFileContents(out.toFile());
+			
+			assertEquals(expectedContents, actualContents);
+		} catch( XbException xbe ) {
+			xbe.printStackTrace();
+			fail(xbe.getMessage());
+		} catch( IOException ioe ) {
+			fail(ioe.getMessage());
+		}
+	}
+	
+	protected void write(XbPackages packs, boolean shouldPass) {
+		XbException e = null;
+		try {
+			IPath out = outputs.append("test.xml");
+			XMLBinding.marshallToFile(packs, out, new NullProgressMonitor());
+		} catch( IOException ioe ) {
+			fail("IOException during testWritePackage operation");
+		}catch( XbException xbe ) {
+			e = xbe;
+		} finally {
+			if( e == null && !shouldPass) {
+				fail("Incomplete Model saved when it should not have been.");
+			}
+			if( e != null && shouldPass) {
+				fail("Model failed to save when it should have saved. " + e.getMessage());
+			}
+		}
+	}
+	
+	protected void writePackage(String name, String toDir, boolean shouldPass) {
+		XbException e = null;
+		XbPackages packs = new XbPackages();
+		XbPackage pack = new XbPackage();
+		pack.setName(name);
+		pack.setToDir(toDir);
+		packs.addChild(pack);
+		write(packs, shouldPass);
+	}
+	
+	public void testWritePackageSuccess() {
+		writePackage("someName", "someFile.jar", true);
+	}
+	public void testWritePackageMissingName() {
+		writePackage(null, "someFile.jar", false);
+	}
+
+	public void testWritePackageMissingDir() {
+		writePackage("someName", null, false);
+	}
+
+	protected void writeProperties(String name, String value, boolean shouldPass) {
+		XbPackages packs = new XbPackages();
+		XbPackage pack = new XbPackage();
+		pack.setName("test");
+		pack.setToDir("test2");
+		XbProperty property = new XbProperty();
+
+		try {
+			property.setName(name);
+			property.setValue(value);
+			pack.getProperties().addProperty(property);
+		} catch( NullPointerException npe ) {
+			if( shouldPass ) 
+				fail("Model failed to save when it should have saved. - " + npe.getMessage());
+			return; // success
+		}
+		
+		packs.addChild(pack);
+		write(packs, shouldPass);
+	}
+
+	public void testWritePropertiesSuccess() {
+		writeProperties("name", "val", true); 
+	}
+	
+	public void testWritePropertiesMissingName() {
+		writeProperties(null, "val", false);
+	}
+	
+	public void testWritePropertiesMissingValue() {
+		writeProperties("name", null, false);
+	}
+	
+	public void writeFolder(String name, boolean shouldPass) {
+		XbPackages packs = new XbPackages();
+		XbPackage pack = new XbPackage();
+		pack.setName("name");
+		pack.setToDir("todir");
+		packs.addChild(pack);
+		XbFolder folder = new XbFolder();
+		folder.setName(name);
+		pack.addChild(folder);
+		write(packs, shouldPass);
+	}
+	
+	public void testWriteFolderSuccess() {
+		writeFolder("someFolder", true);
+	}
+		
+	public void testWriteFolderMissingName() {
+		writeFolder(null, false);
+	}
+		
+	public void writeFileset(String dir, String includes, boolean shouldSucceed) {
+		XbPackages packs = new XbPackages();
+		XbPackage pack = new XbPackage();
+		pack.setName("name");
+		pack.setToDir("todir");
+		packs.addChild(pack);
+		XbFileSet fs = new XbFileSet();
+		fs.setDir(dir);
+		fs.setIncludes(includes);
+		pack.addChild(fs);
+		write(packs, shouldSucceed);
+	}
+	
+	public void testWriteFilesetSuccess() {
+		writeFileset("folder", "includes", true);
+	}
+	
+	public void testWriteFilesetMissingFolder() {
+		writeFileset(null, "includes", false);
+	}
+	
+	public void testWriteFilesetMissingIncludes() {
+		writeFileset("path", null, false);
+	}
+	
+	
+	protected void writeAction(String time, String type, boolean shouldSucceed) {
+		XbPackages packs = new XbPackages();
+		XbPackage pack = new XbPackage();
+		pack.setName("name");
+		pack.setToDir("todir");
+		packs.addChild(pack);
+		XbAction act = new XbAction();
+		act.setTime(time);
+		act.setType(type);
+		pack.addChild(act);
+		write(packs, shouldSucceed);
+	}
+	
+	public void testWriteActionSuccess() {
+		writeAction("preBuild", "ant", true);
+	}
+	
+	public void testWriteActionMissingTime() {
+		writeAction(null, "ant", false);
+	}
+	
+	public void testWriteActionMissingType() {
+		writeAction("preBuild", null, false);
+	}
+}

Copied: trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/XBUnmarshallTest.java (from rev 6803, trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/UnmarshallTest.java)
===================================================================
--- trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/XBUnmarshallTest.java	                        (rev 0)
+++ trunk/core/tests/org.jboss.ide.eclipse.archives.test/src/org/jboss/ide/eclipse/archives/test/model/XBUnmarshallTest.java	2008-03-08 03:44:21 UTC (rev 6804)
@@ -0,0 +1,227 @@
+/**
+ * JBoss, a Division of Red Hat
+ * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+* This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ide.eclipse.archives.test.model;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XMLBinding;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbFileSet;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbPackage;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XbPackages;
+import org.jboss.ide.eclipse.archives.core.model.internal.xb.XMLBinding.XbException;
+import org.jboss.ide.eclipse.archives.test.ArchivesTest;
+import org.jboss.ide.eclipse.archives.test.util.FileIOUtil;
+import org.osgi.framework.Bundle;
+
+/**
+ * Tests unmarshalling and validating packaging files. 
+ * The tests expect XB to throw exceptions when improper 
+ * nonconformant files are parsed. 
+ * 
+ * @author rob.stryker <rob.stryker at redhat.com>
+ *
+ */
+public class XBUnmarshallTest extends TestCase {
+	private Bundle bundle;
+	private IPath bundlePath;
+	private IPath archiveDescriptors;
+	protected void setUp() {
+		if( bundlePath == null ) {
+			try {
+				bundle = ArchivesTest.getDefault().getBundle();
+				URL bundleURL = FileLocator.toFileURL(bundle.getEntry(""));
+				bundlePath = new Path(bundleURL.getFile());
+				archiveDescriptors = bundlePath.append("inputs").append("archiveDescriptors").append("validation");
+			} catch( IOException ioe) {
+				fail("Failed to set up " + getClass().getName());
+			}
+		}
+	}
+
+	public void tearDown() {
+		FileIOUtil.clearFolder(bundlePath.append("tmp").toFile().listFiles());
+	}
+	
+	/*
+	 * The tests
+	 */
+	
+	public void testAbsoluteSchemaFailure() {
+		parse("AbsoluteSchemaFailure.xml", false, "Broken schema successfully parsed.");
+	}
+	
+	public void testAttributeSchemaFailure() {
+		parse("AttributeSchemaFailure.xml", false, "Schema with random attribute successfully parsed.");
+	}
+	
+	
+	public void testCorrectPackage() {
+		parse("CorrectPackage.xml", true, shouldParse("CorrectPackage.xml"));
+	}
+
+	public void testPackageMissingPackageName() {
+		parse("MissingPackageName.xml", false, failedMissingRequiredAtt("name"));
+	}
+
+	public void testPackageMissingDirectory() {
+		parse("MissingPackageDir.xml", false, failedMissingRequiredAtt("todir"));
+	}
+	
+	public void testPackageMissingOptionalType() {
+		parse("MissingPackageType.xml", true, failedFalsePositiveRequiredAtt("type"));
+	}
+	
+	public void testPackageMissingOptionalExploded() {
+		XbPackages packs = parse("MissingPackageExploded.xml", true, failedFalsePositiveRequiredAtt("exploded"));
+		XbPackage pack = (XbPackage)packs.getAllChildren().get(0);
+		assertFalse(pack.isExploded());
+	}
+	
+	public void testPackageMissingOptionalInWorkspace() {
+		XbPackages packs = parse("MissingPackageInWorkspace.xml", true, failedFalsePositiveRequiredAtt("inWorkspace"));
+		XbPackage pack = (XbPackage)packs.getAllChildren().get(0);
+		assertTrue(pack.isInWorkspace());
+	}
+	
+	public void testPackageMissingOptionalId() {
+		XbPackages packs = parse("MissingPackageID.xml", true, failedMissingRequiredAtt("id"));
+		XbPackage pack = (XbPackage)packs.getAllChildren().get(0);
+		assertNull(pack.getId());
+	}
+	
+	public void testCorrectFolder() {
+		parse("CorrectFolder.xml", true, shouldParse("CorrectFolder.xml"));
+	}
+	
+	public void testFolderMissingName() {
+		parse("MissingFolderName.xml", false, failedMissingRequiredAtt("name"));
+	}
+	
+	public void testCorrectProperties() {
+		parse("CorrectProperties.xml", true, shouldParse("CorrectProperties.xml"));
+	}
+	
+	public void testPropertiesMissingKey() {
+		parse("MissingPropertiesName.xml", false, failedMissingRequiredAtt("name"));
+	}
+	
+	public void testPropertiesMissingValue() {
+		parse("MissingPropertiesValue.xml", false, failedMissingRequiredAtt("value"));
+	}
+	
+	
+	public void testCorrectFileset() {
+		parse("CorrectFileset.xml", true, shouldParse("CorrectFileset.xml"));
+	}
+	
+	public void testFilesetMissingDir() {
+		parse("MissingFilesetDir.xml", false, failedMissingRequiredAtt("todir"));
+	}
+
+	public void testFilesetMissingIncludes() {
+		parse("MissingFilesetIncludes.xml", false, failedMissingRequiredAtt("includes"));
+	}
+	
+	public void testFilesetMissingOptionalExcludes() {
+		parse("MissingFilesetExcludes.xml", true, failedFalsePositiveRequiredAtt("excludes"));
+		// no default
+	}
+	
+	public void testFilesetMissingOptionalInWorkspace() {
+		XbPackages packs = parse("MissingFilesetInWorkspace.xml", true, failedFalsePositiveRequiredAtt("inWorkspace"));
+		// no default
+		XbPackage pack = (XbPackage)packs.getAllChildren().get(0);
+		List l = pack.getChildren(XbFileSet.class);
+		assertNotNull(l);
+		assertEquals(1, l.size());
+		XbFileSet fs = (XbFileSet)l.get(0);
+		assertTrue(fs.isInWorkspace());
+	}
+
+	public void testFilesetMissingOptionalFlattened() {
+		XbPackages packs = parse("MissingFilesetFlattened.xml", true, failedFalsePositiveRequiredAtt("flattened"));
+		// no default
+		XbPackage pack = (XbPackage)packs.getAllChildren().get(0);
+		List l = pack.getChildren(XbFileSet.class);
+		assertNotNull(l);
+		assertEquals(1, l.size());
+		XbFileSet fs = (XbFileSet)l.get(0);
+		assertFalse(fs.isFlattened());
+	}
+
+	
+	public void testCorrectAction() {
+		parse("CorrectAction.xml", true, shouldParse("CorrectAction.xml"));
+	}
+	
+	public void testActionMissingTime() {
+		parse("MissingActionTime.xml", false, failedMissingRequiredAtt("time"));
+	}
+	
+	public void testActionMissingType() {
+		parse("MissingActionType.xml", false, failedMissingRequiredAtt("type"));
+	}
+	
+	
+	/*
+	 * Utility
+	 */
+	
+	protected XbPackages parse(String file, boolean shouldSucceed, String failMsg) {
+		FileInputStream fis = null;
+		XbPackages packs = null;
+		try {
+			fis = new FileInputStream(archiveDescriptors.append(file).toFile());
+			packs = XMLBinding.unmarshal(fis, new NullProgressMonitor());
+		} catch( IOException ioe ) {
+			fail(ioe.getMessage());
+		} catch( XbException e ) {
+			if( shouldSucceed )
+				fail(failMsg + " - " + e.getMessage());
+			return packs;
+		}
+		if( !shouldSucceed )
+			fail(failMsg);
+		return packs;
+	}
+	
+	protected String failedMissingRequiredAtt(String att) {
+		return "File parsed while missing a required attribute: " + att;
+	}
+	
+	protected String failedFalsePositiveRequiredAtt(String attribute) {
+		return "File failed to parse even though \"" + attribute + "\" is not required";
+	}
+	
+	protected String shouldParse(String file) {
+		return file + " should parse correctly.";
+	}
+}




More information about the jbosstools-commits mailing list