/** Test for APSTUD-4289. Make sure we don't allow duplicate user agents into the JS index */ @Test public void testDuplicateUserAgents() { // create property PropertyElement property = new PropertyElement(); property.setName("property"); // add all user agents, twice IUserAgentManager manager = CorePlugin.getDefault().getUserAgentManager(); for (IUserAgent userAgent : manager.getAllUserAgents()) { UserAgentElement uaElement = new UserAgentElement(); uaElement.setPlatform(userAgent.getName()); property.addUserAgent(uaElement); property.addUserAgent(uaElement); } // create type for property so we can write it to the index TypeElement type = new TypeElement(); type.setName("Testing"); type.addProperty(property); // write type and its properties JSIndexWriter writer = new JSIndexWriter(); writer.writeType(getIndex(), type); // read property back again JSIndexReader reader = new JSIndexReader(); List<PropertyElement> properties = reader.getProperties(getIndex(), property.getOwningType()); // make sure we have only one of each user agent assertNotNull(properties); assertEquals(1, properties.size()); assertEquals(manager.getAllUserAgents().length, properties.get(0).getUserAgents().size()); }
/** testType */ @Test public void testType() { String typeName = "MyClass"; TypeElement type = new TypeElement(); type.setName(typeName); this.writeType(type); List<TypeElement> retrievedTypes = this.getType(typeName); TypeElement retrievedType = retrievedTypes.get(0); assertNotNull(retrievedType); assertEquals(typeName, retrievedType.getName()); }
@Test public void testSpecialAllUserAgentFlag() { // create property and use all user agents PropertyElement property = new PropertyElement(); property.setName("property"); property.setHasAllUserAgents(); // create type for property so we can write it to the index TypeElement type = new TypeElement(); type.setName("Testing"); type.addProperty(property); // write type and its property JSIndexWriter writer = new JSIndexWriter(); writer.writeType(getIndex(), type); // perform low-level query // @formatter:off List<QueryResult> properties = getIndex() .query( new String[] {IJSIndexConstants.PROPERTY}, type.getName(), SearchPattern.PREFIX_MATCH); // @formatter:on // make sure we got something assertNotNull(properties); assertEquals(1, properties.size()); // split result into columns String word = properties.get(0).getWord(); String[] columns = IndexReader.DELIMITER_PATTERN.split(word); assertEquals(3, columns.length); // grab last column and parse as JSON String json = columns[2]; Object m = JSON.parse(json); // make sure we have a map assertTrue("Expected a Map from the JSON string", m instanceof Map); Map<?, ?> map = (Map<?, ?>) m; // test userAgents for "special value" which is really just a null value. assertTrue("Expected a userAgents property", map.containsKey("userAgents")); assertNull("Expected userAgents property to be null", map.get("userAgents")); }
/** testProperty */ @Test public void testProperty() { String typeName = "MyClass"; String propertyName = "myProperty"; // create type TypeElement type = new TypeElement(); type.setName(typeName); // create property within type PropertyElement property = new PropertyElement(); property.setName(propertyName); type.addProperty(property); // write type to index this.writeType(type); // then retrieve it List<TypeElement> retrievedTypes = this.getType(typeName); TypeElement retrievedType = retrievedTypes.get(0); assertNotNull(retrievedType); assertEquals(typeName, retrievedType.getName()); // make sure we have one property List<PropertyElement> properties = retrievedType.getProperties(); assertNotNull(properties); assertTrue(properties.size() == 1); // make sure the name is correct PropertyElement retrievedProperty = properties.get(0); assertEquals(propertyName, retrievedProperty.getName()); }
/* * (non-Javadoc) * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) */ public Object[] getChildren(Object parentElement) { List<? extends Object> result = Collections.emptyList(); if (parentElement instanceof JSElement) { JSElement root = (JSElement) parentElement; // @formatter:off result = CollectionsUtil.newList( new ClassGroupElement( Messages.JSIndexViewContentProvider_WorkspaceGroupLabel, JSIndexQueryHelper.getJSCoreIndex()), new ClassGroupElement( Messages.JSIndexViewContentProvider_ProjectGroupLabel, root.getIndex())); // @formatter:on } else if (parentElement instanceof ClassGroupElement) { ClassGroupElement group = (ClassGroupElement) parentElement; result = group.getClasses(); } else if (parentElement instanceof ClassElement) { TypeElement type = (ClassElement) parentElement; // NOTE: have to do this "temp" acrobatics to make the compiler happy, due to use of generics // and differing // return types when grabbing properties vs events List<Object> temp = new ArrayList<Object>(); temp.addAll(type.getProperties()); temp.addAll(type.getEvents()); result = temp; } else if (parentElement instanceof EventElement) { EventElement event = (EventElement) parentElement; result = event.getProperties(); } return result.toArray(new Object[result.size()]); }
@Test public void testFunctionParameterWithConstantValues() { String typeName = "MyClass"; String eventName = "event"; String propertyName = "eventProperty"; // create type TypeElement type = new TypeElement(); type.setName(typeName); EventElement ee = new EventElement(); ee.setName(eventName); EventPropertyElement epe = new EventPropertyElement(); epe.fromJSON( CollectionsUtil.newTypedMap( String.class, Object.class, IHasPredefinedValues.CONSTANTS_PROPERTY, CollectionsUtil.newList("Titanium.UI.FILL", "Titanium.UI.ALIGN"))); epe.setName(propertyName); ee.addProperty(epe); type.addEvent(ee); // write type to index this.writeType(type); // then retrieve it List<TypeElement> retrievedTypes = this.getType(typeName); TypeElement retrievedType = retrievedTypes.get(0); assertNotNull(retrievedType); assertEquals(typeName, retrievedType.getName()); EventElement retrievedEvent = retrievedType.getEvent(eventName); assertNotNull(retrievedEvent); List<EventPropertyElement> retrievedProps = retrievedEvent.getProperties(); assertNotNull(retrievedProps); assertEquals(1, retrievedProps.size()); // make sure the name is correct EventPropertyElement retrievedProperty = retrievedProps.get(0); assertEquals(propertyName, retrievedProperty.getName()); // Check constants List<String> constants = retrievedProperty.getConstants(); assertEquals(2, constants.size()); assertTrue(constants.contains("Titanium.UI.FILL")); assertTrue(constants.contains("Titanium.UI.ALIGN")); }
@Test public void testPropertyWithConstantValues() { String typeName = "MyClass"; String propertyName = "myProperty"; // create type TypeElement type = new TypeElement(); type.setName(typeName); // create property within type PropertyElement property = createProperty( "name", propertyName, IHasPredefinedValues.CONSTANTS_PROPERTY, CollectionsUtil.newList("Titanium.UI.FILL", "Titanium.UI.ALIGN")); type.addProperty(property); // write type to index this.writeType(type); // then retrieve it List<TypeElement> retrievedTypes = this.getType(typeName); TypeElement retrievedType = retrievedTypes.get(0); assertNotNull(retrievedType); assertEquals(typeName, retrievedType.getName()); // make sure we have one property List<PropertyElement> properties = retrievedType.getProperties(); assertNotNull(properties); assertEquals(1, properties.size()); // make sure the name is correct PropertyElement retrievedProperty = properties.get(0); assertEquals(propertyName, retrievedProperty.getName()); // Check constants List<String> constants = retrievedProperty.getConstants(); assertEquals(2, constants.size()); assertTrue(constants.contains("Titanium.UI.FILL")); assertTrue(constants.contains("Titanium.UI.ALIGN")); }
/** testMethod */ @Test public void testMethod() { String typeName = "MyClass"; String methodName = "myMethod"; // create type TypeElement type = new TypeElement(); type.setName(typeName); // create method within type FunctionElement method = new FunctionElement(); method.setName(methodName); type.addProperty(method); // write type to index this.writeType(type); // then retrieve it List<TypeElement> retrievedTypes = this.getType(typeName); TypeElement retrievedType = retrievedTypes.get(0); assertNotNull(retrievedType); assertEquals(typeName, retrievedType.getName()); // make sure we have one property List<PropertyElement> properties = retrievedType.getProperties(); assertNotNull(properties); assertTrue(properties.size() == 1); // make sure it is a function PropertyElement property = properties.get(0); assertTrue(property instanceof FunctionElement); // make sure it is the function we added earlier FunctionElement retrievedMethod = (FunctionElement) property; assertEquals(methodName, retrievedMethod.getName()); }
@Test public void testEventPropertyWithConstantValues() { String typeName = "MyClass"; String functionName = "convertUnits"; String paramName = "convertToUnits"; List<String> constants = CollectionsUtil.newList( "Titanium.UI.UNIT_CM", "Titanium.UI.UNIT_MM", "Titanium.UI.UNIT_DIP", "Titanium.UI.UNIT_IN", "Titanium.UI.UNIT_PX"); // create type TypeElement type = new TypeElement(); type.setName(typeName); // create function within type FunctionElement function = new FunctionElement(); function.setName(functionName); ParameterElement parameter = new ParameterElement(); parameter.setName(paramName); parameter.fromJSON( CollectionsUtil.newTypedMap( String.class, Object.class, IHasPredefinedValues.CONSTANTS_PROPERTY, constants)); function.addParameter(parameter); type.addProperty(function); // write type to index this.writeType(type); // then retrieve it List<TypeElement> retrievedTypes = this.getType(typeName); TypeElement retrievedType = retrievedTypes.get(0); assertNotNull(retrievedType); assertEquals(typeName, retrievedType.getName()); // make sure we have one property List<PropertyElement> properties = retrievedType.getProperties(); assertNotNull(properties); assertEquals(1, properties.size()); // make sure the name is correct PropertyElement retrievedProperty = properties.get(0); assertEquals(functionName, retrievedProperty.getName()); assertTrue(retrievedProperty instanceof FunctionElement); FunctionElement retrievedFunction = (FunctionElement) retrievedProperty; List<ParameterElement> retrievedParams = retrievedFunction.getParameters(); assertNotNull(retrievedParams); assertEquals(1, retrievedParams.size()); ParameterElement retrievedParam = retrievedParams.get(0); // Check constants List<String> retrievedConstants = retrievedParam.getConstants(); assertEquals(constants.size(), retrievedConstants.size()); for (String constant : constants) { assertTrue(retrievedConstants.contains(constant)); } }