1   /*
2    * Copyright 2009, Josh Devins, Jitr.org.
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  
17  package org.jitr;
18  
19  import java.lang.reflect.Field;
20  import java.net.ServerSocket;
21  import java.util.Collection;
22  
23  import junit.framework.Assert;
24  
25  import org.jitr.annotation.Port;
26  import org.jitr.core.JitrException;
27  import org.jitr.test.sample.SampleClass0;
28  import org.jitr.test.sample.SampleClass1;
29  import org.jitr.test.sample.SampleClass2;
30  import org.jitr.test.sample.SampleClass3;
31  import org.jitr.test.sample.SampleClass4;
32  import org.junit.Test;
33  
34  public class JitrUtilsTest {
35  
36      private static final String PORT_FIELD_NAME = "port";
37  
38      @Test
39      public void testGetAnnotatedDeclaredFields() {
40  
41          Collection<Field> fields =
42                  JitrUtils.getAnnotatedDeclaredFields(SampleClass0.class, Port.class);
43          Assert.assertTrue(fields.isEmpty());
44  
45          fields = JitrUtils.getAnnotatedDeclaredFields(SampleClass1.class, Port.class);
46          Assert.assertEquals(1, fields.size());
47  
48          fields = JitrUtils.getAnnotatedDeclaredFields(SampleClass4.class, Port.class);
49          Assert.assertEquals(2, fields.size());
50      }
51  
52      @Test(expected = IllegalArgumentException.class)
53      public void testGetAnnotatedDeclaredFields_AnnotationClassNull() {
54          JitrUtils.getAnnotatedDeclaredFields(SampleClass0.class, null);
55      }
56  
57      @Test(expected = IllegalArgumentException.class)
58      public void testGetAnnotatedDeclaredFields_ClassNull() {
59          JitrUtils.getAnnotatedDeclaredFields(null, null);
60      }
61  
62      @Test
63      public void testGetMostSpecificAnnotatedDeclaredFields() throws Exception {
64  
65          Field field =
66                  JitrUtils.getMostSpecificAnnotatedDeclaredField(SampleClass0.class, Port.class);
67          Assert.assertNull(field);
68  
69          final Field testClass1Field = SampleClass1.class.getDeclaredField(PORT_FIELD_NAME);
70  
71          field = JitrUtils.getMostSpecificAnnotatedDeclaredField(SampleClass1.class, Port.class);
72          Assert.assertEquals(testClass1Field, field);
73  
74          field = JitrUtils.getMostSpecificAnnotatedDeclaredField(SampleClass2.class, Port.class);
75          Assert.assertEquals(testClass1Field, field);
76  
77          field = JitrUtils.getMostSpecificAnnotatedDeclaredField(SampleClass3.class, Port.class);
78          Assert.assertEquals(SampleClass3.class.getDeclaredField(PORT_FIELD_NAME), field);
79      }
80  
81      @Test(expected = JitrException.class)
82      public void testGetMostSpecificAnnotatedDeclaredFields_Multiple() throws Exception {
83          JitrUtils.getMostSpecificAnnotatedDeclaredField(SampleClass4.class, Port.class);
84      }
85  
86      /**
87       * Very difficult to test, so try opening a default port, then a few calls. Obviously this does
88       * not test all branches since to do this we would need to cause exceptions opening or closing
89       * the socket.
90       */
91      @Test
92      public void testGetRandomUnusedPort() throws Exception {
93          ServerSocket socket = new ServerSocket(48080);
94  
95          int firstTry = JitrUtils.getRandomUnusedPort();
96          int secondTry = JitrUtils.getRandomUnusedPort();
97          Assert.assertNotSame(firstTry, secondTry);
98  
99          int thirdTry = JitrUtils.getRandomUnusedPort();
100         Assert.assertNotSame(firstTry, thirdTry);
101         Assert.assertNotSame(secondTry, thirdTry);
102 
103         socket.close();
104     }
105 }