1   package com.atlassian.maven.plugins.amps;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   import java.net.ServerSocket;
7   import java.util.Map;
8   import java.util.jar.Manifest;
9   
10  import com.google.common.collect.ImmutableMap;
11  
12  import org.apache.maven.model.Build;
13  import org.apache.maven.project.MavenProject;
14  import org.junit.Before;
15  import org.junit.Test;
16  
17  import static com.atlassian.maven.plugins.amps.util.FileUtils.file;
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNull;
20  import static org.junit.Assert.assertTrue;
21  import static org.mockito.Mockito.mock;
22  import static org.mockito.Mockito.when;
23  
24  public class TestMavenGoals
25  {
26      private MavenContext ctx;
27      private MavenProject project;
28      private Build build;
29      private MavenGoals goals;
30      
31      @Before
32      public void setUp()
33      {
34          project = mock(MavenProject.class);
35          build = mock(Build.class);
36          ctx = mock(MavenContext.class);
37          when(project.getBuild()).thenReturn(build);
38          when(ctx.getProject()).thenReturn(project);
39          
40          goals = new MavenGoals(ctx);
41      }
42      
43      @Test
44      public void testPickFreePort() throws IOException
45      {
46          ServerSocket socket = null;
47          try
48          {
49              socket = new ServerSocket(16829);
50  
51              // Pick any
52              int port = goals.pickFreePort(0);
53              assertTrue(16829 != port);
54              assertTrue(port > 0);
55  
56              // Pick taken
57              port = goals.pickFreePort(16829);
58              assertTrue(16829 != port);
59              assertTrue(port > 0);
60  
61              // Pick free
62              assertEquals(16828, goals.pickFreePort(16828));
63          }
64          finally
65          {
66              socket.close();
67          }
68      }
69      
70      @Test
71      public void testGenerateMinimalManifest() throws Exception
72      {
73          File tempDir = File.createTempFile("TestMavenGoals", "dir");
74          tempDir.delete();
75          tempDir.mkdir();
76          
77          when(build.getOutputDirectory()).thenReturn(tempDir.getAbsolutePath());
78  
79          Map<String, String> attrs = ImmutableMap.of("Attribute-A", "aaa", "Attribute-B", "bbb");
80          
81          goals.generateMinimalManifest(attrs);
82          
83          File mf = file(tempDir.getAbsolutePath(), "META-INF", "MANIFEST.MF");
84          assertTrue(mf.exists());
85          
86          Manifest m = new Manifest(new FileInputStream(mf));
87          assertEquals("aaa", m.getMainAttributes().getValue("Attribute-A"));
88          assertEquals("bbb", m.getMainAttributes().getValue("Attribute-B"));
89          assertNull(m.getMainAttributes().getValue("Bundle-SymbolicName"));
90      }
91  }