| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2012 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Bernd Hufmann - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | package org.eclipse.linuxtools.tmf.ui.tests.views.uml2sd.dialogs; |
| 13 | |
| 14 | import java.util.List; |
| 15 | import java.util.regex.Pattern; |
| 16 | |
| 17 | import junit.framework.TestCase; |
| 18 | |
| 19 | import org.eclipse.jface.dialogs.DialogSettings; |
| 20 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.Criteria; |
| 21 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDFilterProvider; |
| 22 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDGraphNodeSupporter; |
| 23 | |
| 24 | /** |
| 25 | * Test cases to test Criteria class. |
| 26 | */ |
| 27 | @SuppressWarnings("nls") |
| 28 | public class CriteriaTest extends TestCase { |
| 29 | |
| 30 | @Override |
| 31 | public void setUp() throws Exception { |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | public void tearDown() throws Exception { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Test default constructor. |
| 40 | */ |
| 41 | public void testCriteria() { |
| 42 | Criteria criteria = new Criteria(); |
| 43 | assertFalse("testCriteria", criteria.isAsyncMessageReturnSelected()); |
| 44 | assertFalse("testCriteria", criteria.isAsyncMessageSelected()); |
| 45 | assertFalse("testCriteria", criteria.isCaseSenstiveSelected()); |
| 46 | assertFalse("testCriteria", criteria.isLifeLineSelected()); |
| 47 | assertFalse("testCriteria", criteria.isStopSelected()); |
| 48 | assertFalse("testCriteria", criteria.isSyncMessageReturnSelected()); |
| 49 | assertFalse("testCriteria", criteria.isSyncMessageSelected()); |
| 50 | assertNull("testCriteria", criteria.getExpression()); |
| 51 | assertNull("testCriteria", criteria.getPattern()); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Test copy constructor. |
| 56 | */ |
| 57 | public void testCriteriaCriteria() { |
| 58 | Criteria criteria = new Criteria(); |
| 59 | criteria.setExpression("test"); |
| 60 | criteria.setLifeLineSelected(true); |
| 61 | criteria.setSyncMessageSelected(true); |
| 62 | |
| 63 | Criteria copy = new Criteria(criteria); |
| 64 | |
| 65 | assertEquals("testCriteriaCriteria", criteria.isAsyncMessageReturnSelected(), copy.isAsyncMessageReturnSelected()); |
| 66 | assertEquals("testCriteriaCriteria", criteria.isAsyncMessageSelected(), copy.isAsyncMessageSelected()); |
| 67 | assertEquals("testCriteriaCriteria", criteria.isCaseSenstiveSelected(), copy.isCaseSenstiveSelected()); |
| 68 | assertEquals("testCriteriaCriteria", criteria.isLifeLineSelected(), copy.isLifeLineSelected()); |
| 69 | assertEquals("testCriteriaCriteria", criteria.isStopSelected(), copy.isStopSelected()); |
| 70 | assertEquals("testCriteriaCriteria", criteria.isSyncMessageReturnSelected(), copy.isSyncMessageReturnSelected()); |
| 71 | assertEquals("testCriteriaCriteria", criteria.isSyncMessageSelected(), copy.isSyncMessageSelected()); |
| 72 | assertEquals("testCriteriaCriteria", criteria.getExpression(), copy.getExpression()); |
| 73 | assertEquals("testCriteriaCriteria", criteria.getPattern().pattern(), copy.getPattern().pattern()); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Test accessor methods. |
| 78 | */ |
| 79 | public void testAccessors() { |
| 80 | Criteria criteria = new Criteria(); |
| 81 | criteria.setAsyncMessageReturnSelected(true); |
| 82 | criteria.setAsyncMessageSelected(true); |
| 83 | criteria.setCaseSenstiveSelected(true); |
| 84 | criteria.setLifeLineSelected(true); |
| 85 | criteria.setStopSelected(true); |
| 86 | criteria.setSyncMessageReturnSelected(true); |
| 87 | criteria.setSyncMessageSelected(true); |
| 88 | criteria.setExpression("test.*"); |
| 89 | |
| 90 | // set true to all flags |
| 91 | assertTrue("testAccessors", criteria.isAsyncMessageReturnSelected()); |
| 92 | assertTrue("testAccessors", criteria.isAsyncMessageSelected()); |
| 93 | assertTrue("testAccessors", criteria.isCaseSenstiveSelected()); |
| 94 | assertTrue("testAccessors", criteria.isLifeLineSelected()); |
| 95 | assertTrue("testAccessors", criteria.isStopSelected()); |
| 96 | assertTrue("testAccessors", criteria.isSyncMessageReturnSelected()); |
| 97 | assertTrue("testAccessors", criteria.isSyncMessageSelected()); |
| 98 | assertEquals("testAccessors", "test.*", criteria.getExpression()); |
| 99 | assertNotNull("testAccessors", criteria.getPattern()); |
| 100 | assertEquals("testAccessors", "test.*", criteria.getPattern().pattern()); |
| 101 | assertEquals("testAccessors", 0, criteria.getPattern().flags() & Pattern.CASE_INSENSITIVE); |
| 102 | |
| 103 | // set false to all flags |
| 104 | criteria.setAsyncMessageReturnSelected(false); |
| 105 | criteria.setAsyncMessageSelected(false); |
| 106 | criteria.setCaseSenstiveSelected(false); |
| 107 | criteria.setLifeLineSelected(false); |
| 108 | criteria.setStopSelected(false); |
| 109 | criteria.setSyncMessageReturnSelected(false); |
| 110 | criteria.setSyncMessageSelected(false); |
| 111 | |
| 112 | assertFalse("testAccessors", criteria.isAsyncMessageReturnSelected()); |
| 113 | assertFalse("testAccessors", criteria.isAsyncMessageSelected()); |
| 114 | assertFalse("testAccessors", criteria.isCaseSenstiveSelected()); |
| 115 | assertFalse("testAccessors", criteria.isLifeLineSelected()); |
| 116 | assertFalse("testAccessors", criteria.isStopSelected()); |
| 117 | assertFalse("testAccessors", criteria.isSyncMessageReturnSelected()); |
| 118 | assertFalse("testAccessors", criteria.isSyncMessageSelected()); |
| 119 | assertEquals("testAccessors", "test.*", criteria.getExpression()); |
| 120 | assertNotNull("testAccessors", criteria.getPattern()); |
| 121 | assertEquals("testAccessors", "test.*", criteria.getPattern().pattern()); |
| 122 | assertEquals("testAccessors", Pattern.CASE_INSENSITIVE, criteria.getPattern().flags() & Pattern.CASE_INSENSITIVE); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Test compartTo method. |
| 127 | */ |
| 128 | public void testCompareTo() { |
| 129 | Criteria criteria = new Criteria(); |
| 130 | criteria.setExpression("test"); |
| 131 | criteria.setLifeLineSelected(true); |
| 132 | criteria.setSyncMessageSelected(true); |
| 133 | |
| 134 | Criteria copy = new Criteria(criteria); |
| 135 | assertTrue("testCompareTo", criteria.compareTo(copy)); |
| 136 | assertTrue("testCompareTo", copy.compareTo(criteria)); |
| 137 | assertTrue("testCompareTo", criteria.compareTo(criteria)); |
| 138 | |
| 139 | copy.setExpression(null); |
| 140 | assertFalse("testCompareTo", criteria.compareTo(copy)); |
| 141 | assertFalse("testCompareTo", copy.compareTo(criteria)); |
| 142 | |
| 143 | criteria.setExpression(null); |
| 144 | assertTrue("testCompareTo", criteria.compareTo(copy)); |
| 145 | assertTrue("testCompareTo", copy.compareTo(criteria)); |
| 146 | |
| 147 | criteria.setExpression("test"); |
| 148 | copy.setExpression("test.*[12345]"); |
| 149 | assertFalse("testCompareTo", criteria.compareTo(copy)); |
| 150 | assertFalse("testCompareTo", copy.compareTo(criteria)); |
| 151 | |
| 152 | copy.setExpression("test"); |
| 153 | copy.setAsyncMessageReturnSelected(true); |
| 154 | assertFalse("testCompareTo", criteria.compareTo(copy)); |
| 155 | assertFalse("testCompareTo", copy.compareTo(criteria)); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Test save to disk. |
| 160 | */ |
| 161 | public void testSave() { |
| 162 | DialogSettings settings = new DialogSettings("mysettings"); |
| 163 | |
| 164 | Criteria criteria = new Criteria(); |
| 165 | criteria.setExpression("test"); |
| 166 | criteria.setLifeLineSelected(true); |
| 167 | criteria.setSyncMessageSelected(true); |
| 168 | |
| 169 | // Save the criteria to the dialog settings |
| 170 | criteria.save(settings); |
| 171 | |
| 172 | // Check if all values are saved as expected |
| 173 | assertEquals("testSave", "test", settings.get("expression")); |
| 174 | assertEquals("testSave", "false", settings.get("isCaseSenstiveSelected")); |
| 175 | assertEquals("testSave", "false", settings.get("isAsyncMessageReturnSelected")); |
| 176 | assertEquals("testSave", "false", settings.get("isAsyncMessageSelected")); |
| 177 | assertEquals("testSave", "true", settings.get("isLifeLineSelected")); |
| 178 | assertEquals("testSave", "false", settings.get("isStopSelected")); |
| 179 | assertEquals("testSave", "false", settings.get("isSyncMessageReturnSelected")); |
| 180 | assertEquals("testSave", "true", settings.get("isSyncMessageSelected")); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Test restore from disk. |
| 185 | */ |
| 186 | public void testLoad() { |
| 187 | DialogSettings settings = new DialogSettings("mysettings"); |
| 188 | |
| 189 | Criteria criteria = new Criteria(); |
| 190 | criteria.setExpression("test"); |
| 191 | criteria.setLifeLineSelected(true); |
| 192 | criteria.setSyncMessageSelected(true); |
| 193 | |
| 194 | // Save the criteria to the dialog settings |
| 195 | criteria.save(settings); |
| 196 | |
| 197 | // Load the criteria from the dialog settings |
| 198 | Criteria copy = new Criteria(); |
| 199 | copy.load(settings); |
| 200 | |
| 201 | assertTrue("testCompareTo", criteria.compareTo(copy)); |
| 202 | assertTrue("testCompareTo", copy.compareTo(criteria)); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Test graph node summary usage. |
| 207 | */ |
| 208 | public void testGetGraphNodeSummary() { |
| 209 | |
| 210 | // Create a dummy provider |
| 211 | ISDFilterProvider provider = new ISDFilterProvider() { |
| 212 | |
| 213 | @Override |
| 214 | public boolean isNodeSupported(int nodeType) { |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public String getNodeName(int nodeType, String loaderClassName) { |
| 220 | // not clear about purpose loaderClassName |
| 221 | switch (nodeType) { |
| 222 | case ISDGraphNodeSupporter.LIFELINE: |
| 223 | return "MyLifeline"; |
| 224 | case ISDGraphNodeSupporter.SYNCMESSAGE: |
| 225 | return "MySyncMessage"; |
| 226 | case ISDGraphNodeSupporter.SYNCMESSAGERETURN: |
| 227 | return "MySyncMessageReturn"; |
| 228 | case ISDGraphNodeSupporter.ASYNCMESSAGE: |
| 229 | return "MyAsyncMessage"; |
| 230 | case ISDGraphNodeSupporter.ASYNCMESSAGERETURN: |
| 231 | return "MyAsyncMessageReturn"; |
| 232 | case ISDGraphNodeSupporter.STOP: |
| 233 | return "MyStop"; |
| 234 | default: |
| 235 | return ""; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public boolean filter(List<?> filters) { |
| 241 | return false; |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | Criteria criteria = new Criteria(); |
| 246 | criteria.setExpression("BALL_.*"); |
| 247 | criteria.setAsyncMessageReturnSelected(true); |
| 248 | criteria.setAsyncMessageSelected(true); |
| 249 | criteria.setLifeLineSelected(true); |
| 250 | criteria.setStopSelected(true); |
| 251 | criteria.setSyncMessageReturnSelected(true); |
| 252 | criteria.setSyncMessageSelected(true); |
| 253 | |
| 254 | // Test summary when provider is available |
| 255 | String summary = criteria.getGraphNodeSummary(provider, null); |
| 256 | assertEquals("testGetGraphNodeSummary", "[MyLifeline or MySyncMessage or MySyncMessageReturn or MyAsyncMessage or MyAsyncMessageReturn or MyStop]", summary); |
| 257 | |
| 258 | // Test default summary when no provider is provided |
| 259 | summary = criteria.getGraphNodeSummary(null, null); |
| 260 | assertEquals("testGetGraphNodeSummary", "[Lifeline or Synchronous message or Synchronous message return or Asynchronous message or Asynchronous message return or Stop]", summary); |
| 261 | |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Test matches algorithm. |
| 266 | */ |
| 267 | public void testMatches() { |
| 268 | Criteria criteria = new Criteria(); |
| 269 | criteria.setExpression("BALL_.*"); |
| 270 | |
| 271 | /* |
| 272 | * Note that method matches uses the Pattern class. Test |
| 273 | * only case sensitive/case insensitive case. All other regular |
| 274 | * expression cases are covered by Pattern class. |
| 275 | */ |
| 276 | |
| 277 | // case insensitive |
| 278 | assertTrue("testMatches", criteria.matches("BALL_REQUEST")); |
| 279 | assertTrue("testMatches", criteria.matches("BALL_REPLY")); |
| 280 | assertTrue("testMatches", criteria.matches("BALL_R")); |
| 281 | assertTrue("testMatches", criteria.matches("ball_request")); |
| 282 | assertTrue("testMatches", criteria.matches("ball_request")); |
| 283 | assertFalse("testMatches", criteria.matches("NOBALL_REQUEST")); |
| 284 | assertFalse("testMatches", criteria.matches("BLABLA")); |
| 285 | |
| 286 | // case sensitive |
| 287 | criteria.setCaseSenstiveSelected(true); |
| 288 | assertTrue("testMatches", criteria.matches("BALL_REQUEST")); |
| 289 | assertTrue("testMatches", criteria.matches("BALL_REPLY")); |
| 290 | assertTrue("testMatches", criteria.matches("BALL_R")); |
| 291 | assertFalse("testMatches", criteria.matches("ball_request")); |
| 292 | assertFalse("testMatches", criteria.matches("ball_request")); |
| 293 | assertFalse("testMatches", criteria.matches("NOBALL_REQUEST")); |
| 294 | assertFalse("testMatches", criteria.matches("BLABLA")); |
| 295 | } |
| 296 | |
| 297 | } |