blob: 4ac428629c355a64658357c7f84993adc8866c37 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ant.tests.ui;
import java.util.Hashtable;
import java.util.Map;
import junit.framework.TestCase;
import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.PropertyHelper;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.UnknownElement;
import org.apache.tools.ant.types.Path;
import org.eclipse.ant.internal.core.IAntCoreConstants;
import org.eclipse.ant.internal.ui.model.AntModelProject;
/**
* Tests methods from {@link AntModelProject}
*
* @since 3.5.1
*/
public class ModelProjectTests extends TestCase {
/**
* Tests {@link AntModelProject#setNewProperty(String, String)}
*
* @throws Exception
*/
public void testsetNewProperty1() throws Exception {
AntModelProject p = new AntModelProject();
p.setNewProperty("p1", "p1_value");
assertTrue("the property map must have key 'p1'", p.getProperties().containsKey("p1"));
assertNotNull("getProperty() did not return 'p1'", p.getProperty("p1"));
assertNotNull("the property help must have 'p1'", PropertyHelper.getProperty(p, "p1"));
}
/**
* Tests {@link AntModelProject#reset()}
*
* @throws Exception
*/
public void testReset1() throws Exception {
AntModelProject p = new AntModelProject();
p.setDefault("foo");
p.addTarget("t1", new Target());
p.setName("project1");
p.setDescription("A test project with one target");
p.reset();
assertEquals("the name must be the empty string", p.getName(), IAntCoreConstants.EMPTY_STRING);
assertNull("the default target must be null", p.getDefaultTarget());
assertNull("the description must be null", p.getDescription());
assertEquals("The target list must be of size zero", p.getTargets().size(), 0);
}
/**
* Tests {@link AntModelProject#getProperty(String)} where the property we want has been set using
* {@link AntModelProject#setNewProperty(String, String)}
*
* @throws Exception
*/
public void testGetProprty1() throws Exception {
AntModelProject p = new AntModelProject();
p.setNewProperty("testGetProprty1", "p1_value");
assertNotNull("getProperty() did not return 'testGetProprty1'", p.getProperty("testGetProprty1"));
}
/**
* Tests {@link AntModelProject#getProperty(String)} where the property has been set into the user properties
*
* @throws Exception
*/
public void testGetProprty2() throws Exception {
AntModelProject p = new AntModelProject();
p.setUserProperty("testGetProprty2", "p2_value");
assertNotNull("getProperty() did not return 'testGetProprty2'", p.getProperty("testGetProprty2"));
}
/**
* Tests {@link AntModelProject#getReference(String)} where the desired ref has been placed in the primary reference map
* using {@link AntModelProject#addReference(String, Object)}
*
* @throws Exception
*/
public void testGetReference1() throws Exception {
AntModelProject p = new AntModelProject();
p.addReference("testGetReference1", new Object());
assertNotNull("The reference 'testGetReference1' must exist", p.getReference("testGetReference1"));
}
/**
* Tests {@link AntModelProject#getReference(String)} where the desired ref has been placed in the
* secondary 'idrefs' map using {@link AntModelProject#addIdReference(String, Object)}
*
* @throws Exception
*/
public void testGetReference2() throws Exception {
AntModelProject p = new AntModelProject();
p.addIdReference("testGetReference2", new Object());
assertNotNull("the reference 'testGetReference2' must exist", p.getReference("testGetReference2"));
}
/**
* Tests {@link AntModelProject#getReference(String)} where the desired ref is in the secondary 'idrefs' map
* and is an {@link UnknownElement}
*
* @throws Exception
*/
public void testGetReference3() throws Exception {
AntModelProject p = new AntModelProject();
String obj = new String("hello");
UnknownElement ue = new UnknownElement("ue");
ue.setProject(p);
ue.setRealThing(obj);
p.addIdReference("testGetReference3", ue);
Object ref = p.getReference("testGetReference3");
assertNotNull("the reference 'testGetReference3' must exist", ref);
assertTrue("the reference must be a String", ref instanceof String);
}
/**
* Tests {@link AntModelProject#getProperties()} such that all properties (user and 'normal') are returned
* in one call to {@link AntModelProject#getProperties()}
*
* @throws Exception
*/
public void testGetProperties1() throws Exception {
AntModelProject p = new AntModelProject();
p.setProperty("p1", "p1v");
p.setProperty("p2", "p2v");
p.setUserProperty("p3", "p3v");
//3 above and 'basedir'
assertEquals("there must be 4 properties returned", p.getProperties().size(), 4);
}
/**
* Tests {@link AntModelProject#createClassLoader(org.apache.tools.ant.types.Path)} such that only one
* classloader is created for a given path - classloaders are cached per-Path
*
* @throws Exception
*/
public void testCreateClassloader1() throws Exception {
AntModelProject p = new AntModelProject();
Path path = new Path(p);
AntClassLoader loader = p.createClassLoader(path);
assertNotNull("A classloader should have been created", loader);
//create a 'new' one, where passing null causes the classloader for the project path to be returned
AntClassLoader loader2 = p.createClassLoader(null);
assertNotNull("A classloader for a null path should return the project classloader", loader2);
//pointer compare, they should reference the same class loader
assertTrue("The class loaders should be the same object", loader == loader2);
}
/**
* Tests {@link AntModelProject#getReferences()} to ensure the map returned is the live map
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=336936
* @throws Exception
*/
public void testGetReferences1() throws Exception {
AntModelProject p = new AntModelProject();
p.addReference("testGetReferences1", new Object());
//2 refs, ant.propertyHelper is auto-added
assertEquals("there should be one reference", p.getReferences().size(), 2);
Hashtable ht = p.getReferences();
ht.clear();
assertEquals("The references table should be empty", p.getReferences().size(), 0);
}
/**
* Tests {@link AntModelProject#getCopyOfReferences()}
* @throws Exception
*/
public void testGetCopyOfReferences1() throws Exception {
AntModelProject p = new AntModelProject();
//2 refs, ant.propertyHelper is auto-added
p.addReference("testGetCopyOfReferences1", new Object());
assertEquals("there should be one reference", p.getReferences().size(), 2);
Map ht = p.getCopyOfReferences();
ht.clear();
assertEquals("There should be one reference", p.getReferences().size(), 2);
}
}