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