Source Code Cross Referenced for ConstantConversionTest.java in  » Inversion-of-Control » guice-1.0 » com » google » inject » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Inversion of Control » guice 1.0 » com.google.inject 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (C) 2006 Google Inc.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         * http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */package com.google.inject;
016:
017:        import java.lang.annotation.Retention;
018:        import static java.lang.annotation.RetentionPolicy.RUNTIME;
019:        import junit.framework.TestCase;
020:
021:        /**
022:         * @author crazybob@google.com (Bob Lee)
023:         */
024:        public class ConstantConversionTest extends TestCase {
025:
026:            @Retention(RUNTIME)
027:            @BindingAnnotation
028:            @interface NumericValue {
029:            }
030:
031:            @Retention(RUNTIME)
032:            @BindingAnnotation
033:            @interface BooleanValue {
034:            }
035:
036:            @Retention(RUNTIME)
037:            @BindingAnnotation
038:            @interface EnumValue {
039:            }
040:
041:            @Retention(RUNTIME)
042:            @BindingAnnotation
043:            @interface ClassName {
044:            }
045:
046:            public static class Foo {
047:                @Inject
048:                @NumericValue
049:                Integer integerField;
050:                @Inject
051:                @NumericValue
052:                int primitiveIntField;
053:                @Inject
054:                @NumericValue
055:                Long longField;
056:                @Inject
057:                @NumericValue
058:                long primitiveLongField;
059:                @Inject
060:                @BooleanValue
061:                Boolean booleanField;
062:                @Inject
063:                @BooleanValue
064:                boolean primitiveBooleanField;
065:                @Inject
066:                @NumericValue
067:                Byte byteField;
068:                @Inject
069:                @NumericValue
070:                byte primitiveByteField;
071:                @Inject
072:                @NumericValue
073:                Short shortField;
074:                @Inject
075:                @NumericValue
076:                short primitiveShortField;
077:                @Inject
078:                @NumericValue
079:                Float floatField;
080:                @Inject
081:                @NumericValue
082:                float primitiveFloatField;
083:                @Inject
084:                @NumericValue
085:                Double doubleField;
086:                @Inject
087:                @NumericValue
088:                short primitiveDoubleField;
089:                @Inject
090:                @EnumValue
091:                Bar enumField;
092:                @Inject
093:                @ClassName
094:                Class<?> classField;
095:            }
096:
097:            public enum Bar {
098:                TEE, BAZ, BOB;
099:            }
100:
101:            public void testOneConstantInjection() throws CreationException {
102:                BinderImpl builder = new BinderImpl();
103:                builder.bindConstant().annotatedWith(NumericValue.class)
104:                        .to("5");
105:                builder.bind(Simple.class);
106:                Injector injector = builder.createInjector();
107:                Simple simple = injector.getInstance(Simple.class);
108:                assertEquals(5, simple.i);
109:            }
110:
111:            static class Simple {
112:                @Inject
113:                @NumericValue
114:                int i;
115:            }
116:
117:            public void testConstantInjection() throws CreationException {
118:                BinderImpl b = new BinderImpl();
119:                b.bindConstant().annotatedWith(NumericValue.class).to("5");
120:                b.bindConstant().annotatedWith(BooleanValue.class).to("true");
121:                b.bindConstant().annotatedWith(EnumValue.class).to("TEE");
122:                b.bindConstant().annotatedWith(ClassName.class).to(
123:                        Foo.class.getName());
124:                Injector c = b.createInjector();
125:                Foo foo = c.getInstance(Foo.class);
126:
127:                checkNumbers(foo.integerField, foo.primitiveIntField,
128:                        foo.longField, foo.primitiveLongField, foo.byteField,
129:                        foo.primitiveByteField, foo.shortField,
130:                        foo.primitiveShortField, foo.floatField,
131:                        foo.primitiveFloatField, foo.doubleField,
132:                        foo.primitiveDoubleField);
133:
134:                assertEquals(Bar.TEE, foo.enumField);
135:                assertEquals(Foo.class, foo.classField);
136:            }
137:
138:            void checkNumbers(Number... ns) {
139:                for (Number n : ns) {
140:                    assertEquals(5, n.intValue());
141:                }
142:            }
143:
144:            public void testInvalidInteger() throws CreationException {
145:                BinderImpl b = new BinderImpl();
146:                b.bindConstant().annotatedWith(NumericValue.class)
147:                        .to("invalid");
148:                Injector c = b.createInjector();
149:                try {
150:                    c.getInstance(InvalidInteger.class);
151:                    fail();
152:                } catch (ConfigurationException e) { /* expected */
153:                }
154:            }
155:
156:            public static class InvalidInteger {
157:                @Inject
158:                @NumericValue
159:                Integer integerField;
160:            }
161:
162:            public void testInvalidCharacter() throws CreationException {
163:                BinderImpl b = new BinderImpl();
164:                b.bindConstant().annotatedWith(NumericValue.class)
165:                        .to("invalid");
166:                Injector c = b.createInjector();
167:                try {
168:                    c.getInstance(InvalidCharacter.class);
169:                    fail();
170:                } catch (ConfigurationException e) { /* expected */
171:                }
172:            }
173:
174:            public static class InvalidCharacter {
175:                @Inject
176:                @NumericValue
177:                char foo;
178:            }
179:
180:            public void testInvalidEnum() throws CreationException {
181:                BinderImpl b = new BinderImpl();
182:                b.bindConstant().annotatedWith(NumericValue.class)
183:                        .to("invalid");
184:                Injector c = b.createInjector();
185:                try {
186:                    c.getInstance(InvalidEnum.class);
187:                    fail();
188:                } catch (ConfigurationException e) { /* expected */
189:                }
190:            }
191:
192:            public static class InvalidEnum {
193:                @Inject
194:                @NumericValue
195:                Bar foo;
196:            }
197:
198:            public void testToInstanceIsTreatedLikeConstant()
199:                    throws CreationException {
200:                BinderImpl b = new BinderImpl();
201:                b.bind(String.class).toInstance("5");
202:                b.bind(LongHolder.class);
203:                Injector c = b.createInjector();
204:            }
205:
206:            static class LongHolder {
207:                @Inject
208:                LongHolder(long foo) {
209:                }
210:            }
211:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.