1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/*
* Copyright (C) 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.inject;
import junit.framework.TestCase;
/**
* Tests for {@link Binder#requireAtInjectOnConstructors()}
*
* @author sameb@google.com (Sam Berlin)
*/
public class RequireAtInjectOnConstructorsTest extends TestCase {
public void testNoCxtors_explicitBinding() {
try {
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(NoCxtors.class);
binder().requireAtInjectOnConstructors();
}
});
fail();
} catch (CreationException ce) {
assertEquals(1, ce.getErrorMessages().size());
Asserts.assertContains(
ce.getMessage(),
"No injectable constructor for type RequireAtInjectOnConstructorsTest$NoCxtors.",
"at RequireAtInjectOnConstructorsTest$1.configure");
}
}
public void testNoCxtors_jitBinding() {
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
binder().requireAtInjectOnConstructors();
}
});
try {
injector.getInstance(NoCxtors.class);
fail();
} catch (ConfigurationException ce) {
Asserts.assertContains(
ce.getMessage(),
"No injectable constructor for type RequireAtInjectOnConstructorsTest$NoCxtors.",
"while locating RequireAtInjectOnConstructorsTest$NoCxtors");
}
}
public void testNoCxtors_implicitBinding() {
try {
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(Interface.class).to(NoCxtors.class);
binder().requireAtInjectOnConstructors();
}
});
fail();
} catch (CreationException ce) {
assertEquals(1, ce.getErrorMessages().size());
Asserts.assertContains(
ce.getMessage(),
"No injectable constructor for type RequireAtInjectOnConstructorsTest$NoCxtors",
"at RequireAtInjectOnConstructorsTest$3.configure");
}
}
public void testNoCxtors_inheritedByPrivateModules() {
try {
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
binder().requireAtInjectOnConstructors();
install(
new PrivateModule() {
@Override
protected void configure() {
bind(NoCxtors.class);
}
});
}
});
fail();
} catch (CreationException ce) {
assertEquals(1, ce.getErrorMessages().size());
Asserts.assertContains(
ce.getMessage(),
"No injectable constructor for type RequireAtInjectOnConstructorsTest$NoCxtors.",
"at RequireAtInjectOnConstructorsTest$4$1.configure");
}
}
public void testNoCxtors_accumulatesAllErrors() {
try {
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(NoCxtors.class);
bind(AnotherNoCxtors.class);
binder().requireAtInjectOnConstructors();
}
});
fail();
} catch (CreationException ce) {
assertEquals(2, ce.getErrorMessages().size());
Asserts.assertContains(
ce.getMessage(),
"No injectable constructor for type RequireAtInjectOnConstructorsTest$AnotherNoCxtors.",
"at RequireAtInjectOnConstructorsTest$5.configure",
"No injectable constructor for type RequireAtInjectOnConstructorsTest$NoCxtors.",
"at RequireAtInjectOnConstructorsTest$5.configure");
}
}
public void testNoCxtors_separateOptionsForPrivateModules() {
try {
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(AnotherNoCxtors.class);
install(
new PrivateModule() {
@Override
protected void configure() {
binder().requireAtInjectOnConstructors();
bind(NoCxtors.class);
}
});
}
});
fail();
} catch (CreationException ce) {
// This is testing that the parent module doesn't fail because it isn't included
// in the error message.
assertEquals(1, ce.getErrorMessages().size());
Asserts.assertContains(
ce.getMessage(),
"No injectable constructor for type RequireAtInjectOnConstructorsTest$NoCxtors.",
"Injector is configured to require @Inject constructors",
"at RequireAtInjectOnConstructorsTest$6$1.configure");
}
}
public void testManyConstructorsButNoneWithAtInject() {
try {
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(ManyConstructors.class);
binder().requireAtInjectOnConstructors();
}
});
fail();
} catch (CreationException ce) {
assertEquals(1, ce.getErrorMessages().size());
Asserts.assertContains(
ce.getMessage(),
"No injectable constructor for type RequireAtInjectOnConstructorsTest$ManyConstructors.",
"at RequireAtInjectOnConstructorsTest$7.configure");
}
}
public void testRequireAtInjectStillAllowsToConstructorBindings() {
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
try {
bind(ManyConstructors.class)
.toConstructor(ManyConstructors.class.getDeclaredConstructor());
} catch (Exception e) {
throw new RuntimeException(e);
}
binder().requireAtInjectOnConstructors();
}
});
injector.getInstance(ManyConstructors.class);
}
private static interface Interface {}
private static class NoCxtors implements Interface {}
private static class AnotherNoCxtors {}
private static class ManyConstructors {
@SuppressWarnings("unused")
ManyConstructors() {}
@SuppressWarnings("unused")
ManyConstructors(String a) {}
@SuppressWarnings("unused")
ManyConstructors(int a) {}
}
}
|