public void testResume() throws Exception { assertTrue(fromSuite()); testSuspend(); assertTrue("suspend must succeed to create the output first", output != null); assertEquals(output.length(), 562); final Object o = loadJDK(output); assertTrue(o instanceof Continuation); final Continuation c1 = (Continuation) o; final StackRecorder sr1 = (StackRecorder) PrivateAccessor.getField(c1, "stackRecorder"); final Runnable r1 = (Runnable) PrivateAccessor.getField(sr1, "runnable"); assertEquals(SimpleSerializable.class.getName(), r1.getClass().getName()); final SimpleSerializable ss1 = (SimpleSerializable) r1; assertTrue(ss1.g == 0); assertTrue(ss1.l == 0); final Continuation c2 = Continuation.continueWith(c1); final StackRecorder sr2 = (StackRecorder) PrivateAccessor.getField(c2, "stackRecorder"); final Runnable r2 = (Runnable) PrivateAccessor.getField(sr2, "runnable"); assertEquals(SimpleSerializable.class.getName(), r2.getClass().getName()); final SimpleSerializable ss2 = (SimpleSerializable) r2; assertTrue(ss2.g == 1); assertTrue(ss2.l == 1); assertTrue(r1 == r2); }
/** Test of resolveChannelName method from {@link MessageRecipient} */ public void testResolveChannelName() throws Exception { MessageRecipient messageRecipient = new MessageRecipient(); // This is necessary to remove the need to create the entire Volantis // bean _just_ to get two Strings out... PrivateAccessor.setField( messageRecipient, "messageRecipientInfo", new MessageRecipientInfoTestHelper()); messageRecipient.resolveChannelName(false); assertEquals( "Resolved channel not equal to default", "smtp", messageRecipient.getChannelName()); messageRecipient.setChannelName("aardvark"); messageRecipient.resolveChannelName(false); assertEquals( "Resolved channel overrode current value", "aardvark", messageRecipient.getChannelName()); messageRecipient.resolveChannelName(true); assertEquals( "Resolved channel did not override current value", "smtp", messageRecipient.getChannelName()); }
/** * Helper method. * * @param filter the content type. * @return true if the filter is in the container, false otherwise. */ private boolean isFilterInContainer(ScriptFilter filter) throws Exception { if (filter != null) { Map map = (Map) PrivateAccessor.getField(module, "scriptFilters"); if (map != null) { return map.containsKey(filter.getContentType()); } } return false; }
@Before public void setup() throws Exception { SlingSettingsService settings = mock(SlingSettingsService.class); slingId = UUID.randomUUID().toString(); when(settings.getSlingId()).thenReturn(slingId); hello = new HelloWorldModel(); PrivateAccessor.setField(hello, "settings", settings); hello.init(); }
@Before public void setup() throws Exception { resource = context.create().resource("app/content/any/resource"); context.addModelsForPackage("com.thoughtworks.www.samples.models"); String slingId = UUID.randomUUID().toString(); when(settings.getSlingId()).thenReturn(slingId); buttonModel = context.currentResource(resource).adaptTo(ButtonModel.class); PrivateAccessor.setField(buttonModel, "settings", settings); }
public void testFormatExtList_emptyExt() throws Exception, Throwable { System.out.println("formatExtList"); ArrayList exts = new ArrayList(); String formattedExtList = (String) PrivateAccessor.invoke( Sync4jServlet.class, "formatExtListAsProperties", new Class[] {ArrayList.class}, new Object[] {exts}); assertEquals("", formattedExtList); }
public void testFormatExtList_twoExt() throws Exception, Throwable { System.out.println("formatExtList"); ArrayList exts = new ArrayList(); exts.add(new Ext("X-funambol-smartslow", new String[0])); exts.add(new Ext("X-foo", new String[] {"val1", "val2", "val3"})); String formattedExtList = (String) PrivateAccessor.invoke( Sync4jServlet.class, "formatExtListAsProperties", new Class[] {ArrayList.class}, new Object[] {exts}); assertEquals("X-funambol-smartslow,X-foo[val1,val2,val3]", formattedExtList); }
public void testFormatExtList_oneExtWithOnlyName() throws Exception, Throwable { System.out.println("formatExtList"); Ext ext = new Ext("X-funambol-smartslow", new String[0]); ArrayList exts = new ArrayList(); exts.add(ext); String formattedExtList = (String) PrivateAccessor.invoke( Sync4jServlet.class, "formatExtListAsProperties", new Class[] {ArrayList.class}, new Object[] {exts}); assertEquals("X-funambol-smartslow", formattedExtList); }
public void testFormatExtList_replaceChar() throws Exception, Throwable { System.out.println("formatExtList"); ArrayList exts = new ArrayList(); exts.add(new Ext("X-funambol-smartslow", new String[0])); exts.add(new Ext("X-fo,o", new String[] {"val1[pippo]", "val,2", "val3[pip,po]"})); exts.add(new Ext("X-pippo[pippo]", new String[] {"val[pippo]"})); String formattedExtList = (String) PrivateAccessor.invoke( Sync4jServlet.class, "formatExtListAsProperties", new Class[] {ArrayList.class}, new Object[] {exts}); assertEquals( "X-funambol-smartslow,X-fo\\,o[val1\\[pippo\\],val\\,2,val3\\[pip\\,po\\]],X-pippo\\[pippo\\][val\\[pippo\\]]", formattedExtList); }
@SuppressWarnings("unchecked") private Map<String, RequestProperty> collectContent(Param... kvs) throws Throwable { final List<Map.Entry<String, RequestParameter>> params = new ArrayList<Map.Entry<String, RequestParameter>>(); for (int i = 0; i < kvs.length; i++) { final Param kv = kvs[i]; final RequestParameter[] param = new RequestParameter[kv.value.length]; for (int j = 0; j < kv.value.length; j++) { final String strValue = kv.value[j]; final RequestParameter aparam = context.mock(RequestParameter.class, "requestParameter" + i + "#" + j); context.checking( new Expectations() { { allowing(aparam).getString(); will(returnValue(strValue)); } }); param[j] = aparam; } final Map.Entry<String, RequestParameter> entry = context.mock(Map.Entry.class, "entry" + i); context.checking( new Expectations() { { allowing(entry).getKey(); will(returnValue(kv.key)); allowing(entry).getValue(); will(returnValue(param)); } }); params.add(entry); } final Set set = context.mock(Set.class); context.checking( new Expectations() { { one(set).iterator(); will(returnValue(params.iterator())); } }); final RequestParameterMap map = context.mock(RequestParameterMap.class); context.checking( new Expectations() { { one(map).entrySet(); will(returnValue(set)); } }); final SlingHttpServletRequest request = context.mock(SlingHttpServletRequest.class); context.checking( new Expectations() { { Vector names = new Vector(); names.add("./param"); one(request).getParameterNames(); will(returnValue(names.elements())); one(request).getRequestParameterMap(); will(returnValue(map)); } }); final HtmlResponse response = new HtmlResponse(); response.setPath("/test/path"); Map<String, RequestProperty> props = (Map<String, RequestProperty>) PrivateAccessor.invoke( new ModifyOperation(), "collectContent", COLLECT_CLASSES, new Object[] {request, response}); return props; }
/** * Unmarshall a Chromosome instance from a given XML Element representation. * * @param a_activeConfiguration current Configuration object * @param a_xmlElement the XML Element representation of the Chromosome * @return a new Chromosome instance setup with the data from the XML Element representation * @throws ImproperXMLException if the given Element is improperly structured or missing data * @throws UnsupportedRepresentationException if the actively configured Gene implementation does * not support the string representation of the alleles used in the given XML document * @throws GeneCreationException if there is a problem creating or populating a Gene instance * @author Neil Rotstan * @since 1.0 */ public static Gene[] getGenesFromElement( Configuration a_activeConfiguration, Element a_xmlElement) throws ImproperXMLException, UnsupportedRepresentationException, GeneCreationException { // Do some sanity checking. Make sure the XML Element isn't null and // that it in fact represents a set of genes. // ----------------------------------------------------------------- if (a_xmlElement == null || !(a_xmlElement.getTagName().equals(GENES_TAG))) { throw new ImproperXMLException( "Unable to build Chromosome instance from XML Element: " + "given Element is not a 'genes' element."); } List genes = Collections.synchronizedList(new ArrayList()); // Extract the nested gene elements. // --------------------------------- NodeList geneElements = a_xmlElement.getElementsByTagName(GENE_TAG); if (geneElements == null) { throw new ImproperXMLException( "Unable to build Gene instances from XML Element: " + "'" + GENE_TAG + "'" + " sub-elements not found."); } // For each gene, get the class attribute so we know what class // to instantiate to represent the gene instance, and then find // the child text node, which is where the string representation // of the allele is located, and extract the representation. // ------------------------------------------------------------- int numberOfGeneNodes = geneElements.getLength(); for (int i = 0; i < numberOfGeneNodes; i++) { Element thisGeneElement = (Element) geneElements.item(i); thisGeneElement.normalize(); // Fetch the class attribute and create an instance of that // class to represent the current gene. // -------------------------------------------------------- String geneClassName = thisGeneElement.getAttribute(CLASS_ATTRIBUTE); Gene thisGeneObject; Class geneClass = null; try { geneClass = Class.forName(geneClassName); try { Constructor constr = geneClass.getConstructor(new Class[] {Configuration.class}); thisGeneObject = (Gene) constr.newInstance(new Object[] {a_activeConfiguration}); } catch (NoSuchMethodException nsme) { // Try it by calling method newGeneInternal. // ----------------------------------------- Constructor constr = geneClass.getConstructor(new Class[] {}); thisGeneObject = (Gene) constr.newInstance(new Object[] {}); thisGeneObject = (Gene) PrivateAccessor.invoke( thisGeneObject, "newGeneInternal", new Class[] {}, new Object[] {}); } } catch (Throwable e) { throw new GeneCreationException(geneClass, e); } // Find the text node and fetch the string representation of // the allele. // --------------------------------------------------------- NodeList children = thisGeneElement.getChildNodes(); int childrenSize = children.getLength(); String alleleRepresentation = null; for (int j = 0; j < childrenSize; j++) { Element alleleElem = (Element) children.item(j); if (alleleElem.getTagName().equals(ALLELE_TAG)) { alleleRepresentation = alleleElem.getAttribute("value"); } if (children.item(j).getNodeType() == Node.TEXT_NODE) { // We found the text node. Extract the representation. // --------------------------------------------------- alleleRepresentation = children.item(j).getNodeValue(); break; } } // Sanity check: Make sure the representation isn't null. // ------------------------------------------------------ if (alleleRepresentation == null) { throw new ImproperXMLException( "Unable to build Gene instance from XML Element: " + "value (allele) is missing representation."); } // Now set the value of the gene to that reflect the // string representation. // ------------------------------------------------- try { thisGeneObject.setValueFromPersistentRepresentation(alleleRepresentation); } catch (UnsupportedOperationException e) { throw new GeneCreationException( "Unable to build Gene because it does not support the " + "setValueFromPersistentRepresentation() method."); } // Finally, add the current gene object to the list of genes. // ---------------------------------------------------------- genes.add(thisGeneObject); } return (Gene[]) genes.toArray(new Gene[genes.size()]); }