Skip to content

Commit

Permalink
[Features] Ensure unique entries by name
Browse files Browse the repository at this point in the history
* Set Feature#equals() to compare name
* Set FeaturesEList#useEquals() to return true
* This ensures that list methods add() and addAll() don't allow duplicate entries
  • Loading branch information
Phillipus committed Sep 27, 2020
1 parent 68432b1 commit 63e246d
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 7 deletions.
Expand Up @@ -220,5 +220,20 @@ public String toString() {
result.append(')');
return result.toString();
}

/**
* Over-ride this to test equality based on this Feature's name
* This ensures we have unique entries keyed by name
*/
@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}

return (obj instanceof Feature) &&
name != null &&
name.equals(((Feature)obj).name);
}

} //Feature
Expand Up @@ -35,20 +35,18 @@ public IFeature putString(String name, String value, String defaultValue) {

IFeature feature = getFeature(name);

// Value equals default value so remove it
if(value.equals(defaultValue)) {
remove(name);
return feature;
}

// New one
if(feature == null) {
feature = IArchimateFactory.eINSTANCE.createFeature();
feature.setName(name);
feature.setValue(value);
add(feature);
}
// Different
// Value equals default value so remove it
else if(value.equals(defaultValue)) {
remove(feature);
}
// Different value
else if(!value.equals(feature.getValue())) {
feature.setValue(value);
}
Expand Down Expand Up @@ -134,4 +132,13 @@ private void checkNull(String s) {
throw new IllegalArgumentException("key or value cannot be null"); //$NON-NLS-1$
}
}

/**
* Return true so that {@link #contains(Object)} checks {@link Feature#equals(Object)}
* For unique entries keyed by name
*/
@Override
protected boolean useEquals() {
return true;
}
}
Expand Up @@ -162,4 +162,92 @@ public void getFeature() {

assertNull(list.getFeature("bogus"));
}

@Test
public void noDuplicateFeature1() {
IFeature feature = IArchimateFactory.eINSTANCE.createFeature();
feature.setName("uniqueName");
feature.setValue("someValue");

// Add the feature
boolean result = list.add(feature);
assertTrue(result);

// Should not be able to add the first feature again
result = list.add(feature);
assertFalse(result);

// Should not be able to add the first feature again, even if name and value change
feature.setName("uniqueName2");
feature.setValue("someValue2");
result = list.add(feature);
assertFalse(result);
}

@Test
public void noDuplicateFeature2() {
IFeature feature = IArchimateFactory.eINSTANCE.createFeature();
feature.setName("uniqueName");
feature.setValue("someValue");

// Add the feature
boolean result = list.add(feature);
assertTrue(result);

// Should not be able to add a different feature object with the same name
IFeature feature2 = IArchimateFactory.eINSTANCE.createFeature();
feature2.setName("uniqueName");
feature2.setValue("someValue2");
result = list.add(feature2);
assertFalse(result);
}

@Test
public void noDuplicateFeature3() {
IFeature feature = IArchimateFactory.eINSTANCE.createFeature();
feature.setName("uniqueName");
feature.setValue("someValue");

// Add the feature
boolean result = list.add(feature);
assertTrue(result);

// Should be able to add a different feature object with a different name
IFeature feature2 = IArchimateFactory.eINSTANCE.createFeature();
feature2.setName("uniqueName2");
feature2.setValue("someValue2");
result = list.add(feature2);
assertTrue(result);
}

@Test
public void noDuplicateFeature4() {
// Create another list
IArchimateElement element = IArchimateFactory.eINSTANCE.createArtifact();
FeaturesEList list2 = new FeaturesEList(IFeature.class, (InternalEObject)element, IArchimatePackage.ARCHIMATE_CONCEPT__FEATURES);

// Add Feature to first list
IFeature feature1 = IArchimateFactory.eINSTANCE.createFeature();
feature1.setName("uniqueName");
feature1.setValue("someValue");
list.add(feature1);

// Add feature with same name to second list
IFeature feature2 = IArchimateFactory.eINSTANCE.createFeature();
feature2.setName("uniqueName");
feature2.setValue("someValue");
list2.add(feature2);

// Should not be able to add first list to second list
boolean result = list2.addAll(list);
assertFalse(result);

// remove it
list2.remove(feature2);

// Should be able to add it now
result = list2.addAll(list);
assertTrue(result);
}

}

0 comments on commit 63e246d

Please sign in to comment.