summaryrefslogtreecommitdiffstats
path: root/das-java/tags/1.0-incubating-beta2/samples/testing/tomcat/company-webapp/src/test/java/org/apache/tuscany/test/das/DasTestCase.java
blob: faa92d6994c80dbe80b9e4510e20c21853858751 (plain)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.tuscany.test.das;

import java.net.URL;

import junit.framework.TestCase;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

public class DasTestCase extends TestCase {
    public static final String testUrl = "http://localhost:8080/sample-company-webapp/";

    protected WebClient _webClient;
    protected URL _url;

    protected void setUp() throws Exception {
        //New HTMLunit web client
        _webClient = new WebClient();
        _webClient.setRedirectEnabled(true);

        // Going to have the WebClient connect to this URL
        _url = new URL(testUrl);

    }

    public void testHomepage() throws Exception {
        final String TEST_CASE = "HomePage";
        System.out.print("Running:" + TEST_CASE + "\t\t\t");

        final HtmlPage page = (HtmlPage) _webClient.getPage(_url);
        //System.out.println(page.getTitleText());

        String responseText = page.asText();
        //System.out.println("html-->\n" + responseText);
        //check page header
        assertTrue(-1 != responseText.indexOf("Tuscany DAS Companies WEB Example"));
        //check table headers
        assertTrue(-1 != responseText.indexOf("ID"));
        assertTrue(-1 != responseText.indexOf("Name"));
        assertTrue(-1 != responseText.indexOf("Department_ID"));
        assertTrue(-1 != responseText.indexOf("Department_Name"));
        //check companies values
        //assertTrue(-1 != responseText.indexOf("51"));
        assertTrue(-1 != responseText.indexOf("ACME Publishing"));
        //assertTrue(-1 != responseText.indexOf("53"));
        assertTrue(-1 != responseText.indexOf("Do-rite plumbing"));
        //assertTrue(-1 != responseText.indexOf("53"));
        assertTrue(-1 != responseText.indexOf("MegaCorp"));
        //check departments
        assertTrue(-1 != responseText.indexOf("Default Name 2"));
        assertTrue(-1 != responseText.indexOf("Default Name 3"));
        assertTrue(-1 != responseText.indexOf("Default Name 4"));
        assertTrue(-1 != responseText.indexOf("Default Name 5"));
        assertTrue(-1 != responseText.indexOf("Default Name 6"));
        assertTrue(-1 != responseText.indexOf("Default Name 7"));
        assertTrue(-1 != responseText.indexOf("Default Name 8"));

        System.out.println("SUCCESS!!!");
    }


    public void testAllCompanies() throws Exception{
        final String TEST_CASE = "AllCompanies";
        System.out.print("Running:" + TEST_CASE + "\t\t\t");

        // Get the first page
        final HtmlPage page = (HtmlPage) _webClient.getPage(_url);

        // Get the form that we are dealing with and within that form,
        // find the submit button and the field that we want to change.
        final HtmlForm form = (HtmlForm) page.getForms().get(0);
        final HtmlSubmitInput allCompaniesButton = (HtmlSubmitInput)form.getHtmlElementById("doFill");
        // Now submit the form by clicking the button
        final HtmlPage resultPage = (HtmlPage)allCompaniesButton.click();

        //verify the resultPage
        String responseText = resultPage.asText();
        //System.out.println("html-->\n" + responseText);
        // check table headers
        assertTrue(-1 != responseText.indexOf("ID"));
        assertTrue(-1 != responseText.indexOf("Name"));
        //check table values
        //assertTrue(-1 != responseText.indexOf("51"));
        assertTrue(-1 != responseText.indexOf("ACME Publishing"));
        //assertTrue(-1 != responseText.indexOf("52"));
        assertTrue(-1 != responseText.indexOf("Do-rite plumbing"));
        //assertTrue(-1 != responseText.indexOf("53"));
        assertTrue(-1 != responseText.indexOf("MegaCorp"));

        System.out.println("SUCCESS!!!");
    }

    public void testAllCompaniesDepartments() throws Exception{
        final String TEST_CASE = "AllCompaniesDepartments";
        System.out.print("Running:" + TEST_CASE + "\t\t");

        // Get the first page
        final HtmlPage page = (HtmlPage) _webClient.getPage(_url);

        // Get the form that we are dealing with and within that form,
        // find the submit button and the field that we want to change.
        final HtmlForm form = (HtmlForm) page.getForms().get(0);
        final HtmlSubmitInput allCompaniesButton = (HtmlSubmitInput)form.getHtmlElementById("doFillAll");
        // Now submit the form by clicking the button
        final HtmlPage resultPage = (HtmlPage)allCompaniesButton.click();

        //verify the resultPage
        String responseText = resultPage.asText();
        //System.out.println("html-->\n" + responseText);
        //check table headers
        assertTrue(-1 != responseText.indexOf("ID"));
        assertTrue(-1 != responseText.indexOf("Name"));
        assertTrue(-1 != responseText.indexOf("Department_ID"));
        assertTrue(-1 != responseText.indexOf("Department_Name"));
        //check companies values
        //assertTrue(-1 != responseText.indexOf("51"));
        assertTrue(-1 != responseText.indexOf("ACME Publishing"));
        //assertTrue(-1 != responseText.indexOf("53"));
        assertTrue(-1 != responseText.indexOf("Do-rite plumbing"));
        //assertTrue(-1 != responseText.indexOf("53"));
        assertTrue(-1 != responseText.indexOf("MegaCorp"));
        //check departments
        assertTrue(-1 != responseText.indexOf("Default Name 2"));
        assertTrue(-1 != responseText.indexOf("Default Name 3"));
        assertTrue(-1 != responseText.indexOf("Default Name 4"));
        assertTrue(-1 != responseText.indexOf("Default Name 5"));
        assertTrue(-1 != responseText.indexOf("Default Name 6"));
        assertTrue(-1 != responseText.indexOf("Default Name 7"));
        assertTrue(-1 != responseText.indexOf("Default Name 8"));

        System.out.println("SUCCESS!!!");
    }

    public void testAddDepartmentToFirstCompany() throws Exception{
        final String TEST_CASE = "AddDepartmentToFirstCompany";
        System.out.print("Running:" + TEST_CASE + "\t");

        // Get the first page
        final HtmlPage page = (HtmlPage) _webClient.getPage(_url);

        // Get the form that we are dealing with and within that form,
        // find the submit button and the field that we want to change.
        final HtmlForm form = (HtmlForm) page.getForms().get(0);
        final HtmlSubmitInput allCompaniesButton = (HtmlSubmitInput)form.getHtmlElementById("doAddDepartment");
        // Now submit the form by clicking the button
        final HtmlPage resultPage = (HtmlPage)allCompaniesButton.click();

        //verify the resultPage
        String responseText = resultPage.asText();
        //System.out.println("html-->\n" + responseText);
        //check new department
        assertTrue(-1 != responseText.indexOf("8"));

        System.out.println("SUCCESS!!!");
    }

    public void testChangeCompanyDepartmentNames() throws Exception{
        final String TEST_CASE = "ChangeCompanyDepartmentNames";
        System.out.print("Running:" + TEST_CASE + "\t");

        // Get the first page
        final HtmlPage page = (HtmlPage) _webClient.getPage(_url);

        // Get the form that we are dealing with and within that form,
        // find the submit button and the field that we want to change.
        final HtmlForm form = (HtmlForm) page.getForms().get(0);
        final HtmlSubmitInput allCompaniesButton = (HtmlSubmitInput)form.getHtmlElementById("doChangeDepartmentNames");
        // Now submit the form by clicking the button
        final HtmlPage resultPage = (HtmlPage)allCompaniesButton.click();

        //verify the resultPage
        String responseText = resultPage.asText();
        //System.out.println("html-->\n" + responseText);
        //check update departments
        assertTrue(-1 != responseText.indexOf("Dept-"));
        //also, check to not have old departments names (default name)
        assertTrue(-1 == responseText.indexOf("Default Name"));

        System.out.println("SUCCESS!!!");
    }

    public void testDeleteCompanyOneDepartments() throws Exception{
        final String TEST_CASE = "DeleteCompanyOneDepartments";
        System.out.print("Running:" + TEST_CASE + "\t");

        // Get the first page
        final HtmlPage page = (HtmlPage) _webClient.getPage(_url);

        // Get the form that we are dealing with and within that form,
        // find the submit button and the field that we want to change.
        final HtmlForm form = (HtmlForm) page.getForms().get(0);
        final HtmlSubmitInput allCompaniesButton = (HtmlSubmitInput)form.getHtmlElementById("doDeleteDepartments");
        // Now submit the form by clicking the button
        final HtmlPage resultPage = (HtmlPage)allCompaniesButton.click();

        //verify the resultPage
        String responseText = resultPage.asText();
        //System.out.println("html-->\n" + responseText);
        //check that al the company 1 departments are gone..
        assertTrue(-1 == responseText.indexOf("Default Name 2"));
        assertTrue(-1 == responseText.indexOf("Default Name 3"));
        assertTrue(-1 == responseText.indexOf("Default Name 4"));
        assertTrue(-1 == responseText.indexOf("Default Name 5"));
        assertTrue(-1 == responseText.indexOf("Default Name 6"));
        assertTrue(-1 == responseText.indexOf("Default Name 7"));
        assertTrue(-1 == responseText.indexOf("Default Name 8"));

        System.out.println("SUCCESS!!!");
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        DasTestCase t = new DasTestCase();
        try{
            t.testHomepage();
            t.testAllCompanies();
            t.testAllCompaniesDepartments();
            t.testAddDepartmentToFirstCompany();
            t.testChangeCompanyDepartmentNames();
            t.testDeleteCompanyOneDepartments();
        }catch(Exception e){
            e.printStackTrace();
        }

    }

}