PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/nakedobjects-4.0.0/plugins/dnd-viewer/src/test/java/org/nakedobjects/plugins/dnd/viewer/view/text/TextFieldContentTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 274 lines | 215 code | 53 blank | 6 comment | 0 complexity | 4c463fc93d323e0d532c386dc0176bf1 MD5 | raw file
  1. package org.nakedobjects.plugins.dnd.viewer.view.text;
  2. import static org.junit.Assert.assertEquals;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import org.apache.log4j.Level;
  6. import org.apache.log4j.Logger;
  7. import org.jmock.Expectations;
  8. import org.jmock.Mockery;
  9. import org.jmock.integration.junit4.JMock;
  10. import org.jmock.integration.junit4.JUnit4Mockery;
  11. import org.junit.Before;
  12. import org.junit.Test;
  13. import org.junit.runner.RunWith;
  14. import org.nakedobjects.metamodel.config.internal.PropertiesConfiguration;
  15. import org.nakedobjects.metamodel.specloader.SpecificationLoader;
  16. import org.nakedobjects.plugins.dnd.viewer.drawing.Location;
  17. import org.nakedobjects.plugins.dnd.viewer.view.text.CursorPosition;
  18. import org.nakedobjects.plugins.dnd.viewer.view.text.TextBlockTarget;
  19. import org.nakedobjects.plugins.dnd.viewer.view.text.TextContent;
  20. import org.nakedobjects.runtime.authentication.AuthenticationManager;
  21. import org.nakedobjects.runtime.authorization.AuthorizationManager;
  22. import org.nakedobjects.runtime.context.NakedObjectsContextStatic;
  23. import org.nakedobjects.runtime.imageloader.TemplateImageLoader;
  24. import org.nakedobjects.runtime.persistence.PersistenceSession;
  25. import org.nakedobjects.runtime.persistence.PersistenceSessionFactory;
  26. import org.nakedobjects.runtime.session.NakedObjectSessionFactory;
  27. import org.nakedobjects.runtime.session.NakedObjectSessionFactoryDefault;
  28. import org.nakedobjects.runtime.system.DeploymentType;
  29. import org.nakedobjects.runtime.userprofile.UserProfileLoader;
  30. @RunWith(JMock.class)
  31. public class TextFieldContentTest {
  32. private TextContent content;
  33. private Mockery mockery = new JUnit4Mockery();
  34. protected TemplateImageLoader mockTemplateImageLoader;
  35. protected SpecificationLoader mockSpecificationLoader;
  36. private UserProfileLoader mockUserProfileLoader;
  37. protected PersistenceSessionFactory mockPersistenceSessionFactory;
  38. protected PersistenceSession mockPersistenceSession;
  39. protected AuthenticationManager mockAuthenticationManager;
  40. protected AuthorizationManager mockAuthorizationManager;
  41. private List<Object> servicesList;
  42. @Before
  43. public void setUp() throws Exception {
  44. Logger.getRootLogger().setLevel(Level.OFF);
  45. servicesList = Collections.emptyList();
  46. mockTemplateImageLoader = mockery.mock(TemplateImageLoader.class);
  47. mockSpecificationLoader = mockery.mock(SpecificationLoader.class);
  48. mockUserProfileLoader = mockery.mock(UserProfileLoader.class);
  49. mockPersistenceSessionFactory = mockery.mock(PersistenceSessionFactory.class);
  50. mockPersistenceSession = mockery.mock(PersistenceSession.class);
  51. mockAuthenticationManager = mockery.mock(AuthenticationManager.class);
  52. mockAuthorizationManager = mockery.mock(AuthorizationManager.class);
  53. mockery.checking(new Expectations() {
  54. {
  55. ignoring(mockTemplateImageLoader);
  56. ignoring(mockSpecificationLoader);
  57. ignoring(mockUserProfileLoader);
  58. ignoring(mockPersistenceSessionFactory);
  59. ignoring(mockAuthenticationManager);
  60. ignoring(mockAuthorizationManager);
  61. }
  62. });
  63. NakedObjectSessionFactory sessionFactory =
  64. new NakedObjectSessionFactoryDefault(
  65. DeploymentType.EXPLORATION,
  66. new PropertiesConfiguration(),
  67. mockTemplateImageLoader,
  68. mockSpecificationLoader,
  69. mockAuthenticationManager,
  70. mockAuthorizationManager,
  71. mockUserProfileLoader,
  72. mockPersistenceSessionFactory, servicesList);
  73. sessionFactory.init();
  74. NakedObjectsContextStatic.createRelaxedInstance(sessionFactory);
  75. final TextBlockTarget target = new TextBlockTargetExample();
  76. content = new TextContent(target, 4, TextContent.WRAPPING);
  77. }
  78. @Test
  79. public void testCreate() {
  80. assertEquals("", content.getText());
  81. assertEquals(1, content.getNoLinesOfContent());
  82. }
  83. @Test
  84. public void testDeleteLeft() {
  85. content.setText("abcdefghijklm");
  86. content.deleteLeft(new CursorPosition(content, 0, 2));
  87. assertEquals("acdefghijklm", content.getText());
  88. }
  89. @Test
  90. public void testDeleteRight() {
  91. content.setText("abcdefghijklm");
  92. content.deleteRight(new CursorPosition(content, 0, 2));
  93. assertEquals("abdefghijklm", content.getText());
  94. }
  95. @Test
  96. public void testDisplayLineCount() {
  97. assertEquals(4, content.getNoDisplayLines());
  98. }
  99. @Test
  100. public void testMinimalTextEqualsOneLine() {
  101. content.setText("test");
  102. assertEquals(1, content.getNoLinesOfContent());
  103. }
  104. @Test
  105. public void testInsert() {
  106. /*
  107. * CursorPosition cursor = new CursorPosition(0,0); content.insert(cursor, "test string");
  108. */
  109. }
  110. @Test
  111. public void testNumberOfDisplayLines() {
  112. assertEquals(4, content.getNoDisplayLines());
  113. assertEquals(4, content.getDisplayLines().length);
  114. assertEquals("", content.getDisplayLines()[0]);
  115. assertEquals("", content.getDisplayLines()[1]);
  116. assertEquals("", content.getDisplayLines()[2]);
  117. assertEquals("", content.getDisplayLines()[3]);
  118. content.setNoDisplayLines(6);
  119. assertEquals(6, content.getNoDisplayLines());
  120. assertEquals(6, content.getDisplayLines().length);
  121. assertEquals("", content.getDisplayLines()[0]);
  122. assertEquals("", content.getDisplayLines()[1]);
  123. assertEquals("", content.getDisplayLines()[2]);
  124. assertEquals("", content.getDisplayLines()[3]);
  125. assertEquals("", content.getDisplayLines()[4]);
  126. assertEquals("", content.getDisplayLines()[5]);
  127. }
  128. @Test
  129. public void testAlignField() {
  130. // the following text wraps so it takes up 9 line
  131. content
  132. .setText("Naked Objects - a framework that exposes behaviourally complete business objects directly to the user. Copyright (C) 2000 - 2005 Naked Objects Group");
  133. assertEquals(9, content.getNoLinesOfContent());
  134. String[] lines = content.getDisplayLines();
  135. assertEquals(4, lines.length);
  136. assertEquals("Naked Objects - a ", lines[0]);
  137. assertEquals("framework that ", lines[1]);
  138. assertEquals("exposes ", lines[2]);
  139. assertEquals("behaviourally complete ", lines[3]);
  140. content.alignDisplay(6);
  141. assertEquals(4, content.getNoDisplayLines());
  142. lines = content.getDisplayLines();
  143. assertEquals(4, lines.length);
  144. // assertEquals("business objects ", lines[0]);
  145. assertEquals("directly to the user. ", lines[0]);
  146. assertEquals("Copyright (C) 2000 ", lines[1]);
  147. assertEquals("- 2005 Naked ", lines[2]);
  148. assertEquals("Objects Group", lines[3]);
  149. }
  150. @Test
  151. public void testInstert() {
  152. content.setText("at");
  153. final CursorPosition cursor = new CursorPosition(content, 0, 0);
  154. content.insert(cursor, "fl");
  155. assertEquals("flat", content.getText());
  156. assertEquals(4, content.getNoDisplayLines());
  157. assertEquals(1, content.getNoLinesOfContent());
  158. }
  159. @Test
  160. public void testInstertOverTheEndOfLine() {
  161. final CursorPosition cursor = new CursorPosition(content, 0, 0);
  162. content.insert(cursor, "test insert that is longer than the four lines that were originally allocated for this test");
  163. assertEquals("test insert that is longer than the four lines that were originally allocated for this test", content
  164. .getText());
  165. assertEquals(4, content.getNoDisplayLines());
  166. assertEquals(6, content.getNoLinesOfContent());
  167. }
  168. @Test
  169. public void testCursorPostioningAtCorner() {
  170. content.setText("test insert that is longer than a single line");
  171. assertEquals(0, content.cursorAtLine(new Location()));
  172. assertEquals(0, content.cursorAtCharacter(new Location(), 0));
  173. }
  174. @Test
  175. public void testCursorPostioningByLine() {
  176. content.setText("test insert that is longer than a single line");
  177. assertEquals(0, content.cursorAtLine(new Location(1000, 0)));
  178. assertEquals(0, content.cursorAtLine(new Location(1000, 10)));
  179. assertEquals(0, content.cursorAtLine(new Location(1000, 14)));
  180. assertEquals(1, content.cursorAtLine(new Location(1000, 15)));
  181. assertEquals(1, content.cursorAtLine(new Location(1000, 25)));
  182. assertEquals(1, content.cursorAtLine(new Location(1000, 29)));
  183. assertEquals(2, content.cursorAtLine(new Location(1000, 30)));
  184. assertEquals(2, content.cursorAtLine(new Location(1000, 44)));
  185. assertEquals(3, content.cursorAtLine(new Location(1000, 45)));
  186. }
  187. @Test
  188. public void testCursorPostioningByCharacter() {
  189. content.setText("test insert that");
  190. assertEquals(0, content.cursorAtCharacter(new Location(0, 1000), 0));
  191. assertEquals(0, content.cursorAtCharacter(new Location(3, 1000), 0));
  192. assertEquals(1, content.cursorAtCharacter(new Location(4, 1000), 0));
  193. assertEquals(1, content.cursorAtCharacter(new Location(13, 1000), 0));
  194. assertEquals(2, content.cursorAtCharacter(new Location(14, 1000), 0));
  195. assertEquals(2, content.cursorAtCharacter(new Location(23, 1000), 0));
  196. assertEquals(15, content.cursorAtCharacter(new Location(153, 1000), 0));
  197. assertEquals(16, content.cursorAtCharacter(new Location(154, 1000), 0));
  198. assertEquals(16, content.cursorAtCharacter(new Location(199, 1000), 0));
  199. }
  200. @Test
  201. public void testCursorPostioningByCharacterPastEnd() {
  202. content.setText("test insert that");
  203. assertEquals(16, content.cursorAtCharacter(new Location(190, 0), 0));
  204. assertEquals(0, content.cursorAtCharacter(new Location(0, 0), 0));
  205. assertEquals(16, content.cursorAtCharacter(new Location(35, 0), 2));
  206. }
  207. @Test
  208. public void testCursorPostioningByCharacterOnLine2() {
  209. content.setNoDisplayLines(4);
  210. content.setText("test insert that that spans three lines only");
  211. assertEquals(0, content.cursorAtCharacter(new Location(0, 1000), 2));
  212. assertEquals(0, content.cursorAtCharacter(new Location(3, 1000), 2));
  213. assertEquals(1, content.cursorAtCharacter(new Location(4, 1000), 2));
  214. assertEquals(1, content.cursorAtCharacter(new Location(13, 1000), 2));
  215. assertEquals(2, content.cursorAtCharacter(new Location(14, 1000), 2));
  216. assertEquals(2, content.cursorAtCharacter(new Location(23, 1000), 2));
  217. assertEquals(10, content.cursorAtCharacter(new Location(14, 1000), 3));
  218. assertEquals(10, content.cursorAtCharacter(new Location(23, 1000), 3));
  219. }
  220. }
  221. // Copyright (c) Naked Objects Group Ltd.