1   /*
2    * Copyright 2003-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.net.telnet;
17  
18  /***
19   * JUnit test class for TerminalTypeOptionHandler
20   * <p>
21   * @author Bruno D'Avanzo
22   ***/
23  public class TerminalTypeOptionHandlerTest extends TelnetOptionHandlerTestAbstract
24  {
25      /***
26       * main for running the test.
27       ***/
28      public static void main(String args[])
29      {
30          junit.textui.TestRunner.run(TerminalTypeOptionHandlerTest.class);
31      }
32  
33      /***
34       * setUp for the test.
35       ***/
36      protected void setUp()
37      {
38          opthand1 = new TerminalTypeOptionHandler("VT100");
39          opthand2 = new TerminalTypeOptionHandler("ANSI", true, true, true, true);
40          opthand3 = new TerminalTypeOptionHandler("ANSI", false, false, false, false);
41      }
42  
43      /***
44       * test of the constructors.
45       ***/
46      public void testConstructors()
47      {
48          assertEquals(opthand1.getOptionCode(), TelnetOption.TERMINAL_TYPE);
49          super.testConstructors();
50      }
51  
52      /***
53       * test of client-driven subnegotiation.
54       * Checks that no subnegotiation is made.
55       ***/
56      public void testStartSubnegotiation()
57      {
58  
59          int resp1[] = opthand1.startSubnegotiationLocal();
60          int resp2[] = opthand1.startSubnegotiationRemote();
61  
62          assertEquals(resp1, null);
63          assertEquals(resp2, null);
64      }
65  
66  
67      /***
68       * test of client-driven subnegotiation.
69       * Checks that the terminal type is sent
70       ***/
71      public void testAnswerSubnegotiation()
72      {
73          int subn[] =
74          {
75              TelnetOption.TERMINAL_TYPE, 1
76          };
77  
78          int expected1[] =
79          {
80              TelnetOption.TERMINAL_TYPE, 0, 'V', 'T', '1', '0', '0'
81          };
82  
83          int expected2[] =
84          {
85              TelnetOption.TERMINAL_TYPE, 0, 'A', 'N', 'S', 'I'
86          };
87  
88          int resp1[] = opthand1.answerSubnegotiation(subn, subn.length);
89          int resp2[] = opthand2.answerSubnegotiation(subn, subn.length);
90  
91          assertTrue(equalInts(resp1, expected1));
92          assertTrue(equalInts(resp2, expected2));
93      }
94  
95  
96      /***
97       * compares two arrays of int
98       ***/
99      protected boolean equalInts(int a1[], int a2[])
100     {
101         if(a1.length != a2.length)
102         {
103             return(false);
104         }
105         else
106         {
107             boolean result = true;
108             for(int ii=0; ii<a1.length; ii++)
109             {
110                 if(a1[ii]!= a2[ii])
111                     result = false;
112             }
113             return(result);
114         }
115     }
116 }