// inner class or normal class ... change scope if needed private class DragListener { int color = 0; public DragListener(Context context) { color = context.getResource().getColor(R.color.yellow); } } // activity private DragListener mDragListener; public void onCreate(...) { mDragListener = new DragListener(this); // more code }
public String getContentType(Resource resource) { String extension = TapestryInternalUtils.toFileSuffix(resource.getFile()); String contentType = configuration.get(extension); if (contentType != null) return contentType; contentType = context.getMimeType(resource.getFile()); if (contentType != null) return contentType; return "application/octet-stream"; }
File file = context.getRealFile(contextPath); url = context.getResource(contextPath);
private void context(MarkupWriter writer) { List<String> attributeNames = context.getAttributeNames(); if (attributeNames.isEmpty()) return; section(writer, "Context Attributes"); writer.element("dl"); for (String name : attributeNames) { dt(writer, name); writer.element("dd"); masterObjectRenderer.render(context.getAttribute(name), writer); writer.end(); // dd } writer.end(); // dl }
public String getStatus() { return upcase.toUpperCase(String.format("Spring version %s: %s", SpringVersion.getVersion(), context.getAttribute("status-message"))); } }
@Test public void get_url_file_exists() throws Exception { File f = File.createTempFile("Bar", ".txt"); String path = "/foo/" + f.getName(); Context context = mockContext(); expect(context.getRealFile(path)).andReturn(f); replay(); Resource r = new ContextResource(context, "foo/" + f.getName()); assertEquals(r.toURL(), f.toURL()); verify(); }
@Test public void get_url_no_real_file() throws Exception { String path = "/foo/ContextResourceTest.class"; URL url = getClass().getResource("ContextResourceTest.class"); Context context = mockContext(); expect(context.getRealFile(path)).andReturn(null); expect(context.getResource("/foo/ContextResourceTest.class")).andReturn(url); replay(); Resource r = new ContextResource(context, "foo/ContextResourceTest.class"); assertSame(r.toURL(), url); verify(); }
@Test public void get_attribute() { String name = "foo"; Object value = new Object(); ServletContext servletContext = newServletContext(); expect(servletContext.getAttribute(name)).andReturn(value); replay(); Context context = new ContextImpl(servletContext); assertSame(context.getAttribute(name), value); verify(); }
@Test public void get_real_file_exists() throws IOException { String path = "/foo.gif"; File file = File.createTempFile("foo", "gif"); String realPath = file.getPath(); ServletContext servletContext = newServletContext(); train_getRealPath(servletContext, path, realPath); replay(); Context c = new ContextImpl(servletContext); File f = c.getRealFile(path); assertEquals(f, file); verify(); }
URL url = context.getResource(path);
@Test public void get_real_file_missing() { String path = "/foo.gif"; ServletContext servletContext = newServletContext(); train_getRealPath(servletContext, path, null); replay(); Context c = new ContextImpl(servletContext); assertNull(c.getRealFile(path)); verify(); }
protected final void train_getResource(Context context, String path, URL url) { expect(context.getResource(path)).andReturn(url); }
@Test public void access() throws Exception { File f = File.createTempFile("foo", ".properties"); setupFile(f); Context context = mockContext(); expect(context.getRealFile("/bar/" + f.getName())).andReturn(f); replay(); ContextResourceSymbolProvider provider = new ContextResourceSymbolProvider(context, "bar/" + f.getName()); /* test general access */ assertEquals(provider.valueForSymbol("homer"), "simpson"); assertEquals(provider.valueForSymbol("monty"), "burns"); /* check for case-insensitivity */ assertEquals(provider.valueForSymbol("HOMER"), "simpson"); /* non-existent keys should return null */ assertNull(provider.valueForSymbol("marge")); verify(); f.delete(); }