public void testOnSizeExceeded() throws Exception { renderView("/queue-size.xhtml"); for (int i = 0; i <= 3; i++) { clickOnTime(i, "form:button" + i); } TestsResult result = getTestsResult(); List<RequestData> dataList = result.getDataList(); assertEquals(2, dataList.size()); // dropNext is default checkRequestData(dataList.get(0), "form:button0", 0, DEFAULT_REQUEST_TIME, false); checkRequestData( dataList.get(1), "form:button3", DEFAULT_REQUEST_TIME, DEFAULT_REQUEST_TIME * 2, false); NativeArray handlersData = (NativeArray) executeJavaScript("defaultExceededQueueResults"); assertEquals(2, handlersData.getLength()); ScriptableObject firstHandlerData = (ScriptableObject) handlersData.get(0, handlersData); Double firstEventTime = (Double) firstHandlerData.get("_time", firstHandlerData); assertEquals(2d, firstEventTime); ScriptableObject secondHandlerData = (ScriptableObject) handlersData.get(1, handlersData); Double secondEventTime = (Double) secondHandlerData.get("_time", secondHandlerData); assertEquals(3d, secondEventTime); }
public JavascriptArrayFileNameProvider( FileProvider fileProvider, String sourceFile, String variableName) throws IOException { BufferedReader source = fileProvider.getResource(sourceFile); try { Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(); cx.evaluateReader(scope, source, sourceFile, 1, null); Object x = scope.get(variableName, scope); if (!(x instanceof NativeArray)) { throw new RuntimeException( "Could not find array called " + variableName + " in source file " + sourceFile); } NativeArray array = (NativeArray) x; for (int i = 0; i < array.getLength(); i++) { Object o = array.get(i, null); if (!(o instanceof String)) { throw new RuntimeException( "Array contained objects which are not Strings, cannot continue"); } fileNames.add("/" + o); } } finally { Context.exit(); } }
public void bsync(NativeObject jsRWB) { Map<String, Object> jRWB = (Map) Context.jsToJava(jsRWB, Map.class); RWBStatement stmt = RWBStatement.make(); Object req = jRWB.get("request"); if (req != null) { if (req instanceof BEvent) { stmt = stmt.request((BEvent) req); } else if (req instanceof NativeArray) { NativeArray arr = (NativeArray) req; stmt = stmt.request( Arrays.asList(arr.getIndexIds()) .stream() .map(i -> (BEvent) arr.get(i)) .collect(toSet())); } } stmt = stmt.waitFor(convertToEventSet(jRWB.get("waitFor"))) .block(convertToEventSet(jRWB.get("block"))) .breakUpon(convertToEventSet(jRWB.get("breakUpon"))); bthread.bsync(stmt); }
/** * 将Native对象转换为String数组 @Title: toStringArray @Description: TODO(这里用一句话描述这个方法的作用) * * @param 参数 * @return String[] 返回类型 * @throws */ public static String[] toStringArray(NativeArray nv) { String[] result = new String[(int) nv.getLength()]; for (int i = 0; i < result.length; i++) { result[i] = StringUtils.toString(nv.get(i, null)); } return result; }
private static Object[] convertArray(NativeArray jsArray, Map<Object, Boolean> identities) { Object[] ids = jsArray.getIds(); Object[] result = new Object[ids.length]; for (int i = 0; i < ids.length; i++) { Object id = ids[i]; int index = (Integer) id; Object jsValue = jsArray.get(index, jsArray); result[i] = jsToJava(jsValue, identities); } return result; }
/** * 将NativeArray返回结果转换为int[]对象 @Title: toIntArray @Description: TODO(这里用一句话描述这个方法的作用) * * @param 参数 * @return int[] 返回类型 * @throws */ public static int[] toIntArray(NativeArray nv) { int[] result = new int[(int) nv.getLength()]; for (int i = 0; i < result.length; i++) { Object obj = nv.get(i, null); if (obj instanceof Number) { result[i] = ((Number) obj).intValue(); } else { result[i] = StringUtils.toInt(StringUtils.toString(obj), 0); } } return result; }
private EventSet convertToEventSet(Object jsObject) { if (jsObject == null) return Events.emptySet; // This covers event sets AND events. if (jsObject instanceof EventSet) { return (EventSet) jsObject; } else if (jsObject instanceof NativeArray) { NativeArray arr = (NativeArray) jsObject; if (Stream.of(arr.getIds()).anyMatch(id -> arr.get(id) == null)) { throw new RuntimeException("EventSet Array contains null sets."); } return ComposableEventSet.anyOf( Arrays.asList(arr.getIndexIds()) .stream() .map(i -> (EventSet) arr.get(i)) .collect(toSet())); } else { final String errorMessage = "Cannot convert " + jsObject + " of class " + jsObject.getClass() + " to an event set"; Logger.getLogger(BThread.class.getName()).log(Level.SEVERE, errorMessage); throw new IllegalArgumentException(errorMessage); } }
public void testSingleSizedQueueIgnoreDupResponses() throws Exception { renderView("/queue-size.xhtml"); ParametersBuilder parametersBuilder = createAjaxParameters().eventsQueue("singleSizedDefaultQueue").requestDelay(0); ajax(0, "a", parametersBuilder); ajax(500, "b", parametersBuilder); TestsResult result = getTestsResult(); List<RequestData> list = result.getDataList(); assertEquals(1, list.size()); checkRequestData(list.get(0), "a", 0, DEFAULT_REQUEST_TIME, false); NativeArray handlersData = (NativeArray) executeJavaScript("defaultSingleSizedQueueResults"); assertEquals(1, handlersData.getLength()); assertEquals("a", handlersData.get(0, handlersData)); }
public Document[] map(final String docid, final String doc) { context.putThreadLocal("docid", docid); Object ret = systemFun.call(context, scope, null, new Object[] {doc, userFun}); if (ret == null || ret instanceof Undefined) { return new Document[] {}; } else if (ret instanceof RhinoDocument) { return new Document[] {((RhinoDocument) ret).doc}; } else if (ret instanceof NativeArray) { final NativeArray na = (NativeArray) ret; final Document[] mapped = new Document[(int) na.getLength()]; for (int i = 0; i < (int) na.getLength(); i++) { ret = na.get(i, null); if (!(ret instanceof RhinoDocument)) { throw new RuntimeException("Invalid object type: " + ret.getClass().getName()); } mapped[i] = ((RhinoDocument) ret).doc; } return mapped; } throw new RuntimeException("Invalid object type: " + ret.getClass().getName()); }
public static void loadExhibitDataFile( Reader baseReader, String url, String namespace, Map<String, NativeObject> types, Map<String, NativeObject> properties, List<NativeObject> items) throws IOException { ChainingReader reader = new ChainingReader(); reader.addReader(new StringReader("(")); reader.addReader(baseReader); reader.addReader(new StringReader(")")); try { Context context = Context.enter(); Scriptable scope = context.initStandardObjects(); Object o = context.evaluateReader(scope, reader, url, 1, null); if (o instanceof NativeObject) { NativeObject no = (NativeObject) o; Object typesO = ScriptableObject.getProperty(no, "types"); if (typesO instanceof NativeObject) { NativeObject typesNO = (NativeObject) typesO; Object[] propertiesIds = ScriptableObject.getPropertyIds(typesNO); for (Object propertyId : propertiesIds) { String typeID = propertyId.toString(); Object typeO = ScriptableObject.getProperty(typesNO, typeID); if (typeO instanceof NativeObject) { types.put(typeID, (NativeObject) typeO); } } } Object propertiesO = ScriptableObject.getProperty(no, "properties"); if (propertiesO instanceof NativeObject) { NativeObject propertiesNO = (NativeObject) propertiesO; Object[] propertiesIds = ScriptableObject.getPropertyIds(propertiesNO); for (Object propertyId : propertiesIds) { String propertyID = propertyId.toString(); Object propertyO = ScriptableObject.getProperty(propertiesNO, propertyID); if (propertyO instanceof NativeObject) { properties.put(propertyID, (NativeObject) propertyO); } } } Object itemsO = ScriptableObject.getProperty(no, "items"); if (itemsO instanceof NativeArray) { NativeArray itemsArray = (NativeArray) itemsO; long count = itemsArray.getLength(); for (int i = 0; i < count; i++) { Object itemO = itemsArray.get(i, itemsArray); if (itemO instanceof NativeObject) { items.add((NativeObject) itemO); } } } } } finally { reader.close(); } }
protected static Resource _processItem( NativeObject itemNO, String baseURL, SailConnection dataConnection, SailConnection metaConnection, Map<String, String> itemIDToURI) throws SailException { String id = _getStringProperty(itemNO, "id"); String label = _getStringProperty(itemNO, "label"); String uri = _getStringProperty(itemNO, "uri"); if (id == null) { if (label == null) { throw new InternalError("Missing both label and id"); } else { id = label; } } Resource itemResource; try { itemResource = new URIImpl(uri); } catch (Exception e) { uri = baseURL + _encode(id); itemResource = new URIImpl(uri); } String typeID = _getStringProperty(itemNO, "type"); Resource type; if (typeID == null) { type = ExhibitOntology.ITEM; } else { type = _ensureTypeExists(typeID, baseURL, metaConnection); } dataConnection.addStatement(itemResource, RDF.TYPE, type); dataConnection.addStatement(itemResource, ExhibitOntology.ID, new LiteralImpl(id)); if (label != null) { dataConnection.addStatement(itemResource, RDFS.LABEL, new LiteralImpl(label)); } Object[] propertiesIds = ScriptableObject.getPropertyIds(itemNO); for (Object propertyId : propertiesIds) { String propertyID = propertyId.toString(); if (!propertyID.equals("type") && !propertyID.equals("label") && !propertyID.equals("uri") && !propertyID.equals("id")) { URI predicate = _ensurePropertyExists(propertyID, baseURL, metaConnection); Object valueO = ScriptableObject.getProperty(itemNO, propertyID); if (valueO instanceof NativeArray) { NativeArray valuesArray = (NativeArray) valueO; long count = valuesArray.getLength(); for (int i = 0; i < count; i++) { Object itemO = valuesArray.get(i, valuesArray); _addItemProperty( itemResource, predicate, itemO, baseURL, dataConnection, metaConnection, itemIDToURI); } } else { _addItemProperty( itemResource, predicate, valueO, baseURL, dataConnection, metaConnection, itemIDToURI); } } } return itemResource; }
@Test public void getShouldReturnNormalProperties() { array.put("p", array, "a"); assertThat((String) array.get("p", array), is("a")); }