1
1
/*
2
- * Copyright 2012-2014 the original author or authors.
2
+ * Copyright 2012-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
34
34
public class VcapApplicationListenerTests {
35
35
36
36
private final VcapApplicationListener initializer = new VcapApplicationListener ();
37
+
37
38
private final ConfigurableApplicationContext context = new AnnotationConfigApplicationContext ();
39
+
38
40
private final ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent (
39
41
new SpringApplication (), new String [0 ], this .context .getEnvironment ());
40
42
@@ -43,7 +45,18 @@ public void testApplicationProperties() {
43
45
EnvironmentTestUtils
44
46
.addEnvironment (
45
47
this .context ,
46
- "VCAP_APPLICATION:{\" application_users\" :[],\" instance_id\" :\" bb7935245adf3e650dfb7c58a06e9ece\" ,\" instance_index\" :0,\" version\" :\" 3464e092-1c13-462e-a47c-807c30318a50\" ,\" name\" :\" foo\" ,\" uris\" :[\" foo.cfapps.io\" ],\" started_at\" :\" 2013-05-29 02:37:59 +0000\" ,\" started_at_timestamp\" :1369795079,\" host\" :\" 0.0.0.0\" ,\" port\" :61034,\" limits\" :{\" mem\" :128,\" disk\" :1024,\" fds\" :16384},\" version\" :\" 3464e092-1c13-462e-a47c-807c30318a50\" ,\" name\" :\" dsyerenv\" ,\" uris\" :[\" dsyerenv.cfapps.io\" ],\" users\" :[],\" start\" :\" 2013-05-29 02:37:59 +0000\" ,\" state_timestamp\" :1369795079}" );
48
+ "VCAP_APPLICATION:{\" application_users\" :[],"
49
+ + "\" instance_id\" :\" bb7935245adf3e650dfb7c58a06e9ece\" ,"
50
+ + "\" instance_index\" :0,\" version\" :\" 3464e092-1c13-462e-a47c-807c30318a50\" ,"
51
+ + "\" name\" :\" foo\" ,\" uris\" :[\" foo.cfapps.io\" ],"
52
+ + "\" started_at\" :\" 2013-05-29 02:37:59 +0000\" ,"
53
+ + "\" started_at_timestamp\" :1369795079,"
54
+ + "\" host\" :\" 0.0.0.0\" ,\" port\" :61034,"
55
+ + "\" limits\" :{\" mem\" :128,\" disk\" :1024,\" fds\" :16384},"
56
+ + "\" version\" :\" 3464e092-1c13-462e-a47c-807c30318a50\" ,"
57
+ + "\" name\" :\" dsyerenv\" ,\" uris\" :[\" dsyerenv.cfapps.io\" ],"
58
+ + "\" users\" :[],\" start\" :\" 2013-05-29 02:37:59 +0000\" ,"
59
+ + "\" state_timestamp\" :1369795079}" );
47
60
this .initializer .onApplicationEvent (this .event );
48
61
assertEquals ("bb7935245adf3e650dfb7c58a06e9ece" , this .context .getEnvironment ()
49
62
.getProperty ("vcap.application.instance_id" ));
@@ -64,38 +77,67 @@ public void testApplicationUris() {
64
77
public void testUnparseableApplicationProperties () {
65
78
EnvironmentTestUtils .addEnvironment (this .context , "VCAP_APPLICATION:" );
66
79
this .initializer .onApplicationEvent (this .event );
67
- assertNull (this . context . getEnvironment (). getProperty ("vcap" ));
80
+ assertNull (getProperty ("vcap" ));
68
81
}
69
82
70
83
@ Test
71
84
public void testNullApplicationProperties () {
72
85
EnvironmentTestUtils
73
86
.addEnvironment (
74
87
this .context ,
75
- "VCAP_APPLICATION:{\" application_users\" :null,\" instance_id\" :\" bb7935245adf3e650dfb7c58a06e9ece\" ,\" instance_index\" :0,\" version\" :\" 3464e092-1c13-462e-a47c-807c30318a50\" ,\" name\" :\" foo\" ,\" uris\" :[\" foo.cfapps.io\" ],\" started_at\" :\" 2013-05-29 02:37:59 +0000\" ,\" started_at_timestamp\" :1369795079,\" host\" :\" 0.0.0.0\" ,\" port\" :61034,\" limits\" :{\" mem\" :128,\" disk\" :1024,\" fds\" :16384},\" version\" :\" 3464e092-1c13-462e-a47c-807c30318a50\" ,\" name\" :\" dsyerenv\" ,\" uris\" :[\" dsyerenv.cfapps.io\" ],\" users\" :[],\" start\" :\" 2013-05-29 02:37:59 +0000\" ,\" state_timestamp\" :1369795079}" );
88
+ "VCAP_APPLICATION:{\" application_users\" :null,"
89
+ + "\" instance_id\" :\" bb7935245adf3e650dfb7c58a06e9ece\" ,"
90
+ + "\" instance_index\" :0,\" version\" :\" 3464e092-1c13-462e-a47c-807c30318a50\" ,"
91
+ + "\" name\" :\" foo\" ,\" uris\" :[\" foo.cfapps.io\" ],"
92
+ + "\" started_at\" :\" 2013-05-29 02:37:59 +0000\" ,"
93
+ + "\" started_at_timestamp\" :1369795079,"
94
+ + "\" host\" :\" 0.0.0.0\" ,\" port\" :61034,"
95
+ + "\" limits\" :{\" mem\" :128,\" disk\" :1024,\" fds\" :16384},"
96
+ + "\" version\" :\" 3464e092-1c13-462e-a47c-807c30318a50\" ,"
97
+ + "\" name\" :\" dsyerenv\" ,\" uris\" :[\" dsyerenv.cfapps.io\" ],"
98
+ + "\" users\" :[],\" start\" :\" 2013-05-29 02:37:59 +0000\" ,"
99
+ + "\" state_timestamp\" :1369795079}" );
76
100
this .initializer .onApplicationEvent (this .event );
77
- assertNull (this . context . getEnvironment (). getProperty ("vcap" ));
101
+ assertNull (getProperty ("vcap" ));
78
102
}
79
103
80
104
@ Test
81
105
public void testServiceProperties () {
82
106
EnvironmentTestUtils
83
107
.addEnvironment (
84
108
this .context ,
85
- "VCAP_SERVICES:{\" rds-mysql-n/a\" :[{\" name\" :\" mysql\" ,\" label\" :\" rds-mysql-n/a\" ,\" plan\" :\" 10mb\" ,\" credentials\" :{\" name\" :\" d04fb13d27d964c62b267bbba1cffb9da\" ,\" hostname\" :\" mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\" ,\" host\" :\" mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\" ,\" port\" :3306,\" user\" :\" urpRuqTf8Cpe6\" ,\" username\" :\" urpRuqTf8Cpe6\" ,\" password\" :\" pxLsGVpsC9A5S\" }}]}" );
109
+ "VCAP_SERVICES:{\" rds-mysql-n/a\" :[{"
110
+ + "\" name\" :\" mysql\" ,\" label\" :\" rds-mysql-n/a\" ,"
111
+ + "\" plan\" :\" 10mb\" ,\" credentials\" :{"
112
+ + "\" name\" :\" d04fb13d27d964c62b267bbba1cffb9da\" ,"
113
+ + "\" hostname\" :\" mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\" ,"
114
+ + "\" host\" :\" mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\" ,"
115
+ + "\" port\" :3306,\" user\" :\" urpRuqTf8Cpe6\" ,\" username\" :"
116
+ + "\" urpRuqTf8Cpe6\" ,\" password\" :\" pxLsGVpsC9A5S\" }}]}" );
86
117
this .initializer .onApplicationEvent (this .event );
87
- assertEquals ("mysql" ,
88
- this . context . getEnvironment (). getProperty ("vcap.services.mysql.name " ));
118
+ assertEquals ("mysql" , getProperty ( "vcap.services.mysql.name" ));
119
+ assertEquals ( "3306" , getProperty ("vcap.services.mysql.credentials.port " ));
89
120
}
90
121
91
122
@ Test
92
123
public void testServicePropertiesWithoutNA () {
93
124
EnvironmentTestUtils
94
125
.addEnvironment (
95
126
this .context ,
96
- "VCAP_SERVICES:{\" rds-mysql\" :[{\" name\" :\" mysql\" ,\" label\" :\" rds-mysql\" ,\" plan\" :\" 10mb\" ,\" credentials\" :{\" name\" :\" d04fb13d27d964c62b267bbba1cffb9da\" ,\" hostname\" :\" mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\" ,\" host\" :\" mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\" ,\" port\" :3306,\" user\" :\" urpRuqTf8Cpe6\" ,\" username\" :\" urpRuqTf8Cpe6\" ,\" password\" :\" pxLsGVpsC9A5S\" }}]}" );
127
+ "VCAP_SERVICES:{\" rds-mysql\" :[{"
128
+ + "\" name\" :\" mysql\" ,\" label\" :\" rds-mysql\" ,\" plan\" :\" 10mb\" ,"
129
+ + "\" credentials\" :{\" name\" :\" d04fb13d27d964c62b267bbba1cffb9da\" ,"
130
+ + "\" hostname\" :\" mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\" ,"
131
+ + "\" host\" :\" mysql-service-public.clqg2e2w3ecf.us-east-1.rds.amazonaws.com\" ,"
132
+ + "\" port\" :3306,\" user\" :\" urpRuqTf8Cpe6\" ,"
133
+ + "\" username\" :\" urpRuqTf8Cpe6\" ,"
134
+ + "\" password\" :\" pxLsGVpsC9A5S\" }}]}" );
97
135
this .initializer .onApplicationEvent (this .event );
98
- assertEquals ("mysql" ,
99
- this .context .getEnvironment ().getProperty ("vcap.services.mysql.name" ));
136
+ assertEquals ("mysql" , getProperty ("vcap.services.mysql.name" ));
137
+ assertEquals ("3306" , getProperty ("vcap.services.mysql.credentials.port" ));
138
+ }
139
+
140
+ private String getProperty (String key ) {
141
+ return this .context .getEnvironment ().getProperty (key );
100
142
}
101
143
}
0 commit comments