├── README.md ├── github-repo.xml ├── pom.xml ├── release.sh ├── release └── org │ └── springframework │ └── spring-reinject │ ├── 0.1 │ ├── spring-reinject-0.1-sources.jar │ ├── spring-reinject-0.1-sources.jar.md5 │ ├── spring-reinject-0.1-sources.jar.sha1 │ ├── spring-reinject-0.1.jar │ ├── spring-reinject-0.1.jar.md5 │ ├── spring-reinject-0.1.jar.sha1 │ ├── spring-reinject-0.1.pom │ ├── spring-reinject-0.1.pom.md5 │ └── spring-reinject-0.1.pom.sha1 │ ├── 0.2 │ ├── spring-reinject-0.2-sources.jar │ ├── spring-reinject-0.2-sources.jar.md5 │ ├── spring-reinject-0.2-sources.jar.sha1 │ ├── spring-reinject-0.2.jar │ ├── spring-reinject-0.2.jar.md5 │ ├── spring-reinject-0.2.jar.sha1 │ ├── spring-reinject-0.2.pom │ ├── spring-reinject-0.2.pom.md5 │ └── spring-reinject-0.2.pom.sha1 │ ├── 0.3 │ ├── spring-reinject-0.3-sources.jar │ ├── spring-reinject-0.3-sources.jar.md5 │ ├── spring-reinject-0.3-sources.jar.sha1 │ ├── spring-reinject-0.3.jar │ ├── spring-reinject-0.3.jar.md5 │ ├── spring-reinject-0.3.jar.sha1 │ ├── spring-reinject-0.3.pom │ ├── spring-reinject-0.3.pom.md5 │ └── spring-reinject-0.3.pom.sha1 │ ├── maven-metadata.xml │ ├── maven-metadata.xml.md5 │ └── maven-metadata.xml.sha1 ├── src ├── main │ └── java │ │ └── org │ │ └── springframework │ │ └── reinject │ │ ├── ReInjectContext.java │ │ ├── ReInjectFactoryBean.java │ │ └── ReInjectPostProcessor.java └── test │ ├── java │ └── org │ │ └── springframework │ │ └── reinject │ │ ├── ComponentScanTest.java │ │ ├── InjectIntoMockTest.java │ │ ├── InjectListTest.java │ │ ├── InjectMapTest.java │ │ ├── InjectObjectTest.java │ │ ├── JavaConfigTest.java │ │ ├── Printer.java │ │ ├── Service.java │ │ ├── ServiceImpl.java │ │ ├── ServiceMock.java │ │ └── XmlConfigTest.java │ └── resources │ └── org │ └── springframework │ └── reinject │ ├── InjectListTest-context.xml │ ├── InjectMapTest-context.xml │ ├── app-context.xml │ └── reinject-context.xml └── test-github-repo.sh /README.md: -------------------------------------------------------------------------------- 1 | Re-inject 2 | ======== 3 | 4 | Inject mocks into Spring application context easily 5 | 6 | Licensed under [Apache 2 License](http://www.apache.org/licenses/LICENSE-2.0.html) 7 | 8 | ## The goal 9 | The project's goal is to make it possible to override Spring bean definitions with mocks when executing tests without modification of Spring context files or Java-based configuration classes. 10 | 11 | Spring Re-inject is not designed for contexts when all objects are mocks. It is not designed to be used in production context, too. It is designed to make it possible to override specific beans programatically without forcing you to change configuration files (or Java-based configuration classes) every time you need to change a bean's implementation in test environment. 12 | 13 | ## How it works 14 | You add a special bean factory post-processor to your application context. Then you register your mocks in your test's constructor. Then Spring constructs an application context for your test case, and the post-processor replaces bean definitions with new bean definitions which refer to mocks. Original beans are never created. 15 | 16 | Your mock becomes a Spring bean, it means that it can have auto-wired fields like a normal bean and is injected everywhere a normal bean is injected! 17 | 18 | 19 | ## Add a Maven dependency 20 | ``` 21 | 22 | 23 | reinject 24 | reinject 25 | https://github.com/sgri/spring-reinject/raw/master/release 26 | default 27 | 28 | true 29 | 30 | 31 | always 32 | 33 | 34 | 35 | 36 | 37 | org.springframework 38 | spring-reinject 39 | 0.3 40 | test 41 | 42 | 43 | org.springframework 44 | spring-core 45 | 46 | 47 | org.springframework 48 | spring-context 49 | 50 | 51 | 52 | 53 | ``` 54 | ## Add `ReInjectPostProcessor` to your Application context 55 | ### For XML files 56 | _reinject-context.xml_ 57 | ``` 58 | 59 | 63 | 64 | 65 | 66 | ``` 67 | 68 | And your JUnit test may look like: 69 | ``` 70 | @RunWith(SpringJUnit4ClassRunner.class) 71 | @ContextConfiguration(locations = {"classpath:/org/springframework/reinject/reinject-context.xml", ...your XML files}) 72 | public class XmlConfigTest { 73 | 74 | ``` 75 | 76 | ### For Java-based configuration 77 | ``` 78 | @Configuration 79 | public class ReInjectContext { 80 | @Bean 81 | public ReInjectPostProcessor mockInjectionPostProcessor() { 82 | return new ReInjectPostProcessor(); 83 | } 84 | } 85 | ``` 86 | 87 | You JUnit test may look like 88 | ``` 89 | @RunWith(SpringJUnit4ClassRunner.class) 90 | @ContextConfiguration(classes = {ReInjectContext.class, ... your classes}) 91 | public class Test { 92 | ``` 93 | ## Register your mocks 94 | 95 | You need to register mocks before Spring starts, the right place to do it is your test's constructor. 96 | Let's suppose you want to substitute a bean named "service" witch a mock. 97 | 98 | ### You can substitute a bean's implementation class 99 | ``` 100 | @RunWith(SpringJUnit4ClassRunner.class) 101 | @ContextConfiguration(classes = {ReInjectContext.class, ... your classes}) 102 | class MyTest { 103 | public MyTest() { 104 | ReInjectPostProcessor.inject("service", ServiceMock.class); 105 | } 106 | ``` 107 | ### You can provide a pre-instantiated implementation 108 | ``` 109 | @RunWith(SpringJUnit4ClassRunner.class) 110 | @ContextConfiguration(classes = {ReInjectContext.class, ... your classes}) 111 | class MyTest { 112 | public MyTest() { 113 | ReInjectPostProcessor.inject("service",Service.class, new ServiceImpl() { 114 | @Override 115 | public String hello() { 116 | return "goodbye!"; 117 | } 118 | }); 119 | } 120 | ``` 121 | 122 | ### Using EasyMock is possible 123 | You can use whatever library you like to deal with mocks, for example, EasyMock. 124 | ``` 125 | @RunWith(SpringJUnit4ClassRunner.class) 126 | @ContextConfiguration(classes = {ReInjectContext.class, ... your classes}) 127 | class MyTest { 128 | public MyTest() { 129 | IMocksControl niceControl = EasyMock.createNiceControl(); 130 | Service mock = niceControl.createMock(Service.class); 131 | EasyMock.expect(mock.hello()).andReturn("easyMock"); 132 | niceControl.replay(); 133 | ReInjectPostProcessor.inject("service", mock); 134 | } 135 | ``` 136 | ## Code samples 137 | Code samples can be found [here](https://github.com/sgri/spring-reinject/tree/master/src/test/java/org/springframework/reinject) 138 | -------------------------------------------------------------------------------- /github-repo.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | org.springframework 5 | 0.2 6 | spring-reinject-github 7 | Test integration with GitHub repo 8 | 9 | 10 | reinject 11 | reinject 12 | https://github.com/sgri/spring-reinject/raw/master/release 13 | default 14 | 15 | true 16 | 17 | 18 | always 19 | 20 | 21 | 22 | 23 | 24 | org.springframework 25 | spring-reinject 26 | ${project.version} 27 | test 28 | 29 | 30 | org.springframework 31 | spring-core 32 | 33 | 34 | org.springframework 35 | spring-context 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | org.springframework 5 | 0.3 6 | spring-reinject 7 | 8 | Spring Re-inject 9 | Injection of mocks into Spring application context 10 | 11 | 12 | The Apache Software License, Version 2.0 13 | http://www.apache.org/licenses/LICENSE-2.0.txt 14 | repo 15 | A business-friendly OSS license 16 | 17 | 18 | 19 | 20 | 21 | false 22 | file 23 | local file 24 | file:release 25 | default 26 | 27 | 28 | 29 | 30 | 31 | 3.2.4.RELEASE 32 | 33 | 34 | 35 | 36 | 37 | org.springframework 38 | spring-core 39 | ${springVersion} 40 | 41 | 42 | org.springframework 43 | spring-context 44 | ${springVersion} 45 | 46 | 47 | 48 | org.springframework 49 | spring-test 50 | ${springVersion} 51 | test 52 | 53 | 54 | 55 | org.easymock 56 | easymock 57 | 3.1 58 | test 59 | 60 | 61 | org.slf4j 62 | slf4j-log4j12 63 | 1.6.6 64 | test 65 | 66 | 67 | javax.inject 68 | javax.inject 69 | 1 70 | test 71 | 72 | 73 | junit 74 | junit 75 | 4.10 76 | test 77 | 78 | 79 | log4j 80 | log4j 81 | 1.2.17 82 | test 83 | 84 | 85 | 86 | 87 | 88 | org.springframework 89 | spring-core 90 | 91 | 92 | org.springframework 93 | spring-context 94 | 95 | 96 | org.springframework 97 | spring-test 98 | 99 | 100 | 101 | org.easymock 102 | easymock 103 | 104 | 105 | org.slf4j 106 | slf4j-log4j12 107 | 108 | 109 | javax.inject 110 | javax.inject 111 | 1 112 | test 113 | 114 | 115 | junit 116 | junit 117 | 118 | 119 | log4j 120 | log4j 121 | 122 | 123 | 124 | 125 | 126 | org.apache.maven.plugins 127 | maven-compiler-plugin 128 | 3.1 129 | 130 | 1.7 131 | 1.7 132 | UTF-8 133 | 134 | 135 | 136 | org.apache.maven.plugins 137 | maven-source-plugin 138 | 2.1.2 139 | 140 | 141 | attach-sources 142 | package 143 | 144 | jar-no-fork 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | -------------------------------------------------------------------------------- /release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | mvn deploy -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.1/spring-reinject-0.1-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sgri/spring-reinject/15f4a200ad3f469690ae0972f00d9ddec03d600a/release/org/springframework/spring-reinject/0.1/spring-reinject-0.1-sources.jar -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.1/spring-reinject-0.1-sources.jar.md5: -------------------------------------------------------------------------------- 1 | b3d3531536334681b5c44df7fe40661b -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.1/spring-reinject-0.1-sources.jar.sha1: -------------------------------------------------------------------------------- 1 | 026a295dbadb689dd4755fb90938231c04390f61 -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.1/spring-reinject-0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sgri/spring-reinject/15f4a200ad3f469690ae0972f00d9ddec03d600a/release/org/springframework/spring-reinject/0.1/spring-reinject-0.1.jar -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.1/spring-reinject-0.1.jar.md5: -------------------------------------------------------------------------------- 1 | 8eeeca1d5adbb74fc7abe6912ee8576d -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.1/spring-reinject-0.1.jar.sha1: -------------------------------------------------------------------------------- 1 | c3bae09250e446c3ba030d64bb3c7a8d6cbf1376 -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.1/spring-reinject-0.1.pom: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | org.springframework 5 | 0.1 6 | spring-reinject 7 | 8 | Spring Re-inject 9 | Injection of mocks into Spring application context 10 | 11 | 12 | The Apache Software License, Version 2.0 13 | http://www.apache.org/licenses/LICENSE-2.0.txt 14 | repo 15 | A business-friendly OSS license 16 | 17 | 18 | 19 | 20 | 21 | false 22 | file 23 | local file 24 | file:release 25 | default 26 | 27 | 28 | 29 | 30 | 31 | 3.2.4.RELEASE 32 | 33 | 34 | 35 | 36 | 37 | org.springframework 38 | spring-core 39 | ${springVersion} 40 | 41 | 42 | org.springframework 43 | spring-context 44 | ${springVersion} 45 | 46 | 47 | 48 | org.springframework 49 | spring-test 50 | ${springVersion} 51 | test 52 | 53 | 54 | 55 | org.easymock 56 | easymock 57 | 3.1 58 | test 59 | 60 | 61 | org.slf4j 62 | slf4j-log4j12 63 | 1.6.6 64 | test 65 | 66 | 67 | javax.inject 68 | javax.inject 69 | 1 70 | test 71 | 72 | 73 | junit 74 | junit 75 | 4.10 76 | test 77 | 78 | 79 | log4j 80 | log4j 81 | 1.2.17 82 | test 83 | 84 | 85 | 86 | 87 | 88 | org.springframework 89 | spring-core 90 | 91 | 92 | org.springframework 93 | spring-context 94 | 95 | 96 | org.springframework 97 | spring-test 98 | 99 | 100 | 101 | org.easymock 102 | easymock 103 | 104 | 105 | org.slf4j 106 | slf4j-log4j12 107 | 108 | 109 | javax.inject 110 | javax.inject 111 | 1 112 | test 113 | 114 | 115 | junit 116 | junit 117 | 118 | 119 | log4j 120 | log4j 121 | 122 | 123 | 124 | 125 | 126 | org.apache.maven.plugins 127 | maven-compiler-plugin 128 | 3.1 129 | 130 | 1.7 131 | 1.7 132 | UTF-8 133 | 134 | 135 | 136 | org.apache.maven.plugins 137 | maven-source-plugin 138 | 2.1.2 139 | 140 | 141 | attach-sources 142 | package 143 | 144 | jar-no-fork 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.1/spring-reinject-0.1.pom.md5: -------------------------------------------------------------------------------- 1 | e25919d23774f40996788d3c1d89515d -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.1/spring-reinject-0.1.pom.sha1: -------------------------------------------------------------------------------- 1 | 8e5b32feb5616582be37164b3cc9281f49957cdf -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.2/spring-reinject-0.2-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sgri/spring-reinject/15f4a200ad3f469690ae0972f00d9ddec03d600a/release/org/springframework/spring-reinject/0.2/spring-reinject-0.2-sources.jar -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.2/spring-reinject-0.2-sources.jar.md5: -------------------------------------------------------------------------------- 1 | 35e49113923a961bb55b0ff632778c90 -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.2/spring-reinject-0.2-sources.jar.sha1: -------------------------------------------------------------------------------- 1 | df7c83d45053fb3be39d6fe4c0bac729add2489e -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.2/spring-reinject-0.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sgri/spring-reinject/15f4a200ad3f469690ae0972f00d9ddec03d600a/release/org/springframework/spring-reinject/0.2/spring-reinject-0.2.jar -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.2/spring-reinject-0.2.jar.md5: -------------------------------------------------------------------------------- 1 | f083bf613675085e49787ba6a2641718 -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.2/spring-reinject-0.2.jar.sha1: -------------------------------------------------------------------------------- 1 | 65faa0f5191792b064f7cc656c6af5dcf3c84353 -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.2/spring-reinject-0.2.pom: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | org.springframework 5 | 0.2 6 | spring-reinject 7 | 8 | Spring Re-inject 9 | Injection of mocks into Spring application context 10 | 11 | 12 | The Apache Software License, Version 2.0 13 | http://www.apache.org/licenses/LICENSE-2.0.txt 14 | repo 15 | A business-friendly OSS license 16 | 17 | 18 | 19 | 20 | 21 | false 22 | file 23 | local file 24 | file:release 25 | default 26 | 27 | 28 | 29 | 30 | 31 | 3.2.4.RELEASE 32 | 33 | 34 | 35 | 36 | 37 | org.springframework 38 | spring-core 39 | ${springVersion} 40 | 41 | 42 | org.springframework 43 | spring-context 44 | ${springVersion} 45 | 46 | 47 | 48 | org.springframework 49 | spring-test 50 | ${springVersion} 51 | test 52 | 53 | 54 | 55 | org.easymock 56 | easymock 57 | 3.1 58 | test 59 | 60 | 61 | org.slf4j 62 | slf4j-log4j12 63 | 1.6.6 64 | test 65 | 66 | 67 | javax.inject 68 | javax.inject 69 | 1 70 | test 71 | 72 | 73 | junit 74 | junit 75 | 4.10 76 | test 77 | 78 | 79 | log4j 80 | log4j 81 | 1.2.17 82 | test 83 | 84 | 85 | 86 | 87 | 88 | org.springframework 89 | spring-core 90 | 91 | 92 | org.springframework 93 | spring-context 94 | 95 | 96 | org.springframework 97 | spring-test 98 | 99 | 100 | 101 | org.easymock 102 | easymock 103 | 104 | 105 | org.slf4j 106 | slf4j-log4j12 107 | 108 | 109 | javax.inject 110 | javax.inject 111 | 1 112 | test 113 | 114 | 115 | junit 116 | junit 117 | 118 | 119 | log4j 120 | log4j 121 | 122 | 123 | 124 | 125 | 126 | org.apache.maven.plugins 127 | maven-compiler-plugin 128 | 3.1 129 | 130 | 1.7 131 | 1.7 132 | UTF-8 133 | 134 | 135 | 136 | org.apache.maven.plugins 137 | maven-source-plugin 138 | 2.1.2 139 | 140 | 141 | attach-sources 142 | package 143 | 144 | jar-no-fork 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.2/spring-reinject-0.2.pom.md5: -------------------------------------------------------------------------------- 1 | 9751a997cd23cede8a6e6bf8696e72aa -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.2/spring-reinject-0.2.pom.sha1: -------------------------------------------------------------------------------- 1 | 15f63af299b0547e65c3863bd5feea6e273bf453 -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.3/spring-reinject-0.3-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sgri/spring-reinject/15f4a200ad3f469690ae0972f00d9ddec03d600a/release/org/springframework/spring-reinject/0.3/spring-reinject-0.3-sources.jar -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.3/spring-reinject-0.3-sources.jar.md5: -------------------------------------------------------------------------------- 1 | 5fb5199bd93cd19d532e10a5d5de3836 -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.3/spring-reinject-0.3-sources.jar.sha1: -------------------------------------------------------------------------------- 1 | 2c22bba69c25aa8e0fc99f5009f3ab7c86b05e5e -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.3/spring-reinject-0.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sgri/spring-reinject/15f4a200ad3f469690ae0972f00d9ddec03d600a/release/org/springframework/spring-reinject/0.3/spring-reinject-0.3.jar -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.3/spring-reinject-0.3.jar.md5: -------------------------------------------------------------------------------- 1 | 64d070077155dc3af63c427ba1868b1f -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.3/spring-reinject-0.3.jar.sha1: -------------------------------------------------------------------------------- 1 | f1ddab7e9591f5ab15f42ea591bdce9cbb34b76e -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.3/spring-reinject-0.3.pom: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | org.springframework 5 | 0.3 6 | spring-reinject 7 | 8 | Spring Re-inject 9 | Injection of mocks into Spring application context 10 | 11 | 12 | The Apache Software License, Version 2.0 13 | http://www.apache.org/licenses/LICENSE-2.0.txt 14 | repo 15 | A business-friendly OSS license 16 | 17 | 18 | 19 | 20 | 21 | false 22 | file 23 | local file 24 | file:release 25 | default 26 | 27 | 28 | 29 | 30 | 31 | 3.2.4.RELEASE 32 | 33 | 34 | 35 | 36 | 37 | org.springframework 38 | spring-core 39 | ${springVersion} 40 | 41 | 42 | org.springframework 43 | spring-context 44 | ${springVersion} 45 | 46 | 47 | 48 | org.springframework 49 | spring-test 50 | ${springVersion} 51 | test 52 | 53 | 54 | 55 | org.easymock 56 | easymock 57 | 3.1 58 | test 59 | 60 | 61 | org.slf4j 62 | slf4j-log4j12 63 | 1.6.6 64 | test 65 | 66 | 67 | javax.inject 68 | javax.inject 69 | 1 70 | test 71 | 72 | 73 | junit 74 | junit 75 | 4.10 76 | test 77 | 78 | 79 | log4j 80 | log4j 81 | 1.2.17 82 | test 83 | 84 | 85 | 86 | 87 | 88 | org.springframework 89 | spring-core 90 | 91 | 92 | org.springframework 93 | spring-context 94 | 95 | 96 | org.springframework 97 | spring-test 98 | 99 | 100 | 101 | org.easymock 102 | easymock 103 | 104 | 105 | org.slf4j 106 | slf4j-log4j12 107 | 108 | 109 | javax.inject 110 | javax.inject 111 | 1 112 | test 113 | 114 | 115 | junit 116 | junit 117 | 118 | 119 | log4j 120 | log4j 121 | 122 | 123 | 124 | 125 | 126 | org.apache.maven.plugins 127 | maven-compiler-plugin 128 | 3.1 129 | 130 | 1.7 131 | 1.7 132 | UTF-8 133 | 134 | 135 | 136 | org.apache.maven.plugins 137 | maven-source-plugin 138 | 2.1.2 139 | 140 | 141 | attach-sources 142 | package 143 | 144 | jar-no-fork 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.3/spring-reinject-0.3.pom.md5: -------------------------------------------------------------------------------- 1 | 8cf0454bbcf7d166bca8bdfa3e261c6a -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/0.3/spring-reinject-0.3.pom.sha1: -------------------------------------------------------------------------------- 1 | ef5736a34f030a6d45732c336a8b172ec5a70633 -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/maven-metadata.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.springframework 4 | spring-reinject 5 | 6 | 0.3 7 | 8 | 0.1 9 | 0.2 10 | 0.3 11 | 12 | 20160207111033 13 | 14 | 15 | -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/maven-metadata.xml.md5: -------------------------------------------------------------------------------- 1 | de6157fb69e95d76a99ef8fa41e04f8d -------------------------------------------------------------------------------- /release/org/springframework/spring-reinject/maven-metadata.xml.sha1: -------------------------------------------------------------------------------- 1 | 60e616f25583d8f7611d69559185981f60bfa198 -------------------------------------------------------------------------------- /src/main/java/org/springframework/reinject/ReInjectContext.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | /** 7 | * @author Sergey Grigoriev 8 | */ 9 | @Configuration 10 | public class ReInjectContext { 11 | @Bean 12 | public ReInjectPostProcessor mockInjectionPostProcessor() { 13 | return new ReInjectPostProcessor(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/reinject/ReInjectFactoryBean.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import org.springframework.beans.BeansException; 7 | import org.springframework.beans.factory.FactoryBean; 8 | import org.springframework.context.ApplicationContext; 9 | import org.springframework.context.ApplicationContextAware; 10 | 11 | /** 12 | * @author Sergey Grigoriev 13 | */ 14 | class ReInjectFactoryBean implements FactoryBean, ApplicationContextAware { 15 | private ApplicationContext applicationContext; 16 | private final Object object; 17 | private final Class objectType; 18 | 19 | public ReInjectFactoryBean(Object object, Class objectType) { 20 | this.object = object; 21 | this.objectType = objectType; 22 | } 23 | 24 | @Override 25 | public Object getObject() throws Exception { 26 | applicationContext.getAutowireCapableBeanFactory().autowireBean(object); 27 | return object; 28 | } 29 | 30 | @Override 31 | public Class getObjectType() { 32 | return objectType == null ? object.getClass(): objectType; 33 | } 34 | 35 | @Override 36 | public boolean isSingleton() { 37 | return true; 38 | } 39 | 40 | @Override 41 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 42 | this.applicationContext = applicationContext; 43 | } 44 | 45 | /** 46 | * Stub implementation to allow re-injecting maps 47 | */ 48 | public void setSourceMap(Map sourceMap) { 49 | // no-op 50 | } 51 | 52 | /** 53 | * Stub implementation to allow re-injecting lists 54 | */ 55 | public void setSourceList(List sourceList) { 56 | // no-op 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/springframework/reinject/ReInjectPostProcessor.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.beans.PropertyValue; 5 | import org.springframework.beans.factory.config.*; 6 | import org.springframework.beans.factory.support.BeanDefinitionRegistry; 7 | import org.springframework.beans.factory.support.GenericBeanDefinition; 8 | import org.springframework.stereotype.Component; 9 | 10 | import java.util.LinkedHashMap; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | /** 15 | * @author Sergey Grigoriev 16 | */ 17 | @Component 18 | public class ReInjectPostProcessor implements BeanFactoryPostProcessor { 19 | private static final Map classesByName = new LinkedHashMap<>(); 20 | private static final Map objectsByName = new LinkedHashMap<>(); 21 | private static final Map constructorArgsMap = new LinkedHashMap<>(); 22 | 23 | /** 24 | * Replace a bean definition with a another class 25 | * 26 | * @param name bean id 27 | * @param clazz new class which replaces the original bean definition class 28 | */ 29 | public static void inject(String name, Class clazz) { 30 | classesByName.put(name, clazz); 31 | } 32 | 33 | /** 34 | * Replace a bean definition with a factory bean which returns a pre-instantiated object 35 | * 36 | * @param name bean id 37 | * @param beanType object type, see {@linkplain org.springframework.beans.factory.FactoryBean#getObjectType()} 38 | * @param object bean instance 39 | */ 40 | public static void inject(String name, Class beanType, Object object) { 41 | if (!beanType.isAssignableFrom(object.getClass())) { 42 | throw new IllegalArgumentException(String.format("%s is not assignable form %s", beanType.getClass().getName(), object.getClass().getName())); 43 | } 44 | objectsByName.put(name, object); 45 | ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues(); 46 | constructorArgumentValues.addGenericArgumentValue(object); 47 | constructorArgumentValues.addGenericArgumentValue(beanType); 48 | constructorArgsMap.put(name, constructorArgumentValues); 49 | } 50 | 51 | /** 52 | * See {@linkplain #inject(String, Class, Object)} 53 | */ 54 | public static void inject(String name, Object object) { 55 | inject(name, object.getClass(), object); 56 | } 57 | 58 | @Override 59 | public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 60 | for (String beanName : beanFactory.getBeanDefinitionNames()) { 61 | BeanDefinition bd = beanFactory.getBeanDefinition(beanName); 62 | if (classesByName.containsKey(beanName) || objectsByName.containsKey(beanName)) { 63 | GenericBeanDefinition overriddenBd = new GenericBeanDefinition(bd); 64 | overriddenBd.setFactoryBeanName(null); 65 | overriddenBd.setFactoryMethodName(null); 66 | if (classesByName.containsKey(beanName)) { 67 | overriddenBd.setBeanClass(classesByName.get(beanName)); 68 | } else if (objectsByName.containsKey(beanName)) { 69 | if (ListFactoryBean.class.getName().equals(bd.getBeanClassName())) { 70 | final PropertyValue originalProp = bd.getPropertyValues().getPropertyValue("sourceList"); 71 | int index = overriddenBd.getPropertyValues().getPropertyValueList().indexOf(originalProp); 72 | final Object reInjectedObject = objectsByName.get(beanName); 73 | if (!List.class.isAssignableFrom(reInjectedObject.getClass())) { 74 | throw new IllegalArgumentException(); 75 | } 76 | PropertyValue modifiedProp = new PropertyValue(originalProp.getName(), reInjectedObject); 77 | overriddenBd.getPropertyValues().setPropertyValueAt(modifiedProp, index); 78 | } else if (MapFactoryBean.class.getName().equals(bd.getBeanClassName())) { 79 | final PropertyValue originalProp = bd.getPropertyValues().getPropertyValue("sourceMap"); 80 | int index = overriddenBd.getPropertyValues().getPropertyValueList().indexOf(originalProp); 81 | final Object reInjectedObject = objectsByName.get(beanName); 82 | if (!Map.class.isAssignableFrom(reInjectedObject.getClass())) { 83 | throw new IllegalArgumentException(); 84 | } 85 | PropertyValue modifiedProp = new PropertyValue(originalProp.getName(), reInjectedObject); 86 | overriddenBd.getPropertyValues().setPropertyValueAt(modifiedProp, index); 87 | } 88 | else { 89 | overriddenBd.setBeanClassName(ReInjectFactoryBean.class.getName()); 90 | ConstructorArgumentValues constructorArgumentValues = constructorArgsMap.get(beanName); 91 | overriddenBd.setConstructorArgumentValues(constructorArgumentValues); 92 | } 93 | } 94 | BeanDefinitionRegistry bdr = (BeanDefinitionRegistry) beanFactory; 95 | bdr.removeBeanDefinition(beanName); 96 | bdr.registerBeanDefinition(beanName, overriddenBd); 97 | } 98 | } 99 | cleanup(); 100 | } 101 | 102 | private void cleanup() { 103 | classesByName.clear(); 104 | objectsByName.clear(); 105 | constructorArgsMap.clear(); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/reinject/ComponentScanTest.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.context.annotation.ComponentScan; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 13 | 14 | /** 15 | * @author Sergey Grigoriev 16 | */ 17 | @RunWith(SpringJUnit4ClassRunner.class) 18 | @ContextConfiguration(classes = {ComponentScanTest.AppContext.class}) 19 | public class ComponentScanTest { 20 | @Autowired private Service service; 21 | 22 | public ComponentScanTest() { 23 | ReInjectPostProcessor.inject("service", ServiceMock.class); 24 | } 25 | 26 | @Test 27 | public void injection() { 28 | assertEquals("goodbye!", service.hello()); 29 | } 30 | 31 | @Configuration 32 | @ComponentScan(basePackages = "org.springframework.reinject") 33 | static class AppContext { 34 | 35 | } 36 | } -------------------------------------------------------------------------------- /src/test/java/org/springframework/reinject/InjectIntoMockTest.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | import javax.inject.Inject; 4 | 5 | import junit.framework.Assert; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | /** 14 | * @author Sergey Grigoriev 15 | */ 16 | @RunWith(SpringJUnit4ClassRunner.class) 17 | @ContextConfiguration(classes = {ReInjectContext.class, InjectIntoMockTest.InjectIntoMockTestContext.class}) 18 | public class InjectIntoMockTest { 19 | @Inject private Printer printer; 20 | 21 | public InjectIntoMockTest() { 22 | ReInjectPostProcessor.inject("printer", new Printer(){ 23 | @Inject private Service service; 24 | @Override 25 | public String print() { 26 | Assert.assertNotNull(service); 27 | return super.print(); 28 | } 29 | }); 30 | } 31 | 32 | @Test 33 | public void injectIntoMock() { 34 | printer.print(); 35 | } 36 | 37 | @Configuration 38 | static class InjectIntoMockTestContext { 39 | @Bean 40 | public Service service() { 41 | return new ServiceImpl(); 42 | } 43 | 44 | @Bean 45 | public Printer printer() { 46 | return new Printer(); 47 | } 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/reinject/InjectListTest.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.annotation.Qualifier; 7 | import org.springframework.beans.factory.config.ListFactoryBean; 8 | import org.springframework.test.annotation.DirtiesContext; 9 | import org.springframework.test.context.ContextConfiguration; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | import static org.junit.Assert.assertEquals; 16 | 17 | @RunWith(SpringJUnit4ClassRunner.class) 18 | @ContextConfiguration(locations = {"classpath:/org/springframework/reinject/reinject-context.xml", "classpath:/org/springframework/reinject/InjectListTest-context.xml"}) 19 | @DirtiesContext 20 | public class InjectListTest { 21 | 22 | private static final String BEAN_NAME = "injectedList"; 23 | 24 | private static final String STUB_ENTRY_1 = "Mesa list entry 1"; 25 | 26 | @Autowired 27 | @Qualifier(BEAN_NAME) 28 | ListFactoryBean stringList; 29 | 30 | private List listStub = new ArrayList<>(); 31 | 32 | public InjectListTest() { 33 | listStub.add(STUB_ENTRY_1); 34 | ReInjectPostProcessor.inject(BEAN_NAME, listStub); 35 | } 36 | 37 | @Test 38 | public void injectList() throws Exception { 39 | assertEquals(listStub, stringList.getObject()); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/reinject/InjectMapTest.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Qualifier; 8 | import org.springframework.beans.factory.config.MapFactoryBean; 9 | import org.springframework.test.annotation.DirtiesContext; 10 | import org.springframework.test.context.ContextConfiguration; 11 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 12 | 13 | import java.util.HashMap; 14 | import java.util.Map; 15 | 16 | import static org.junit.Assert.assertEquals; 17 | 18 | @RunWith(SpringJUnit4ClassRunner.class) 19 | @ContextConfiguration(locations = {"classpath:/org/springframework/reinject/reinject-context.xml", "classpath:/org/springframework/reinject/InjectMapTest-context.xml"}) 20 | @DirtiesContext 21 | public class InjectMapTest { 22 | 23 | private static final String BEAN_NAME = "injectedMap"; 24 | 25 | private static final String STUB_KEY_1 = "MESA.KEY"; 26 | 27 | private static final String STUB_VALUE_1 = "JarJar says hello"; 28 | 29 | @Autowired 30 | @Qualifier(BEAN_NAME) 31 | MapFactoryBean injectedMap; 32 | 33 | private Map mapStub = new HashMap<>(); 34 | 35 | public InjectMapTest() { 36 | mapStub.put(STUB_KEY_1, STUB_VALUE_1); 37 | ReInjectPostProcessor.inject(BEAN_NAME, mapStub); 38 | } 39 | 40 | @Test 41 | public void injectMap() throws Exception { 42 | assertEquals(mapStub, injectedMap.getObject()); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/reinject/InjectObjectTest.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | 4 | import static org.junit.Assert.assertEquals; 5 | 6 | import javax.inject.Inject; 7 | import javax.inject.Named; 8 | 9 | import org.junit.Assert; 10 | import org.junit.Test; 11 | import org.junit.runner.RunWith; 12 | import org.springframework.beans.factory.FactoryBean; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.test.context.ContextConfiguration; 15 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 16 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 17 | 18 | /** 19 | * @author Sergey Grigoriev 20 | */ 21 | @RunWith(SpringJUnit4ClassRunner.class) 22 | @ContextConfiguration(classes = {ReInjectContext.class, InjectObjectTest.ProviderTestContext.class}) 23 | public class InjectObjectTest { 24 | 25 | @Inject @Named("service1") private Service service1; 26 | @Inject @Named("service2") private Service service2; 27 | 28 | public InjectObjectTest() { 29 | ReInjectPostProcessor.inject("service1",Service.class, new ServiceMock() { 30 | @Override 31 | public String hello() { 32 | return "hello1"; 33 | } 34 | }); 35 | ReInjectPostProcessor.inject("service2", new ServiceMock() { 36 | @Override 37 | public String hello() { 38 | return "hello2"; 39 | } 40 | }); 41 | } 42 | 43 | @Test 44 | public void overrideProvider() { 45 | assertEquals("hello1", service1.hello()); 46 | assertEquals("hello2", service2.hello()); 47 | } 48 | 49 | static class ProviderTestContext { 50 | @Bean 51 | public FactoryBean service1() { 52 | FactoryBean factoryBean = new FactoryBean() { 53 | @Override 54 | public Service getObject() throws Exception { 55 | return new ServiceImpl(); 56 | } 57 | 58 | @Override 59 | public Class getObjectType() { 60 | return Service.class; 61 | } 62 | 63 | @Override 64 | public boolean isSingleton() { 65 | return true; 66 | } 67 | }; 68 | return factoryBean; 69 | } 70 | @Bean 71 | public Service service2() { 72 | return new ServiceImpl(); 73 | } 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /src/test/java/org/springframework/reinject/JavaConfigTest.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import javax.inject.Inject; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.context.annotation.Configuration; 11 | import org.springframework.test.context.ContextConfiguration; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; 14 | 15 | /** 16 | * @author Sergey Grigoriev 17 | */ 18 | @RunWith(SpringJUnit4ClassRunner.class) 19 | @ContextConfiguration(classes = {ReInjectContext.class, JavaConfigTest.AppContext.class}) 20 | public class JavaConfigTest { 21 | @Inject private Service service; 22 | public JavaConfigTest() { 23 | ReInjectPostProcessor.inject("service", ServiceMock.class); 24 | } 25 | 26 | @Test 27 | public void inject() { 28 | assertEquals("goodbye!", service.hello()); 29 | } 30 | 31 | @Configuration 32 | static class AppContext { 33 | 34 | @Bean public Object dependent() { 35 | assertEquals("goodbye!", service().hello()); 36 | return new Object(); 37 | } 38 | 39 | @Bean 40 | public Service service() { 41 | return new ServiceImpl(); 42 | } 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/reinject/Printer.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | import javax.inject.Inject; 4 | 5 | /** 6 | * @author Sergey Grigoriev 7 | */ 8 | public class Printer { 9 | private @Inject Service service; 10 | public String print() { 11 | return service.hello(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/reinject/Service.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | /** 4 | * @author Sergey Grigoriev 5 | */ 6 | public interface Service { 7 | String hello(); 8 | } 9 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/reinject/ServiceImpl.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | /** 6 | * @author Sergey Grigoriev 7 | */ 8 | @Component("service") 9 | public class ServiceImpl implements Service { 10 | public String hello() { 11 | return "hello!"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/reinject/ServiceMock.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | /** 4 | * @author Sergey Grigoriev 5 | */ 6 | public class ServiceMock extends ServiceImpl { 7 | @Override 8 | public String hello() { 9 | return "goodbye!"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/test/java/org/springframework/reinject/XmlConfigTest.java: -------------------------------------------------------------------------------- 1 | package org.springframework.reinject; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import javax.inject.Inject; 6 | 7 | import org.easymock.EasyMock; 8 | import org.easymock.IMocksControl; 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.springframework.test.context.ContextConfiguration; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | 14 | /** 15 | * @author Sergey Grigoriev 16 | */ 17 | 18 | @RunWith(SpringJUnit4ClassRunner.class) 19 | @ContextConfiguration(locations = {"classpath:/org/springframework/reinject/reinject-context.xml", "classpath:/org/springframework/reinject/app-context.xml" }) 20 | public class XmlConfigTest { 21 | @Inject private Printer printer; 22 | 23 | public XmlConfigTest() { 24 | IMocksControl niceControl = EasyMock.createNiceControl(); 25 | Service mock = niceControl.createMock(Service.class); 26 | EasyMock.expect(mock.hello()).andReturn("easyMock"); 27 | niceControl.replay(); 28 | ReInjectPostProcessor.inject("service", mock); 29 | } 30 | 31 | @Test 32 | public void inject() { 33 | assertEquals("easyMock", printer.print()); 34 | } 35 | 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/test/resources/org/springframework/reinject/InjectListTest-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 1 12 | 2 13 | 3 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/test/resources/org/springframework/reinject/InjectMapTest-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/test/resources/org/springframework/reinject/app-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/test/resources/org/springframework/reinject/reinject-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /test-github-repo.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | rm -R $HOME/.m2/repository/org/springframework/spring-reinject 3 | workDir=`mktemp -d` 4 | cp github-repo.xml $workDir 5 | cd $workDir 6 | mvn dependency:analyze -f github-repo.xml 7 | exit $? 8 | --------------------------------------------------------------------------------