PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/netbeans-7.3/j2ee.dd/test/unit/src/org/netbeans/modules/j2ee/dd/impl/web/metadata/WebAppMetadataImplTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 223 lines | 127 code | 19 blank | 77 comment | 8 complexity | 1e6e7b42484a47069a4f44c04490bf59 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2009 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.j2ee.dd.impl.web.metadata;
  43. import java.io.IOException;
  44. import java.util.ArrayList;
  45. import java.util.List;
  46. import org.junit.Before;
  47. import org.junit.Test;
  48. import org.netbeans.junit.AssertionFailedErrorException;
  49. import org.netbeans.junit.NbTestCase;
  50. import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException;
  51. import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
  52. import org.netbeans.modules.j2ee.dd.api.web.WebApp;
  53. import org.netbeans.modules.j2ee.dd.api.web.WebFragment;
  54. import org.netbeans.modules.j2ee.dd.api.web.WebFragmentProvider;
  55. import org.openide.filesystems.FileObject;
  56. import org.openide.filesystems.FileUtil;
  57. /**
  58. * @author Petr Slechta
  59. */
  60. public class WebAppMetadataImplTest extends NbTestCase {
  61. private FileObject dataFolder;
  62. public WebAppMetadataImplTest(String testName) {
  63. super(testName);
  64. }
  65. @Before
  66. @Override
  67. public void setUp() {
  68. System.out.println("setUp() .......................");
  69. dataFolder = FileUtil.toFileObject(getDataDir());
  70. assertTrue("dataFolder not found", dataFolder != null);
  71. }
  72. /**
  73. * Fragments: A B C D E F
  74. * Constraints: (relative ordering) O<A O<C B<O O<C F<O F<B (O=others)
  75. * Expected sort result: F B D E C A
  76. */
  77. @Test
  78. public void testSortFragments1a() {
  79. System.out.println("testSortFragments1a() .........");
  80. List<WebFragment> list = getFragments(1, new String[] {"A","B","C","D","E","F"});
  81. testOrder(list, new String[] {"A","B","C","D","E","F"});
  82. List<WebFragment> sorted = WebAppMetadataImpl.sortFragments(null, list);
  83. testOrder(sorted, new String[] {"F","B","D","E","C","A"});
  84. }
  85. /**
  86. * Fragments: web.xml A B C D
  87. * Constraints: (absolute ordering) C A
  88. * Expected sort result: C A
  89. */
  90. @Test
  91. public void testSortFragments1b() {
  92. System.out.println("testSortFragments1b() .........");
  93. List<WebFragment> list = getFragments(1, new String[] {"A","B","C","D"});
  94. testOrder(list, new String[] {"A","B","C","D"});
  95. WebApp webXml = getWebXml(1, "A");
  96. List<WebFragment> sorted = WebAppMetadataImpl.sortFragments(webXml, list);
  97. testOrder(sorted, new String[] {"C","A"});
  98. }
  99. /**
  100. * Fragments: web.xml A B C D
  101. * Constraints: (absolute ordering) C others A
  102. * Expected sort result: C B D A
  103. */
  104. @Test
  105. public void testSortFragments1c() {
  106. System.out.println("testSortFragments1c() .........");
  107. List<WebFragment> list = getFragments(1, new String[] {"A","B","C","D"});
  108. testOrder(list, new String[] {"A","B","C","D"});
  109. WebApp webXml = getWebXml(1, "B");
  110. List<WebFragment> sorted = WebAppMetadataImpl.sortFragments(webXml, list);
  111. testOrder(sorted, new String[] {"C","B","D","A"});
  112. }
  113. /**
  114. * Fragments: A B C D E F
  115. * Constraints: (relative ordering) O<A A<C B<O O<D E<O (O=others)
  116. * Expected sort result: B E F A C D
  117. */
  118. @Test
  119. public void testSortFragments2a() {
  120. System.out.println("testSortFragments2a() .........");
  121. List<WebFragment> list = getFragments(2, new String[] {"A","B","C","D","E","F"});
  122. testOrder(list, new String[] {"A","B","C","D","E","F"});
  123. List<WebFragment> sorted = WebAppMetadataImpl.sortFragments(null, list);
  124. testOrder(sorted, new String[] {"B","E","F","A","C","D"});
  125. }
  126. /**
  127. * Fragments: web.xml A B C D E F
  128. * Constraints: (absolute ordering) F D C B
  129. * Expected sort result: F D C B
  130. */
  131. @Test
  132. public void testSortFragments2b() {
  133. System.out.println("testSortFragments2b() .........");
  134. List<WebFragment> list = getFragments(2, new String[] {"A","B","C","D","E","F"});
  135. testOrder(list, new String[] {"A","B","C","D","E","F"});
  136. WebApp webXml = getWebXml(2, "A");
  137. List<WebFragment> sorted = WebAppMetadataImpl.sortFragments(webXml, list);
  138. testOrder(sorted, new String[] {"F","D","C","B"});
  139. }
  140. /**
  141. * Fragments: web.xml A B C D E F
  142. * Constraints: (absolute ordering) F D others C B
  143. * Expected sort result: F D A E C B
  144. */
  145. @Test
  146. public void testSortFragments2c() {
  147. System.out.println("testSortFragments2c() .........");
  148. List<WebFragment> list = getFragments(1, new String[] {"A","B","C","D","E","F"});
  149. testOrder(list, new String[] {"A","B","C","D","E","F"});
  150. WebApp webXml = getWebXml(2, "B");
  151. List<WebFragment> sorted = WebAppMetadataImpl.sortFragments(webXml, list);
  152. testOrder(sorted, new String[] {"F","D","A","E","C","B"});
  153. }
  154. // -------------------------------------------------------------------------
  155. // HELPERS
  156. // -------------------------------------------------------------------------
  157. private void testOrder(List<WebFragment> list, String[] order) {
  158. int i = 0;
  159. for (WebFragment f : list) {
  160. try {
  161. String[] names = f.getName();
  162. String name = names != null && names.length > 0 ? names[0] : null;
  163. assertTrue("fragment "+i+" does not have a name: "+list, name != null);
  164. assertTrue("fragment "+i+" in list has wrong order. expected: "+order[i]+", found: "+name, order[i].equals(name));
  165. } catch (VersionNotSupportedException ex) {
  166. throw new AssertionFailedErrorException("getName() failed for fragment "+i+" in "+list, ex);
  167. }
  168. i++;
  169. }
  170. }
  171. private WebApp getWebXml(int testNo, String name) {
  172. String fileName = "fragments-test"+testNo+"/web"+name+".xml";
  173. FileObject fo = dataFolder.getFileObject(fileName);
  174. assertTrue("web.xml '"+fileName+"' not found", fo != null);
  175. try {
  176. return DDProvider.getDefault().getDDRoot(fo);
  177. }
  178. catch (IOException ex) {
  179. throw new AssertionFailedErrorException("getWebXml failed", ex);
  180. }
  181. }
  182. private List<WebFragment> getFragments(int testNo, String[] names) {
  183. List<WebFragment> res = new ArrayList<WebFragment>();
  184. for (String name : names) {
  185. WebFragment f = getFragment(testNo, name);
  186. res.add(f);
  187. }
  188. return res;
  189. }
  190. private WebFragment getFragment(int testNo, String name) {
  191. FileObject fo = getFragmentFile(testNo, name);
  192. try {
  193. return WebFragmentProvider.getDefault().getWebFragmentRoot(fo);
  194. } catch (IOException ex) {
  195. throw new AssertionFailedErrorException("getFragment failed", ex);
  196. }
  197. }
  198. private FileObject getFragmentFile(int testNo, String name) {
  199. String fileName = "fragments-test"+testNo+"/web-fragment"+name+".xml";
  200. FileObject fo = dataFolder.getFileObject(fileName);
  201. assertTrue("web fragment '"+fileName+"' not found", fo != null);
  202. return fo;
  203. }
  204. }