krysalis-version/src/test/java/org/krysalis/version VersionManagerTest.java,NONE,1.1 VersionTestCase.java,NONE,1.1 VersionRuntimeTests.java,NONE,1.1

Nick Chalko <[email protected]> Tue, 09 Nov 2004 07:54:54 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-version/src/test/java/org/krysalis/version
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2087/src/test/java/org/krysalis/version

Added Files:
	VersionManagerTest.java VersionTestCase.java 
	VersionRuntimeTests.java 
Log Message:
Coppied over a bunch of tests.

--- NEW FILE: VersionRuntimeTests.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.version;

import junit.framework.TestCase;

import org.krysalis.common.util.classpath.PathContext;
import org.krysalis.common.util.classpath.PathSet;
import org.krysalis.version.context.VersionInformation;
import org.krysalis.version.discovery.loading.LoadType;
import org.krysalis.version.impl.VersionImplementationConstants;

/**
 * @author ajack
 */
public class VersionRuntimeTests extends TestCase {

	public VersionRuntimeTests(String arg0) {
		super(arg0);
	}

	public static void main(String[] args) {
		junit.textui.TestRunner.run(VersionRuntimeTests.class);
	}

	public void testEquality() {
		VersionRuntime context1 = VersionRuntime.getRuntime();
		VersionRuntime context2 = VersionRuntime.getRuntime();
		assertEquals("Same Runtime", context1, context1);
		assertEquals("Same Runtime", context2, context2);
		assertEquals("Same Runtime", context1, context2);
		assertTrue("Same Runtime Object", (context1 == context2));
		assertTrue(
			"Same Runtime",
			(context1.hashCode() == context2.hashCode()));
	}

	public void testInequality() {
		PathContext pc1 = new PathContext(PathSet.getSystemBootClasspath());
		PathContext pc2 = new PathContext(PathSet.getSystemClasspath());

		VersionRuntime runtime1 = VersionRuntime.getRuntime(pc1);
		VersionRuntime runtime2 = VersionRuntime.getRuntime(pc2);
		assertTrue("Different Runtimes", !runtime1.equals(runtime2));
	}

	public void testAPICalls() throws VersionException {
		VersionRuntime runtime = VersionRuntime.getRuntime();

		runtime.getEnvironment();
		runtime.getVersions();
		runtime.getConstraints();
	}
	
	public void testResolve() throws VersionException {
		VersionRuntime runtime = VersionRuntime.getRuntime();

		VersionInformation info =
			runtime.resolve(VersionImplementationConstants.VERSION_PACKAGE);

		assertNotNull("Resolved version", info);
		assertEquals("Resolved version by API", LoadType.API, info.getLoadContext().getType());
	}
}

--- NEW FILE: VersionManagerTest.java ---
/*
 * $Id: VersionManagerTest.java,v 1.1 2004/11/09 07:54:52 chalko Exp $
 */
package org.krysalis.version;

import junit.framework.TestCase;

/**
 * @author <a href="http://nick.chalko.com">Nick Chalko </a>
 * @author $Author: chalko $
 * @version $Revision: 1.1 $
 */
public class VersionManagerTest extends TestCase {
    private VersionManager vm;

    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
        vm = VersionManager.getManager();
    }

    /*
     * @see TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        vm = null;
        super.tearDown();
    }

    /*
     * Class under test for VersionMarker createVersionMarker(String, String)
     */
    public void testCreateVersionMarkerIdVersion() throws VersionException {

        VersionMarker marker = vm.createVersionMarker("test", "1.2.1-dev");
        String expected = "1.2.1-dev";
        assertEquals("long format", expected, marker.getLongVersion());

    }

}
--- NEW FILE: VersionTestCase.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.krysalis.version;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import org.krysalis.version.specification.VersionSpecification;
import org.krysalis.version.specification.formatting.VersionFormatException;

/**
 *  
 */
public class VersionTestCase extends TestCase {
	private final class Compatibility {

		private String name;

		/**
		 * @param string
		 */
		public Compatibility(String string) {
			name = string;
		}

		public String toString() {
			return name;
		}
	};

	public final Compatibility INCOMPATIBLE = new Compatibility("Incompatible");

	public final Compatibility FORWARD = new Compatibility("Forward");

	public final Compatibility BACKWARDS = new Compatibility("Backwards");

	private static final VersionManager MANAGER = VersionManager.getManager();

	private final VersionSpecification specification;

	private Version base;

	private Version same;

	/**
	 * @param specification
	 */
	public VersionTestCase(final VersionSpecification specification) {
		super();
		this.specification = specification;
	}

	public void setUp() throws VersionException {
		base = createVersion(getBaseVersionString());
		same = createVersion(getBaseVersionString());

	}

	public void tearDown() {
		base = null;
	}

	public void testCore() {
		assertNotNull("Version for " + getBaseVersionString()
				+ " should not be null", base);
		assertSymetricNotEquals(base, Version.UNKNOWN);

		assertVersionReleation(base, same, FORWARD, 0);

	}

	public void testIncrement() throws VersionException {
		assert5Increments(base, getNonCompatibleIncrementList(), INCOMPATIBLE);
		assert5Increments(base, getForwardCompatibleIncrementList(), FORWARD);
		assert5Increments(base, getBackwardCompatibleIncrementList(), BACKWARDS);

	}

	/**
	 * @return
	 */
	public List getBackwardCompatibleIncrementList() {

		return Arrays.asList(new String[] { "minor" });
	}

	/**
	 * @param start
	 * @param incrementLevel
	 * @param compatible
	 * @throws VersionException
	 */
	public void assert5Increments(Version start, final List list,
			Compatibility compatibility) throws VersionException {

		assertIncrementing(start, list, compatibility, 5);
	}

	/**
	 * @param start
	 * @param list
	 * @param compatibility
	 * @param i
	 * @throws VersionException
	 */
	private void assertIncrementing(Version start, List list,
			Compatibility compatibility, int count) throws VersionException {

		for (Iterator i = list.iterator(); i.hasNext();) {
			String incrementLevel = (String) i.next();
			assertIncrementing(start, incrementLevel, compatibility, count);

		}

	}

	/**
	 * @param start
	 * @param incrementLevel
	 * @param compatible
	 * @param iterations
	 * @throws VersionException
	 */
	public void assertIncrementing(Version start, final String incrementLevel,
			Compatibility compatability, int iterations)
			throws VersionException {
		Version last = start;
		for (int i = 0; i < iterations; i++) {
			Version next = last.increment(incrementLevel);
			assertVersionReleation(start, next, compatability, -1);
			assertVersionReleation(last, next, compatability, -1);

		}
	}

	/**
	 * Assert the ordering, and compatablity of two versions. Tests eqauls,
	 * Comparable,Comparator and isCompatible.
	 * 
	 * @param versionString1
	 *            a Version String
	 * @param versionString2
	 *            a Version String
	 * @param compatible
	 * 
	 * @param compare
	 *            -1 if version1 less than version2, 0 if equals and 1 if
	 *            greater than
	 * @throws VersionFormatException
	 */
	public void assertVersionReleation(String versionString1,
			String versionString2, Compatibility compatibility, int compare)
			throws VersionFormatException {
		Version v1 = createVersion(versionString1);
		Version v2 = createVersion(versionString2);
		assertVersionReleation(v1, v2, compatibility, compare);

	}

	/**
	 * Assert the ordering, and compatablity of two versions. Tests eqauls,
	 * Comparable,Comparator and isCompatible.
	 * 
	 * @param v1
	 *            a Version
	 * @param v2
	 *            a Version
	 * @param compatible
	 * @param
	 */
	public void assertVersionReleation(Version v1, Version v2,
			final Compatibility compatibility, int compare) {

		if (compare == 0 && compatibility != FORWARD) {
			throw new IllegalArgumentException(
					"If a version is equal it must be forward compatable.  The test setup is illegal");
		}
		if (compare == 0) {
			assertSymetricEquals(v1, v2);
			assertSymetricComparableEquals(v1, v2);
			assertSymetricComparatorEquals(v1, v2);
		} else {
			assertSymetricNotEquals(v1, v2);
			Version less = v1, more = v2;
			if (compare > 0) {
				less = v2;
				more = v1;
			}
			assertSymetricLess(less, more);
		}

		assertCompatability(v1, v2, compatibility);
	}

	/**
	 * @param less
	 * @param more
	 * @param expected
	 */
	private void assertCompatability(Version less, Version more,
			final Compatibility compatibility) {
		if (compatibility == INCOMPATIBLE) {
			assertCompatability(less, more, false);
			assertCompatability(more, less, false);
		} else if (compatibility == BACKWARDS) {
			assertCompatability(less, more, false);
			assertCompatability(more, less, true);
		} else if (compatibility == FORWARD) {
			assertCompatability(less, more, true);
			assertCompatability(more, less, true);
		} else {
			throw new IllegalArgumentException("Uknown COMPATIBILITY "
					+ compatibility);
		}

	}

	/**
	 * v1 is compatible v2
	 * 
	 * @param v1
	 * @param v2
	 * @param expected
	 */
	private void assertCompatability(Version v1, Version v2, boolean expected) {
		assertEquals(v1 + " compatbile with " + v2, expected, v1
				.isCompatible(v2));

	}

	/**
	 * @param less
	 * @param more
	 */
	private void assertSymetricLess(Version less, Version more) {
		assertSymetricComparator(less, more);
		assertSymetricCompare(less, more);
	}

	/**
	 * @param v1
	 * @param v2
	 */
	private void assertSymetricComparatorEquals(Version v1, Version v2) {
		Comparator c = v1.getComparator();
		assertEquals("Using first comparator   " + c + ". " + v1
				+ " compare to " + v2, 0, v1.compareTo(v2));
		assertEquals("Using first comparator " + c + ". " + v2 + " compare to "
				+ v1, 0, v2.compareTo(v1));
		c = v2.getComparator();
		assertEquals("Using second comparator " + c + ". " + v1
				+ " compare to " + v2, 0, v1.compareTo(v2));
		assertEquals("Using second comparator " + c + ". " + v2, 0, v2
				.compareTo(v1));
	}

	/**
	 * @param less
	 * @param more
	 */
	private void assertSymetricComparator(Version less, Version more) {
		Comparator c = less.getComparator();
		assertTrue("Using comparator " + c + " of the lesser. " + less
				+ " compare to " + more + " should be <0 but was "
				+ less.compareTo(more), less.compareTo(more) < 0);
		assertTrue("Using comparator " + c + " ofthe lesser. " + more
				+ " compare to " + less + " should be <0 but was "
				+ more.compareTo(less), more.compareTo(less) > 0);
		c = more.getComparator();
		assertTrue("Using comparator " + c + " of the greator. " + less
				+ " compare to " + more + " should be <0 but was "
				+ less.compareTo(more), less.compareTo(more) < 0);
		assertTrue("Using comparator " + c + " ofthe greator. " + more
				+ " compare to " + less + " should be <0 but was "
				+ more.compareTo(less), more.compareTo(less) > 0);
	}

	/**
	 * @param less
	 * @param more
	 */
	private void assertSymetricCompare(Version less, Version more) {
		assertTrue(less + " compare " + more + " should be <0 but was "
				+ less.compareTo(more), less.compareTo(more) < 0);
		assertTrue(more + " compare " + less + " should be > 0 but was "
				+ more.compareTo(less), more.compareTo(less) > 0);
	}

	/**
	 * @param v1
	 * @param v2
	 */
	private void assertSymetricComparableEquals(Version v1, Version v2) {
		assertEquals(v1 + " compare " + v2, 0, v1.compareTo(v2));
		assertEquals(v2 + " compare " + v1, 0, v2.compareTo(v1));
	}

	/**
	 * @param versionString
	 * @return @throws
	 *         VersionFormatException
	 */
	private Version createVersion(String versionString)
			throws VersionFormatException {
		return MANAGER.createVersion(specification, versionString);
	}

	/**
	 * @return
	 */
	private List getNonCompatibleIncrementList() {
		return Arrays.asList(new String[] { "major" });
	}

	/**
	 * @return
	 */
	public List getForwardCompatibleIncrementList() {
		return Arrays.asList(new String[] { "point" });
	}

	/**
	 * assert !v1.eqauls(v2) && !v2.equals(v1)
	 * 
	 * @param v1
	 * @param v2
	 */
	private void assertSymetricNotEquals(Version v1, Version v2) {
		assertTrue(v2 + " should not equals " + v1, !v2.equals(v1));
		assertTrue(v1 + " should not equals " + v2, !v1.equals(v2));
	}

	/**
	 * assert v1.eqauls(v2) && v2.equals(v1) and v1.hashCode == v2.hashCode
	 * 
	 * @param v1
	 * @param v2
	 */
	private void assertSymetricEquals(Version v1, Version v2) {
		assertEquals(v2 + " should equal " + v1, v2, v1);
		assertEquals(v1 + " should equal " + v2, v1, v2);
		assertEquals("hashcode of " + v1 + " and " + v2, v1.hashCode(), v2
				.hashCode());
	}

	/**
	 * @return
	 */
	protected String getBaseVersionString() {
		return "1";
	}

	/**
	 * @param inOrder
	 * @param compatible
	 * @throws VersionFormatException
	 */
	public void assertVersionInOrder(String[] inOrder,
			Compatibility compatability) throws VersionFormatException {
		for (int i = 1; i < inOrder.length; i++) {
			for (int j = 0; j < i; j++) {
				assertVersionReleation(inOrder[j], inOrder[i], compatability,
						-1);
			}
		}
	}

	/**
	 * @param same
	 * @throws VersionFormatException
	 */
	public void assertAllEqual(String[] same) throws VersionFormatException {
		for (int i = 1; i < same.length; i++) {
			for (int j = 0; j < i; j++) {
				assertVersionReleation(same[j], same[i], FORWARD, 0);
			}
		}
	}

}


-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click