| 1 | /* |
|---|
| 2 | * Created on Aug 2, 2005 |
|---|
| 3 | * |
|---|
| 4 | * $Log$ |
|---|
| 5 | * Revision 1.2 2005/09/21 22:00:46 dsowen |
|---|
| 6 | * Split out the access stuff into accesslib. |
|---|
| 7 | * New Creator interface off-loads object creation to user. |
|---|
| 8 | * |
|---|
| 9 | * Revision 1.1 2005/08/19 17:51:17 dsowen |
|---|
| 10 | * Fixed: NPEs when using accessors from paths, &c. Included tests. |
|---|
| 11 | * Fixed: parsing exception wasn't very enlightening. |
|---|
| 12 | * |
|---|
| 13 | * Revision 1.2 2005/08/12 19:01:46 dsowen |
|---|
| 14 | * Feature: can automatically fill missing parts of a graph. |
|---|
| 15 | * |
|---|
| 16 | * Revision 1.1 2005/08/03 00:35:29 dsowen |
|---|
| 17 | * Initial commit. |
|---|
| 18 | * |
|---|
| 19 | */ |
|---|
| 20 | package ws.fugue88; |
|---|
| 21 | |
|---|
| 22 | import java.util.HashMap; |
|---|
| 23 | |
|---|
| 24 | import junit.framework.TestCase; |
|---|
| 25 | import ws.fugue88.access.Accessor; |
|---|
| 26 | import ws.fugue88.jpath.Context; |
|---|
| 27 | import ws.fugue88.jpath.ContextFactory; |
|---|
| 28 | import ws.fugue88.jpath.GraphCreator; |
|---|
| 29 | import ws.fugue88.jpath.SimplePathParser; |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * @author dsowen |
|---|
| 33 | */ |
|---|
| 34 | public class ContextTest extends TestCase { |
|---|
| 35 | |
|---|
| 36 | public void testFieldIdentifier() throws Exception |
|---|
| 37 | { |
|---|
| 38 | class A { |
|---|
| 39 | |
|---|
| 40 | String b = "test"; |
|---|
| 41 | } |
|---|
| 42 | A a = new A(); |
|---|
| 43 | |
|---|
| 44 | ContextFactory factory = new ContextFactory(); |
|---|
| 45 | Context context = factory.createContext(a, GraphCreator.NULL); |
|---|
| 46 | assertSame(a.b, context.getTarget(new SimplePathParser().parse("/b"))); |
|---|
| 47 | |
|---|
| 48 | Accessor accessor = context.getTargetAccessor(new SimplePathParser().parse("/b")); |
|---|
| 49 | assertSame(a.b, accessor.getValue()); |
|---|
| 50 | accessor.setValue("done"); |
|---|
| 51 | assertEquals("done", accessor.getValue()); |
|---|
| 52 | assertEquals("done", a.b); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public void testGetterIdentifier() throws Exception |
|---|
| 56 | { |
|---|
| 57 | class A { |
|---|
| 58 | |
|---|
| 59 | String getB() |
|---|
| 60 | { |
|---|
| 61 | return "test"; |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | A a = new A(); |
|---|
| 65 | |
|---|
| 66 | ContextFactory factory = new ContextFactory(); |
|---|
| 67 | Context context = factory.createContext(a, GraphCreator.NULL); |
|---|
| 68 | assertEquals("test", |
|---|
| 69 | context.getTarget(new SimplePathParser().parse("/b"))); |
|---|
| 70 | try { |
|---|
| 71 | context.getTargetAccessor(new SimplePathParser().parse("/b")).setValue( |
|---|
| 72 | "done"); |
|---|
| 73 | assertTrue(false); |
|---|
| 74 | } catch(UnsupportedOperationException e) {} |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | public void testGetterSetterIdentifier() throws Exception |
|---|
| 78 | { |
|---|
| 79 | class A { |
|---|
| 80 | |
|---|
| 81 | String getB() |
|---|
| 82 | { |
|---|
| 83 | return x; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void setB(String s) |
|---|
| 87 | { |
|---|
| 88 | x = s; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | String x = "test"; |
|---|
| 92 | } |
|---|
| 93 | A a = new A(); |
|---|
| 94 | |
|---|
| 95 | ContextFactory factory = new ContextFactory(); |
|---|
| 96 | Context context = factory.createContext(a, GraphCreator.NULL); |
|---|
| 97 | assertEquals("test", |
|---|
| 98 | context.getTarget(new SimplePathParser().parse("/b"))); |
|---|
| 99 | context.getTargetAccessor(new SimplePathParser().parse("/b")).setValue( |
|---|
| 100 | "done"); |
|---|
| 101 | assertEquals("done", a.getB()); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | public void testKey() throws Exception |
|---|
| 105 | { |
|---|
| 106 | class A { |
|---|
| 107 | |
|---|
| 108 | HashMap b = new HashMap(); |
|---|
| 109 | |
|---|
| 110 | A() |
|---|
| 111 | { |
|---|
| 112 | b.put("c", "test"); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | A a = new A(); |
|---|
| 116 | |
|---|
| 117 | ContextFactory factory = new ContextFactory(); |
|---|
| 118 | Context context = factory.createContext(a, GraphCreator.NULL); |
|---|
| 119 | assertEquals("test", |
|---|
| 120 | context.getTarget(new SimplePathParser().parse("/b['c']"))); |
|---|
| 121 | context.getTargetAccessor(new SimplePathParser().parse("/b['c']")).setValue( |
|---|
| 122 | "done"); |
|---|
| 123 | assertEquals("done", a.b.get("c")); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | public void testEarlyNull() throws Exception |
|---|
| 127 | { |
|---|
| 128 | class A { |
|---|
| 129 | |
|---|
| 130 | String s; |
|---|
| 131 | } |
|---|
| 132 | class B { |
|---|
| 133 | |
|---|
| 134 | A a; |
|---|
| 135 | } |
|---|
| 136 | B b = new B(); |
|---|
| 137 | |
|---|
| 138 | ContextFactory factory = new ContextFactory(); |
|---|
| 139 | Context context = factory.createContext(b, GraphCreator.NULL); |
|---|
| 140 | assertNull(context.getTarget(new SimplePathParser().parse("/a/s"))); |
|---|
| 141 | assertNull(b.a); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | static class A { |
|---|
| 145 | |
|---|
| 146 | String s; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | public void testAutoGraphTargets() throws Exception |
|---|
| 150 | { |
|---|
| 151 | class B { |
|---|
| 152 | |
|---|
| 153 | A a; |
|---|
| 154 | } |
|---|
| 155 | B b = new B(); |
|---|
| 156 | |
|---|
| 157 | ContextFactory factory = new ContextFactory(); |
|---|
| 158 | Context context = factory.createContext(b, GraphCreator.DEFAULT); |
|---|
| 159 | assertNull(context.getTarget(new SimplePathParser().parse("/a/s"))); |
|---|
| 160 | assertNotNull(b.a); |
|---|
| 161 | assertNull(b.a.s); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | public void testAutoGraphAccessors() throws Exception |
|---|
| 165 | { |
|---|
| 166 | class B { |
|---|
| 167 | |
|---|
| 168 | A a; |
|---|
| 169 | } |
|---|
| 170 | B b = new B(); |
|---|
| 171 | |
|---|
| 172 | ContextFactory factory = new ContextFactory(); |
|---|
| 173 | Context context = factory.createContext(b, GraphCreator.DEFAULT); |
|---|
| 174 | Accessor accessor = context.getTargetAccessor(new SimplePathParser().parse("/a/s")); |
|---|
| 175 | assertNotNull(b.a); |
|---|
| 176 | assertNull(b.a.s); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | private static class PrivateClass { |
|---|
| 180 | |
|---|
| 181 | String s; |
|---|
| 182 | |
|---|
| 183 | private PrivateClass() |
|---|
| 184 | { |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | public void testConstructPrivate() throws Exception |
|---|
| 189 | { |
|---|
| 190 | class B { |
|---|
| 191 | |
|---|
| 192 | PrivateClass x; |
|---|
| 193 | } |
|---|
| 194 | B b = new B(); |
|---|
| 195 | |
|---|
| 196 | ContextFactory factory = new ContextFactory(); |
|---|
| 197 | Context context = factory.createContext(b, GraphCreator.DEFAULT); |
|---|
| 198 | assertNull(context.getTarget(new SimplePathParser().parse("/x/s"))); |
|---|
| 199 | assertNotNull(b.x); |
|---|
| 200 | assertNull(b.x.s); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|