uidesigner/com.nokia.sdt.emf.dm.tests/src/com/nokia/sdt/emf/dm/tests/PropertyContainerCopierTest.java
author timkelly
Mon, 06 Apr 2009 14:15:39 -0500
changeset 52 e26ecc088f24
parent 2 d760517a8095
permissions -rw-r--r--
Add tests for regressing bug 8804.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
cawthron
parents:
diff changeset
     1
/*
cawthron
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
cawthron
parents:
diff changeset
     3
* All rights reserved.
cawthron
parents:
diff changeset
     4
* This component and the accompanying materials are made available
cawthron
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
cawthron
parents:
diff changeset
     6
* which accompanies this distribution, and is available
cawthron
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
cawthron
parents:
diff changeset
     8
*
cawthron
parents:
diff changeset
     9
* Initial Contributors:
cawthron
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
cawthron
parents:
diff changeset
    11
*
cawthron
parents:
diff changeset
    12
* Contributors:
cawthron
parents:
diff changeset
    13
*
cawthron
parents:
diff changeset
    14
* Description: 
cawthron
parents:
diff changeset
    15
*
cawthron
parents:
diff changeset
    16
*/
cawthron
parents:
diff changeset
    17
package com.nokia.sdt.emf.dm.tests;
cawthron
parents:
diff changeset
    18
cawthron
parents:
diff changeset
    19
import com.nokia.sdt.emf.dm.*;
cawthron
parents:
diff changeset
    20
import com.nokia.sdt.emf.dm.impl.PropertyContainerCopier;
cawthron
parents:
diff changeset
    21
cawthron
parents:
diff changeset
    22
import junit.framework.TestCase;
cawthron
parents:
diff changeset
    23
cawthron
parents:
diff changeset
    24
public class PropertyContainerCopierTest extends TestCase {
cawthron
parents:
diff changeset
    25
	
cawthron
parents:
diff changeset
    26
	IDesignerData root;
cawthron
parents:
diff changeset
    27
	IPropertyContainer nodeProperties;
cawthron
parents:
diff changeset
    28
	INode rootNode, child1, child2;
cawthron
parents:
diff changeset
    29
	
cawthron
parents:
diff changeset
    30
	static final String NAME1 = "child1";
cawthron
parents:
diff changeset
    31
	static final String NAME2 = "child2";
cawthron
parents:
diff changeset
    32
cawthron
parents:
diff changeset
    33
	protected void setUp() throws Exception {
cawthron
parents:
diff changeset
    34
		root = DmFactory.eINSTANCE.createIDesignerData();
cawthron
parents:
diff changeset
    35
		rootNode = DmFactory.eINSTANCE.createINode();
cawthron
parents:
diff changeset
    36
		root.getRootContainers().add(rootNode);
cawthron
parents:
diff changeset
    37
		nodeProperties = rootNode.getProperties();
cawthron
parents:
diff changeset
    38
		
cawthron
parents:
diff changeset
    39
		child1 = DmFactory.eINSTANCE.createINode();
cawthron
parents:
diff changeset
    40
		child1.getProperties().set("name", nodeProperties.createLiteral(NAME1));
cawthron
parents:
diff changeset
    41
		rootNode.getChildren().add(child1);
cawthron
parents:
diff changeset
    42
		child2 = DmFactory.eINSTANCE.createINode();
cawthron
parents:
diff changeset
    43
		rootNode.getChildren().add(child2);
cawthron
parents:
diff changeset
    44
		child2.getProperties().set("name", nodeProperties.createLiteral(NAME2));
cawthron
parents:
diff changeset
    45
	}
cawthron
parents:
diff changeset
    46
	
cawthron
parents:
diff changeset
    47
	String getKey(IPropertyContainer pc, String propertyID) {
cawthron
parents:
diff changeset
    48
		IPropertyValue pv = (IPropertyValue) pc.getProperties().get(propertyID);
cawthron
parents:
diff changeset
    49
		assertNotNull(pv);
cawthron
parents:
diff changeset
    50
		assertTrue(pv.hasStringValue());
cawthron
parents:
diff changeset
    51
		assertTrue(pv.getStringValue().isKey());
cawthron
parents:
diff changeset
    52
		return pv.getStringValue().getValue();
cawthron
parents:
diff changeset
    53
	}
cawthron
parents:
diff changeset
    54
	
cawthron
parents:
diff changeset
    55
	void checkString(IPropertyContainer pc, String propertyID, int type, String expected) {
cawthron
parents:
diff changeset
    56
		Object value = pc.get(propertyID);
cawthron
parents:
diff changeset
    57
		assertNotNull(value);
cawthron
parents:
diff changeset
    58
		assertTrue(value instanceof StringValue);
cawthron
parents:
diff changeset
    59
		StringValue sv = (StringValue) value;
cawthron
parents:
diff changeset
    60
		assertEquals(type, sv.getType());
cawthron
parents:
diff changeset
    61
		String text = pc.lookupString(sv);
cawthron
parents:
diff changeset
    62
		assertEquals(expected, text);
cawthron
parents:
diff changeset
    63
	}
cawthron
parents:
diff changeset
    64
	
cawthron
parents:
diff changeset
    65
	public void testUndo() {
cawthron
parents:
diff changeset
    66
		// simulate an undo scenario by initializing a compound
cawthron
parents:
diff changeset
    67
		// property, capturing undo state, changing the properties,
cawthron
parents:
diff changeset
    68
		// undoing, and checking the values
cawthron
parents:
diff changeset
    69
		IPropertyValue compoundPV = nodeProperties.createPropertyContainerForProperty("test");
cawthron
parents:
diff changeset
    70
		nodeProperties.getProperties().put("test", compoundPV);
cawthron
parents:
diff changeset
    71
		IPropertyContainer srcProperties = compoundPV.getCompoundValue();
cawthron
parents:
diff changeset
    72
		
cawthron
parents:
diff changeset
    73
		srcProperties.set("literal", srcProperties.createLiteral("literal1"));
cawthron
parents:
diff changeset
    74
		srcProperties.set("macro1", srcProperties.createMacro("macro1-value"));
cawthron
parents:
diff changeset
    75
		srcProperties.set("macro2", srcProperties.createMacro("macro2-value"));
cawthron
parents:
diff changeset
    76
		srcProperties.set("localized1", srcProperties.createLocalized("localized1-value"));
cawthron
parents:
diff changeset
    77
		srcProperties.set("localized2", srcProperties.createLocalized("localized2-value"));
cawthron
parents:
diff changeset
    78
		srcProperties.set("reference1", srcProperties.createReference(NAME1));
cawthron
parents:
diff changeset
    79
		srcProperties.set("reference2", srcProperties.createReference(NAME2));
cawthron
parents:
diff changeset
    80
		IPropertyValue nestedCompoundPV = srcProperties.createPropertyContainerForProperty("nested-compound1");
cawthron
parents:
diff changeset
    81
		srcProperties.getProperties().put("nested-compound1", nestedCompoundPV);
cawthron
parents:
diff changeset
    82
		IPropertyContainer nestedCompoundProperties = nestedCompoundPV.getCompoundValue();
cawthron
parents:
diff changeset
    83
		nestedCompoundProperties.set("nested-literal", nestedCompoundProperties.createLiteral("nested-literal1"));
cawthron
parents:
diff changeset
    84
		nestedCompoundProperties.set("nested-macro", nestedCompoundProperties.createMacro("nested-macro1"));
cawthron
parents:
diff changeset
    85
		nestedCompoundProperties.set("nested-localized", nestedCompoundProperties.createLocalized("nested-localized1"));
cawthron
parents:
diff changeset
    86
		IPropertyValue nestedCompound2PV = srcProperties.createPropertyContainerForProperty("nested-compound2");
cawthron
parents:
diff changeset
    87
		srcProperties.getProperties().put("nested-compound2", nestedCompound2PV);
cawthron
parents:
diff changeset
    88
		IPropertyContainer nestedCompound2Properties = nestedCompound2PV.getCompoundValue();
cawthron
parents:
diff changeset
    89
		nestedCompound2Properties.set("aaa", nestedCompoundProperties.createLiteral("xyz"));
cawthron
parents:
diff changeset
    90
		
cawthron
parents:
diff changeset
    91
		// record expected table keys, so we know new ones aren't assigned
cawthron
parents:
diff changeset
    92
		String topMacro1Key = getKey(srcProperties, "macro1");
cawthron
parents:
diff changeset
    93
		String topMacro2Key = getKey(srcProperties, "macro2");	
cawthron
parents:
diff changeset
    94
		String topLocalized1Key = getKey(srcProperties, "localized1");
cawthron
parents:
diff changeset
    95
		String topLocalized2Key = getKey(srcProperties, "localized2");
cawthron
parents:
diff changeset
    96
		String nestedMacroKey = getKey(nestedCompoundProperties, "nested-macro");
cawthron
parents:
diff changeset
    97
		String nestedLocalizedKey = getKey(nestedCompoundProperties, "nested-localized");
cawthron
parents:
diff changeset
    98
		
cawthron
parents:
diff changeset
    99
		PropertyContainerCopier copier = new PropertyContainerCopier(srcProperties);
cawthron
parents:
diff changeset
   100
		assertNotNull(copier.getCopy());
cawthron
parents:
diff changeset
   101
		
cawthron
parents:
diff changeset
   102
		// change values prior to undo
cawthron
parents:
diff changeset
   103
		srcProperties.set("newly-added", srcProperties.createLocalized("new localized"));
cawthron
parents:
diff changeset
   104
		srcProperties.set("literal", srcProperties.createLiteral("another literal"));
cawthron
parents:
diff changeset
   105
		srcProperties.set("macro1", srcProperties.createMacro("another macro"));
cawthron
parents:
diff changeset
   106
		srcProperties.reset("macro2");
cawthron
parents:
diff changeset
   107
		srcProperties.set("localized1", srcProperties.createLocalized("another localized"));
cawthron
parents:
diff changeset
   108
		srcProperties.reset("localized2");
cawthron
parents:
diff changeset
   109
		nestedCompoundProperties.set("nested-literal", nestedCompoundProperties.createLocalized("loc123"));
cawthron
parents:
diff changeset
   110
		nestedCompoundProperties.reset("nested-macro");
cawthron
parents:
diff changeset
   111
		nestedCompoundProperties.set("nested-localized", nestedCompoundProperties.createLiteral("literal123"));
cawthron
parents:
diff changeset
   112
		srcProperties.reset("nested-compound2");
cawthron
parents:
diff changeset
   113
		
cawthron
parents:
diff changeset
   114
		// remove child node 2. Upon undo the reference should not be restored
cawthron
parents:
diff changeset
   115
		rootNode.getChildren().remove(child2);
cawthron
parents:
diff changeset
   116
		assertNull(srcProperties.getProperties().get("reference2"));
cawthron
parents:
diff changeset
   117
		
cawthron
parents:
diff changeset
   118
		copier.undo(true, true);
cawthron
parents:
diff changeset
   119
		
cawthron
parents:
diff changeset
   120
		srcProperties = (IPropertyContainer) nodeProperties.get("test");
cawthron
parents:
diff changeset
   121
		assertNotNull(srcProperties);
cawthron
parents:
diff changeset
   122
		nestedCompoundProperties = (IPropertyContainer) srcProperties.get("nested-compound1");
cawthron
parents:
diff changeset
   123
		assertNotNull(nestedCompoundProperties);
cawthron
parents:
diff changeset
   124
		nestedCompound2Properties = (IPropertyContainer) srcProperties.get("nested-compound2");
cawthron
parents:
diff changeset
   125
		assertNotNull(nestedCompound2Properties);
cawthron
parents:
diff changeset
   126
		
cawthron
parents:
diff changeset
   127
		assertEquals(topMacro1Key, getKey(srcProperties, "macro1"));
cawthron
parents:
diff changeset
   128
		assertEquals(topMacro2Key, getKey(srcProperties, "macro2"));
cawthron
parents:
diff changeset
   129
		assertEquals(topLocalized1Key, getKey(srcProperties, "localized1"));
cawthron
parents:
diff changeset
   130
		assertEquals(topLocalized2Key, getKey(srcProperties, "localized2"));
cawthron
parents:
diff changeset
   131
		assertEquals(nestedMacroKey, getKey(nestedCompoundProperties, "nested-macro"));
cawthron
parents:
diff changeset
   132
		assertEquals(nestedLocalizedKey, getKey(nestedCompoundProperties, "nested-localized"));
cawthron
parents:
diff changeset
   133
		
cawthron
parents:
diff changeset
   134
		assertNull(srcProperties.getProperties().get("newly-added"));
cawthron
parents:
diff changeset
   135
		
cawthron
parents:
diff changeset
   136
		checkString(srcProperties, "literal", StringValue.LITERAL, "literal1");
cawthron
parents:
diff changeset
   137
		checkString(srcProperties, "macro1", StringValue.MACRO, "macro1-value");
cawthron
parents:
diff changeset
   138
		checkString(srcProperties, "macro2", StringValue.MACRO, "macro2-value");
cawthron
parents:
diff changeset
   139
		checkString(srcProperties, "localized1", StringValue.LOCALIZED, "localized1-value");
cawthron
parents:
diff changeset
   140
		checkString(srcProperties, "localized2", StringValue.LOCALIZED, "localized2-value");
cawthron
parents:
diff changeset
   141
		checkString(srcProperties, "reference1", StringValue.REFERENCE, NAME1);
cawthron
parents:
diff changeset
   142
		assertNull(srcProperties.getProperties().get("reference2"));
cawthron
parents:
diff changeset
   143
		checkString(nestedCompoundProperties, "nested-literal", StringValue.LITERAL, "nested-literal1");
cawthron
parents:
diff changeset
   144
		checkString(nestedCompoundProperties, "nested-macro", StringValue.MACRO, "nested-macro1");
cawthron
parents:
diff changeset
   145
		checkString(nestedCompoundProperties, "nested-localized", StringValue.LOCALIZED, "nested-localized1");
cawthron
parents:
diff changeset
   146
		checkString(nestedCompound2Properties, "aaa", StringValue.LITERAL, "xyz");
cawthron
parents:
diff changeset
   147
	}
cawthron
parents:
diff changeset
   148
cawthron
parents:
diff changeset
   149
}