public void testExceptionHandling() { final ComponentAdapter componentAdapter = new ThreadLocalizing.ThreadLocalized( new ConstructorInjection.ConstructorInjector( TargetInvocationExceptionTester.class, ThrowingComponent.class, null)); final TargetInvocationExceptionTester tester = (TargetInvocationExceptionTester) componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class); try { tester.throwsCheckedException(); fail("ClassNotFoundException expected"); } catch (final ClassNotFoundException e) { assertEquals("junit", e.getMessage()); } try { tester.throwsRuntimeException(); fail("RuntimeException expected"); } catch (final RuntimeException e) { assertEquals("junit", e.getMessage()); } try { tester.throwsError(); fail("Error expected"); } catch (final Error e) { assertEquals("junit", e.getMessage()); } }
/** * Test usage from multiple threads. * * @throws InterruptedException if interrupted */ public final void testInstancesUsedFromMultipleThreads() throws InterruptedException { final Set<Touchable> set = Collections.synchronizedSet(new HashSet<Touchable>()); final List<Touchable> list = Collections.synchronizedList(new ArrayList<Touchable>()); final ComponentAdapter componentAdapter = new ThreadLocalizing.ThreadLocalized( new ConstructorInjection.ConstructorInjector( Touchable.class, SimpleTouchable.class, null)); final Touchable touchable = (Touchable) componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class); final Thread[] threads = { new Thread(new Runner(touchable, list, set), "junit-1"), new Thread(new Runner(touchable, list, set), "junit-2"), new Thread(new Runner(touchable, list, set), "junit-3"), }; for (int i = threads.length; i-- > 0; ) { threads[i].start(); } Thread.sleep(300); for (int i = threads.length; i-- > 0; ) { synchronized (threads[i]) { threads[i].notifyAll(); } } Thread.sleep(300); for (int i = threads.length; i-- > 0; ) { threads[i].interrupt(); } Thread.sleep(300); assertEquals(6, list.size()); assertEquals(3, set.size()); }
/** Test ComponentAdapter using simple keys. */ public final void testSimpleKeys() { final ComponentAdapter componentAdapter = new ThreadLocalizing.ThreadLocalized<ArrayList>( new ConstructorInjection.ConstructorInjector("List", ArrayList.class, null)); final List hello = (List) componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class); assertNotNull(hello); }
public void testThreadLocalInstancesEqual() throws Exception { final ComponentAdapter componentAdapter = new ThreadLocalizing.ThreadLocalized( new ConstructorInjection.ConstructorInjector( Touchable.class, SimpleTouchable.class, null)); final Touchable touchable = (Touchable) componentAdapter.getComponentInstance(null, ComponentAdapter.NOTHING.class); assertEquals(touchable, touchable); }
/** * Test the value. This is called only if the pre-check returned true, because accessing the value * might be potentially costly * * @param renderer * @param adapter * @return */ private boolean test(Component renderer, ComponentAdapter adapter) { if (!adapter.isTestable(testColumn)) return false; String value = adapter.getString(testColumn); if ((value == null) || (value.length() == 0)) { return false; } return pattern.matcher(value).find(); // this is pre-767-swingx: consistent string api // Object value = adapter.getValue(testColumn); // // if (value == null) { // return false; // } else { // return pattern.matcher(value.toString()).find(); // } }
/** * Returns the number of records in the filtered view. * * @return the number of records in the filtered view */ public int getOutputSize() { // JW: don't need to check - but that's heavily dependent on the // implementation detail that there's always the identityFilter // (which might change any time) if (!isAssigned()) return 0; Filter last = last(); return (last == null) ? adapter.getRowCount() : last.getSize(); }
@Override protected void applyBackground(Component renderer, ComponentAdapter adapter) { if (!adapter.isSelected()) { Object colorMemory = ((JComponent) renderer).getClientProperty("rendererColorMemory.background"); if (colorMemory instanceof ColorMemory) { renderer.setBackground(((ColorMemory) colorMemory).color); } else { ((JComponent) renderer) .putClientProperty( "rendererColorMemory.background", new ColorMemory(renderer.getBackground())); } } }
/** * A quick pre-check. * * @param renderer * @param adapter * @return */ private boolean isHighlightCandidate(Component renderer, ComponentAdapter adapter) { return (pattern != null) && ((highlightColumn < 0) || (highlightColumn == adapter.viewToModel(adapter.column))); }
/** * returns the unfiltered data adapter size or 0 if unassigned. * * @return the unfiltered data adapter size or 0 if unassigned */ public int getInputSize() { return isAssigned() ? adapter.getRowCount() : 0; }
/** * Applies a suitable foreground for the renderer component within the specified adapter. * * <p>This implementation applies its foreground or selectedfForeground color (depending on the * adapter's selected state) if != null. Otherwise it does nothing. * * @param renderer the cell renderer component that is to be decorated * @param adapter the ComponentAdapter for this decorate operation */ protected void applyForeground(Component renderer, ComponentAdapter adapter) { Color color = adapter.isSelected() ? getSelectedForeground() : getForeground(); if (color != null) { renderer.setForeground(color); } }
protected void applyForeground(Component renderer, ComponentAdapter adapter) { if (!adapter.isSelected()) { renderer.setForeground(null); } }